import store from '@/store'

//获取环境接口地址
import {
	envHost
} from '@/common/env';
var user = require('@/common/user.js');

/**
 *  判断传入参数的类型，以字符串的形式返回
 *  @obj：数据
 **/
function dataType(obj) {
	if (obj === null) return "Null";
	if (obj === undefined) return "Undefined";
	return Object.prototype.toString.call(obj).slice(8, -1);
};
/**
 * 处理对象参数值，排除对象参数值为”“、null、undefined，并返回一个新对象
 **/
function dealObjectValue(obj) {
	var param = {};
	if (obj === null || obj === undefined || obj === "") return param;
	for (var key in obj) {
		if (dataType(obj[key]) === "Object") {
			param[key] = dealObjectValue(obj[key]);
		} else if (obj[key] !== null && obj[key] !== undefined && obj[key] !== "") {
			param[key] = obj[key];
		}
	}
	return param;
};


const post = function(url, data, showloading) {
	if (showloading != false) {
		uni.showLoading({
			mask: true,
			title: '加载中'
		});
	}
	return new Promise(function(resolve, reject) {
		data = dealObjectValue(data);
		const jwt = store.getters['getJwt'];
		const header = {
			'content-type': 'application/x-www-form-urlencoded',
			'token': jwt //自定义请求头信息
		}
		uni.request({
			url: envHost + url,
			method: 'POST',
			data: data,
			header: header,
			success: function(res) {
				console.info(res)
				if (res.statusCode == 200) {
					if (res.data.code == 403) {
						store.commit('logout');
						user.loginByWeixinCode().then(res => {
                            console.info(res)
						}).catch(err => {

						});
					} else {
						resolve(res.data);
					}
				} else {
					reject(res.errMsg);
				}
			},
			fail: function(err) {
				reject();
				// let errmsg = err.response.data;
				// switch (err.response.status) {
				// 	case 0:
				// 		console.log("网络连接错误")
				// 		errmsg = "网络连接错误";
				// 		break;
				// 	case 401:
				// 		console.log("请登录后操作")
				// 		errmsg = '请登录后操作';
				// 		store.dispatch('logout');
				// 		uni.redirectTo({
				// 			url: '/pages/auth/login'
				// 		})
				// 		break
				// 	case 404:
				// 		errmsg = "接口不存在";
				// 		break
				// }
				// uni.showToast({
				// 	title: errmsg,
				// 	icon: 'none'
				// })
			},
			complete: function() {
				uni.hideLoading();
			}
		});
	});
}

const get = function(url, data, showloading) {
	if (showloading != false) {
		uni.showLoading({
			mask: true,
			title: '加载中'
		});
	}
	return new Promise(function(resolve, reject) {
		data = dealObjectValue(data);
		const jwt = store.getters['getJwt'];
		const header = {
			'token': jwt //自定义请求头信息
		}
		uni.request({
			url: envHost + url,
			method: 'GET',
			data: data,
			header: header,
			success: function(res) {
				//console.info(res)
				if (res.statusCode == 200) {
					if (res.data.code == 403) {
						store.commit('logout');
						user.loginByWeixinCode().then(res => {
						    console.info(res)
						}).catch(err => {
						
						});
					} else {
						resolve(res.data);
					}
				} else {
					reject(res.errMsg);
				}
			},
			fail: function(err) {
				reject();
				// let errmsg = err.response.data;
				// switch (err.response.status) {
				// 	case 0:
				// 		console.log("网络连接错误")
				// 		errmsg = "网络连接错误";
				// 		break;
				// 	case 401:
				// 		console.log("请登录后操作")
				// 		errmsg = '请登录后操作';
				// 		store.dispatch('logout');
				// 		uni.redirectTo({
				// 			url: '/pages/auth/login'
				// 		})
				// 		break
				// 	case 404:
				// 		errmsg = "接口不存在";
				// 		break
				// }
				// uni.showToast({
				// 	title: errmsg,
				// 	icon: 'none'
				// })
			},
			complete: function() {
				uni.hideLoading();
			}
		});
	});
}
const upload = function(url, file, data, showloading) {
	if (showloading != false) {
		uni.showLoading({
			mask: true,
			title: '上传中...'
		});
	}
	return new Promise((resolve, reject) => {
		data = dealObjectValue(data);
		const jwt = store.getters['getJwt'];
		const header = {
			'token': jwt //自定义请求头信息
		}
		uni.uploadFile({
			url: envHost + url,
			// files: files,
			filePath: file,
			name: 'file',
			formData: data,
			header: header,
			success: (res) => {
				resolve(res.data);
			},
			fail: (err) => {
				uni.showModal({
					content: err.errMsg,
					showCancel: false
				})
				reject(err);
				console.log('uploadImage fail', err);
			},
			complete: () => {
				uni.hideLoading();
			}
		})
	});
}

const uploadAsync = function(url, file, data, success, showloading) {
	if (showloading != false) {
		uni.showLoading({
			mask: true,
			title: '上传中...'
		});
	}
	data = dealObjectValue(data);
	const jwt = store.getters['getJwt'];
	const header = {
		'token': jwt //自定义请求头信息
	}
	return uni.uploadFile({
		url: envHost + url,
		// files: files,
		filePath: file,
		name: 'file',
		formData: data,
		header: header,
		success: (res) => {
			console.info(res)
			if (success)
				success(JSON.parse(res.data));
		},
		fail: (err) => {
			uni.showModal({
				content: err.errMsg,
				showCancel: false
			})
			console.log('uploadImage fail', err);
		},
		complete: () => {
			uni.hideLoading();
		}
	});
}

const uploadFiles = function(url, files, data, showloading) {
	if (showloading != false) {
		uni.showLoading({
			mask: true,
			title: '上传中...'
		});
	}
	console.info(files)
	return new Promise((resolve, reject) => {
		data = dealObjectValue(data);
		const jwt = store.getters['getJwt'];
		const header = {
			'token': jwt //自定义请求头信息
		}
		uni.uploadFile({
			url: envHost + url,
			files: files,
			formData: data,
			header: header,
			success: (res) => {
				console.info(JSON.stringify(res))
				resolve(JSON.parse(res.data));
			},
			fail: (err) => {
				uni.showModal({
					content: err.errMsg,
					showCancel: false
				})
				reject(err);
				console.log('uploadImage fail', err);
			},
			complete: () => {
				uni.hideLoading();
			}
		})
	});
}

export default {
	post,
	upload,
	uploadAsync,
	uploadFiles,
	get
}
