import Vue from 'vue'
//const BASE_URL = 'https://ymt.prpiano.com/api'
//const BASE_URL = 'https://ht.test.prpiano.com/api'
//xw
//const BASE_URL = 'http://10.0.52.100:82'
//dh
var BASE_URL
var BASE_URL2
//lb
//const BASE_URL = 'http://10.0.126.219:82'
if (process.env.NODE_ENV === 'development') {
	//线上 https://bytesailing.cn/api/storage/cloudApi
	BASE_URL = 'http://36.134.91.96:10001/api'
	BASE_URL2 = 'http://36.134.91.96:10001/api'
} else {
	//线上
	BASE_URL = 'http://36.134.91.96:10001/api'
	BASE_URL2 = 'http://36.134.91.96:10001/api'
}
/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */


export function get(url, req = {}) {
	return new Promise((resolve, reject) => {
		if (this.$storage.getStorageSync('token')) {
			var hander = {
				'Blade-Auth': 'bearer ' + this.$storage.getStorageSync('token'),
				'Authorization':'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
				'Content-Type':'application/json;charset=UTF-8',
			}
		} else {
			var hander = {}
		}
		uni.request({
			url: BASE_URL + url,
			data: req,
			method: 'GET',
			header: hander,
			success: (res) => {
				resolve(res.data);
			},
			fail: (err) => {
				return Promise.reject(err);
			}
		});
	})
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */
var urlList = []
export function post(url, req = {}, headers) {
	var httpUrl
	if(url.indexOf('/cloudApi') == -1){
		httpUrl = BASE_URL
	}else{
		httpUrl = BASE_URL2
	}
	if(urlList.indexOf(url) == -1 || url == '/wx/inventory/selectFabricWTypeDetails' || '/wx/inventory/selectInkEW'){
		urlList.push(url)
		setTimeout(()=>{
			urlList.splice(urlList.indexOf(url),1)
		},500)
	}else{
		return Promise.reject('请勿频繁提交');
	}
	return new Promise((resolve, reject) => {
		if (this.$storage.getStorageSync('token')) {
			
			var hander = {
				'Blade-Auth': 'bearer ' + this.$storage.getStorageSync('token'),
				'Authorization':'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
				'Content-Type':'application/json;charset=UTF-8',
			}
		} else {
			var hander = {}
		} 
		if(headers) hander = headers
		uni.request({
			url: httpUrl + url,
			data: req,
			method: 'POST',
			header: hander,
			success: (res) => {
				console.log(res)
				if (res.data.code == 401) {
					uni.showToast({
						icon: "error",
						title: '前往登录',
						duration: 2000
					});
					Vue.prototype.$token = null
					uni.redirectTo({
						url: "/pages/user/login"
					})
				} else if (res.data.code == 500) {
					uni.showToast({
						icon: "error",
						title: res.data.msg,
						duration: 2000
					});
					
				} else if(res.data.code == 200){
					resolve(res.data);
				} else if(!res.data.code){
					uni.showToast({
						icon: "error",
						title: '服务器错误',
						duration: 2000
					});
				} else {
					uni.showToast({
						icon: "error",
						title: res.data.msg,
						duration: 2000
					});
				}
			},
			fail: (err) => {
				uni.showToast({
					icon: "error",
					title: '服务器错误',
					duration: 2000
				});
				return Promise.reject(err);
			}
		});
	})
}

export default {
	post,
	get
}