EASYAIuniappNewUI/dist/build/mp-weixin/utils/request.js
2025-02-16 11:40:12 +08:00

2 lines
2.4 KiB
JavaScript
Raw 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.

"use strict";const e=require("../common/vendor.js"),o=require("../composables/useCommon.js"),t=(n,l={},i=!0)=>{const h=n.startsWith("/")?n:`/${n}`;return new Promise(((u,d)=>{e.index.request({url:`${a()}${h}`,header:{Authorization:`Bearer ${r()}`},...l,success:async e=>{if(401===e.statusCode&&i){const{token:a}=await s(c());o.saveLoginInfo({token:a}),console.log("刷新后获取的token",a),a?(l.header={Authorization:`Bearer ${a}`},t(n,l,!1).then((e=>{u(e)})).catch((e=>{d(e)}))):d(e)}else e.data.data?u(e.data.data):u(e.data)},fail:async e=>{console.log(233333333)},complete:async e=>{console.log("请求完成",e)}})}))},s=async(o,t="/auth/refreshTokens",s=3,n=200,r=5e3)=>{const c=t.startsWith("/")?t:`/${t}`;let l=0;for(;l<s;){l++,console.log(`尝试第 ${l} 次刷新Token, refreshToken:`,o);try{return await new Promise(((t,s)=>{e.index.request({url:`${a()}${c}`,method:"POST",headers:{"Content-Type":"application/json"},data:{refreshToken:o},timeout:r,success:e=>{e.statusCode>=400?(console.log("refreshToken",e),s(e)):(console.log("refreshToken",e),t(e.data))},fail:e=>{console.error("刷新Token失败",e),s(e)}})}))}catch(i){"AbortError"===i.name?console.error(`${l} 次刷新Token超时`):console.error(`${l} 次刷新Token失败`,i)}l<s&&(console.log(`等待 ${n} 毫秒后重试...`),await new Promise((e=>setTimeout(e,n))))}return console.log("刷新Token失败已达到最大重试次数"),null},n=(t,l={},i="/file/upload")=>{const h=i.startsWith("/")?i:`/${i}`;return new Promise(((i,u)=>{e.index.uploadFile({url:`${a()}${h}`,filePath:t,name:"file",header:{Authorization:`Bearer ${r()}`},...l,success:async e=>{if(401===e.statusCode){const a=c();a||u(e);const{token:r}=await s(a);o.saveLoginInfo({token:r}),console.log("刷新后获取的token",r),r?(l.header={Authorization:`Bearer ${r}`},n(t,l).then((e=>{i(e)})).catch((e=>{u(e)}))):u(e)}else{console.log("success",e);const o=JSON.parse(e.data);o.data?i(o.data):i(o)}},fail:async e=>{if(console.log("fail",e),e){const a=c();a||u(e);const{token:r}=await s(a);o.saveLoginInfo({token:r}),console.log("刷新后获取的token",r),r?(l.header={Authorization:`Bearer ${r}`},n(t,l).then((e=>{i(e)})).catch((e=>{u(e)}))):u(e)}else u(e)}})}))},a=()=>"https://scschool.cc/api",r=()=>o.getLoginInfo().token,c=()=>o.getLoginInfo().refresh_token;exports.getBaseWsURL=()=>"wss://scschool.cc/websocket",exports.request=t,exports.uploadFile=n;