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 otherUserId = getUrlParam('otherUserId'); var unionPhone = getUrlParam('unionPhone'); if (jpuid) { var obj = carhelp.get("xpgj_wx_user_info") if (obj == null || obj == '') { obj = { } } obj.unionId = jpuid obj.otherUserId = otherUserId 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 (uid&&unionPhone) { this.ckUid(); } else if (!openId) { this.getOpenId(); } else { this.getPersion(openId) } } } else { var NODE_NAME = process.env['NODE_NAME'] if (NODE_NAME == "production") { alert("请使用微信打开本页面") } } }, ckUid() { if (uid&&unionPhone) { API_WeiXin.findByPhone(uid, unionPhone).then(response => { carhelp.setOpenId(response.data.regUser.openId) openId = response.data.openId; carhelp.set("xpgj_wx_user_info", { unionPhone: unionPhone, openId: response.data.regUser.openId, unionId: response.data.regUser.unionId, }) var token = response ? response.data.token : ''; carhelp.setPersonInfo(response.data.tenantInfo); carhelp.setPersonInfoPlus(response.data); carhelp.setToken(token); //location.reload(); }).catch(error => { console.log(error); //this.getOpenId() }); } else { this.getOpenId(); } }, 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 (obj && obj.unionPhone) { loginurl = "/mobile/employeeUser/findByPhone" data.phone = obj.unionPhone } 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() { 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