index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. <view class="close-btn" @click="close"></view>
  8. <!-- 分享选项 -->
  9. <scroll-view scroll-x class="share-scroll" :show-scrollbar="false">
  10. <view class="share-list">
  11. <!-- 微信好友 -->
  12. <view class="share-item" @click="select('session')">
  13. <view class="share-icon wechat-icon"></view>
  14. <text class="share-text">微信好友</text>
  15. </view>
  16. <!-- 朋友圈 -->
  17. <view class="share-item" @click="select('timeline')">
  18. <view class="share-icon timeline-icon"></view>
  19. <text class="share-text">朋友圈</text>
  20. </view>
  21. </view>
  22. <!-- <view class="share-close" @click="close">取消</view> -->
  23. </scroll-view>
  24. </view>
  25. </uni-popup>
  26. </template>
  27. <script setup>
  28. import {
  29. ref
  30. } from 'vue'
  31. // 定义props
  32. const props = defineProps({
  33. // 分享标题
  34. title: {
  35. type: String,
  36. default: '分享标题'
  37. },
  38. // type=0: 图文分享(需要 title + summary + imageUrl)
  39. // type=2: 纯图片分享(只需要 imageUrl)
  40. type: {
  41. type: [String, Number],
  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. uni.downloadFile({
  85. url: props.image, // 你的网络图片地址
  86. success: (downloadRes) =>{
  87. // 获取分享类型,确保是数字类型
  88. const shareType = Number(props.type)
  89. // 构建基础分享配置
  90. const shareConfig = {
  91. provider: 'weixin',
  92. scene: scene === 'timeline' ? 'WXSceneTimeline' : 'WXSceneSession',
  93. type: shareType,
  94. success: function(res) {
  95. emit('success', {
  96. scene
  97. })
  98. },
  99. fail: function(err) {
  100. emit('error', err)
  101. handleShareError(err)
  102. }
  103. }
  104. // 根据类型添加不同的参数
  105. if (shareType == 2) {
  106. // 纯图片分享
  107. // shareConfig.imageUrl = props.image
  108. shareConfig.imageUrl = downloadRes.tempFilePath
  109. } else {
  110. // 图文或链接分享(兼容原有逻辑)
  111. shareConfig.title = props.title
  112. shareConfig.summary = props.desc
  113. shareConfig.imageUrl = props.image
  114. shareConfig.href = props.link
  115. }
  116. console.log('分享配置:', shareConfig)
  117. uni.share(shareConfig)
  118. },
  119. fail:() => {
  120. // 其他错误
  121. uni.showToast({
  122. title: '海报分享失败,请检测网络状态',
  123. icon: 'none',
  124. duration: 3000
  125. })}
  126. })
  127. }
  128. // const shareToWeChat = (scene) => {
  129. // console.log('props.link',props.link);
  130. // uni.share({
  131. // provider: 'weixin',
  132. // scene: scene === 'timeline' ? 'WXSceneTimeline' : 'WXSceneSession',
  133. // type: Number(props.type),
  134. // title: props.title,
  135. // summary: props.desc,
  136. // href: props.link,
  137. // imageUrl: props.image || 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni@2x.png',
  138. // success: function(res) {
  139. // emit('success', {
  140. // scene
  141. // })
  142. // },
  143. // fail: function(err) {
  144. // emit('error', err)
  145. // handleShareError(err)
  146. // }
  147. // })
  148. // }
  149. // 错误处理
  150. const handleShareError = (error) => {
  151. console.error('分享失败:', error)
  152. const errCode = error.errCode || error.code
  153. const errMsg = error.errMsg || error.message
  154. // 微信未安装
  155. if (errCode === 1004 || errCode === -4004) {
  156. uni.showModal({
  157. title: '提示',
  158. content: '请先安装微信',
  159. showCancel: false
  160. })
  161. return
  162. }
  163. // 用户取消不提示
  164. if (errCode === 1001 || errCode === -4001) {
  165. return
  166. }
  167. // 其他错误
  168. uni.showToast({
  169. title: errMsg || '分享失败,请重试',
  170. icon: 'none',
  171. duration: 3000
  172. })
  173. }
  174. // 暴露方法给父组件
  175. defineExpose({
  176. open,
  177. close
  178. })
  179. </script>