index.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <!-- 分享弹窗 -->
  3. <uni-popup ref="popupRef" type="bottom" background-color="#fff" :mask-click="false">
  4. <view class="share-popup-box">
  5. <!-- 标题 -->
  6. <view class="share-title">分享到 </view>
  7. <!-- 分享选项 -->
  8. <scroll-view scroll-x class="share-scroll" :show-scrollbar="false">
  9. <view class="share-list">
  10. <!-- 微信好友 -->
  11. <view class="share-item" @click="select('session')">
  12. <view class="share-icon wechat-icon"></view>
  13. <text class="share-text">微信好友</text>
  14. </view>
  15. <!-- 朋友圈 -->
  16. <view class="share-item" @click="select('timeline')">
  17. <view class="share-icon timeline-icon"></view>
  18. <text class="share-text">朋友圈</text>
  19. </view>
  20. </view>
  21. <view class="share-close" @click="close">取消</view>
  22. </scroll-view>
  23. </view>
  24. </uni-popup>
  25. </template>
  26. <script setup>
  27. import {
  28. ref
  29. } from 'vue'
  30. // 定义props
  31. const props = defineProps({
  32. // 分享标题
  33. title: {
  34. type: String,
  35. default: '分享标题'
  36. },
  37. // type=0: 图文分享(需要 title + summary + imageUrl)
  38. // type=1: 纯图片分享(只需要 imageUrl)
  39. // type=2: 链接分享(需要 title + summary + href + imageUrl)
  40. type: {
  41. type: String,
  42. default: "0" // 0:图文
  43. },
  44. // 分享描述
  45. desc: {
  46. type: String,
  47. default: '分享描述'
  48. },
  49. // 分享链接
  50. link: {
  51. type: String,
  52. default: ''
  53. },
  54. // 分享图片
  55. image: {
  56. type: String,
  57. default: ''
  58. },
  59. })
  60. const emit = defineEmits(['close', 'select', 'success', 'error'])
  61. const popupRef = ref(null)
  62. // 打开弹窗
  63. const open = () => {
  64. popupRef.value.open()
  65. }
  66. // 关闭弹窗
  67. const close = () => {
  68. popupRef.value.close()
  69. emit('close')
  70. }
  71. // 选择分享方式
  72. const select = (scene) => {
  73. //emit('select', scene)
  74. try {
  75. shareToWeChat(scene)
  76. close()
  77. } catch (error) {
  78. emit('error', error)
  79. handleShareError(error)
  80. }
  81. }
  82. // 分享到微信
  83. const shareToWeChat = (scene) => {
  84. console.log('props.link',props.link);
  85. uni.share({
  86. provider: 'weixin',
  87. scene: scene === 'timeline' ? 'WXSceneTimeline' : 'WXSceneSession',
  88. type: Number(props.type),
  89. title: props.title,
  90. summary: props.desc,
  91. href: props.link,
  92. imageUrl: props.image || 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni@2x.png',
  93. success: function(res) {
  94. emit('success', {
  95. scene
  96. })
  97. },
  98. fail: function(err) {
  99. emit('error', err)
  100. handleShareError(err)
  101. }
  102. })
  103. }
  104. // 错误处理
  105. const handleShareError = (error) => {
  106. console.error('分享失败:', error)
  107. const errCode = error.errCode || error.code
  108. const errMsg = error.errMsg || error.message
  109. // 微信未安装
  110. if (errCode === 1004 || errCode === -4004) {
  111. uni.showModal({
  112. title: '提示',
  113. content: '请先安装微信',
  114. showCancel: false
  115. })
  116. return
  117. }
  118. // 用户取消不提示
  119. if (errCode === 1001 || errCode === -4001) {
  120. return
  121. }
  122. // 其他错误
  123. uni.showToast({
  124. title: errMsg || '分享失败,请重试',
  125. icon: 'none',
  126. duration: 3000
  127. })
  128. }
  129. // 暴露方法给父组件
  130. defineExpose({
  131. open,
  132. close
  133. })
  134. </script>