let mpi = {}


mpi.requestPayment = (params) => {
	return new Promise((resolve, reject) => {
		uni.requestPayment({
			provider: 'wxpay',
			...params,
			success: function(res) {
				if (res.errMsg == "requestPayment:ok") {
					resolve(res)
				}
			},
			fail: function(err) {
				reject(err)
			}
		});
	})
}
mpi.navigateToMiniProgram = (appId, path) => {
	return new Promise((resolve, reject) => {
		uni.navigateToMiniProgram({
			appId,
			path,
			success(res) {
				resolve(res)
			},
			fail(err) {
				reject(err)
			}
		
		})
	})
}

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

//选择图片 res.tempFilePaths[0]
mpi.chooseImage = (count = 1) => {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count,
			//最多可以选择的图片张数，默认9
			sourceType: ['album'],
			sizeType: ['compressed'],
			//可选择原图或压缩后的图片
			success: res => {
				resolve(res.tempFilePaths)
			},
			fail(err) {
				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
