12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- import store from '@/common/store/index.js'
- module.exports = (vm) => {
-
- uni.$u.http.setConfig((config) => {
-
- config.header = {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'Access-Control-Allow-Headers': '*'
- }
- config.baseURL = vm.$BASE_URL;
-
- config.withCredentials = true;
- return config
- })
-
- uni.$u.http.interceptors.request.use(async (config) => {
-
- let token = uni.getStorageSync('token');
- if(config.custom&&config.custom.auth&&!token){
- uni.$u.toast('请登录后再操作')
- setTimeout(()=>{
- vm.$Router.push('/pages/login/login')
- },1000)
- return new Promise(() => { })
- }
- if(config.custom&&config.custom.timeout){
- config.timeout = config.custom.timeout
- }
- if(config.method === 'GET'){
- config.header = {
- 'Content-Type': 'application/json;charset=UTF-8',
- }
- }else{
- config.header = {
- 'Content-Type': 'application/json;charset=UTF-8',
- }
- }
- if(config.url.includes('structured-law')){
-
- config.header = {
- 'Content-Type': 'application/x-www-form-urlencoded',
- }
- }
- let data = config.method==="GET" ? config.params : config.data;
- if(token) {
- config.header['x-access-token'] = token
- }
- return config;
- }, config => {
- return Promise.reject(config)
- })
-
- uni.$u.http.interceptors.response.use(async (response) => {
- const data = response.data;
- const custom = response.config.custom;
- if (data.code <= 0) {
- uni.hideLoading()
- if (!custom.toast) {
- uni.$u.toast(data.msg);
- console.log(custom)
- }
-
- if (custom?.catch) {
- return Promise.reject(data)
- } else {
-
- return new Promise(() => { })
- }
- }
- return !data ? {} : data;
- },async (response) => {
- console.log(response)
- uni.hideLoading()
-
- if (response.statusCode == 401) {
- uni.$u.toast('登录已过期,请重新登录')
- store.dispatch('userLogout')
- }else if (response.statusCode == 500) {
- uni.$u.toast('网络开了点小差,请重试');
- } else if (response.statusCode == 404) {
- return;
- } else {
- uni.$u.toast(response.data.msg);
- }
- return new Promise(() => { });
- })
- }
|