NGTools/uni_modules/uni-pay/uniCloud/cloudfunctions/uni-pay-co/middleware/auth.js
ldeyun 9dbfdc6c71 V1.0.0
微信小程序运行成功;
H5运行成功
2024-09-30 01:30:39 +08:00

29 lines
718 B
JavaScript
Raw Permalink 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.

module.exports = async function(key = true) {
if (this.authInfo) { // 多次执行auth时如果第一次成功后续不再执行
return;
}
// 获取请求参数
const params = this.getParams();
// 获取token
let token = this.getUniIdToken();
// 如果没有token尝试从参数中获取
if (!token && params[0]) {
token = params[0].uniIdToken || params[0].uni_id_token;
}
const payload = await this.uniIdCommon.checkToken(token);
if (payload.errCode) {
if (key) {
throw payload;
} else {
return;
}
}
this.authInfo = payload;
if (payload.token && typeof this.response === "object") {
this.response.newToken = {
token: payload.token,
tokenExpired: payload.tokenExpired
}
}
}