You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
看了大佬对 axios 的封装,参照着封装后,我的类型是
但是看大佬的封装,request 是 any 类型
我在 api 层想通过泛型的方式去拿到返回值的具体类型,会报如下错误
完整代码如下
// request.tsimporttype{AxiosRequestConfig,AxiosResponse}from'axios'importaxiosfrom'axios'importqsfrom'qs'importcookiefrom'js-cookie'importstoragefrom'store2'import{message}from'@/utils/message'import{TOKEN,LOCALE_LANG,TENANT_ID}from'@/constants/store'import{LOGIN_URL}from'@/constants/links'// invalid token codeconstFORBIDDEN_TOKEN_CODE='40301'// invalid authconstFORBIDDEN_AUTH_CODE='40302'// successconstSUCCESS_CODE='0'constrequest=axios.create({baseURL: '/api',timeout: 30000,headers: {'X-Requested-With': 'XMLHttpRequest'}})consterrorHandler=(error: any)=>{if(error.response){const{ status, statusText }=error.responseif(status===403){message('禁止的操作!')}else{message(statusText||'HTTP 错误')}returnPromise.reject(error.response)}returnPromise.reject(error)}constqueryParamsSerializer=(params: object)=>qs.stringify(params,{arrayFormat: 'comma'})request.interceptors.request.use((config: AxiosRequestConfig)=>{consttoken=cookie.get(TOKEN)constlanguage=storage.get(LOCALE_LANG)consttenant=cookie.get(TENANT_ID)// if not loginif(token){config.headers!.fauthorization=token}else{// if have tenant redirect to login pageif(tenant){consturl=encodeURIComponent(window.location.href)returnwindow.location.href=`${LOGIN_URL}?url=${url}&tenant=${tenant}&systemSource=CLOUD_OA`}else{// not have tenant redirect to guide pagereturn`${import.meta.env.VITE_APP_PROJECT_URL}/401`}}if(language){config.headers!.language=language}config.paramsSerializer=queryParamsSerializerreturnconfig},errorHandler)constneedCompleteResponse=(response: AxiosResponse)=>{const{ responseType }=response.requestreturnresponseType==='blob'}request.interceptors.response.use((response: AxiosResponse)=>{if(needCompleteResponse(response)){returnresponse}const{data: httpData}=responseconst{ code, data,message: msg}=httpDataconstcodeStr=`${code}`switch(codeStr){caseSUCCESS_CODE:
returndatacaseFORBIDDEN_TOKEN_CODE: {consttenant=cookie.get('TENANT_ID')if(tenant){consturl=encodeURIComponent(window.location.href)returnwindow.location.href=`${LOGIN_URL}?url=${url}&tenant=${tenant}&systemSource=CLOUD_OA`}else{// not have tenant redirect to guide pagereturn`${import.meta.env.VITE_APP_PROJECT_URL}/401`}}caseFORBIDDEN_AUTH_CODE:
return`${import.meta.env.VITE_APP_PROJECT_URL}/401`default:
message(msg||'业务错误')returnPromise.reject(httpData)}},errorHandler)exportdefaultrequest
// api.tsimporttype{AxiosPromise}from'axios'importtype{IRootObject}from'@/model/rootObject'importtype{IUser,ITenantItem,IMenuItem}from'@/model/userModel'importrequestfrom'@/utils/request'constBASE_URL='/api'exportconstapi={info: '/resources/user/info',}/** * get user basic info * @return {void} user basic info */exportconstfetchUser=()=>request<IRootObject<IUser>>({baseURL: BASE_URL,url: api.info,method: 'post'})
谢谢大佬
The text was updated successfully, but these errors were encountered:
看了大佬对 axios 的封装,参照着封装后,我的类型是
但是看大佬的封装,request 是 any 类型
我在 api 层想通过泛型的方式去拿到返回值的具体类型,会报如下错误
完整代码如下
谢谢大佬
The text was updated successfully, but these errors were encountered: