var siteinfo = require('../siteinfo.js'); //require这个js模块  
module.exports = function(a) {
	let data = a.data || (a.data = {}),
		url = a.url,
		showLoading = a.showLoading ? false : true;
	showLoading && uni.showLoading({
		mask: true,
		title: '上传中...'
	})
	// console.log(data)
	return Upload(url, data)
}
var host = siteinfo.siteroot + '/index.php?s=/api/';
function Upload(url, data) {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: host + url,
			filePath: data.url,
			name: 'img',
			formData: {
				user_id: data.user_id,
				admin_id: data.admin_id,
			},
			success: (r)=> {
				// console.log(r)
				let res = JSON.parse(r.data);
				if (res.code === '200') {
					resolve(res.data);
				} else {
				    reject(res)	
					// _show_error(1, res.message)
					if (res.code == 1002 || res.message == '缺少token') {
						uni.reLaunch({
							url: "/pages/login/login"
						})
						return;
					}
				}
			},
			fail: err => {
				console.log(err)
				_show_error(0, "");
				// reject(err)
			},
			complete: res => {
				uni.hideLoading()
			}
		})
	})
}

function _show_error(error_code, msg) {
	let title = error_code == 1 ? msg : "抱歉，出现了一个错误";
	// console.log(title)
	uni.showToast({
		title: title,
		icon: 'none',
		duration: 2000
	})
}
