share-boxN.vue 5.5 KB

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