Model/assets/Script/module/Share/share.ts

105 lines
3.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Fx } from "../GameStart/GameAppStart";
var shareConfig = {
gameId: "100009",
shareLine: "zDLsruVI",
EK:"hui231%1"
};
// 定义微信配置数据的接口
interface IWeChatConfig {
appId: string;
timestamp: number;
nonceStr: string;
signature: string;
jsApiList: [];
}
// 微信操作类
export class WeChat {
static setShare(url) {
var urlTemp = this.removeQueryParams(url);
shareConfig.shareLine = urlTemp;
WeChat.getSignature(url);
}
static getResult(res){
if(res){
var data = res.data;
// @ts-ignore
wx.config({
debug: false,
appId: data.appId,
timestamp: data.timestamp,
nonceStr: data.nonceStr,
signature: data.signature,
jsApiList: ['onMenuShareTimeline','updateAppMessageShareData', 'updateTimelineShareData', 'onMenuShareAppMessage']
});
// @ts-ignore
wx.checkJsApi({
jsApiList: ['updateAppMessageShareData'], // 需要检测的JS接口列表所有JS接口列表见附录2,
success: function(res) {
setTimeout(() => {
WeChat.changeShare();
}, 200);
setTimeout(() => {
WeChat.changeShare();
}, 500);
}
});
}
}
static changeShare(){
// @ts-ignore
wx.ready(() => {
// @ts-ignore
wx.updateAppMessageShareData({
title: '记忆力认知测评', // 分享标题
desc: '你的注意力和工作记忆有问题吗?', // 分享描述
link: shareConfig.shareLine, // 分享链接该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: 'https://static.sparkus.cn/public/shootsun.jpg', // 分享图标
success: function () {
// 设置成功
console.log("分享好友成功回调");
}
});
setTimeout(() => {
// @ts-ignore
wx.updateTimelineShareData({
title: '记忆力认知测评', // 分享标题
link: shareConfig.shareLine, // 分享链接该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: 'https://static.sparkus.cn/public/shootsun.jpg', // 分享图标
success: function () {
// 设置成功
console.log("分享朋友圈成功回调");
}
})
}, 200);
});
}
static getSignature(url: string): Promise<IWeChatConfig> {
return new Promise((resolve) => {
WeChat.getShareInfo((encodeURIComponent(url)),WeChat.getResult);
});
}
static async getShareInfo(shareUrl: string, callback:Function): Promise<any> {
const time = Math.floor((new Date().getTime()) / 1000)
const url = Fx.HttpUtil.apiSign(`/api/share/cfg?gameId=${Fx.GameConfig.GM_INFO.gameId}&time=${time}&url=${shareUrl}`,{})
return Fx.HttpUtil.get(url,callback,3)
}
static containsNanana(str) {
return /test/i.test(str);
}
static removeQueryParams(url) {
return url.replace(/\?.*$/, '');
}
}