var getType = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(res) {
	return typeof res;
} : function(res) {
	return res && "function" == typeof Symbol && res.constructor === Symbol && res !== Symbol.prototype ? "symbol" :
		typeof res;
}
module.exports = {
	str2Obj: function(str) {
		if ("string" != typeof str) return str
		if (str.indexOf("&") < 0 && str.indexOf("=") < 0) return {}
		let newStr = str.split("&"),
			newOjb = {}
		newStr.forEach(value => {
			if (value.indexOf("=") > -1) {
				let newStr1 = value.split("=")
				newOjb[newStr1[0]] = newStr1[1]
			}
		})
		return newOjb
	},
	onShareAppMessage: function(path, title, showDesc = "") {
		let userid = uni.getStorageSync('userInfo')['id']
		path = path || "/pages/index/index"
		return {
			'title': title ? title : "余乐兑",
			'desc': showDesc ? showDesc : '',
			'path': path + "mid=" + userid
		}
	},
	pdata: function(t) {
		return t.currentTarget.dataset;
	},
	data: function(t) {
		return t.target.dataset;
	},
	pay: function(payinfo, success = null, fail = null) {
		// 支付订单有效时间,单位秒
		let expireTime = 6 * 60 * 60 //六个小时
		if ("object" == (void 0 === payinfo ? "undefined" : getType(payinfo))) {
			if ("function" == typeof success) {
				payinfo.success = success
			}
			if ("function" == typeof fail) {
				payinfo.fail = fail
			}
			let payHistoryData = uni.getStorageSync("payHistoryData")
			if (payHistoryData && payHistoryData.payinfo == JSON.stringify(payinfo)) {
				let diffTime = new Date().getTime() - payHistoryData.create_time
				diffTime = diffTime / 1000
				if (diffTime > expireTime) {
					uni.showModal({
						title: "提示",
						content: "订单有效时间为2分钟,请重新下单支付",
						showCancel: false
					})
					return
				}
			} else {
				uni.setStorageSync("payHistoryData", {
					"payinfo": JSON.stringify(payinfo),
					"create_time": new Date().getTime()
				})
			}
			// #ifndef H5
			return uni.requestPayment(payinfo)
			// #endif
			//#ifdef H5
			 ap.tradePay({
			      tradeNO: payinfo.orderInfo["trade_no"]
			    }, success)
			// #endif
		}
	},
	upload: function(callback) {
		var _this = this
		uni.chooseImage({
			success: function(chosImgRes) {
				let uploadUrl =
					"https://yld.angyakeji.com/apiv3/fileUploadAndDownload_FSCuANkzFs0/upload"
				uni.showToast({
					title: "正在上传...",
					icon: "loading",
					mask: false,
					duration: 1000 * 20
				})
				let resSize = chosImgRes.tempFiles[0].size
				let maxMB = 4
				let maxByte = maxMB * 1024 * 1024
				if (resSize > maxByte) {
					uni.hideToast()
					uni.showModal({
						showCancel: false,
						title: "提示",
						content: "上传图片大小不能超过" + maxMB + "MB"
					})
					return
				}
				uni.uploadFile({
					url: uploadUrl,
					filePath: chosImgRes.tempFilePaths[0],
					name: "file",
					success: function(upRes) {
						setTimeout(function() {
							uni.hideToast()
						}, 666)
						var upResObj = JSON.parse(upRes.data)
						if (0 != upResObj.code) {
							uni.showModal({
								title: '提示',
								content: '上传失败' + upRes.msg
							})
						} else {
							if ("function" == typeof callback) {
								callback(upResObj.data.file)
							}
						}
					}
				})
			}
		})
	},
	alert: function(content) {
		uni.showModal({
			title: "提示",
			content: content,
			showCancel: false,
		})
	},
}
