let mpi = {}

// 微信登陆
mpi.wxLogin = () => {
	return new Promise((resolve, reject) => {
		uni.login({
			success: function(res) {
				resolve(res)
			},
			fail: function(err) {
				console.warn(err)
				reject(err)
			}
		})
	})
}

mpi.chooseLocation = () => {
	return new Promise((resolve, reject) => {
		uni.chooseLocation({
			success: function(res) {
				resolve(res)
			},
			fail: function(err) {
				reject(err)
			}
		});
	})
}

mpi.photoToBase64 = () => {
	uni.chooseImage({
		count: 9,
		success: res => {
			uni.request({
				url: res.tempFilePaths[0],
				method: 'GET',
				responseType: 'arraybuffer',
				success: resp => {
					let base64 = uni.arrayBufferToBase64(resp.data); //把arraybuffer转成base64
					base64 = 'data:image/jpeg;base64,' + base64; //不加上这串字符，在页面无法显示
					console.log(base64);
				}
			});
		}
	});
}

//选择图片 res.tempFilePaths[0]
mpi.chooseImage = (count = 1, size = 2) => {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count,
			//最多可以选择的图片张数，默认9
			sourceType: ['album', 'camera'],
			sizeType: ['compressed'],
			//可选择原图或压缩后的图片
			success: res => {
				if (res.tempFiles[0].size > size * 1024 * 1024) {
					uni.showToast({
						title: `上传的照片不能超过${size}m`,
						icon: "none"
					})
					return
				}
				resolve(res.tempFilePaths)
			},
			fail(err) {
				reject(err)
			}
		});

	})
}


mpi.downloadFile = (url) => {
	return new Promise((resolve, reject) => {
		uni.downloadFile({
			url, //仅为示例，并非真实的资源
			success: (res) => {
				if (res.statusCode === 200) {
					resolve(res.tempFilePath)
				} else {
					reject(res)
				}
			},
			fail: function(err) {
				reject(err)
			}
		});
	})
}




mpi.saveImg = (filePath) => {
	return new Promise((resolve, reject) => {
		uni.saveImageToPhotosAlbum({
			filePath,
			success: function(res) {
				uni.showToast({
					title: "保存成功",
					icon: "none"
				});
				resolve(res)
			},
			fail: function(err) {
				uni.showToast({
					title: "保存失败，请稍后重试",
					icon: "none"
				});
				reject(err)
			}
		});
	})
}




mpi.getLocation = () => {
	return new Promise((resolve, reject) => {
		uni.getLocation({
			type: 'gcj02',
			success: function(res) {
				resolve(res)
			},
			fail: function(err) {
				reject(err)
			}
		});
	})
}

mpi.getConnectedWifi = () => {
	return new Promise((resolve, reject) => {
		uni.startWifi({
			success: function(res) {
				wx.getConnectedWifi({
					success: res => {
						resolve(res)
					},
					fail: err => {
						reject(err)
					}
				})
			},
			fail: function(err) {
				reject(err)
			}
		});
	})
}
/**
 * 订阅认证信息
 */
mpi.subscribe = (tmplIds) => {
		return new Promise((resolve, reject) => {
			uni.requestSubscribeMessage({
				tmplIds,
				success(res) {
					resolve(res)
				},
				fail(err) {
					reject(err)
				}
			})
		})
	},


	// 获取用户信息
	mpi.wxGetUserInfo = () => {
		return new Promise((resolve, reject) => {
			uni.getUserInfo({
				withCredentials: true,
				success: function(res) {
					resolve(res)
				},
				fail: function(err) {
					reject(err)
				},
			})
		})
	}

//新版本的获取用户信息
mpi.getUserProfile = () => {
	return new Promise((resolve, reject) => {
		uni.getUserProfile({
			desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
			success: (res) => {
				resolve(res)
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

let compareVersion = (v1, v2) => {
	v1 = v1.split('.')
	v2 = v2.split('.')
	const len = Math.max(v1.length, v2.length)

	while (v1.length < len) {
		v1.push('0')
	}
	while (v2.length < len) {
		v2.push('0')
	}

	for (let i = 0; i < len; i++) {
		const num1 = parseInt(v1[i])
		const num2 = parseInt(v2[i])

		if (num1 > num2) {
			return 1
		} else if (num1 < num2) {
			return -1
		}
	}
	return 0
}

mpi.versioning = (minVersion) => {
	const version = wx.getSystemInfoSync().SDKVersion
	return new Promise((resolve, reject) => {
		if (compareVersion(version, minVersion) >= 0) {
			resolve()
		} else {
			uni.showModal({
				content: "当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。"
			})
		}
	})
}









export default mpi
