feat: add wxshare
This commit is contained in:
14
frontend/wechat/src/hooks/useRandom.js
Normal file
14
frontend/wechat/src/hooks/useRandom.js
Normal 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,
|
||||
};
|
||||
}
|
||||
80
frontend/wechat/src/hooks/useWxSDK.js
Normal file
80
frontend/wechat/src/hooks/useWxSDK.js
Normal 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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user