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

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

var app = {
	init: function() {
		 
		if(isWeiXin()|| !(process.env.NODE_ENV === "development") ){
			if (isWeiXin()) {
				if (checkOpenId) {
					if (!openId) {
						this.getOpenId();
					} else {
					 
					}
				}
			} else{
				alert("请使用微信打开本页面")
			}
		}else{
			carhelp.setOpenId(process.car.openId)
		}
		
		
	},
	 
	getOpenId() {
		
		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
