1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import {
- http,
- toast
- } from '@/uni_modules/uview-plus'
- import store from '@/store'
- const requestInterceptors = (vm) => {
-
- http.interceptors.request.use((config) => {
-
- config.data = config.data || {}
-
- console.log('config', config)
-
- config.header.Authorization = 'Bearer ' + store.state.vuex_token
- if (config?.custom?.loading) {
- uni.showLoading({
- title: '数据加载中...'
- });
- }
- return config
- }, (config) =>
- Promise.reject(config))
- }
- const responseInterceptors = (vm) => {
-
- http.interceptors.response.use((response) => {
-
- if (response?.config?.custom?.isCloseLoad) {
- uni.hideLoading();
- }
-
- const data = response.data
-
-
- const custom = response.config?.custom
- if (data.code !== 200) {
-
- if (custom.toast !== false) {
- toast(data.msg)
- }
- if (data.code === 401) {
- uni.showToast({
- icon: 'none',
- title: '请先登录'
- })
- uni.redirectTo({
- url: '/pages/subpack/pages/login/login'
- })
- }
-
- if (custom?.catch) {
- uni.hideLoading();
- return Promise.reject(data)
- } else {
-
- return new Promise(() => {})
- }
- }
- if (!data.data && data.rows) data.data = data.rows;
- return data.data || {}
- }, (response) => {
-
- return Promise.reject(response)
- })
- }
- export {
- requestInterceptors,
- responseInterceptors
- }
|