import {
	checkLogin
} from '@/api/user'
var isEmpty = function(obj) {
	if (typeof obj == "undefined" || obj == null || obj == "") {
		return true;
	} else {
		return false;
	}
}

var getDictLabelName = function(key, dictValue) {
	if (dictValue == null) {
		return "";
	}
	var dicts = uni.getStorageSync('dicts');
	dicts = JSON.parse(dicts);
	var dict = dicts[key]
	var name = "";
	dict.forEach(function(item, index, array) {
		if (dictValue.toString() == item.dictValue.toString()) {
			name = item.dictLabel
		}
	});
	return name;
}
var getDict = function(key) {

	var dicts = uni.getStorageSync('dicts');
	dicts = JSON.parse(dicts);
	var dict = dicts[key]
	return dict;
}

var photosToArr = function(photoUrl) {
	var photos = [];
	if (photoUrl != null && photoUrl != '') {
		photos = photoUrl.split(',');
	}
	return photos
}

var dateFormat = function dateFormat(fmt, date) {
	let ret;
	const opt = {
		"Y+": date.getFullYear().toString(), // 年
		"m+": (date.getMonth() + 1).toString(), // 月
		"d+": date.getDate().toString(), // 日
		"H+": date.getHours().toString(), // 时
		"M+": date.getMinutes().toString(), // 分
		"S+": date.getSeconds().toString() // 秒
		// 有其他格式化字符需求可以继续添加，必须转化成字符串
	};
	for (let k in opt) {
		ret = new RegExp("(" + k + ")").exec(fmt);
		if (ret) {
			fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
		};
	};
	return fmt;
}

var getProvider = service => {
	return new Promise((resolve, reject) => {
		// 获取当前环境的服务商
		uni.getProvider({
			service: service || 'oauth',
			success: function(res) {
				// 此处可以排除h5
				if (res.provider) {
					resolve(res.provider[0])
				}
			},
			fail() {
				reject('获取环境服务商失败')
			},
		})
	}).catch(error => {
		console.log('167', error)
	})
}

var parsePhone = function parsePhone(mobile) {
	var str = mobile.substr(0, 3) + "****" + mobile.substr(7);
	return str;
}

var getAge = function GetAge(strBirthday) {
	var returnAge,
		strBirthdayArr = strBirthday.split("-"),
		birthYear = strBirthdayArr[0],
		birthMonth = strBirthdayArr[1],
		birthDay = strBirthdayArr[2],
		d = new Date(),
		nowYear = d.getFullYear(),
		nowMonth = d.getMonth() + 1,
		nowDay = d.getDate();
	if (nowYear == birthYear) {
		returnAge = 0; //同年 则为0周岁
	} else {
		var ageDiff = nowYear - birthYear; //年之差
		if (ageDiff > 0) {
			if (nowMonth == birthMonth) {
				var dayDiff = nowDay - birthDay; //日之差
				if (dayDiff < 0) {
					returnAge = ageDiff - 1;
				} else {
					returnAge = ageDiff;
				}
			} else {
				var monthDiff = nowMonth - birthMonth; //月之差
				if (monthDiff < 0) {
					returnAge = ageDiff - 1;
				} else {
					returnAge = ageDiff;
				}
			}
		} else {
			returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
		}
	}
	return returnAge; //返回周岁年龄
}
const urlToObj = function(url) {
	let obj = {}
	let str = url.slice(url.indexOf('?') + 1)
	let arr = str.split('&')
	for (let j = arr.length, i = 0; i < j; i++) {
		let arr_temp = arr[i].split('=')
		obj[arr_temp[0]] = arr_temp[1]
	}
	return obj
}

var isLogin = function() {
	var token = uni.getStorageSync('AppToken');
	if (token != null && token != "") {
		return true;
	} else {
		return false;
	}
	// return new Promise((resolve, reject) => {
	// 	checkLogin().then(
	// 		res => {
	// 			console.log(res)
	// 			if(res.code==200){
	// 				resolve(true);
	// 			}else{
	// 				resolve(false);
	// 			}
	// 		},
	// 		rej => { }
	// 	);
	// });
}

/**
 * 随机字符串
 * 
 */
export function generateRandomString(length) {
	let result = '';
	const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
	const charactersLength = characters.length;

	for (let i = 0; i < length; i++) {
		result += characters.charAt(Math.floor(Math.random() * charactersLength));
	}
	return result;
}

var logout = function() {
	uni.getStorageSync('storeId', null);
	uni.setStorageSync('AppToken', null);
}

module.exports = {
	isEmpty: isEmpty,
	getDictLabelName: getDictLabelName,
	photosToArr: photosToArr,
	dateFormat: dateFormat,
	getProvider: getProvider,
	parsePhone: parsePhone,
	getAge: getAge,
	urlToObj: urlToObj,
	getDict: getDict,
	logout: logout,
	isLogin: isLogin,
	generateRandomString
};