import carhelp from '@/apis/utils/mixin.js'

import * as API_WeiXin from '@/apis/weixin.js'

import {
	getUrlParam,
	getWeixinRedirectURI,
	isWeiXin
} from '@/apis/utils'
var checkOpenId = true; //是否需要获取openId
var openId = carhelp.getOpenId();
var uid = carhelp.getUID();
var unionPhone=carhelp.getUnionPhone()
var app = {

	init: function() {
		// isWeiXin()|| 
		const jpuid = getUrlParam('unionid');
		const relationCompanyName = getUrlParam('relationCompanyName');
		var unionPhone = getUrlParam('unionPhone');
		
		if (jpuid) {
			var obj = carhelp.get("xpgj_wx_user_info")
			if (obj == null || obj == '') {
				obj = {
					
				}
			}
			obj.unionId = jpuid
			if(obj.relationCompanyName!=relationCompanyName){
				
				carhelp.set("token_tdate",'');
			}
			
			obj.relationCompanyName = relationCompanyName
			obj.unionPhone=unionPhone
			carhelp.set("xpgj_wx_user_info", obj)
			uid = carhelp.getUID();
			unionPhone=carhelp.getUnionPhone()
		}

		if (isWeiXin() || (process.env.NODE_ENV === "development")) {

			if (checkOpenId) {
				 if (!openId&&!uid) {
					this.getOpenId();

				} else {
					this.getPersion(openId)
				}
			}
		} else {
			var NODE_NAME = process.env['NODE_NAME']
			if (NODE_NAME == "production") {
				alert("请使用微信打开本页面")
			}
		}



	},
	
	getPersion(thisopenId) {
		console.log("init")
		var tdate = new Date(+new Date() + 8 * 3600 * 1000).toJSON().substr(0, 15).replace("T", " ")
		var token_tdate = carhelp.get("token_tdate"); //每10分钟存储一次token

		if (token_tdate && token_tdate == tdate) {
			return
		}

		var loginurl = "/mobile/employeeUser/findByOpenId"
		var data = {
			openId: thisopenId,
			unionid: carhelp.getUID(),

			//data.type="5"
			code: "reading"
		}
		var obj = carhelp.get("xpgj_wx_user_info")
		if ('prodv4'==process.car.NODE_ENV) {
			loginurl = "/mobile/employeeUser/findByCNameAndPhone"
			data.phone = obj.unionPhone
			data.companyName=obj.relationCompanyName
			data.unionId=obj.unionId
		}
		
		 
		uni.request({
			method: 'get',
			url: process.car.BASE_URL + loginurl,
			data: data,
			header: {
				'Content-Type': 'application/x-www-form-urlencoded',
				'X-Requested-With': 'XMLHttpRequest',
				//'Authorization':token
			}
		}).then((response) => {
			let [error, res] = response;

			if (res.data.code == 200 && res.data.result) {

				var token = res ? res.data.data.token : '';
				carhelp.setPersonInfo(res.data.data.regUser);
				carhelp.setPersonInfoPlus(res.data.data);

				carhelp.setToken(token);

			} else {
				carhelp.logoff();
			}
		}).catch(error => {


		})
	},
	getOpenId() {
		if (process.env.NODE_ENV === "development"){
			carhelp.setOpenId(process.car.openId)
			location.reload();
			return
		}
		const code = getUrlParam('code');
		var openId = carhelp.getOpenId()
		if (!openId) {
			if (!code) {
				var url = document.URL;
				var getUrl = getWeixinRedirectURI(process.car.VUE_APP_WXAPPID, url);

				window.location.href = getUrl;
			} else {

				API_WeiXin.getDataByCode(code).then(response => {

					carhelp.setOpenId(response.data.openid)
					openId = response.data.openid;
					carhelp.set("xpgj_wx_user_info", response.data)

					location.reload();
				}).catch(error => {
					console.log(error);
				});
			}
		}
	}
}
module.exports = app