feat: add wxshare

This commit is contained in:
Rogee
2025-04-30 17:06:10 +08:00
parent af0507d0c1
commit 42c1c17c0a
24 changed files with 313 additions and 147 deletions

View File

@@ -0,0 +1,14 @@
export function useRandom() {
function string(size) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for (let i = 0; i < size; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
return {
string,
};
}

View File

@@ -0,0 +1,80 @@
import wx from "weixin-js-sdk";
export function useWxSDK() {
/**
* 初始化设置
*/
function initConfig(config) {
return new Promise((resolve) => {
wx.config({
debug: config.debug,
appId: config.appId,
timestamp: config.timestamp,
nonceStr: config.nonceStr,
signature: config.signature,
jsApiList: [
"chooseImage",
"uploadImage",
"previewImage",
"onMenuShareTimeline",
"onMenuShareAppMessage",
"chooseWXPay",
],
openTagList: [],
});
wx.ready(() => {
console.log("wx.ready called");
resolve(true);
});
});
}
/** 设置微信分享 */
function setShareInfo(
shareInfo,
onSuccess = () => { },
onCancel = () => { }
) {
wx.onMenuShareTimeline({
title: shareInfo.title, // 分享标题
link: shareInfo.link, // 分享链接可以不是当前页面该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: shareInfo.imgUrl,
success: function () {
// 用户确认分享后执行的回调函数
onSuccess();
},
cancel: function () {
onCancel();
// 用户取消分享后执行的回调函数
},
});
wx.onMenuShareAppMessage({
title: shareInfo.title, // 分享标题
desc: shareInfo.desc,
link: shareInfo.link, // 分享链接可以不是当前页面该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: shareInfo.imgUrl,
type: "link", // 分享类型,music、video或link不填默认为link
success: function (e) {
// 用户确认分享后执行的回调函数
onSuccess();
console.log("分享成功", e);
},
cancel: function (e) {
// 用户取消分享后执行的回调函数
onCancel();
console.log("分享取消", e);
},
});
}
/** 是否是ios微信 */
function isiOSWechat() {
return window.__wxjs_is_wkwebview;
}
return {
initConfig,
setShareInfo,
isiOSWechat,
};
}