index.js 2.05 KB
import store from '../../store/index';

import config from '@/config.js'

import http from './interface'

import user from './user.js'

import receipt from './receipt.js'

import dict from './dict.js'

import task from './task.js'

import material from './material.js'

import inventory from './inventory.js'

import cycle from './cycle.js'

import shipment from "./shipment.js"

//------------------------------------------------------------------------------------------
//---------------------------------------   拦截器  ----------------------------------
//------------------------------------------------------------------------------------------
//服务器地址
// #ifdef H5
http.config.baseUrl = '/api'
// #endif

// #ifndef H5
http.config.baseUrl = config.server
// #endif


//设置请求前拦截器
http.interceptor.request = (config) => {
	//添加通用参数
	let token = uni.getStorageSync('userData').access_token;
	delete config.header['Authorization'];
	if (token) {
		config.header['Authorization'] = 'Bearer ' + token
	}

}

//设置请求结束后拦截器
http.interceptor.response = async (res) => {
	console.log(res)
	let code = res.data.code;
	let err = null;

	//判断返回状态 执行相应操作
	switch (code) {
		//token过期
		case 401:
			store.dispatch("logout");
			uni.showModal({
				title: '',
				content: "登录过期,请重新登录",
				confirmText: "去登录",
				confirmColor: '#3CC51F',
				success: function(msg) {
					if (msg.confirm) {
						uni.navigateTo({
							url: '/pages/login/login'
						});
					} else if (msg.cancel) {

					}
				}
			});
			// return res.data = await doRequest(res, url)
			break;
		default:
			break;
	}

	return res
}




//刷新token并继续之前请求
 function doRequest(response, url) {
	this.$http.user.refreshToken();
	
	let config = response.config
	var postData = {
		url: url,
		data: config.data,
		method: config.method
	}
	const resold = request(postData)
	return resold
}

// 默认全部导出
export default {
	user,
	receipt,
	dict,
	task,
	material,
	inventory,
	cycle,
	shipment
}