export default {
	// 继承对象
	Extend(old, ...obj) {
		return Object.assign(old, ...obj)
	},
	// 判断是否是空对象、数组
	IsEmpty(obj) {
		if (obj == null) return true;
		if (obj.length > 0) return false;
		if (obj.length === 0) return true;
		for (var key in obj) {
			if (Object.prototype.hasOwnProperty.call(obj, key)) return false;
		}
		return true;
	},
	// 完美深拷贝
	DeepCopy(obj) {
		var str, newobj = obj.constructor === Array ? [] : {};
		if (typeof obj !== 'object') {
			return;
		} else if (JSON) {
			str = JSON.stringify(obj), //系列化对象
				newobj = JSON.parse(str); //还原
		} else {
			for (var i in obj) {
				newobj[i] = typeof obj[i] === 'object' ?
					cloneObj(obj[i]) : obj[i];
			}
		}
		return newobj;
	},
	// 拷贝对象，对于null，undefined，function等数据会失效
	Origin(obj) {
		if (typeof obj === 'object') {
			try {
				return JSON.parse(JSON.stringify(obj))
			} catch (err) {}
		}
		console.log('Fun-Origin 错误的对象格式', obj)
		return obj
	},
	// 数组转成对象
	ArrayToObject(arr, key) {
		let obj = {}
		if (Array.isArray(arr) && arr.length) {
			arr.forEach((item, index) => {
				obj[item[key] ? item[key] : index] = this.Origin(item)
			})
		}
		return obj
	},
	//
	// 缓存
	//
	Cache(key, val, timeout) {
		if (key) {
			let date = new Date()
			if (val === null) {
				// 清除缓存
				uni.removeStorageSync(`${key}_timeout`)
				return uni.removeStorageSync(key)
			} else if (val === undefined && timeout === undefined) {
				// 获取缓存
				let cache = uni.getStorageSync(key)
				if (cache === undefined) {
					return undefined
				} else {
					let expired = uni.getStorageSync(`${key}_timeout`)
					if (expired && date > expired) {
						uni.removeStorageSync(key)
						uni.removeStorageSync(`${key}_timeout`)
						return undefined
					}
					return cache
				}
			} else if (val !== undefined) {
				// 存储缓存
				if (timeout) {
					let throughSecond = 0
					if (typeof timeout === 'number') {
						throughSecond = timeout
					} else if (typeof timeout === 'string') {
						let number = parseInt(timeout)
						let unit = timeout.charAt(timeout.length - 1)
						if (number && number > 0) {
							if (unit === 'd' || unit === 'day' || unit === 'D') {
								throughSecond = number * 60 * 60 * 24
							} else if (unit === 'h' || unit === 'hour' || unit === 'H') {
								throughSecond = number * 60 * 60
							} else if (unit === 'm' || unit === 'minute' || unit === 'H') {
								throughSecond = number * 60
							} else {
								throughSecond = number
							}
						}
					}
					if (throughSecond) {
						let expired = new Date(throughSecond * 1000 + (new Date()).valueOf())
						uni.setStorageSync(`${key}_timeout`, expired)
					}
				}
				return uni.setStorageSync(key, val)
			}
		}
		return undefined
	},
	CacheClean() {
		uni.clearStorage()
	},
	Token(token) {
		if (token === undefined) {
			return this.Cache('token')
		} else if (token === null) {
			return this.Cache('token', null)
		} else if (token) {
			return this.Cache('token', token)
		}
	},
	//
	// 时间
	//
	Time(data, format = "yyyy-MM-dd hh:mm") {
		return this.UnTimestamp(data, format)
	},
	Timestamp(date) {
		let time = date ? new Date(date) : new Date()
		return Date.parse(time) / 1000
	},
	UnTimestamp(time = new Date(), format = "yyyy-MM-dd hh:mm:ss") {
		if (typeof time === 'string' || typeof time === 'number') {
			if (time % 1 === 0 && time < 10000000000) {
				time = new Date(time * 1000)
			} else {
				time = new Date(time)
			}
		}
		if (time === null || time === '0000-00-00 00:00:00' || time == '') {
			time = new Date(0)
		}
		let date = {
			"M+": time.getMonth() + 1,
			"d+": time.getDate(),
			"h+": time.getHours(),
			"m+": time.getMinutes(),
			"s+": time.getSeconds(),
			"q+": Math.floor((time.getMonth() + 3) / 3),
			"S+": time.getMilliseconds()
		};
		if (/(y+)/i.test(format)) {
			format = format.replace(RegExp.$1, (time.getFullYear() + '').substr(4 - RegExp.$1.length));
		}
		for (let k in date) {
			if (new RegExp("(" + k + ")").test(format)) {
				format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
					date[k] : ("00" + date[k]).substr(("" + date[k]).length));
			}
		}
		return format
	},
}
