123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- import {
- http,
- toast
- } from '@/uni_modules/uview-plus'
- import store from '@/store'
- const requestInterceptors = (vm) => {
-
- http.interceptors.request.use((config) => {
-
- config.data = config.data || {}
-
-
-
- config.header.Authorization = 'Bearer ' + store.state.vuex_token
- return config
- }, (config) =>
- Promise.reject(config))
- }
- const responseInterceptors = (vm) => {
-
- http.interceptors.response.use((response) => {
- console.log('response: ', response);
-
-
- const data = response.data
-
-
- const custom = response.config?.custom
- if (data.meta.code !== '1') {
-
- if (custom.toast !== false) {
- toast(data.meta.message)
- }
- if (data.meta.code === 401) {
- uni.showToast({
- icon: 'none',
- title: '请先登录'
- })
- uni.redirectTo({
- url: '/pages/login/login'
- })
- }
-
- if (custom?.catch) {
- uni.hideLoading();
- return Promise.reject(data)
- } else {
-
- return new Promise(() => {})
- }
- }
- return data.data || {}
- }, (response) => {
-
- return Promise.reject(response)
- })
- }
- export {
- requestInterceptors,
- responseInterceptors
- }
|