https://segmentfault.com/a/1190000021966277
在app.js中加上:
//接口请求地址
wx.$requestServe = "http://localhost:3087/employee_applet";
//async、await
function promisify(fn) {
return async function (args) {
return new Promise((resolve, reject) => {
fn({
...(args || {}),
success: (res) => resolve(res),
fail: (err) => reject(err),
});
});
};
}
function toAsync(...names) {
return (names || [])
.map((name) => ({ name, member: wx[name] }))
.filter((t) => typeof t.member === "function")
.reduce((r, t) => {
r[t.name] = promisify(wx[t.name]);
return r;
}, {});
}
Promise.prototype.ignoreError = function () {
return this.catch(() => {});
};
wx.$awx = toAsync("request");
App({
onShow(option) {},
globalData: {},
});
3、在页面中使用:
async onShow() {
let res = await wx.$awx
.request({
url: `${wx.$requestServe}/compareFaceToIdCard`,
})
.ignoreError();
console.log(res);
}