init commit
This commit is contained in:
162
src/services/wx/index.js
Normal file
162
src/services/wx/index.js
Normal file
@@ -0,0 +1,162 @@
|
||||
import { getConfig } from "../config";
|
||||
import { request } from "../../utils/request";
|
||||
import { toAny } from "../../utils/toany";
|
||||
|
||||
const WX_JS_SDK_URL = "https://res.wx.qq.com/open/js/jweixin-1.6.0.js";
|
||||
const WX_SCAN_API = "scanQRCode";
|
||||
|
||||
let _wxReadyPromise = null;
|
||||
let _wxReady = false;
|
||||
|
||||
function getWx() {
|
||||
if (typeof window === "undefined") {
|
||||
return null;
|
||||
}
|
||||
return window.wx;
|
||||
}
|
||||
|
||||
function loadWxScript() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const wx = getWx();
|
||||
if (wx && wx.config && wx.scanQRCode) {
|
||||
resolve(wx);
|
||||
return;
|
||||
}
|
||||
let initWechatJssdk = toAny(getConfig("initWechatJssdk"), {});
|
||||
let sdkUrl = toAny(initWechatJssdk.sdkUrl, WX_JS_SDK_URL);
|
||||
if (!sdkUrl) {
|
||||
reject(new Error("initWechatJssdk.sdkUrl is required, but not found"));
|
||||
return;
|
||||
}
|
||||
let script = document.getElementById("__wx_jssdk__");
|
||||
if (script) {
|
||||
script.addEventListener("load", () => resolve(getWx()));
|
||||
script.addEventListener("error", reject);
|
||||
return;
|
||||
}
|
||||
script = document.createElement("script");
|
||||
script.id = "__wx_jssdk__";
|
||||
script.src = sdkUrl;
|
||||
script.onload = () => resolve(getWx());
|
||||
script.onerror = reject;
|
||||
document.head.appendChild(script);
|
||||
});
|
||||
}
|
||||
|
||||
function fetchWxConfig() {
|
||||
let initWechatJssdk = toAny(getConfig("initWechatJssdk"), {});
|
||||
let apiUrl = toAny(initWechatJssdk.apiUrl, "");
|
||||
if (!apiUrl) {
|
||||
return Promise.reject(new Error("initWechatJssdk.apiUrl is required, but not found"));
|
||||
}
|
||||
return request({
|
||||
url: apiUrl,
|
||||
method: "GET",
|
||||
data: {
|
||||
url: window.location.href.split("#")[0]
|
||||
}
|
||||
}).then(res => {
|
||||
let data = toAny(res.data, {});
|
||||
if (!data) {
|
||||
return null;
|
||||
}
|
||||
if (data.code !== 0) {
|
||||
throw new Error(data.msg || "wx config fetch failed");
|
||||
}
|
||||
if (data.data) {
|
||||
return data.data;
|
||||
}
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
||||
export function isWxEnv() {
|
||||
return typeof navigator !== "undefined"
|
||||
&& /micromessenger/i.test(navigator.userAgent || "");
|
||||
}
|
||||
|
||||
export function isSupportWxScan() {
|
||||
const wx = getWx();
|
||||
return isWxEnv()
|
||||
&& _wxReady
|
||||
&& wx
|
||||
&& wx.scanQRCode;
|
||||
}
|
||||
|
||||
export function initWxJssdk() {
|
||||
if (!isWxEnv()) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
if (_wxReadyPromise) {
|
||||
return _wxReadyPromise;
|
||||
}
|
||||
_wxReadyPromise = Promise.all([loadWxScript(), fetchWxConfig()]).then(items => {
|
||||
const wx = items[0];
|
||||
const config = items[1];
|
||||
if (!wx || !wx.config) {
|
||||
throw new Error("wx jssdk is not ready");
|
||||
}
|
||||
if (!config) {
|
||||
throw new Error("wx config is empty");
|
||||
}
|
||||
const jsApiList = config.jsApiList || [];
|
||||
if (jsApiList.indexOf(WX_SCAN_API) === -1) {
|
||||
jsApiList.push(WX_SCAN_API);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.ready(() => {
|
||||
_wxReady = true;
|
||||
resolve(wx);
|
||||
});
|
||||
wx.error(err => {
|
||||
_wxReady = false;
|
||||
_wxReadyPromise = null;
|
||||
reject(err);
|
||||
});
|
||||
wx.config(Object.assign({}, config, {
|
||||
jsApiList
|
||||
}));
|
||||
});
|
||||
}).catch(err => {
|
||||
_wxReady = false;
|
||||
_wxReadyPromise = null;
|
||||
throw err;
|
||||
});
|
||||
return _wxReadyPromise;
|
||||
}
|
||||
|
||||
export function startScanForWx(options) {
|
||||
return initWxJssdk().then(() => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const {
|
||||
needResult = 1,
|
||||
scanType = ["qrCode", "barCode"]
|
||||
} = options || {};
|
||||
const wx = getWx();
|
||||
if (!wx || !wx.scanQRCode) {
|
||||
reject(new Error("wx.scanQRCode is not supported"));
|
||||
return;
|
||||
}
|
||||
wx.scanQRCode({
|
||||
needResult,
|
||||
scanType,
|
||||
success: res => {
|
||||
resolve({
|
||||
success: true,
|
||||
result: res.resultStr,
|
||||
code: res.resultStr
|
||||
});
|
||||
},
|
||||
cancel: () => {
|
||||
resolve({
|
||||
success: false,
|
||||
error: "用户取消扫码"
|
||||
});
|
||||
},
|
||||
fail: err => {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user