48 lines
1.7 KiB
TypeScript
48 lines
1.7 KiB
TypeScript
import axios from "axios";
|
|
|
|
// 初始化微信js-sdk配置
|
|
const initConfig = (config: any) => {
|
|
function jsdkCB(data: any) {
|
|
let jsApiList = ["showWatermark"];
|
|
if (typeof window.wx !== "object") {
|
|
return;
|
|
}
|
|
if (!config) {
|
|
config = {};
|
|
}
|
|
jsApiList = jsApiList.concat(config.jsApiList || []);
|
|
const wxError = {
|
|
status: "",
|
|
errmsg: "配置失败,请退出重试"
|
|
};
|
|
if (data.appid) {
|
|
window.wx.config({
|
|
beta: true,
|
|
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
|
|
appId: data.appid, // 必填,政务微信的cropID
|
|
timestamp: data.timestamp, // 必填,生成签名的时间戳
|
|
nonceStr: data.nonce, // 必填,生成签名的随机串
|
|
signature: data.signature, // 必填,签名,见附录1
|
|
jsApiList: jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
|
|
});
|
|
window.wx.ready(function (e: any) {
|
|
console.log("config注册成功");
|
|
// window['_initSuccess'] = true;
|
|
// 注册政务微信权限API,需要AgentId
|
|
// wxApi.registerAgentConfig(data, config);
|
|
typeof config.sucesscb === "function" && config.sucesscb(e);
|
|
});
|
|
window.wx.error(function (e: any) {
|
|
console.log("initConfig wx error:", e);
|
|
typeof config.errorcb === "function" && config.errorcb(wxError);
|
|
});
|
|
}
|
|
}
|
|
axios.get("/zwwxgzt/jsapi").then((res) => {
|
|
res && jsdkCB(res);
|
|
});
|
|
};
|
|
|
|
export {
|
|
initConfig
|
|
}; |