// 服务器地址 开发环境 生产环境
const REQUESTURL = {
	development: "http://192.168.2.164:8089",
	production: ""
};

// 请求数据拦截
const REQUESTINTERCPTOR = function(request) {
	let _token = uni.getStorageSync('TOKEN');
	var token = _token ? 'JWT ' + _token : '';
	request['header'] = {
		'Content-Type': 'application/x-www-form-urlencoded',
		'Authorization': token
	}
	return request
}

// 接受数据拦截
const RESPONSEINTERCPTOR = function(response) {
	if (response.data.code != 0) ERRINTERCPTOR(response.data.msg)
	return response.data;
}
// 报错拦截
const ERRINTERCPTOR = function(err) {
	uni.showModal({
		title: "警告",
		content: `请求失败：${err.message || JSON.stringify(err)}`,
		confirmText: "关闭",
		showCancel: false,
		confirmColor: "#fbbd08"
	})
	return err
}


/**
 * request 
 * @param {Object} options  请求参数
 */
export default function(options) {
	// 开发环境 生产环境
	if (options.url) options.url = process.env.NODE_ENV == "development" ? REQUESTURL.development + options.url :
		REQUESTURL.production + options.url;
	return new Promise((resolve, reject) => {
		uni.request({
			...REQUESTINTERCPTOR(options),
			success: res => resolve(RESPONSEINTERCPTOR(res)),
			fail: err => reject(ERRINTERCPTOR("请检查网络！"))
		})
	})
}
