123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- import { extend } from 'umi-request'
- import cleanDeep from 'clean-deep'
- import { browser, openLoading, closeLoading } from '@/helpers/utils'
- import { setLogout, setLoginError, state } from '@/state'
- import { postMessage } from './native-message'
- import { showLoadingToast, showToast, closeToast } from 'vant'
- export interface SearchInitParams {
- rows?: string | number
- page?: string | number
- }
- const request = extend({
- // requestType: 'form',
- timeout: 20000,
- timeoutMessage: '请求超时'
- })
- // request.use(async (ctx, next) => {
- // const { url, options } = ctx.req
- // const prefix = options.prefix || '';
- // const baseUrl: string = url.replace(prefix, '') || '';
- // const linkUrl: string = (ApiRouter as any)[baseUrl];
- // if (linkUrl) {
- // ctx.req.url = prefix + linkUrl;
- // }
- // await next();
- // })
- // 是否是初始化接口
- let initRequest = false
- let toast: ReturnType<typeof setTimeout>
- request.interceptors.request.use(
- (url, options: any) => {
- // openLoading();
- if (!options.hideLoading) {
- clearTimeout(toast)
- showLoadingToast({
- message: '加载中...',
- forbidClick: true,
- loadingType: 'spinner',
- duration: 0
- })
- }
- initRequest = options.initRequest || false
- const Authorization = sessionStorage.getItem('Authorization') || ''
- const authHeaders: any = {}
- if (
- Authorization &&
- ![
- '/api-oauth/userlogin',
- // `${state.platformApi}/appLoginUser/getUserInfo`,
- '/api-oauth/open/sendSms'
- ].includes(url)
- ) {
- authHeaders.Authorization = Authorization
- }
- if (state?.user?.data?.schoolInfos) {
- const schoolId = (state.user.data.schoolInfos || [])
- .map((item) => {
- return item.id
- })
- .join(',')
- if (schoolId) {
- // console.log('schoolId', schoolId)
- // options.data ? (options.data.schoolId = schoolId) : null
- // options.params ? (options.params.schoolId = schoolId) : null
- authHeaders.schoolId = schoolId
- }
- }
- return {
- url,
- options: {
- ...options,
- params: cleanDeep(options.params),
- data: cleanDeep(options.data),
- headers: {
- ...options.headers,
- ...authHeaders
- }
- }
- }
- },
- { global: false }
- )
- request.interceptors.response.use(
- async (res) => {
- toast = setTimeout(() => {
- closeToast()
- }, 100)
- if (res.status > 299 || res.status < 200) {
- clearTimeout(toast)
- const msg = '服务器错误,状态码' + res.status
- showToast(msg)
- throw new Error(msg)
- }
- const data = await res.clone().json()
- // 999 为特殊code码
- if (data.code !== 200 && data.errCode !== 0 && data.code !== 999) {
- let msg = data.msg || data.message || '处理失败,请重试'
- if (initRequest) {
- if (data.code === 403 || data.code === 5000) {
- setLogout()
- } else {
- setLoginError()
- }
- }
- console.log(data.code, '5104')
- if (!(data.code === 403 || data.code === 5000)) {
- clearTimeout(toast)
- showToast(msg)
- }
- const browserInfo = browser()
- if (data.code === 5000) {
- msg += '5000'
- if (browserInfo.isApp) {
- postMessage({
- api: 'login'
- })
- } else {
- setLogout()
- }
- }
- throw new Error(msg)
- }
- return res
- },
- { global: false }
- )
- export default request
|