index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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=1: 纯图片分享(只需要 imageUrl)
  40. // type=2: 链接分享(需要 title + summary + href + imageUrl)
  41. type: {
  42. type: String,
  43. default: "0" // 0:图文
  44. },
  45. // 分享描述
  46. desc: {
  47. type: String,
  48. default: '分享描述'
  49. },
  50. // 分享链接
  51. link: {
  52. type: String,
  53. default: ''
  54. },
  55. // 分享图片
  56. image: {
  57. type: String,
  58. default: ''
  59. },
  60. })
  61. const emit = defineEmits(['close', 'select', 'success', 'error'])
  62. const popupRef = ref(null)
  63. // 打开弹窗
  64. const open = () => {
  65. popupRef.value.open()
  66. }
  67. // 关闭弹窗
  68. const close = () => {
  69. popupRef.value.close()
  70. emit('close')
  71. }
  72. // 选择分享方式
  73. const select = (scene) => {
  74. //emit('select', scene)
  75. try {
  76. shareToWeChat(scene)
  77. close()
  78. } catch (error) {
  79. emit('error', error)
  80. handleShareError(error)
  81. }
  82. }
  83. // 分享到微信
  84. const shareToWeChat = (scene) => {
  85. console.log('props.link',props.link);
  86. uni.share({
  87. provider: 'weixin',
  88. scene: scene === 'timeline' ? 'WXSceneTimeline' : 'WXSceneSession',
  89. type: Number(props.type),
  90. title: props.title,
  91. summary: props.desc,
  92. href: props.link,
  93. imageUrl: props.image || 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni@2x.png',
  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. // 错误处理
  106. const handleShareError = (error) => {
  107. console.error('分享失败:', error)
  108. const errCode = error.errCode || error.code
  109. const errMsg = error.errMsg || error.message
  110. // 微信未安装
  111. if (errCode === 1004 || errCode === -4004) {
  112. uni.showModal({
  113. title: '提示',
  114. content: '请先安装微信',
  115. showCancel: false
  116. })
  117. return
  118. }
  119. // 用户取消不提示
  120. if (errCode === 1001 || errCode === -4001) {
  121. return
  122. }
  123. // 其他错误
  124. uni.showToast({
  125. title: errMsg || '分享失败,请重试',
  126. icon: 'none',
  127. duration: 3000
  128. })
  129. }
  130. // 暴露方法给父组件
  131. defineExpose({
  132. open,
  133. close
  134. })
  135. </script>