index.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. currentPage: {
  61. type: String,
  62. default: ''
  63. },
  64. })
  65. const emit = defineEmits(['close', 'select', 'success', 'error'])
  66. const popupRef = ref(null)
  67. // 打开弹窗
  68. const open = () => {
  69. popupRef.value.open()
  70. }
  71. // 关闭弹窗
  72. const close = () => {
  73. popupRef.value.close()
  74. emit('close')
  75. }
  76. // 选择分享方式
  77. const select = (scene) => {
  78. //emit('select', scene)
  79. try {
  80. if (props.currentPage == 'haibao') {
  81. shareToWeChatHaibao(scene)
  82. } else {
  83. shareToWeChat(scene)
  84. }
  85. close()
  86. } catch (error) {
  87. emit('error', error)
  88. handleShareError(error)
  89. }
  90. }
  91. // 分享到微信
  92. const shareToWeChat = (scene) => {
  93. // 获取分享类型,确保是数字类型
  94. const shareType = Number(props.type)
  95. // 构建基础分享配置
  96. const shareConfig = {
  97. provider: 'weixin',
  98. scene: scene === 'timeline' ? 'WXSceneTimeline' : 'WXSceneSession',
  99. type: shareType,
  100. success: function(res) {
  101. emit('success', {
  102. scene
  103. })
  104. },
  105. fail: function(err) {
  106. emit('error', err)
  107. handleShareError(err)
  108. }
  109. }
  110. // 根据类型添加不同的参数
  111. if (shareType == 2) {
  112. // 纯图片分享
  113. shareConfig.imageUrl = props.image
  114. } else {
  115. // 图文或链接分享(兼容原有逻辑)
  116. shareConfig.title = props.title
  117. shareConfig.summary = props.desc
  118. shareConfig.imageUrl = props.image
  119. shareConfig.href = props.link
  120. }
  121. console.log('分享配置:', shareConfig)
  122. uni.share(shareConfig)
  123. }
  124. // 分享针对海报
  125. const shareToWeChatHaibao = (scene) => {
  126. uni.downloadFile({
  127. url: props.image, // 你的网络图片地址
  128. success: (downloadRes) => {
  129. // 获取分享类型,确保是数字类型
  130. const shareType = Number(props.type)
  131. // 构建基础分享配置
  132. const shareConfig = {
  133. provider: 'weixin',
  134. scene: scene === 'timeline' ? 'WXSceneTimeline' : 'WXSceneSession',
  135. type: shareType,
  136. success: function(res) {
  137. emit('success', {
  138. scene
  139. })
  140. },
  141. fail: function(err) {
  142. emit('error', err)
  143. handleShareError(err)
  144. }
  145. }
  146. // 根据类型添加不同的参数
  147. if (shareType == 2) {
  148. // 纯图片分享
  149. // shareConfig.imageUrl = props.image
  150. shareConfig.imageUrl = downloadRes.tempFilePath
  151. } else {
  152. // 图文或链接分享(兼容原有逻辑)
  153. shareConfig.title = props.title
  154. shareConfig.summary = props.desc
  155. shareConfig.imageUrl = props.image
  156. shareConfig.href = props.link
  157. }
  158. console.log('分享配置:', shareConfig)
  159. uni.share(shareConfig)
  160. }
  161. }).fail(() => {
  162. // 其他错误
  163. uni.showToast({
  164. title: '海报分享失败,请检测网络状态',
  165. icon: 'none',
  166. duration: 3000
  167. })
  168. })
  169. }
  170. // const shareToWeChat = (scene) => {
  171. // console.log('props.link',props.link);
  172. // uni.share({
  173. // provider: 'weixin',
  174. // scene: scene === 'timeline' ? 'WXSceneTimeline' : 'WXSceneSession',
  175. // type: Number(props.type),
  176. // title: props.title,
  177. // summary: props.desc,
  178. // href: props.link,
  179. // imageUrl: props.image || 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni@2x.png',
  180. // success: function(res) {
  181. // emit('success', {
  182. // scene
  183. // })
  184. // },
  185. // fail: function(err) {
  186. // emit('error', err)
  187. // handleShareError(err)
  188. // }
  189. // })
  190. // }
  191. // 错误处理
  192. const handleShareError = (error) => {
  193. console.error('分享失败:', error)
  194. const errCode = error.errCode || error.code
  195. const errMsg = error.errMsg || error.message
  196. // 微信未安装
  197. if (errCode === 1004 || errCode === -4004) {
  198. uni.showModal({
  199. title: '提示',
  200. content: '请先安装微信',
  201. showCancel: false
  202. })
  203. return
  204. }
  205. // 用户取消不提示
  206. if (errCode === 1001 || errCode === -4001) {
  207. return
  208. }
  209. // 其他错误
  210. uni.showToast({
  211. title: errMsg || '分享失败,请重试',
  212. icon: 'none',
  213. duration: 3000
  214. })
  215. }
  216. // 暴露方法给父组件
  217. defineExpose({
  218. open,
  219. close
  220. })
  221. </script>