export default {
	// 页面提示信息函数
	showPrompt(msg) {
		uni.showToast({
			title: msg,
			icon: 'none',
			mask: true
		})
	},
	// APP名称
	appName() {
		return "贞达市政工程"
	},
	formatRichText(html){
	  let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
		match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
		match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
		match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
		return match;
	  });
	  newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
		match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
		return match;
	  });
	  newContent = newContent.replace(/<br[^>]*\/>/gi, '');
	  newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
	  return newContent;
	},
	getHeight(e) {
		console.log(e)
	},
	changeTwoDecimal_f(x) {
		var f_x = parseFloat(x);
		if (isNaN(f_x)) {
			return 0;
		}
		var f_x = Math.round(x * 100) / 100;
		var s_x = f_x.toString();
		var pos_decimal = s_x.indexOf('.');
		if (pos_decimal < 0) {
			pos_decimal = s_x.length;
			s_x += '.';
		}
		while (s_x.length <= pos_decimal + 2) {
			s_x += '0';
		}
		return s_x;
	},
	// token过期
	tokenOverdue() {
		uni.setStorage({
			key: "jumpUrl",
			data: ''
		})
		uni.setStorage({
			key: "jumpType",
			data: ''
		})
		uni.navigateTo({
			url: "/pages/login/login"
		})
	},
	checkLoginTo(url, type) {
		console.log(type)
		let status = uni.getStorageSync('token')
		console.log(status)

		if (status) {
			uni.navigateTo({
				url: url
			})
		} else {
			uni.showModal({
				title: '',
				content: '您未登录，是否登录？',
				success: res => {
					if (res.confirm) {
						uni.setStorage({
							key: "jumpUrl",
							data: url
						})
						uni.setStorage({
							key: "jumpType",
							data: type
						})
						uni.navigateTo({
							url: "/pages/login/login"
						})
					}
				}
			})
		}
	},
	// 拨打电话
	toCall(tel) {
		if (!tel) {
			this.showPrompt('暂无商家电话')
			return
		}
		uni.makePhoneCall({
			phoneNumber: tel
		});
	},
	toBack() {
		uni.navigateBack({
			delta: 1
		})
	},
	// 图片预览
	previewImage(url) {
		let urlList = []
		if (typeof url == 'string') {
			urlList.push(url)
		} else {
			urlList = url
		}
		uni.previewImage({
			urls: urlList
		})
	},
	// 登录成功后的跳转
	loginSuccess() {
		let type = uni.getStorageSync('jumpType')
		// console.log(type)
		if (type) {
			let url = uni.getStorageSync('jumpUrl')
			// console.log(url)
			if (!url) {
				uni.navigateBack({
					delta: 1
				});
				return
			}
			if (type == 'page') {
				uni.redirectTo({
					url: url
				})
			} else {
				uni.switchTab({
					url: url,
					fail: error => {
						console.log(error)
					}
				})
			}
		} else {
			// uni.switchTab({
			// 	url: "/pages/index/index"
			// })
			uni.navigateBack({
				delta: 1
			});
		}
	},
	parseNumber(num) {
		return num < 10 ? "0" + num : num;
	},
	// 时间格式化基类
	dateFormat(date, formatStr) {
		let dateObj = {},
			rStr = /\{([^}]+)\}/,
			mons = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];
		dateObj["Y"] = date.getFullYear();
		dateObj["M"] = date.getMonth() + 1;
		dateObj["MM"] = this.parseNumber(dateObj["M"]);
		dateObj["Mon"] = mons[dateObj['M'] - 1];
		dateObj["D"] = date.getDate();
		dateObj["DD"] = this.parseNumber(dateObj["D"]);
		dateObj["h"] = date.getHours();
		dateObj["hh"] = this.parseNumber(dateObj["h"]);
		// dateObj["t"] = dateObj["h"] > 12 ? dateObj["h"] - 12 : dateObj["h"];
		dateObj["t"] = dateObj["h"]
		dateObj["tt"] = this.parseNumber(dateObj["t"]);
		// dateObj["A"] = dateObj["h"] > 12 ? '下午' : '上午';
		dateObj["i"] = date.getMinutes();
		dateObj["ii"] = this.parseNumber(dateObj["i"]);
		dateObj["s"] = date.getSeconds();
		dateObj["ss"] = this.parseNumber(dateObj["s"]);
		while (rStr.test(formatStr)) {
			formatStr = formatStr.replace(rStr, dateObj[RegExp.$1]);
		}
		return formatStr;
	},
	// 时间格式 年月日时分秒
	gettime(shorttime, type) {
		shorttime = shorttime.toString().length < 13 ? shorttime * 1000 : shorttime;
		let now = (new Date()).getTime();
		let cha = (now - parseInt(shorttime)) / 1000
		// type: 格式化类型  1为年月日时分秒  2为年月日时分  3为年月日
		if (type == 1) {
			return this.dateFormat(new Date(shorttime), "{Y}-{MM}-{DD} {t}:{ii}:{ss}");
		} else if (type == 2) {
			return this.dateFormat(new Date(shorttime), "{Y}-{MM}-{DD} {t}:{ii}");
		} else if (type == 3) {
			return this.dateFormat(new Date(shorttime), "{Y}-{MM}-{DD}");
		}

	},
	// 时间格式 年月日
	getDate(shorttime) {
		shorttime = shorttime.toString().length < 13 ? shorttime * 1000 : shorttime
		let now = (new Date()).getTime()
		let cha = (now - parseInt(shorttime)) / 1000
		return this.dateFormat(new Date(shorttime), "{Y}-{MM}-{DD}");
	},
	// 时间格式 月日
	getDateMd(shorttime) {
		shorttime = shorttime.toString().length < 13 ? shorttime * 1000 : shorttime
		let now = (new Date()).getTime()
		let cha = (now - parseInt(shorttime)) / 1000
		return this.dateFormat(new Date(shorttime), "{MM}-{DD}");
	},
	getSevenDate(num) {
		let date1 = new Date();
		//今天时间
		let time1 = ((date1.getMonth() + 1) < 10 ? '0' + (date1.getMonth() + 1).toString() : (date1.getMonth() + 1)) +
			"-" +
			(date1.getDate() < 10 ? '0' + date1.getDate().toString() : date1.getDate())
		let date2 = new Date(date1);
		date2.setDate(date1.getDate() + num);
		//num是正数表示之后的时间，num负数表示之前的时间，0表示今天
		let time2 = (date2.getMonth() + 1) + "-" + date2.getDate();
		return this.getdiffdate(time1, time2, num)
	},
	//获取两日期之间日期列表函数
	getdiffdate(stime, etime, num) {
		//初始化日期列表，数组
		let diffdate = new Array()
		let i = 0
		//开始日期小于等于结束日期,并循环
		while (i <= num - 1) {
			diffdate.push({
				dateStr: stime,
				weekStr: this.getWeek(new Date().getFullYear().toString() + "-" + stime)
			})
			//获取开始日期时间戳
			let stime_ts = new Date(stime).getTime()
			// console.log('当前日期：'+stime   +'当前时间戳：'+stime_ts)	        
			//增加一天时间戳后的日期
			let next_date = stime_ts + (24 * 60 * 60 * 1000)
			//拼接年月日，这里的月份会返回（0-11），所以要+1
			let next_dates_y = new Date(next_date).getFullYear() + '-'
			let next_dates_m = (new Date(next_date).getMonth() + 1 < 10) ? '0' + (new Date(next_date).getMonth() + 1) +
				'-' : (
					new Date(next_date).getMonth() + 1) + '-'
			let next_dates_d = (new Date(next_date).getDate() < 10) ? '0' + new Date(next_date).getDate() : new Date(
				next_date).getDate()
			stime = next_dates_m + next_dates_d
			// 增加数组key
			i++
		}
		return diffdate
	},
	// 根据日期转换星期
	getWeek(dateString) {
		let dateArray = dateString.split("-");
		let date = new Date(dateArray[0], parseInt(dateArray[1] - 1), dateArray[2]);
		return "周" + "日一二三四五六".charAt(date.getDay());
	},
	// 获取当前位置
	getNowAddress() {
		console.log(222)
		return new Promise((resolve, reject) => {
			let mapkey = ''
			// #ifdef MP-WEIXIN
			mapkey = 'c827c04d1d74d8589ec826fce02a135a'
			// #endif
			// #ifdef APP-PLUS
			mapkey = '7923ab86c887ab9c77e9e28828567622	'
			// #endif
			console.log(mapkey)
			let amapPlugin = new amap.AMapWX({
				key: mapkey
			})
			amapPlugin.getRegeo({
				success: res => {
					resolve(res[0])
				},
				fail: error => {
					console.log(error)
					uni.showToast({
						mask: true,
						icon: "none",
						title: error
					})
				}
			})
		})
	}
}
