oak-general-business/lib/features/wechatSdk.web.js

121 lines
4.2 KiB
JavaScript
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.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WechatSdk = void 0;
const tslib_1 = require("tslib");
const Feature_1 = require("oak-frontend-base/es/types/Feature");
const utils_1 = require("oak-frontend-base/es/utils/utils");
const promisify_1 = require("oak-frontend-base/es/utils/promisify");
const lodash_1 = require("oak-domain/lib/utils/lodash");
const weixin_js_sdk_1 = tslib_1.__importDefault(require("weixin-js-sdk"));
class WechatSdk extends Feature_1.Feature {
cache;
storage;
environment;
landingUrl; //解决在IOS上无论路由切换到哪个页面实际真正有效的的签名URL是【第一次进入应用时的URL】;
lastConfigUrl;
constructor(cache, storage, environment) {
super();
this.cache = cache;
this.storage = storage;
this.landingUrl = undefined;
this.environment = environment;
}
async signatureJsSDK(url) {
const env = await this.environment.getEnv();
const { result } = await this.cache.exec('signatureJsSDK', {
url,
env: env,
});
return result;
}
async getConfig(config) {
return new Promise((resolve, reject) => {
weixin_js_sdk_1.default.config(config);
weixin_js_sdk_1.default.ready(() => {
console.log('调用wx.config通过');
resolve('');
});
weixin_js_sdk_1.default.error((err) => {
const error = `调用wx.config出错: ${JSON.stringify(err)},请重新尝试`;
reject(error);
});
});
}
setLandingUrl(url) {
if (utils_1.isIos && utils_1.isWeiXin) {
this.landingUrl = url;
}
}
initedApiList = [];
async init(options) {
if (!utils_1.isWeiXin) {
console.warn('只能在微信客户端初始化JSSDK');
return;
}
const { name } = options || {};
let url = window.location.href;
//在ios上 实际真正有效的的签名URL是【第一次进入应用时的URL】
if (utils_1.isIos && !utils_1.isWeiXinDevTools && this.landingUrl) {
url = this.landingUrl;
}
if (this.initedApiList.includes(name) && this.lastConfigUrl === url) {
return;
}
const splitUrl = url.split('#')[0];
const result = await this.signatureJsSDK(splitUrl); // 接口回来的是noncestr 不是nonceStr
const dftApiList = [
'updateAppMessageShareData',
'updateTimelineShareData',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'getLocation',
'chooseWXPay',
'scanQRCode',
'openLocation',
'chooseImage',
'previewImage',
'uploadImage',
'downloadImage',
];
const jsApiList = (0, lodash_1.uniq)(this.initedApiList.length > 0 ? this.initedApiList.concat([name]) : dftApiList.concat([name]));
/**
* https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html
* 文档中有四个ts声明可能不及时
* by Xc 20240321
*/
const openTagList = [
'wx-open-launch-weapp',
'wx-open-launch-app',
'wx-open-subscribe',
'wx-open-audio'
];
await this.getConfig({
debug: process.env.NODE_ENV === 'development',
appId: result.appId,
timestamp: result.timestamp,
nonceStr: result.noncestr,
signature: result.signature,
jsApiList,
openTagList,
});
this.initedApiList = jsApiList;
this.lastConfigUrl = url;
}
/**
* 微信jssdk 传入方法名
*/
async loadWxAPi(name, options) {
await this.init({ name });
const wxFn = (0, promisify_1.promisify)(weixin_js_sdk_1.default[name]);
const result = await wxFn(options);
return result;
}
async subscribeMpMessage(messageTypes, haveToAccept, tip) {
console.error('native无需该操作');
return true;
}
}
exports.WechatSdk = WechatSdk;