ShareDialog.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <template>
  2. <Popup ref="popRef" title="分享到" mode="bottom" isClose>
  3. <template #content>
  4. <view class="share-options">
  5. <!-- 微信 -->
  6. <view class="share-option" @click="shareToWechat('friend')">
  7. <view class="option-icon">
  8. <up-icon name="weixin-fill" color="#07C160" size="48"></up-icon>
  9. </view>
  10. <trans class="option-text" _t="微信"></trans>
  11. </view>
  12. <!-- 微信朋友圈 -->
  13. <view class="share-option" @click="shareToWechat('timeline')">
  14. <view class="option-icon">
  15. <up-icon
  16. name="moments-circel-fill"
  17. color="#07C160"
  18. size="48"
  19. ></up-icon>
  20. </view>
  21. <trans class="option-text" _t="朋友圈"></trans>
  22. </view>
  23. </view>
  24. </template>
  25. <template #footer>
  26. <!-- 取消按钮 -->
  27. <view class="cancel-btn" @click="close">
  28. <trans _t="取消" />
  29. </view>
  30. </template>
  31. </Popup>
  32. </template>
  33. <script setup>
  34. import { ref, getCurrentInstance } from "vue";
  35. import { Toast } from "@/utils";
  36. import Popup from "./popup.vue";
  37. const popRef = ref(null);
  38. const emit = defineEmits(["open", "close"]);
  39. // 分享内容配置
  40. const shareConfig = ref({
  41. title: "推荐一个好内容",
  42. description: "快来看看这个页面吧",
  43. imageUrl: "/static/login.png", // 默认分享图片
  44. });
  45. // 获取当前页面URL
  46. const getCurrentPageUrl = () => {
  47. // 获取当前页面实例
  48. const pages = getCurrentPages();
  49. const currentPage = pages[pages.length - 1];
  50. const pagePath = currentPage.route;
  51. const query = currentPage.options;
  52. // 拼接参数
  53. let queryStr = "";
  54. if (Object.keys(query).length > 0) {
  55. queryStr =
  56. "?" +
  57. Object.entries(query)
  58. .map(([k, v]) => `${k}=${v}`)
  59. .join("&");
  60. }
  61. console.log(`https://vavabuy.net/#/${pagePath}${queryStr}`, "pages");
  62. // 实际项目中这里应该是你们的H5地址 + 页面路径
  63. return `https://vavabuy.net/#/${pagePath}${queryStr}`;
  64. };
  65. // 打开分享弹框
  66. const open = () => {
  67. uni.getProvider({
  68. service: "share",
  69. success: (res) => {
  70. if (!res.provider.includes("weixin")) {
  71. Toast("当前环境不支持微信分享");
  72. }
  73. },
  74. });
  75. popRef.value && popRef.value.open();
  76. };
  77. // 关闭分享弹框
  78. const close = () => {
  79. popRef.value && popRef.value.close();
  80. emit("close");
  81. };
  82. // 分享到微信
  83. const shareToWechat = (type) => {
  84. close();
  85. // 获取当前页面URL作为分享链接
  86. const shareUrl = getCurrentPageUrl();
  87. // 配置分享参数
  88. const shareParams = {
  89. type: "weixin",
  90. title: shareConfig.value.title,
  91. content: shareConfig.value.description,
  92. href: shareUrl,
  93. thumbs: shareConfig.value.imageUrl,
  94. scene: type === "friend" ? "WXSceneSession" : "WXSceneTimeline",
  95. };
  96. uni.share({
  97. provider: "weixin",
  98. scene: shareParams.scene,
  99. type: 0, // 0:图文(链接),1:纯文字,2:图片,3:音乐,4:视频
  100. title: shareParams.title,
  101. summary: shareParams.content,
  102. href: shareParams.href,
  103. imageUrl: shareParams.thumbs,
  104. success: () => {
  105. Toast("分享成功!");
  106. },
  107. fail: (err) => {
  108. Toast(`分享失败: ${err.errMsg}`);
  109. },
  110. });
  111. };
  112. // 设置分享内容
  113. const setShareConfig = (config) => {
  114. shareConfig.value = { ...shareConfig.value, ...config };
  115. };
  116. // 暴露方法给父组件
  117. defineExpose({ setShareConfig, open, close });
  118. </script>
  119. <style lang="scss" scoped>
  120. // 分享选项
  121. .share-options {
  122. width: 100%;
  123. display: flex;
  124. justify-content: center;
  125. padding: 20rpx 0;
  126. }
  127. // 分享选项项
  128. .share-option {
  129. display: flex;
  130. flex-direction: column;
  131. align-items: center;
  132. margin: 0 60rpx;
  133. cursor: pointer;
  134. }
  135. // 选项图标容器
  136. .option-icon {
  137. width: 120rpx;
  138. height: 120rpx;
  139. border-radius: 50%;
  140. background-color: #f5f5f5;
  141. display: flex;
  142. align-items: center;
  143. justify-content: center;
  144. margin-bottom: 15rpx;
  145. }
  146. // 朋友圈图标特殊样式
  147. .circle-bg {
  148. position: relative;
  149. &::after {
  150. content: "";
  151. position: absolute;
  152. width: 30rpx;
  153. height: 30rpx;
  154. border-radius: 50%;
  155. background-color: #07c160;
  156. bottom: 10rpx;
  157. right: 10rpx;
  158. border: 4rpx solid #fff;
  159. }
  160. }
  161. // 选项文本
  162. .option-text {
  163. font-size: 28rpx;
  164. color: #666;
  165. }
  166. // 取消按钮
  167. .cancel-btn {
  168. width: 90%;
  169. height: 90rpx;
  170. line-height: 90rpx;
  171. background-color: #f5f5f7;
  172. color: #333;
  173. text-align: center;
  174. font-size: 32rpx;
  175. border-radius: 45rpx;
  176. margin: 20rpx auto 0;
  177. display: block;
  178. border: none;
  179. }
  180. </style>