import {
	wechatConfig,
	share,
	getWxconfig
} from '@/api/wx';
import {
	getCompany
} from '@/api/index.js'
import store from '../store';
// 保存wx对象
// #ifdef H5
let weixinObj = require('jweixin-module');
// #endif
// #ifdef MP-WEIXIN
let weixinObj
// #endif
// // #ifdef MP-WEIXIN
// let weixinObj = wx
// // #endif
// 保存分享数据
let shareData = '';
// 保存注册返回appId数据
let appId = '';

//微信登录
/**
 * @param {string} 当前页面地址信息
 */
export function loginWinxin() {
	let pages, page, path;
	try {
		pages = getCurrentPages();
		// 获取跳转前页面
		page = pages[pages.length - 1];
		// 获取跳转前路由地址
		path = page.route;
	} catch (e) {
		console.log(e);
	}
	// 判断是否在登录页
	if (path != 'pages/redirect/redirect') {
		setTimeout((e) => {
			weixinObj.ready(() => {
				console.log('weixinObj.ready');
				// 调用
				try {
					console.log(shareData);
					console.log(path);
					weixinSq(shareData, path);
				} catch (e) {
					console.log(e);
					//TODO handle the exception
				}
			});
			weixinObj.error((e) => {
				console.log(e);
			})
		}, 10)

	}
};
// 微信授权登录
function weixinSq(data, path) {
	// 微信授权后跳转页面
	try {
		let ul = encodeURIComponent(store.state.baseURL + store.state.urlFile + '/#/pages/redirect/redirect');
		console.log(ul, "ul")
		// 打开微信授权页面

		let url =
			'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
			appId +
			'&redirect_uri=' +
			ul +
			'&response_type=code&scope=snsapi_userinfo&state=' +
			new Date().getTime() +
			'#wechat_redirect';
		console.log(url, "url")
		window.location.href = url;
	} catch (e) {
		console.log(e);
	}
};

// 微信注册
export function weixindata(config) {
	// console.log(config);
	//注册微信信息
	let weixinObj = require('jweixin-module');
	// 保存微信对象到全局仓库
	store.commit('setWeiChatObj', weixinObj)
	console.log(window.location.href);
	console.log('当前注册url地址', window.location.href);
	getWxconfig({
		url: store.state.baseURL + store.state.urlFile + '/',
	}).then(({
		data
	}) => {
		appId = data.appId;
		// 微信信息配置
		weixinObj.config({
			debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
			appId: data.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
			timestamp: data.timestamp, // 必填,生成签名的时间戳
			nonceStr: data.nonceStr, // 必填,生成签名的随机串
			signature: data.signature, // 必填,签名,见附录1
			jsApiList: data.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
		});
		weixinObj.ready((e) => {
			console.log('分享数据', shareData);
			if (shareData == '') {
				getCompany().then(({
					data
				}) => {
					// 保存appId
					shareData = {
						title: data.company_name,
						imgUrl: data.wechat_share_image,
						desc: data.share_info,
					}
					shareLoad(config)
				})
				
			} else {
				shareLoad(config)
			}
		})
	})

	// wechatConfig({
	// 		// url: 'pages/applic/location',			
	// 		url: store.state.baseURL+ store.state.urlFile+'/',
	// 	})
	// 	.then(({
	// 		data
	// 	}) => {
	// 		// uni.showModal({ 
	// 		// 	// url: 'http://yyred.igxys.com/index/',
	// 		// 	title:'地址',
	// 		// 	content: window.location.href
	// 		// })

	// 		// 保存appId
	// 		store.commit('setWeiChatInfo', data)

	// 		appId = data.appId;
	// 		// 微信信息配置
	// 		weixinObj.config({
	// 			debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
	// 			appId: data.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
	// 			timestamp: data.timestamp, // 必填,生成签名的时间戳
	// 			nonceStr: data.nonceStr, // 必填,生成签名的随机串
	// 			signature: data.signature, // 必填,签名,见附录1
	// 			jsApiList: data.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
	// 		});
	// 		weixinObj.ready((e) => {
	// 			console.log('分享数据',shareData);
	// 			if (shareData == '') {
	// 				share({}).then(({
	// 					data
	// 				}) => {
	// 					shareData = data.data
	// 					shareLoad(config)
	// 				});
	// 			} else {
	// 				shareLoad(config)
	// 			}
	// 		})
	// 	})
	// 	.catch(e => {
	// 		console.log(e);
	// 	});
}
// 加载分享数据
function shareLoad(config) {
	let mess;
	if (config) {
		mess = {
			link: config.link, // 分享链接
			imgUrl: config.imgUrl,
			desc: config.desc,
			title: config.title,
			success: config.success,
			fail: config.fail || function(e) {
				console.log(e);
			}
		}
	} else {
		// console.log(window.location.href, 55)
		// let url = window.location.href + '?spread=' + store.state.userInfo.uid;
		let url = window.location.href;
		url = url.replace(/[\?,&]{0,1}from=singlemessage/g, '');
		mess = {
			title: shareData.title,
			link: url, // 分享链接
			imgUrl: shareData.img, // 分享图标
			desc: shareData.synopsis,
			success: function() {},
			fail: function(e) {
				console.log(e);
			}
		}
	}
	// 获取仓库数据
	// 分享好友
	// weixinObj.updateAppMessageShareData(mess);
	weixinObj.onMenuShareAppMessage(mess); // 即将废弃
	// 分享朋友圈
	// weixinObj.updateTimelineShareData(mess)
	weixinObj.onMenuShareTimeline(mess); // 即将废弃
}

// 隐藏功能按钮
export function weixinhideMenu(config) {
	console.log(config)
	weixinObj.ready((e) => {
		weixinObj.hideMenuItems({
			menuList: config, // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3
		});
	})

}

// export function getLoca(config) {
// 	console.log(config)
// 	weixinObj.ready((e) => {
// 		weixinObj.getLocation({
// 		    type: 'wgs84',
// 		    success: function (res) {
// 					config.longitude = res.longitude;
// 					config.latitude = res.latitude;
// 					uni.showModal({
// 						title: '提示',
// 						content: res.longitude
// 					})
// 					console.log('当前位置的经度:' + res.longitude);
// 					console.log('当前位置的纬度:' + res.latitude);
// 		    }
// 		});
// 	})

// }

export default {
	weixinObj,
	shareData,
	appId
}