| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- import qs from 'qs'
- import axios from 'axios'
- import { Toast } from 'vant'
- import cleanDeep from 'clean-deep'
- import { sessionStorage as storage } from 'js-storage'
- import setLoading from '@/utils/loading'
- const instance = axios.create({
- baseURL: '/api-teacher'
- })
- instance.interceptors.request.use(config => {
- if (config.hideLoading !== true) {
- setLoading(true)
- }
- config.data = cleanDeep(config.data)
- config.params = cleanDeep(config.params)
- if (config.method.toLocaleUpperCase() === 'POST' && config.requestType === 'form') {
- config.data = qs.stringify(config.data)
- }
- const Authorization = storage.get('token') || localStorage.getItem('Authorization') || localStorage.getItem('userInfo')
- if (config.baseURL !== '/api-auth' && Authorization) {
- config.headers = {
- ...config.headers,
- Authorization,
- }
- }
- const tenantId = sessionStorage.getItem('tenantId')
- if(tenantId && tenantId != 'undefined') {
- config.headers['tenantId'] = tenantId
- }
- return config
- })
- instance.interceptors.response.use(res => {
- if (res.config.hideLoading !== true) {
- setLoading(false)
- }
- if (!(res.status > 200 || res.status < 200)) {
- if (res.data.code === 200) {
- return res.data
- } else if(res.data.code == 100 || res.data.code == 201) { // 支付时会用到的自定交code
- return res.data
- } else {
- if (res.config.hint !== true) {
- Toast(res.data.msg || '接口返回错误')
- }
- return Promise.reject(res.data)
- }
- } else {
- if (res.config.hint !== true) {
- Toast(res.data.msg || '接口返回错误')
- }
- return Promise.reject('网络错误')
- }
- }, () => {
- setLoading(false)
- })
- export default instance
|