share-boxN.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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="copyUrl()" v-if="operate.indexOf('copyUrl') > -1">
  13. <image class="image" src="/static/images/icon_copy_link.png" mode=""></image>
  14. <text class="text">复制链接</text>
  15. </view>
  16. </view>
  17. <text class="cancel-btn" @click="closeShare">取消</text>
  18. </view>
  19. </template>
  20. <script>
  21. import {shareCourse} from '@/api/course'
  22. import { shareVideo } from '@/api/shortvideo'
  23. export default {
  24. name: "share-box",
  25. props: {
  26. shareItem: {
  27. type: Object,
  28. default: function() {
  29. return {
  30. imageUrl: "",
  31. title: "",
  32. url: "",
  33. summary: url,
  34. courseId: 0,
  35. videoId: 0,
  36. isMini: false,
  37. compressImage: 0, // 是否需要压缩
  38. }; // type 0:课程 1视频
  39. }
  40. },
  41. operate: {
  42. type: Array,
  43. default: ['WXSceneSession','WXSceneTimeline','copyUrl']
  44. }
  45. },
  46. data() {
  47. return {
  48. viewWidth: 0,
  49. };
  50. },
  51. mounted() {
  52. const systemInfo = uni.getSystemInfoSync();
  53. this.viewWidth = systemInfo.screenWidth;
  54. },
  55. methods: {
  56. wxShare(type) {
  57. if (this.isEmpty(this.shareItem.title)) {
  58. uni.showToast({
  59. title: "数据未加载",
  60. icon: "none",
  61. position: 'top',
  62. duration: 2000
  63. })
  64. return;
  65. }
  66. // #ifdef APP-PLUS
  67. if (!plus.runtime.isAgreePrivacy()) {
  68. uni.showToast({
  69. title: "请同意隐私政策",
  70. icon: "none",
  71. position: 'top',
  72. duration: 2000
  73. })
  74. return;
  75. }
  76. // #endif
  77. let that = this;
  78. let sceneStr = type == 0 ? "WXSceneSession" : "WXSceneTimeline";
  79. // 仅支持分享微信小程序到微信聊天界面,想进入朋友圈需改为分享图片方式,在图片中包含小程序码
  80. if (sceneStr == 'WXSceneTimeline' || !this.shareItem.isMini) {
  81. uni.share({
  82. provider: "weixin",
  83. scene: sceneStr,
  84. type: 0,
  85. imageUrl: this.shareItem.imageUrl, //分享封面图片
  86. title: this.shareItem.title, // 分享标题
  87. href: this.shareItem.url, //跳转链接
  88. summary: this.shareItem.summary, // 分享内容文字
  89. success: function(res) {
  90. that.doShare();
  91. uni.showToast({
  92. title: "分享成功",
  93. icon: 'none'
  94. });
  95. },
  96. fail: function(err) {
  97. // 此处是调起微信分享失败的回调
  98. },
  99. complete: (e) => {
  100. console.log("WXSceneTimeline", e)
  101. }
  102. });
  103. } else {
  104. if(this.shareItem.compressImage == 1) {
  105. uni.downloadFile({
  106. url: this.shareItem.imageUrl, //仅为示例,并非真实接口地址。
  107. success: (images)=> {
  108. uni.compressImage({
  109. src: images.tempFilePath,
  110. quality: 40,
  111. width: '50%',
  112. height: '50%',
  113. success: res => {
  114. this.shareMini(res.tempFilePath,sceneStr)
  115. },
  116. fail: function(err) {
  117. // 此处是压缩失败
  118. }
  119. })
  120. }
  121. });
  122. }else{
  123. this.shareMini(this.shareItem.imageUrl,sceneStr)
  124. }
  125. }
  126. },
  127. shareMini(imageUrl,sceneStr) {
  128. const that = this
  129. uni.share({
  130. provider: "weixin",
  131. scene: sceneStr,
  132. type: 5,
  133. imageUrl: imageUrl, //分享封面图片
  134. title: this.shareItem.title, // 分享标题
  135. //summary: this.shareItem.summary, // 分享内容文字
  136. miniProgram: {
  137. id: "gh_7a6a32e5ef61",
  138. path: this.shareItem.path,
  139. type: 0, //0-正式版; 1-测试版; 2-体验版。 默认值为0。
  140. webUrl: "http://uniapp.dcloud.io"
  141. },
  142. success: function(res) {
  143. that.doShare();
  144. uni.showToast({
  145. title: "分享成功",
  146. icon: 'none'
  147. });
  148. },
  149. fail: function(err) {
  150. // 此处是调起微信分享失败的回调
  151. },
  152. complete: (e) => {
  153. console.log("WXSceneSession", e)
  154. }
  155. });
  156. },
  157. doShare() {
  158. if (!this.shareItem.isMini) {
  159. if (this.shareItem.courseId > 0) {
  160. this.shareCourse();
  161. }
  162. }
  163. if(this.shareItem.videoId > 0) {
  164. this.shareVideo();
  165. }
  166. },
  167. isEmpty(obj) {
  168. if (typeof obj == "undefined" || obj == null || obj == "") {
  169. return true;
  170. } else {
  171. return false;
  172. }
  173. },
  174. shareCourse() {
  175. shareCourse(this.shareItem.courseId).then(res => {
  176. },
  177. rej => {}
  178. );
  179. },
  180. shareVideo() {
  181. shareVideo({videoId:this.shareItem.videoId})
  182. },
  183. copyUrl() {
  184. uni.setClipboardData({
  185. data: this.shareItem.url,
  186. success: () => {
  187. uni.showToast({
  188. title: '复制成功',
  189. icon: 'none'
  190. });
  191. },
  192. fail: () => {
  193. uni.showToast({
  194. title: '复制失败',
  195. icon: 'none'
  196. });
  197. }
  198. });
  199. },
  200. closeShare() {
  201. return this.$emit('closeShare');
  202. },
  203. showToast(title) {
  204. uni.showToast({
  205. icon: 'none',
  206. title: title,
  207. duration: 3000
  208. });
  209. }
  210. }
  211. }
  212. </script>
  213. <style scoped>
  214. .share-content {
  215. border-radius: 40upx 40upx 0px 0px;
  216. width: 100%;
  217. height: 250rpx;
  218. /* background-color:blue; */
  219. }
  220. .share-inner {
  221. padding: 0 0 0 0;
  222. width: 100%;
  223. flex: 1;
  224. height: 160rpx;
  225. flex-direction: row;
  226. flex-wrap: wrap;
  227. }
  228. .share-item {
  229. width: 25%;
  230. flex-direction: column;
  231. align-items: center;
  232. justify-content: center;
  233. height: 160rpx;
  234. /* background-color:red; */
  235. }
  236. .share-item .image {
  237. width: 80rpx;
  238. height: 80rpx;
  239. margin-top: 40rpx;
  240. }
  241. .text {
  242. font-size: 28rpx;
  243. font-weight: 500;
  244. color: #111111;
  245. height: 60rpx;
  246. margin-top: 20rpx;
  247. }
  248. .cancel-btn {
  249. height: 70upx;
  250. line-height: 80upx;
  251. text-align: center;
  252. font-size: 32upx;
  253. font-family: PingFang SC;
  254. font-weight: 500;
  255. color: #111111;
  256. border-top: 2upx solid #E8E8E8;
  257. background-color: #FFF;
  258. }
  259. </style>