143 lines
6.2 KiB
JavaScript
143 lines
6.2 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.WeiXinJsSdk = void 0;
|
|
var tslib_1 = require("tslib");
|
|
var Feature_1 = require("oak-frontend-base/lib/types/Feature");
|
|
var env_1 = require("../utils/env");
|
|
var lodash_1 = require("oak-domain/lib/utils/lodash");
|
|
// const weixin = require('weixin-js-sdk');
|
|
var weixin_js_sdk_1 = tslib_1.__importDefault(require("weixin-js-sdk"));
|
|
var WeiXinJsSdk = /** @class */ (function (_super) {
|
|
tslib_1.__extends(WeiXinJsSdk, _super);
|
|
function WeiXinJsSdk(cache, storage) {
|
|
var _this = _super.call(this) || this;
|
|
_this.cache = cache;
|
|
_this.storage = storage;
|
|
return _this;
|
|
}
|
|
WeiXinJsSdk.prototype.signatureJsSDK = function (url) {
|
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
var env, result;
|
|
return tslib_1.__generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0: return [4 /*yield*/, (0, env_1.getEnv)()];
|
|
case 1:
|
|
env = _a.sent();
|
|
return [4 /*yield*/, this.cache.exec('signatureJsSDK', {
|
|
url: url,
|
|
env: env,
|
|
})];
|
|
case 2:
|
|
result = (_a.sent()).result;
|
|
return [2 /*return*/, result];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
WeiXinJsSdk.prototype.wxConfig = function (config) {
|
|
return new Promise(function (resolve, reject) {
|
|
weixin_js_sdk_1.default.config(config);
|
|
weixin_js_sdk_1.default.ready(function () {
|
|
console.log('调用wx.config通过');
|
|
resolve('');
|
|
});
|
|
weixin_js_sdk_1.default.error(function (err) {
|
|
var error = "\u8C03\u7528wx.config\u51FA\u9519: ".concat(JSON.stringify(err), "\uFF0C\u8BF7\u91CD\u65B0\u5C1D\u8BD5");
|
|
reject(error);
|
|
});
|
|
});
|
|
};
|
|
WeiXinJsSdk.prototype.initWeiXinJsSDK = function (options) {
|
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
var _a, jsApiList, openTagList, url, splitUrl, result, jsApiList2, openTagList2;
|
|
return tslib_1.__generator(this, function (_b) {
|
|
switch (_b.label) {
|
|
case 0:
|
|
_a = options || {}, jsApiList = _a.jsApiList, openTagList = _a.openTagList;
|
|
url = window.location.href;
|
|
splitUrl = url.split('#')[0];
|
|
return [4 /*yield*/, this.signatureJsSDK(splitUrl)];
|
|
case 1:
|
|
result = _b.sent();
|
|
jsApiList2 = [
|
|
'updateAppMessageShareData',
|
|
'updateTimelineShareData',
|
|
'onMenuShareTimeline',
|
|
'onMenuShareAppMessage',
|
|
'onMenuShareQQ',
|
|
'onMenuShareWeibo',
|
|
'getLocation',
|
|
'chooseWXPay',
|
|
'scanQRCode',
|
|
'openLocation',
|
|
'chooseImage',
|
|
'previewImage',
|
|
'uploadImage',
|
|
'downloadImage',
|
|
];
|
|
if (jsApiList && jsApiList instanceof Array) {
|
|
jsApiList2 = (0, lodash_1.uniq)(jsApiList2.concat(jsApiList));
|
|
}
|
|
openTagList2 = ['wx-open-launch-weapp'];
|
|
if (openTagList && openTagList instanceof Array) {
|
|
openTagList2 = (0, lodash_1.uniq)(openTagList2.concat(openTagList));
|
|
}
|
|
return [2 /*return*/, this.wxConfig({
|
|
debug: process.env.NODE_ENV === 'development',
|
|
appId: result.appId,
|
|
timestamp: result.timestamp,
|
|
nonceStr: result.noncestr,
|
|
signature: result.signature,
|
|
jsApiList: jsApiList2,
|
|
beta: false,
|
|
openTagList: openTagList2,
|
|
})];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
/**
|
|
* 微信jssdk 传入方法名
|
|
*/
|
|
WeiXinJsSdk.prototype.loadWeiXinJsSDK = function (name, options, jsApiList, openTagList) {
|
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
/**
|
|
* 将小程序的API封装成支持Promise的API
|
|
*/
|
|
function wxPromisify(fn) {
|
|
return function (obj) {
|
|
if (obj === void 0) { obj = {}; }
|
|
return new Promise(function (resolve, reject) {
|
|
obj.success = function (res) {
|
|
resolve(res);
|
|
};
|
|
obj.fail = function (res) {
|
|
reject(res);
|
|
};
|
|
// 微信jsSdk api有cancel回调
|
|
obj.cancel = function () {
|
|
reject({ errMsg: 'request:cancel' });
|
|
};
|
|
fn(obj);
|
|
});
|
|
};
|
|
}
|
|
var fn, result;
|
|
return tslib_1.__generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0: return [4 /*yield*/, this.initWeiXinJsSDK({ jsApiList: jsApiList, openTagList: openTagList })];
|
|
case 1:
|
|
_a.sent();
|
|
fn = wxPromisify(weixin_js_sdk_1.default[name]);
|
|
return [4 /*yield*/, fn(options)];
|
|
case 2:
|
|
result = _a.sent();
|
|
return [2 /*return*/, result];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
return WeiXinJsSdk;
|
|
}(Feature_1.Feature));
|
|
exports.WeiXinJsSdk = WeiXinJsSdk;
|