SharePopup.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <template>
  2. <view class="share-modal" v-show="visible">
  3. <view class="modal-overlay" @click="handleClose"></view>
  4. <transition name="fade-up">
  5. <view v-if="visible" class="modal-content">
  6. <view class="share-options">
  7. <view v-for="option in shareOptions" :key="option.id" class="share-item" @click="handleShare(option.id)">
  8. <view class="share-icon">
  9. <image :src="option.icon" :alt="option.title" mode="aspectFit"></image>
  10. </view>
  11. <text class="option-title">{{ option.title }}</text>
  12. </view>
  13. </view>
  14. </view>
  15. </transition>
  16. </view>
  17. </template>
  18. <script>
  19. import { generateSecureUrlParams } from '@/common/encryption.js'
  20. export default {
  21. name: 'ShareModal',
  22. props: {
  23. visible: {
  24. type: Boolean,
  25. default: false
  26. },
  27. userId: {
  28. default: 0
  29. },
  30. shareTitle: {
  31. type: String,
  32. default: ''
  33. },
  34. shareDesc: {
  35. type: String,
  36. default: '快来加入萌创星球吧!'
  37. },
  38. shareImg: {
  39. type: String,
  40. default: ''
  41. },
  42. view: {
  43. type: String,
  44. default: ''
  45. },
  46. isReportContent: {
  47. type: Boolean,
  48. default: false
  49. }
  50. },
  51. data() {
  52. return {
  53. shareOptions: [
  54. { id: 'wechat', title: '微信好友', icon: '/static/icon/sy_icon_weixin.png' },
  55. { id: 'moments', title: '朋友圈', icon: '/static/icon/sy_icon_pengyouquan.png' },
  56. // { id: 'qq', title: 'QQ好友', icon: '../../static/icon/sy_icon_qq.png' },
  57. { id: 'copy', title: '复制链接', icon: '/static/icon/sy_icon_fuzhilianjie.png' },
  58. { id: 'report', title: '举报', icon: '/static/icon/sy_icon_jubao01.png' },
  59. // { id: 'more', title: '更多', icon: '../../static/icon/sy_icon_gengduo.png' }
  60. ],
  61. from_id: 0
  62. }
  63. },
  64. computed: {
  65. shareUrl() {
  66. const urlParams = {
  67. userId: this.userId,
  68. action: this.view,
  69. timestamp: new Date().getTime(),
  70. from_id: this.from_id
  71. }
  72. const secureParams = generateSecureUrlParams(urlParams)
  73. return `${this.$shareUrl}?params=${secureParams}`
  74. }
  75. },
  76. created() {
  77. uni.$emit('check_login', () => {
  78. this.from_id = getApp().globalData.user_id
  79. })
  80. },
  81. methods: {
  82. handleClose() {
  83. this.$emit('close')
  84. },
  85. handleShare(type) {
  86. switch (type) {
  87. case 'copy':
  88. this.copyToClipboard(this.shareUrl)
  89. break
  90. case 'wechat':
  91. this.shareToWechat()
  92. break
  93. case 'moments':
  94. this.shareToMoments()
  95. break
  96. case 'report':
  97. this.handleReport()
  98. break
  99. case 'qq':
  100. this.qqShare()
  101. break
  102. case 'more':
  103. this.moreShare()
  104. break
  105. }
  106. },
  107. copyToClipboard(text) {
  108. // #ifdef H5
  109. const input = document.createElement('input')
  110. input.value = text
  111. document.body.appendChild(input)
  112. input.select()
  113. document.execCommand('copy')
  114. document.body.removeChild(input)
  115. // #endif
  116. // #ifdef APP-PLUS || MP
  117. uni.setClipboardData({
  118. data: text,
  119. success: () => {
  120. uni.showToast({
  121. title: '链接已复制',
  122. icon: 'success'
  123. })
  124. },
  125. fail: () => {
  126. uni.showToast({
  127. title: '复制失败',
  128. icon: 'none'
  129. })
  130. }
  131. })
  132. // #endif
  133. },
  134. shareToWechat() {
  135. // #ifdef APP-PLUS
  136. uni.share({
  137. provider: 'weixin',
  138. scene: 'WXSceneSession',
  139. type: 0,
  140. title: this.shareTitle,
  141. summary: this.shareDesc,
  142. imageUrl: this.shareImg || this.$icon,
  143. href: this.shareUrl,
  144. success: () => {
  145. uni.showToast({
  146. title: '分享成功',
  147. icon: 'success'
  148. })
  149. },
  150. fail: (err) => {
  151. uni.showToast({
  152. title: '分享失败',
  153. icon: 'none'
  154. })
  155. console.error('分享失败:', err)
  156. }
  157. })
  158. // #endif
  159. // #ifdef H5
  160. if (typeof wx !== 'undefined') {
  161. wx.ready(() => {
  162. wx.updateAppMessageShareData({
  163. title: this.shareTitle,
  164. desc: this.shareDesc,
  165. link: this.shareUrl,
  166. imgUrl: this.shareImg || this.$icon,
  167. success: () => {
  168. uni.showToast({
  169. title: '分享成功',
  170. icon: 'success'
  171. })
  172. },
  173. fail: (err) => {
  174. uni.showToast({
  175. title: '分享失败',
  176. icon: 'none'
  177. })
  178. console.error('分享失败:', err)
  179. }
  180. })
  181. })
  182. } else {
  183. uni.showToast({
  184. title: '请在微信中打开',
  185. icon: 'none'
  186. })
  187. }
  188. // #endif
  189. // #ifdef MP-WEIXIN
  190. uni.showShareMenu({
  191. withShareTicket: true,
  192. menus: ['shareAppMessage'],
  193. success: () => {
  194. uni.updateShareMenu({
  195. withShareTicket: true,
  196. isPrivateMessage: true
  197. })
  198. }
  199. })
  200. // #endif
  201. },
  202. shareToMoments() {
  203. // #ifdef APP-PLUS
  204. uni.share({
  205. provider: 'weixin',
  206. scene: 'WXSenceTimeline',
  207. type: 0,
  208. title: this.shareTitle,
  209. summary: this.shareDesc,
  210. imageUrl: this.shareImg || this.$icon,
  211. href: this.shareUrl,
  212. success: () => {
  213. uni.showToast({
  214. title: '分享成功',
  215. icon: 'success'
  216. })
  217. },
  218. fail: (err) => {
  219. uni.showToast({
  220. title: '分享失败',
  221. icon: 'none'
  222. })
  223. console.error('分享失败:', err)
  224. }
  225. })
  226. // #endif
  227. // #ifdef H5
  228. if (typeof wx !== 'undefined') {
  229. wx.ready(() => {
  230. wx.updateTimelineShareData({
  231. title: this.shareTitle,
  232. link: this.shareUrl,
  233. imgUrl: this.shareImg || this.$icon,
  234. success: () => {
  235. uni.showToast({
  236. title: '分享成功',
  237. icon: 'success'
  238. })
  239. },
  240. fail: (err) => {
  241. uni.showToast({
  242. title: '分享失败',
  243. icon: 'none'
  244. })
  245. console.error('分享失败:', err)
  246. }
  247. })
  248. })
  249. } else {
  250. uni.showToast({
  251. title: '请在微信中打开',
  252. icon: 'none'
  253. })
  254. }
  255. // #endif
  256. // #ifdef MP-WEIXIN
  257. uni.showShareMenu({
  258. withShareTicket: true,
  259. menus: ['shareTimeline']
  260. })
  261. // #endif
  262. },
  263. handleReport() {
  264. // 实现举报功能
  265. uni.$emit('check_login', () => {
  266. uni.navigateTo({
  267. url: '/pages/my/feedback?isReportContent=' + this.isReportContent
  268. })
  269. })
  270. },
  271. qqShare() {
  272. // #ifdef APP-PLUS
  273. uni.share({
  274. provider: 'qq',
  275. type: 0,
  276. title: this.shareTitle,
  277. summary: this.shareDesc,
  278. imageUrl: this.shareImg || this.$icon,
  279. href: this.shareUrl,
  280. success: () => {
  281. uni.showToast({
  282. title: '分享成功',
  283. icon: 'success'
  284. })
  285. }
  286. })
  287. // #endif
  288. },
  289. moreShare() {
  290. // 更多分享方式
  291. uni.showToast({
  292. title: '更多分享功能开发中',
  293. icon: 'none'
  294. })
  295. }
  296. }
  297. }
  298. </script>
  299. <style scoped>
  300. .share-modal
  301. {
  302. position: fixed;
  303. left: 0;
  304. right: 0;
  305. top: 0;
  306. bottom: 0;
  307. z-index: 1000;
  308. }
  309. .modal-overlay
  310. {
  311. position: absolute;
  312. left: 0;
  313. right: 0;
  314. top: 0;
  315. bottom: 0;
  316. background-color: rgba(0, 0, 0, 0.4);
  317. }
  318. .modal-content
  319. {
  320. position: absolute;
  321. left: 0;
  322. right: 0;
  323. bottom: 0;
  324. background-color: #f2f6f2;
  325. border-top-left-radius: 12px;
  326. border-top-right-radius: 12px;
  327. padding: 16px;
  328. }
  329. /* 弹出动画 */
  330. .fade-up-enter-active, .fade-up-leave-active
  331. {
  332. transition: all 0.3s cubic-bezier(.55, 0, .1, 1);
  333. }
  334. .fade-up-enter, .fade-up-leave-to
  335. {
  336. opacity: 0;
  337. transform: translateY(100%);
  338. }
  339. .fade-up-leave, .fade-up-enter-to
  340. {
  341. opacity: 1;
  342. transform: translateY(0);
  343. }
  344. .share-options
  345. {
  346. display: flex;
  347. flex-wrap: wrap;
  348. padding: 8px 16px;
  349. }
  350. .share-item
  351. {
  352. width: 25%;
  353. display: flex;
  354. flex-direction: column;
  355. align-items: center;
  356. gap: 8px;
  357. padding: 8px 0;
  358. }
  359. .share-icon
  360. {
  361. width: 44px;
  362. height: 44px;
  363. display: flex;
  364. justify-content: center;
  365. align-items: center;
  366. }
  367. .share-icon image
  368. {
  369. width: 100%;
  370. height: 100%;
  371. }
  372. .option-title
  373. {
  374. font-size: 13px;
  375. color: #333333;
  376. text-align: center;
  377. }
  378. </style>