share-box.vue 8.7 KB

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