import wx from 'weixin-js-sdk'
import * as API_WeiXin from '@/apis/weixin.js'
import * as API_Common from '@/apis/common.js'

import Qs from 'qs';
import request from './request.js'
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','openLocation'];
	 
	let promise = (jsApiList => {
		return new Promise((resolve, reject) => {
//			console.log(jsApiList);
			API_WeiXin.getConfig().then(response => {
				var wxconfig = response.data.wxConfig;
			
				wx.config({
					debug: false, // 开启调试模式,
					appId: wxconfig.appId, // 必填，企业号的唯一标识，此处填写企业号corpid
					timestamp: wxconfig.timestamp, // 必填，生成签名的时间戳
					nonceStr: wxconfig.nonceStr, // 必填，生成签名的随机串
					signature: wxconfig.signature, // 必填，签名，见附录1
					jsApiList: jsApiList, // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
					openTagList:['wx-open-subscribe']
				});
				wx.ready(function() {
					resolve(response.data);
				})
				wx.error(function(res) {
					reject('微信api配置出错');
				});
			}).catch(error => {
				//mui.toast(error);
				reject(error);
			})
		})
	})(jsApiList)

	return promise;
}

//选择图片,多图上传和单图上传的回调处理不一样
export function chooseImage(count,islocalId) {
	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;
				
				if(islocalId){
					
					resolve(localIds);
				}else{
					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);

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

//获取坐标
export function getLocation() {
	if(process.env.NODE_ENV === "development" && process.car.SIMPLE_RUN){
		
		let promise =new Promise((resolve, reject) => {
			var obj= {
			longitude:112.28541,
			latitude:30.308354,
			errMsg:"getLocation:ok"
			}
			resolve(obj);
		})
		return promise
	}
	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') {
						
						res="定位超时，请检查是否开启'定位'"
					} else if (res.errMsg == 'getLocation:ERROR_NETWORK') {
						//mui.alert("网络异常");
						res="定位超时，请检查是否开启'定位'"
					} else if (res.errMsg == 'getLocation:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF') {
						res="没开启系统定位"
						//mui.alert("没开启系统定位");
					} else if (res.errMsg == 'getLocation:system permission denied') {
						res="未给微信位置授权"
						//mui.alert("未给微信位置授权");
					} else if (res.errMsg == 'getLocation:location permission') {
						res="未给微信位置授权"
						//mui.alert("未给微信位置授权");
					} else if (res.errMsg == 'getLocation:auth denied') {
						res="用户在小程序中未授权"
						//mui.alert("用户在小程序中未授权");
					} else if (res.errMsg == 'getLocation:fail authorize no response') {
						res="用户在小程序中未授权"
						//mui.alert("用户在小程序中未授权");
					} else if (res.errMsg == 'getLocation:gps closed') {
						res="用户已取消定位"
						//mui.alert("用户已取消定位");
					}
					reject(res);
				},
				complete() {}
			})
		})
	})
	return promise;
}

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

				reject(res);
			},
			complete() {
				if(obj){
					obj.showLoading(false)	
				}
			}
		})
	});
	return promise;
}

export function requestSubscribeMessage(sz) {
	 
	let promise = new Promise((resolve, reject) => {
		wx.ready(function() {
			wx.requestSubscribeMessage({
			
			tmplIds: sz,
			
				success(res) { 
						resolve(res);
				}
				
			})
		})
		

	});
	return promise;
}




//扫描二维码
export function openLocation(obj) {
 
	let promise = new Promise((resolve, reject) => {
		wx.openLocation(obj)
	});
	return promise;
}