import wx from 'weixin-js-sdk'
import * as API_WeiXin from '$project/apis/weixin'
import * as API_Common from '$project/apis/common'

import Qs from 'qs';
import request from '../utils/request'
export function addSysLog(message) {

	return request({
		url: '/mobile/sysLogApi/submit',
		data: Qs.stringify({
			message: JSON.stringify(message)
		}),
		method: 'post',
	})
}
//获取微信配置
export function getWxConfig(jsApiList) {
	var jsApiList = jsApiList || ['chooseImage', 'getLocalImgData', 'scanQRCode'];
	let promise = (jsApiList => {
		return new Promise((resolve, reject) => {
			console.log(jsApiList);
			API_WeiXin.getConfig().then(response => {
				var wxconfig = response.wxConfig;
				console.log(wxconfig.nonceStr)
				wx.config({
					debug: false, // 开启调试模式,
					appId: wxconfig.appId, // 必填，企业号的唯一标识，此处填写企业号corpid
					timestamp: wxconfig.timestamp, // 必填，生成签名的时间戳
					nonceStr: wxconfig.nonceStr, // 必填，生成签名的随机串
					signature: wxconfig.signature, // 必填，签名，见附录1
					jsApiList: jsApiList // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
				});
				wx.ready(function() {
					resolve(response);
				})
				wx.error(function(res) {
					reject('微信api配置出错');
				});
			}).catch(error => {
				mui.toast(error);
				reject(error);
			})
		})
	})(jsApiList)

	return promise;
}

//选择图片,多图上传和单图上传的回调处理不一样
export function chooseImage(count) {
	var count = count || 1;
	let promise = new Promise((resolve, reject) => {
		wx.chooseImage({
			count: count,
			sizeType: ['compressed'],
			sourceType: ['album', 'camera'],
			success: function(res) {
				var localIds = res.localIds;
				console.log(localIds);
				if (count == 1) {
					wx.getLocalImgData({
						localId: localIds[0],
						success: function(res) {
							resolve(res);
						}
					});
				} else {
					resolve(localIds);
				}
			}
		});
	});
	return promise;
}

//获取图片，用于多图上传时的递归处理
export function getLocalImgData(localId, fun) {
	wx.getLocalImgData({
		localId: localId,
		success: function(res) {
			fun(res);
		}
	});
}

//上传图片
export function uploadPic(imgBase64) {
	let promise = new Promise((resolve, reject) => {
		var formData = new FormData();
		formData.append('photoName', '1.jpg');
		formData.append('photoBase64Data', imgBase64);

		console.log(formData);
		console.log(typeof formData);

		API_Common.uploadBase64(formData).then(response => {
			resolve(response);
		}).catch(error => {
			mui.toast(error);
			reject(error);
		})
	});
	return promise;
}

//获取坐标
export function getLocation() {
	let promise = new Promise((resolve, reject) => {
		wx.ready(function() {
			wx.getLocation({
				type: 'gcj02', // 默认为wgs84的gps坐标，可传入'gcj02'
				success: function(res) {
					resolve(res);
				},
				faile: function(res) {
					reject(res);
				},
				fail: function(res) {
					if (res.errMsg == 'getLocation:timeout') {
						mui.alert("定位超时，请检查是否开启'定位'");
					} else if (res.errMsg == 'getLocation:ERROR_NETWORK') {
						mui.alert("网络异常");
					} else if (res.errMsg == 'getLocation:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF') {
						mui.alert("没开启系统定位");
					} else if (res.errMsg == 'getLocation:system permission denied') {
						mui.alert("未给微信位置授权");
					} else if (res.errMsg == 'getLocation:location permission') {
						mui.alert("未给微信位置授权");
					} else if (res.errMsg == 'getLocation:auth denied') {
						mui.alert("用户在小程序中未授权");
					} else if (res.errMsg == 'getLocation:fail authorize no response') {
						mui.alert("用户在小程序中未授权");
					}
					reject(res);
				},
				complete() {}
			})
		})
	})
	return promise;
}

//扫描二维码
export function scanQRCode(needResult) {
	var needResult = needResult || 1;
	let promise = new Promise((resolve, reject) => {
		wx.scanQRCode({
			needResult: needResult,
			scanType: ["qrCode"],
			success: function(res) {
				var url = res.resultStr;
				resolve(url);
			},
			faile: function(res) {
				reject(res);
			},
			fail: function(res) {


				if (res.errMsg == 'getLocation:timeout') {
					mui.alert("定位超时，请检查是否开启'定位'");
				} else if (res.errMsg == 'getLocation:ERROR_NETWORK') {
					mui.alert("网络异常");
				} else if (res.errMsg == 'getLocation:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF') {
					mui.alert("没开启系统定位");
				} else if (res.errMsg == 'getLocation:system permission denied') {
					mui.alert("未给微信位置授权");
				} else if (res.errMsg == 'getLocation:location permission') {
					mui.alert("未给微信位置授权");
				} else if (res.errMsg == 'getLocation:auth denied') {
					mui.alert("用户在小程序中未授权");
				} else if (res.errMsg == 'getLocation:fail authorize no response') {
					mui.alert("用户在小程序中未授权");
				}
				reject(res);
			},
			complete() {}
		})
	});
	return promise;
}
