import Vue from 'vue'
import uni_request from './uni_request.js'

const request = uni_request({
	timeout: 123456,
	baseURL: 'https://soap.cliu.cc/api', //正式版
})

request.interceptors.request.use(config => {
	config.header.Authorization = 'Bearer ' + uni.getStorageSync('token')
	// config.header.Authorization = 'Bearer ' + 
	// 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9zb2FwLmNsaXUuY2NcL2FwaVwvYXV0aFwvd3hMb2dpbiIsImlhdCI6MTY4OTMwMDQ5OCwiZXhwIjoxNjkwMTY0NDk4LCJuYmYiOjE2ODkzMDA0OTgsImp0aSI6IlR2bTkyamF2bFp6RVUwMjAiLCJzdWIiOjQyMCwicHJ2IjoiMzA3M2QwNzExYzk3MGYyODhiYWI2NzJkN2FiZTMxODczZDhiODM4NCJ9.p641z1hA5jRpAsVVrBbpI7Ae62hKLc76xRKLliJiiPg'
	
	return config
})
request.interceptors.response.use(async (response, ...args) => { // 拦截器
	uni.hideLoading()
	if (response.data.code == 50000 ||response.data.code == 40021 || response.statusCode == 401) { //token过期或者没token
		// 判断是否是退差价页面
		let pages = getCurrentPages();
		let currentPage = pages[pages.length-1];
		let url = currentPage.route;
		let tip = url == 'pages/CustomerReturnPay/returnedInfo' ? '当前登录状态已过期， 点击‘确定’跳转至售后首页自动登录， 自动登录后请关闭网页重新扫码进入退差价页面' : '登录过期'
		
		uni.showModal({
			title: '错误提示',
			content: tip,
			showCancel: false,
			success: res => {
				uni.removeStorageSync('token')
				uni.removeStorageSync('userInfo')
				uni.switchTab({
					url: '../../pages/index/index'
				})
			}
		})
		// response = await doRequest(response, args[1]);
		return
	}
	return response
})

request.onerror = (...args) => { // 请求失败统一处理方法
	uni.hideLoading()
	uni.$emit('HIDELOADING') // 隐藏加载
	uni.showModal({
		title: "请求失败",
		content: "网络出错",
		showCancel: false,
	});
	return Promise.reject(args);
}
export default request