import service from './newRequest.js'
import store from '../store';
import {
	saveUrl,
	interceptor
} from '@/utils/loginUtils.js';
// 请求完成后拦截
service.interceptors.response(
	response => {
		try {
			const res = response.data;
			// 解析字符串为数字
			if (res.code !== 1) { // 请求失败
				return Promise.reject(new Error(res.msg || 'Error'))
			} else { // 成功
				return res
			}
		} catch (e) {
			console.log(e);
		}
	},
	error => {
		uni.showToast({
			title: "加载错误请重试",
			duration: 1500,
			mask: false,
			icon: 'none',
		})
		return Promise.reject(error)
	}
)

// 请求前拦截器
service.interceptors.request(
	config => {
		// config.data.token = getToken();
		// console.log(config);
		let token = uni.getStorageSync('token');
		// console.log(token);
		if (!config.header) {
			config.header = {
				"Authori-zation": 'Bearer ' + token,
			}
		} else {
			// 添加key请求头
			config.header["Authori-zation"] = 'Bearer ' + token;
		}
		return config
	},
	error => {
		// 错误处理
		console.log(error)
		return Promise.reject(error)
	}
)
let upFilse = service.upFilse;
export {
	upFilse
};
export default service.open;
