| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <template>
- <Popup ref="popRef" title="分享到" mode="bottom" isClose>
- <template #content>
- <view class="share-options">
- <!-- 微信 -->
- <view class="share-option" @click="shareToWechat('friend')">
- <view class="option-icon">
- <up-icon name="weixin-fill" color="#07C160" size="48"></up-icon>
- </view>
- <trans class="option-text" _t="微信"></trans>
- </view>
- <!-- 微信朋友圈 -->
- <view class="share-option" @click="shareToWechat('timeline')">
- <view class="option-icon">
- <up-icon
- name="moments-circel-fill"
- color="#07C160"
- size="48"
- ></up-icon>
- </view>
- <trans class="option-text" _t="朋友圈"></trans>
- </view>
- </view>
- </template>
- <template #footer>
- <!-- 取消按钮 -->
- <view class="cancel-btn" @click="close">
- <trans _t="取消" />
- </view>
- </template>
- </Popup>
- </template>
- <script setup>
- import { ref, getCurrentInstance } from "vue";
- import { Toast } from "@/utils";
- import Popup from "./popup.vue";
- const popRef = ref(null);
- const emit = defineEmits(["open", "close"]);
- // 分享内容配置
- const shareConfig = ref({
- title: "推荐一个好内容",
- description: "快来看看这个页面吧",
- imageUrl: "/static/login.png", // 默认分享图片
- });
- // 获取当前页面URL
- const getCurrentPageUrl = () => {
- // 获取当前页面实例
- const pages = getCurrentPages();
- const currentPage = pages[pages.length - 1];
- const pagePath = currentPage.route;
- const query = currentPage.options;
- // 拼接参数
- let queryStr = "";
- if (Object.keys(query).length > 0) {
- queryStr =
- "?" +
- Object.entries(query)
- .map(([k, v]) => `${k}=${v}`)
- .join("&");
- }
- console.log(`https://vavabuy.net/#/${pagePath}${queryStr}`, "pages");
- // 实际项目中这里应该是你们的H5地址 + 页面路径
- return `https://vavabuy.net/#/${pagePath}${queryStr}`;
- };
- // 打开分享弹框
- const open = () => {
- uni.getProvider({
- service: "share",
- success: (res) => {
- if (!res.provider.includes("weixin")) {
- Toast("当前环境不支持微信分享");
- }
- },
- });
- popRef.value && popRef.value.open();
- };
- // 关闭分享弹框
- const close = () => {
- popRef.value && popRef.value.close();
- emit("close");
- };
- // 分享到微信
- const shareToWechat = (type) => {
- close();
- // 获取当前页面URL作为分享链接
- const shareUrl = getCurrentPageUrl();
- // 配置分享参数
- const shareParams = {
- type: "weixin",
- title: shareConfig.value.title,
- content: shareConfig.value.description,
- href: shareUrl,
- thumbs: shareConfig.value.imageUrl,
- scene: type === "friend" ? "WXSceneSession" : "WXSceneTimeline",
- };
- uni.share({
- provider: "weixin",
- scene: shareParams.scene,
- type: 0, // 0:图文(链接),1:纯文字,2:图片,3:音乐,4:视频
- title: shareParams.title,
- summary: shareParams.content,
- href: shareParams.href,
- imageUrl: shareParams.thumbs,
- success: () => {
- Toast("分享成功!");
- },
- fail: (err) => {
- Toast(`分享失败: ${err.errMsg}`);
- },
- });
- };
- // 设置分享内容
- const setShareConfig = (config) => {
- shareConfig.value = { ...shareConfig.value, ...config };
- };
- // 暴露方法给父组件
- defineExpose({ setShareConfig, open, close });
- </script>
- <style lang="scss" scoped>
- // 分享选项
- .share-options {
- width: 100%;
- display: flex;
- justify-content: center;
- padding: 20rpx 0;
- }
- // 分享选项项
- .share-option {
- display: flex;
- flex-direction: column;
- align-items: center;
- margin: 0 60rpx;
- cursor: pointer;
- }
- // 选项图标容器
- .option-icon {
- width: 120rpx;
- height: 120rpx;
- border-radius: 50%;
- background-color: #f5f5f5;
- display: flex;
- align-items: center;
- justify-content: center;
- margin-bottom: 15rpx;
- }
- // 朋友圈图标特殊样式
- .circle-bg {
- position: relative;
- &::after {
- content: "";
- position: absolute;
- width: 30rpx;
- height: 30rpx;
- border-radius: 50%;
- background-color: #07c160;
- bottom: 10rpx;
- right: 10rpx;
- border: 4rpx solid #fff;
- }
- }
- // 选项文本
- .option-text {
- font-size: 28rpx;
- color: #666;
- }
- // 取消按钮
- .cancel-btn {
- width: 90%;
- height: 90rpx;
- line-height: 90rpx;
- background-color: #f5f5f7;
- color: #333;
- text-align: center;
- font-size: 32rpx;
- border-radius: 45rpx;
- margin: 20rpx auto 0;
- display: block;
- border: none;
- }
- </style>
|