import { PreQuest , create } from '@prequest/miniprogram' ; import { errorRetryMiddleware } from './error-retry' ; import { lock , wrapper } from './token' ; PreQuest . defaults . baseURL = 'http://localhost:8080' ; interface Req { skipTokenCheck : boolean ; } interface Res { } export const prequest = create < Req , Res > ( uni . request ) ; prequest . use ( errorRetryMiddleware ) . use ( async ( ctx , next ) => { if ( ctx . request . skipTokenCheck ) return next ( ) ; const token = await wrapper ( getToken ) ; console . log ( '中间件 token' , token ) ; await next ( ) ; } ) . use ( async ( ctx , next ) => { await next ( ) ; const { data , statusCode } = ctx . response ; if ( statusCode !== 200 ) { if ( statusCode === 401 ) { lock . clear ( ) ; } throw new Error ( data as string || '响应数据有误' ) ; } ctx . response = data as any ; } ) ; function getToken ( ) { return prequest ( '/token' , { skipTokenCheck : true } ) ; }