import md5Libs from "uview-ui/libs/function/md5";
var t = function (t) {
	return (t = t.toString())[1] ? t : "0" + t;
};

// const getToken = function(){
// 	const app = getApp()
// 	let token = app && app.globalData && app.globalData.token || ''
// 	if(!token){
// 		token = uni.getStorageSync('token')
// 	}
// 	return token
// }
// const setToken = function(token){
// 	const app = getApp()
// 	uni.setStorageSync('token',token)
// 	if(app && app.globalData){
// 		app.globalData.token = token
// 	}
// }

const getOpenid = function () {
	const app = getApp()
	let openid = app && app.globalData && app.globalData.openid || ''
	if (!openid) {
		openid = uni.getStorageSync('openid')
	}
	return openid
}
const setOpenid = function (openid) {
	const app = getApp()
	uni.setStorageSync('openid', openid)
	if (app && app.globalData) {
		app.globalData.openid = openid
	}
}

function validImages(imgurl) {
	return new Promise((resolve, reject) => {
		uni.getImageInfo({
			src: imgurl,
			success(res) {
				resolve(true)
			},
			fail() {
				reject(false)
			}
		})
	})
}

/**
 * ====图片本地缓存====
 * @param {Object} vm	当前vue实例
 * @param {Object} src	图片地址
 * @param {Object} host	图片前缀，默认使用qn_base_url
 */
const imageCache = async function (vm, src, host) {
	host = host || process.uniEnv.qn_base_url
	let image_url = host + src
	let image_name = md5Libs.md5(image_url)
	var arr = Object.keys(vm.vuex_cache);
	const result = await validImages(vm.vuex_cache[image_name])
	if (vm.vuex_cache && arr.length && arr.includes(image_name) && result) {
		return vm.vuex_cache[image_name]
	} else {
		// 本地没有缓存 需要下载
		uni.downloadFile({
			url: image_url,
			success: (res) => {
				if (res.statusCode === 200) {
					vm.$u.vuex(`vuex_cache.${image_name}`, res.tempFilePath)
				}
			}
		});
		return image_url
	}
}

/* 
fromData 多文件提交
 @params url  请求的地址
 @params data 请求所带参数
 */
const submitFormData = (url, data = null) => new Promise((resolve) => {
	/* 规定上传文件的键值做参数处理 */
	// 比如
	// data = {
	// 	files : [],
	// 	...params,
	// }
	/* 把对应的数据处理归位 */
	/* 这里有个要注意
	虽然 填写了 files uniapp不会去拿 name 和filePath
	但 uploadFile 的 name 和 filePath 必填 不然会报错
	 */
	let files = [];
	for (let key in data.files) {
		files.push({
			name: key,
			uri: data.files[key]
		});
	}
	delete data.files;
	options.formData = data;
	options.files = files;
	uni.uploadFile({
		success: ({
			statusCode,
			data
		}) => resolve(success(statusCode, data)),
		fail: ({
			errMsg
		}) => resolve(fail(errMsg)),
		filePath: '',
		name: '',
		...options
	})
})

module.exports = {
	formatTime: function (n) {
		var e = n.getFullYear(), r = n.getMonth() + 1, o = n.getDate(), i = n.getHours(), u = n.getMinutes(), a = n.getSeconds();
		return [e, r, o].map(t).join("-") + " " + [i, u, a].map(t).join(":");
	},
	throttle: function (t, n) {
		null != n && null != n || (n = 1500);
		var e = null;
		return function () {
			var r = +new Date();
			console.log(r + "," + e + "，" + new Date()), (r - e > n || !e) && (t(), e = r);
		};
	},
	formatLonLat: function (t, n) {
		if (void 0 === t || "" == t) return "";
		if (t += "", 0 == n) return parseFloat(t).toFixed(6);
		var e = t.indexOf("."), r = e < 0 ? t : t.substring(0, e), o = 0, i = 0;
		return e > 0 && (o = 60 * (o = "0" + t.substring(e)) + "", 2 == n ? (e = o.indexOf(".")) > 0 && (i = "0" + o.substring(e),
			o = o.substring(0, e), e = (i = 60 * i + "").indexOf("."), i = i.substring(0, e + 4),
			i = parseFloat(i).toFixed(0)) : 1 == n && (o = parseFloat(o).toFixed(6))), 2 == n ? r + "°" + o + "′" + i + "″" : 1 == n ? r + "°" + o + "′" : void 0;
	},
	// getToken,
	// setToken,
	getOpenid,
	setOpenid,
	imageCache,
	submitFormData,
};