import wx from 'weixin-js-sdk'; import axios from "axios"; import { Toast } from "vant"; import {getShareConfig } from '@/api' const imgUrl = new URL(`@/assets/images/share.jpg`, import.meta.url).href const linkUrl = import.meta.env.VITE_HOST + import.meta.env.VITE_FOLDER export function wxShare(option) { let url = location.href.split("#")[0]; getShareConfig({url:url}) .then((res) => { console.log('分享配置:', { 'title': option.title, 'desc': option.desc, 'link': url }); let data = res.data; wx.config({ debug: false, // 开启调试模式 appId: import.meta.env.VITE_APPID, // 必填,公众号的唯一标识 timestamp: data.timestamp, // 必填,生成签名的时间戳 nonceStr: data.nonceStr, // 必填,生成签名的随机串 signature: data.signature, // 必填,签名,见附录1 jsApiList: [ 'checkJsApi', 'showMenuItems', 'onMenuShareAppMessage', 'onMenuShareTimeline', ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 }) wx.ready(function () { wx.onMenuShareTimeline({ link: import.meta.env.VITE_URL, // 分享链接 title: option.title, // 分享标题 desc: option.desc, // 分享描述 imgUrl: imgUrl, // 分享图标 success() {// 用户成功分享后执行的回调函数 Toast('分享成功') }, }); wx.onMenuShareAppMessage({ link: import.meta.env.VITE_URL, // 分享链接 title: option.title, // 分享标题 desc: option.desc, // 分享描述 imgUrl: imgUrl, // 分享图标 success() {// 用户成功分享后执行的回调函数 Toast('分享成功') }, }) }) }) .catch(() => { }); }