import config from './common.js'
// import storage from './storge.js'
import Cache from './cache'

export const babyImg = '/static/images/my/default_baby.png'

export default {
	// 平台币转人民币
	gstToRmb(amount) {
		let coinToRmb = Cache.get('coinToRmb')
		let rmb = amount * 1 * coinToRmb
		return rmb
	},
	// 返回上一页
	back() {
		uni.navigateBack({
			delta: 1
		})
	},
	// 提示框
	toast(msg, callback = false) {
		uni.showToast({
			icon: 'none',
			title: msg,
			duration: 2000
		})
		if (callback !== false) {
			setTimeout(() => {
				callback()
			}, 1500)
		}
	},
	// 复制文本
	copy(text) {
		let self = this;
		uni.setClipboardData({
			data: text,
			success: function() {
				uni.showToast({
					icon: 'none',
					title: '已复制到剪切板'
				})
			}
		});
	},
	// 复制文本或打电话
	callOrCopy(phoneNumber) {
		uni.showActionSheet({
			itemList: ['拨打电话', '复制'],
			success: function(res) {
				// console.log(res)
				if (res.tapIndex === 0) {
					let phoneReg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
					if (!phoneReg.test(phoneNumber)) {
						uni.showToast({
							title: '该号码不是手机号,无法拨打',
							icon: 'none'
						})
						return
					} else {
						uni.makePhoneCall({
							phoneNumber: phoneNumber
						});
					}
				} else {
					this.copy(phoneNumber)
				}

			},
			fail: function(res) {
				console.log(res.errMsg);
			}
		});
	},
	// 暂未开放
	unopen() {
		uni.showModal({
			content: '暂未开放'
		})
	},
	//上传图片
	uploadImg() {
		let base_url = config.base_api
		return new Promise((resolve) => {
			uni.chooseImage({
				sourceType: ['album'], //从相册选择
				success: (res) => {
					uni.showLoading({
						title: '图片上传中',
						mask: true,
						success: () => {
							const tempFilePaths = res.tempFilePaths;
							uni.uploadFile({
								url: base_url + '/ajax/upload',
								filePath: tempFilePaths[0],
								name: 'file',
								header: {
									// 'XX-Device-Type': this.$storage.get('model'),
									// 'token': this.$storage.get('token')
									'token': 'e3ddddf5-9ec8-4189-9099-5b81b76e8a45'
								},
								success: (uploadFileRes) => {
									let res = uploadFileRes.data
									res = JSON.parse(res)
									if (res.code === 1) {
										uni.showModal({
											title: '图片上传成功',
											showCancel: false,
											success: (re) => {
												if (re
													.confirm) {
													// this.sj_pz = tempFilePaths[0]
													// this.sj = res.data.path
													console.log(
														res);
													// url = res.data.path
													resolve(res
														.data
														.url
													)
												}
											}
										})
									} else {
										this.$toast('上传失败，请重新上传')
									}
								},
								complete: () => {
									uni.hideLoading()
								}
							})
						}
					})

				}
			})
		})
	},
	// 保存图片
	savePhoto(val) {
		uni.showModal({
			title: "保存二维码",
			success: (res) => {
				if (res.confirm) {
					uni.saveImageToPhotosAlbum({
						filePath: val,
						success: () => {
							this.toast("保存成功")
						}
					});
				} else if (res.cancel) {
					this.toast("取消保存")
				}
			}
		})
	},
	// 下载安装包
	updateApk(data) {
		if (!data.appDownload) return;
		uni.showModal({
			title: '更新提示',
			content: data.up_info,
			success: function(res) {
				if (res.confirm) {
					plus.runtime.openURL(data.appDownload);
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
	},
	// 热更新
	hotUpdate(data) {
		if (!data.wgtDownload) return;
		uni.showModal({
			title: '更新提示',
			content: data.up_info,
			success: function(res) {
				if (res.confirm) {
					uni.showLoading({
						title: '正在下载更新',
						success() {
							uni.downloadFile({
								url: data.wgtDownload,
								success: (downloadResult) => {
									uni.hideLoading();
									if (downloadResult.statusCode === 200) {
										uni.showLoading({
											title: '正在升级',
											success() {
												plus.runtime.install(
													downloadResult
													.tempFilePath, {
														force: false
													},
													function() {
														uni.hideLoading();
														plus.runtime
															.restart();
													},
													function(e) {
														console.error(
															'install fail...'
														);
														uni.showToast({
															title: "安装失败",
															icon: 'none'
														})
													});
												setTimeout(function() {
													uni.hideLoading()();
												}, 3000)
											}
										})
									}
								}
							});
						}
					})
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		})
	},

}