// import qq from '@/utils/qqmap-wx-jssdk.min.js'
// import QQMapWX from './qqmap-wx-jssdk.min.js';
// import QQMapWX from './qqmap-wx-jssdk.min.js';
import log from "@/utils/log";

const QQMapWX=require('./qqmap-wx-jssdk.min.js')
import * as QQMapwx from '@/utils/qqmap-wx-jssdk.min.js'
import {getSTS} from "@/api/other";
var qqmapsdk; 
import {imghost, host, oss } from '@/config/host.js'
const api = {
	
	loginModalState: true,
	mapLocation:()=>{
		
		return new Promise((relove, reject) => {
			
			wx.authorize({
				scope: 'scope.userFuzzyLocation',
				success(res) {
					if(res.errMsg == 'authorize:ok'){
						wx.getFuzzyLocation({
						    type: 'wgs84',
						    success(re) {
								console.log(re);
								uni.chooseLocation({
									latitude:re.latitude,
									longitude:re.longitude,
									success: function (data) {
										console.log(data);
										let obj={
											latitude:data.latitude,
											longitude: data.longitude,
											address:data.address
										} 
										if(data.errMsg=='chooseLocation:ok'){
											qqmapsdk=new QQMapWX({
												key:'K65BZ-LULE4-744UV-X4EIA-27RA5-A4FWT'
											})
											qqmapsdk.reverseGeocoder({
												location:{ 
													latitude:data.latitude ,
													longitude: data.longitude
												},
												success: function(mapres){
													console.log(mapres);
													if(mapres.message=='query ok'){
														obj.name=mapres.result.formatted_addresses.recommend
														obj.city=mapres.result.address_component.city
														relove(obj)
													}
												}
											})
										}
										// console.log('位置名称:' + data.name);
										// console.log('详细地址:' + data.address);
										// console.log('纬度:' + data.latitude);
										// console.log('经度:' + data.longitude);
									}
								});
						    }
						})
					}
				},
				fail(err) {
					console.log(err)   
				}
			})
		})
	},
	getloction(details){
		return new Promise((relove, reject)=>{
			qqmapsdk=new QQMapWX({
				key:'K65BZ-LULE4-744UV-X4EIA-27RA5-A4FWT'
			})
			qqmapsdk.geocoder({
			      //获取表单传入地址
			      address: details.activedeta.city+details.activedeta.address, //地址参数,例:固定地址,address: '北京市海淀区彩和坊路海淀西大街74号'
			      success: function(res) {//成功后的回调
					console.log(res);
					relove(res.result)
			      },
			      fail: function(error) {
			        console.error(error);
			      },
			      complete: function(res) {
			        console.log(res);
			      }
			})
		})
	},
	urlToObj:(url)=>{
		let obj = {}
		  let str = url.slice(url.indexOf('?') + 1)
		  let arr = str.split('&')
		  for (let j = arr.length, i = 0; i < j; i++) {
		    let arr_temp = arr[i].split('=')
		    obj[arr_temp[0]] = arr_temp[1]
		  }
		  return obj
	},
	upfile:(file)=>{
		// console.log(file);
		return new Promise((relove, reject) => {
			// console.log(file);
			uni.showLoading({
				title:'上传中'
			})
		uni.uploadFile({
			url: `${host}/other/upload`, //仅为示例,非真实的接口地址
			filePath:file,
			name: 'file',
			success: (uploadFileRes) => {
				uni.hideLoading()
				relove(JSON.parse(uploadFileRes.data).data)
				// console.log(JSON.parse(uploadFileRes.data).data);
			}
		});
		})
	},
	imgurl:(url)=>{
		if(url){
			if(url.indexOf('http')==-1){
				return imghost+url
			}else{
				return url
			}
		}else{
			return ''
		}
		
	},
	imgurl2:(url)=>{
		if(url){
			if(url.indexOf('https')==-1){
				console.log(url.replace('http','https'))
				return url.replace('http','https')
			}else{
				return url
			}
		}else{
			return ''
		}
		
	},
	/**
	 * 失败
	 */
	error: (msg) => {
	
		return new Promise((relove, reject) => {
			uni.showToast({
				title: msg,
				icon: "none"
			})
			setTimeout(() => {
				relove();
			}, 1500)
		})
	
	},
	/**
	 * 成功提示
	 */
	success: (msg) => {

		return new Promise((relove, reject) => {
			uni.showToast({
				title: msg,
				icon: "success"
			})
			setTimeout(() => {
				relove();
			}, 1500)
		})

	},
	/**
	 * 确认对话框
	 * @param {Object} contents
	 * @param {Object} callback
	 */
	confirm: (contents) => {

		return new Promise((relove, reject) => {

			uni.showModal({
				title: '提示',
				content: contents,
				success: function(res) {
					if (res.confirm) {
						relove();
					}
				}
			});

		})

	},
	payment: (param) => {

		return new Promise((relove, reject) => {
			uni.requestPayment({
				provider: 'wxpay',
				timeStamp: param.timeStamp,
				nonceStr: param.nonceStr,
				package: param.package,
				signType: param.signType,
				paySign: param.paySign,
				success: function(res) {
					relove(res);
				},
				fail: function(err) {
					console.log(err);
					// relove(err);
					api.error("支付失败");
				}
			});

		})
	},
	navHeight: () => {
		let system,menu,systemBar,navigationBar,navPaddingTop,navHeight,headerPadding;
		//获取系统信息
		try{
			wx.getSystemInfo({
				success: res => {
					this.system = res
				}
			})
				log('res',this.system);
				//获取胶囊信息
				this.menu = wx.getMenuButtonBoundingClientRect()
				this.systemBar = this.system.statusBarHeight //状态栏高度
				this.navigationBar = (this.menu.top - this.system.statusBarHeight) * 2 + this.menu.height //胶囊高度
				this.navPaddingTop = this.systemBar;
				this.navHeight = this.menu.height;
				this.headerPadding = (this.menu.top - this.systemBar);
			
				this.systemBarHeight = (this.navPaddingTop+this.navHeight + (this.headerPadding *2))
		}catch(e){
			//默认高度,刘海屏
			this.navPaddingTop = 43;
			this.navHeight = 32;
			this.headerPadding = 4;
		}
		return {
			navPaddingTop:this.navPaddingTop,
			navHeight:this.navHeight,
			headerPadding:this.headerPadding,
			systemBarHeight:this.systemBarHeight,
			windowHeihgt:(this.system.windowHeight - this.systemBarHeight)
		};
	},
	wxSystemInfo: () => {
		let system;
		//获取系统信息
		try{
			wx.getSystemInfo({
				success: res => {
					this.system = res
				}
			})
		}catch(e){
			
		}
		return {
			system:this.system
		};
	},
	uniSysInfo:() => {
		return new Promise((resolve,reject)=>{
			let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
			let sysMenuHeight = uni.$u.sys().statusBarHeight;
			let menuButtonHeight = menuButtonInfo.height
			let menuButtonPadding = menuButtonInfo.top - sysMenuHeight
			let data = {
				sysMenuHeight:sysMenuHeight,
				menuButtonHeight:menuButtonHeight,
				menuButtonPadding:menuButtonPadding,
				sysAndMenuHeight:(sysMenuHeight+menuButtonHeight+(menuButtonPadding*2))
			}
			uni.setStorageSync('sysInfo',data);
			resolve(data);
		})
	},
	uploadOssFile:(file)=>{
		return new Promise((resolve, reject) => {
			getSTS({}).then(res => {
				// const client = new OSS(res.data);
				// console.log(file);
				uni.showLoading({
					title:'上传中'
				})
				let timer = new Date();
				let address = timer.getFullYear() + '' + (timer.getMonth() + 1) + '' + timer.getDate();
				address = 'upload/' + address + '/';
				var imageSrc = file;
				let str = file.substr(file.lastIndexOf('.'));
				let nameStr = address + timer.getTime() + str;
				uni.uploadFile({
					url: `${oss}`, //仅为示例,非真实的接口地址
					filePath:file,
					name: 'file',
					header: {
						"content-type": "application/json;charset=utf-8",
					},
					formData:Object.assign({
						name:nameStr,
						key:nameStr,
						success_action_status: '200',
					},res.data),
					success: (uploadFileRes) => {
						uni.hideLoading()
						resolve ({show_path:oss+'/' + nameStr});
						// relove(JSON.parse(uploadFileRes.data).data)
						// console.log(JSON.parse(uploadFileRes.data).data);
					},
					fail:(res) =>{
						uni.hideLoading()
						console.log(res)
						return {};
					}
				});
			});
		})
	},
}
export default api;