auth.js 718 B

12345678910111213141516171819202122232425262728
  1. module.exports = async function(key = true) {
  2. if (this.authInfo) { // 多次执行auth时如果第一次成功后续不再执行
  3. return;
  4. }
  5. // 获取请求参数
  6. const params = this.getParams();
  7. // 获取token
  8. let token = this.getUniIdToken();
  9. // 如果没有token,尝试从参数中获取
  10. if (!token && params[0]) {
  11. token = params[0].uniIdToken || params[0].uni_id_token;
  12. }
  13. const payload = await this.uniIdCommon.checkToken(token);
  14. if (payload.errCode) {
  15. if (key) {
  16. throw payload;
  17. } else {
  18. return;
  19. }
  20. }
  21. this.authInfo = payload;
  22. if (payload.token && typeof this.response === "object") {
  23. this.response.newToken = {
  24. token: payload.token,
  25. tokenExpired: payload.tokenExpired
  26. }
  27. }
  28. }