share-boxN.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <template>
  2. <view class="share-content" :style="{'width':viewWidth+'px'}">
  3. <view class="share-inner" style="width: 100%;">
  4. <view class="share-item" :style="{'width':viewWidth/4.0+'px'}" @click="wxShare(0)">
  5. <image class="image" src="/static/images/icon_wx.png" mode=""></image>
  6. <text class="text">微信</text>
  7. </view>
  8. <view class="share-item" :style="{'width':viewWidth/4.0+'px'}" @click="wxShare(1)">
  9. <image class="image" src="/static/images/icon_pyq.png" mode=""></image>
  10. <text class="text">朋友圈</text>
  11. </view>
  12. <!-- <view class="share-item" :style="{'width':viewWidth/4.0+'px'}" @click="qwShare()">
  13. <image class="image" src="/static/images/icon_qwshare.png" mode=""></image>
  14. <text class="text">发给同事</text>
  15. </view> -->
  16. <view class="share-item" :style="{'width':viewWidth/4.0+'px'}" @click="copyUrl()" v-if="operate.indexOf('copyUrl') > -1">
  17. <image class="image" src="/static/images/icon_copy_link.png" mode=""></image>
  18. <text class="text">复制链接</text>
  19. </view>
  20. </view>
  21. <text class="cancel-btn" @click="closeShare">取消</text>
  22. </view>
  23. </template>
  24. <script>
  25. import {shareCourse} from '@/api/course'
  26. import { shareVideo } from '@/api/shortvideo'
  27. import { getQwCompanyList } from '@/api/common.js'
  28. import { registerQW } from '@/utils/common.js'
  29. export default {
  30. name: "share-box",
  31. props: {
  32. shareItem: {
  33. type: Object,
  34. default: function() {
  35. return {
  36. imageUrl: "",
  37. title: "",
  38. url: "",
  39. summary: url,
  40. courseId: 0,
  41. videoId: 0,
  42. isMini: false,
  43. compressImage: 0, // 是否需要压缩
  44. }; // type 0:课程 1视频
  45. }
  46. },
  47. operate: {
  48. type: Array,
  49. default: ['WXSceneSession','WXSceneTimeline','copyUrl']
  50. }
  51. },
  52. data() {
  53. return {
  54. viewWidth: 0,
  55. companyList:[]
  56. };
  57. },
  58. mounted() {
  59. const systemInfo = uni.getSystemInfoSync();
  60. this.viewWidth = systemInfo.screenWidth;
  61. },
  62. methods: {
  63. wxShare(type) {
  64. if (this.isEmpty(this.shareItem.title)) {
  65. uni.showToast({
  66. title: "数据未加载",
  67. icon: "none",
  68. position: 'top',
  69. duration: 2000
  70. })
  71. return;
  72. }
  73. // #ifdef APP-PLUS
  74. if (!plus.runtime.isAgreePrivacy()) {
  75. uni.showToast({
  76. title: "请同意隐私政策",
  77. icon: "none",
  78. position: 'top',
  79. duration: 2000
  80. })
  81. return;
  82. }
  83. // #endif
  84. let that = this;
  85. let sceneStr = type == 0 ? "WXSceneSession" : "WXSceneTimeline";
  86. // 仅支持分享微信小程序到微信聊天界面,想进入朋友圈需改为分享图片方式,在图片中包含小程序码
  87. if (sceneStr == 'WXSceneTimeline' || !this.shareItem.isMini) {
  88. uni.share({
  89. provider: "weixin",
  90. scene: sceneStr,
  91. type: 0,
  92. imageUrl: this.shareItem.imageUrl, //分享封面图片
  93. title: this.shareItem.title, // 分享标题
  94. href: this.shareItem.url, //跳转链接
  95. summary: this.shareItem.summary, // 分享内容文字
  96. success: function(res) {
  97. that.doShare();
  98. uni.showToast({
  99. title: "分享成功",
  100. icon: 'none'
  101. });
  102. },
  103. fail: function(err) {
  104. // 此处是调起微信分享失败的回调
  105. },
  106. complete: (e) => {
  107. console.log("WXSceneTimeline", e)
  108. }
  109. });
  110. } else {
  111. if(this.shareItem.compressImage == 1) {
  112. uni.downloadFile({
  113. url: this.shareItem.imageUrl, //仅为示例,并非真实接口地址。
  114. success: (images)=> {
  115. uni.compressImage({
  116. src: images.tempFilePath,
  117. quality: 40,
  118. width: '50%',
  119. height: '50%',
  120. success: res => {
  121. this.shareMini(res.tempFilePath,sceneStr)
  122. },
  123. fail: function(err) {
  124. // 此处是压缩失败
  125. }
  126. })
  127. }
  128. });
  129. } else {
  130. this.shareMini(this.shareItem.imageUrl)
  131. }
  132. }
  133. },
  134. shareMini(imageUrl,sceneStr) {
  135. const that = this
  136. uni.share({
  137. provider: "weixin",
  138. scene: sceneStr,
  139. type: 5,
  140. imageUrl: imageUrl, //分享封面图片
  141. title: this.shareItem.title, // 分享标题
  142. //summary: this.shareItem.summary, // 分享内容文字
  143. miniProgram: {
  144. id: getApp().globalData.miniprogamId,
  145. path: this.shareItem.path,
  146. type: 0, //0-正式版; 1-测试版; 2-体验版。 默认值为0。
  147. webUrl: "http://uniapp.dcloud.io"
  148. },
  149. success: function(res) {
  150. that.doShare();
  151. uni.showToast({
  152. title: "分享成功",
  153. icon: 'none'
  154. });
  155. },
  156. fail: function(err) {
  157. // 此处是调起微信分享失败的回调
  158. },
  159. complete: (e) => {
  160. console.log("WXSceneSession", e)
  161. }
  162. });
  163. },
  164. doShare() {
  165. if (!this.shareItem.isMini) {
  166. if (this.shareItem.courseId > 0) {
  167. this.shareCourse();
  168. }
  169. }
  170. if(this.shareItem.videoId > 0) {
  171. this.shareVideo();
  172. }
  173. },
  174. isEmpty(obj) {
  175. if (typeof obj == "undefined" || obj == null || obj == "") {
  176. return true;
  177. } else {
  178. return false;
  179. }
  180. },
  181. shareCourse() {
  182. shareCourse(this.shareItem.courseId).then(res => {
  183. },
  184. rej => {}
  185. );
  186. },
  187. shareVideo() {
  188. shareVideo({videoId:this.shareItem.videoId})
  189. },
  190. copyUrl() {
  191. uni.setClipboardData({
  192. data: this.shareItem.url,
  193. success: () => {
  194. uni.showToast({
  195. title: '复制成功',
  196. icon: 'none'
  197. });
  198. },
  199. fail: () => {
  200. uni.showToast({
  201. title: '复制失败',
  202. icon: 'none'
  203. });
  204. }
  205. });
  206. },
  207. closeShare() {
  208. return this.$emit('closeShare');
  209. },
  210. showToast(title) {
  211. uni.showToast({
  212. icon: 'none',
  213. title: title,
  214. duration: 3000
  215. });
  216. },
  217. qwShare() {
  218. if (this.isEmpty(this.shareItem.title)) {
  219. uni.showToast({
  220. title: "数据未加载",
  221. icon: "none",
  222. position: 'top',
  223. duration: 2000
  224. })
  225. return;
  226. }
  227. // #ifdef APP-PLUS
  228. if (!plus.runtime.isAgreePrivacy()) {
  229. uni.showToast({
  230. title: "请同意隐私政策",
  231. icon: "none",
  232. position: 'top',
  233. duration: 2000
  234. })
  235. return;
  236. }
  237. // #endif
  238. this.getQwCompanyList()
  239. },
  240. getQwCompanyList(){
  241. let token = uni.getStorageSync('CompanyUserToken');
  242. if (!token) {
  243. uni.showToast({
  244. title: "请先登录销售账号",
  245. icon: 'none'
  246. });
  247. return
  248. }
  249. uni.showLoading()
  250. getQwCompanyList().then(res=>{
  251. uni.hideLoading()
  252. if(res.code == 200&&res.qwCompanyList&&res.qwCompanyList.length > 0) {
  253. this.companyList = res.qwCompanyList
  254. if(this.companyList.length==1) {
  255. this.initregisterQW(0)
  256. } else {
  257. uni.showActionSheet({
  258. itemList: this.companyList.map(item=>item.corpName),
  259. success: (res)=> {
  260. console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
  261. this.initregisterQW(res.tapIndex)
  262. },
  263. fail: (res)=> {
  264. console.log(res.errMsg);
  265. }
  266. });
  267. }
  268. } else {
  269. uni.showToast({
  270. title: "暂无可分享企业微信账号",
  271. icon: 'none'
  272. });
  273. }
  274. }).catch(()=>{
  275. uni.hideLoading()
  276. })
  277. },
  278. // 初始化注册到企业微信并分享
  279. initregisterQW(index) {
  280. getApp().globalData.shareSchema = this.companyList[index].yjfyySchema; //应用跳转标识,显示在具体应用下的 Schema字段
  281. getApp().globalData.shareCorpId = this.companyList[index].corpId; //企业唯一标识。创建企业后显示在,我的企业 CorpID字段
  282. getApp().globalData.shareAgentid = this.companyList[index].yjfyyAgentId; //应用
  283. getApp().globalData.shareAppid_gh = this.companyList[index].yjfyyAppId;
  284. registerQW((installed) => {
  285. this.doShare();
  286. this.$emit('closeShare')
  287. },this.shareItem)
  288. }
  289. }
  290. }
  291. </script>
  292. <style scoped lang="scss">
  293. .share-content {
  294. border-radius: 40upx 40upx 0px 0px;
  295. width: 100%;
  296. height: 250rpx;
  297. /* background-color:blue; */
  298. }
  299. .share-inner {
  300. padding: 0 0 0 0;
  301. width: 100%;
  302. flex: 1;
  303. height: 160rpx;
  304. flex-direction: row;
  305. flex-wrap: wrap;
  306. }
  307. .share-item {
  308. width: 25%;
  309. flex-direction: column;
  310. align-items: center;
  311. justify-content: center;
  312. height: 160rpx;
  313. /* background-color:red; */
  314. }
  315. .share-item .image {
  316. width: 80rpx;
  317. height: 80rpx;
  318. margin-top: 40rpx;
  319. }
  320. .text {
  321. font-size: 28rpx;
  322. font-weight: 500;
  323. color: #111111;
  324. height: 60rpx;
  325. margin-top: 20rpx;
  326. }
  327. .cancel-btn {
  328. height: 70upx;
  329. line-height: 80upx;
  330. text-align: center;
  331. font-size: 32upx;
  332. font-family: PingFang SC;
  333. font-weight: 500;
  334. color: #111111;
  335. border-top: 2upx solid #E8E8E8;
  336. background-color: #FFF;
  337. }
  338. </style>