import { getToken, setToken, getOpenId, setOpenId, setUserInfo, getUserInfo, getSessionKey, setSessionKey, } from "../../utils/auth"; import { wxLogin, maLogin, getCodeImg, getMaOpenId, refreshMobileToken, findUserInfoByCache, } from "../../api/login"; import { findMyProjectList, findProSubDeptsUserInfo } from "../../api/project"; import config from "../../config"; const app = getApp(); Page({ /** * 页面的初始数据 */ data: { username: "", password: "", code: "", uuid: "", codeUrl: "", checked: true, logo: config.baseImgUrl + "/cdn/app_logo.png", }, //获取填写的账号信息 name: function (even) { this.setData({ username: even.detail.value, }); }, //获取填写的密码信息 psw: function (even) { this.setData({ password: even.detail.value, }); }, //获取填写的密码信息 code: function (even) { this.setData({ code: even.detail.value, }); }, onLoad: function (option) { this.getMaOpenId(); if (getToken()) { console.log("Authorization...{}", getToken()); //刷新权限信息 refreshMobileToken().then((res) => { if (res.code == "200") { this.getUserInfoByCache(); } else { this.loadCodeImage(); } }); } else { console.log("未查询到Token...{}...准备重新登录"); this.loadCodeImage(); } }, /** * 授权登录 */ getMaOpenId: function () { wx.login({ success: (res) => { getMaOpenId({ code: res.code }).then((response) => { setOpenId(response.data.openid); setSessionKey(response.data.sessionKey); }); }, }); }, loadCodeImage: function () { getCodeImg().then((res) => { let captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled; if (captchaEnabled) { this.setData({ codeUrl: "data:image/gif;base64," + res.img, uuid: res.uuid, }); } }); }, //登录 userLogin: function () { var username = this.data.username; var password = this.data.password; var code = this.data.code; if (username == "") { app.toast("登录账号不能为空"); return; } if (password == "") { app.toast("登录密码不能为空"); return; } if (code == "") { app.toast("验证码不能为空"); return; } //启动蒙版 let that = this; //发送请求 wxLogin({ username: username, password: password, code: code, uuid: that.data.uuid, }).then((res) => { if (res.code == "200") { setToken(res.data.access_token); //跳转页面 this.getUserInfoByCache(); } else { that.loadCodeImage(); } }); }, getPhoneNumber(e) { if (e.detail.code) { let data = { openId: getOpenId(), sessionKey: getSessionKey(), iv: e.detail.iv, encryptedData: e.detail.encryptedData, }; maLogin(data).then((res) => { setToken(res.data.access_token); this.getUserInfoByCache(); }); } else { //用户决绝授权 app.toast("请允许微信手机号一键登录"); } }, /** * 查询 * 用户详细信息 * @param {*} e */ getUserInfoByCache: function () { findUserInfoByCache().then((res) => { if (res.code == 200) { res.user.isTechnician = (res.user.roles || []).find( (role) => role.roleKey.indexOf("zbjsy_") >= 0 ) != null; setUserInfo(res.user); app.globalData.userData = res.user; this.getMyProjectList(); } }); }, /** * 查询 * 用户项目信息 * 根据项目配置进入不同页面... */ getMyProjectList: function () { findMyProjectList().then((res) => { if (res.code == 200) { app.globalData.projectInfoList = res.rows; if (res.rows.length > 0) { if (res.rows.length == 1) { app.globalData.useProjectId = res.rows[0].id; app.globalData.useProjectName = res.rows[0].projectName; findProSubDeptsUserInfo(app.globalData.useProjectId).then( (detail) => { if (detail.code == 200) { let userInfo = getUserInfo(); userInfo.projectUserInfo = detail.data; setUserInfo(userInfo); //单项目直接进入项目页面 wx.redirectTo({ url: "../../pageage/project_info/index", }); } } ); } else { //多项目进入项目切换页面 wx.redirectTo({ url: "../../pageage/project_list/index", }); } } else { app.toast("未查询到当前用户项目,信息审核中或人员已离场"); return false; } } }); }, });