etms_wechat_mini_oem/yunbainian/etms_wechat_mini/app.js

78 lines
1.6 KiB
JavaScript

const {
etmsPostTemp
} = require("./com");
App({
onLaunch(opts, data) {
if (opts && opts.scene === 1154) {
this.globalData.singlePage = true;
} else {
this.globalData.singlePage = false;
}
this.etmsLogin("onLaunch");
},
onShow() {
if (!this.globalData.s) {
this.etmsLogin("onShow");
}
},
async etmsLogin(msg) {
if (this.globalData.islogin) {
return
} else {
// 登录
this.globalData.islogin = true
const data = await this.login();
}
},
async login() {
if (this.globalData.singlePage) {
return;
}
return new Promise((resolve, reject) => {
wx.login({
success: (res) => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
resolve(etmsPostTemp(
"openParent2/WxMiniLogin", {
code: res.code,
},
this.setLoginInfo,
))
},
});
})
},
setLoginInfo(res) {
this.globalData.s = res.s;
this.globalData.u = res.u;
this.globalData.openId = res.openId;
this.globalData.unionid = res.unionid;
this.globalData.nickName = res.nickName;
this.globalData.name = res.name;
this.globalData.avatarUrl = res.avatarUrl;
this.globalData.phone = res.phone;
if (res.avatarUrl) {
this.globalData.isGetUser = true;
}
},
globalData: {
islogin: false,
s: "",
u: "",
openId: "",
unionid: "",
nickName: "",
name: "",
avatarUrl: "",
isGetUser: false,
microWebHomeUrl: "https://baidu.com",
currentActivityBag: {},
currentActivityBag2: {},
singlePage: false
},
});