import { wxLogin, wxRequestGet, wxRequestPost } from "./utils/promise-wx-api"; import { doAfterLogin, nextTick } from "./utils/tools"; import { apiUrl } from "@config/config"; App({ globalData: { // 小程序初始化启动时间 appLaunchTime: "", previewImage: false, landing: false, // 是否微信登陆中 extAppid: "", // 第三方APPID token: "", // 是否新手体验区环境 isNoviceEnv: false, // 是否手机号登录 isPhoneAccount: false, // 是否已经登录并选择网格啦 isLogined: false, customerId: "", gridId: "", agencyId: "", // 调查问卷详情传参用 orgInfo: { orgId: "", orgName: "", orgType: "", }, // 组织或网格的经纬度 latitude: 0, longitude: 0, userRoleList: [], currentGridName: "", userInfo: { userStreetTrueName: "", userStreetName: "", realName: "", nickname: "", userHeadPhoto: "", }, userPhone: "", pageCustomerData: {}, customOptions: {}, tempData: { subjectTitle: "", subjectSuggest: "", invitationId: "", }, isHintedFillIdNum: false, }, async onLaunch() { setTimeout(() => { // this.loadFontFace(); }, 100); this.globalData.appLaunchTime = new Date().getTime(); let menuButtonObject = wx.getMenuButtonBoundingClientRect(); wx.getSystemInfo({ success: (res) => { //导航高度 let statusBarHeight = res.statusBarHeight, navTop = menuButtonObject.top, navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2; this.globalData.navHeight = navHeight; this.globalData.statusHeight = statusBarHeight; this.globalData.navTop = navTop; this.globalData.windowHeight = res.windowHeight; }, fail(err) { console.log(err); }, }); let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}; console.log("extConfig", extConfig); this.globalData.extAppid = extConfig.extAppid; wx.setStorageSync("extAppid", extConfig.extAppid); this.syncLocalAccountInfo(); }, loadFontFace() { wx.loadFontFace({ global: true, family: "Source Han Serif SC", scopes: ["webview", "native"], source: 'url("' + apiUrl.slice(0, -4) + 'fonts/syst/SourceHanSerifSC-Regular.otf")', success(res) { console.log( " 字体加载------------------------------------------res ", res ); }, fail(err) { console.log( " 字体加载------------------------------------------err ", err ); }, }); wx.loadFontFace({ global: true, family: "Source Han Serif SC", scopes: ["webview", "native"], desc: { weight: "bold", }, source: 'url("' + apiUrl.slice(0, -4) + '/fonts/syst/SourceHanSerifCN-Bold.otf")', success(res) { console.log(" 重体---------------------res ", res); }, fail(err) { console.log(" 重体---------------------err ", err); }, }); // wx.loadFontFace({ // global: true, // family: "Source Han Serif SC", // desc: { // weight: "800", // }, // source: // 'url("' + // apiUrl.slice(0, -4) + // '/fonts/syst/SourceHanSerifCN-Bold.otf")', // success(res) { // console.log(" 重体---------------------res ", res); // }, // fail(err) { // console.log(" 重体---------------------err ", err); // }, // }); // wx.loadFontFace({ // global: true, // family: "Source Han Serif SC", // desc: { // weight: "900", // }, // source: // 'url("' + // apiUrl.slice(0, -4) + // '/fonts/syst/SourceHanSerifCN-Bold.otf")', // success(res) { // console.log(" 重体---------------------res ", res); // }, // fail(err) { // console.log(" 重体---------------------err ", err); // }, // }); }, // 异步函数:登陆后再操作 doAfterLogin, // 从本地数据库同步账户信息 syncLocalAccountInfo() { this.globalData.isLogined = wx.getStorageSync("isLogined") === false ? false : true; if (this.globalData.isLogined) { this.globalData.isNoviceEnv = wx.getStorageSync("isNoviceEnv") || false; this.globalData.isPhoneAccount = wx.getStorageSync("isPhoneAccount") || false; if (this.globalData.isPhoneAccount) { this.globalData.token = wx.getStorageSync("token") || ""; } } return this.globalData.isLogined; }, // 设置账户信息 并保存本地 setAccountInfo(obj = {}) { Object.keys(obj).forEach((k) => { this.globalData[k] = obj[k]; wx.setStorageSync(k, obj[k]); }); }, // 后端记录退出操作 async logoutRecord() { const { msg, data: { data }, } = await wxRequestPost( "auth/thirdlogin/delresitoken", {}, { isQuiet: true, } ); if (msg != "success" || data.code !== 0) { console.log("退出失败"); return false; } else { console.log("退出成功"); return true; } }, // 退出登录 async logout() { this.setAccountInfo({ token: "", isNoviceEnv: false, isPhoneAccount: false, isLogined: false, }); this.logoutRecord(); wx.showToast({ title: "退出成功", icon: "none", duration: 2000, }); await nextTick(1500); this.toGuidePage(); }, toGuidePage() { wx.reLaunch({ url: "/pages/guide/guide", }); }, async toGuidePageAfterHint() { wx.showToast({ title: "登录已失效", icon: "none", duration: 2000, }); await nextTick(1500); this.toGuidePage(); }, // 手机账户下 token换取token async loginbytoken() { const { msg, data: { data }, } = await wxRequestPost( "auth/thirdlogin/tonewtoken", {}, { isQuiet: false, } ); if (msg != "success" || data.code !== 0 || !data.data || !data.data.token) { console.log("换取token失败", msg); return false; } else { console.log("换取token成功", data.data.token); this.setAccountInfo({ token: data.data.token }); return true; } }, // 微信code获取token async loginbywxcode() { const retWxLogin = await wxLogin({}); console.log(retWxLogin); if (retWxLogin.msg != "success") { console.log("获取微信code失败:", retWxLogin); return false; } const extAppid = wx.getStorageSync("extAppid"); const url = extAppid ? "auth/thirdlogin/resilogin" : "auth/login/resiwxmp/loginbywxcode"; const { msg, data: { data }, } = await wxRequestPost( url, { wxCode: retWxLogin.data.code, app: "resi", client: "wxmp", encryptedData: "", iv: "", appId: extAppid, }, { hasToken: false, isQuiet: false, } ); if (msg != "success" || data.code !== 0) { console.log("登录失败", msg); return false; } else { console.log("登录成功", data.data.token); this.setAccountInfo({ token: data.data.token }); return true; } }, // 获取token async loginGetToken() { // 手机账户要做单独处理 const { isPhoneAccount } = this.globalData; if (isPhoneAccount) { if (!(await this.loginbytoken())) { this.toGuidePageAfterHint(); return false; } else { return true; } } else { return await this.loginbywxcode(); } }, // 获取最近的网格 async getLatestGridData(noSkip = false) { const extAppid = wx.getStorageSync("extAppid"); const url = extAppid ? "resi/mine/mygrid/latestgridinfo" : "resi/mine/mygrid/getlatestgridinfo"; const { msg, data: { data }, } = await wxRequestGet( url, { appId: extAppid }, { isQuiet: false, } ); if (msg != "success") { console.log(msg); return false; } else { if (data.code === 0) { if (data.data && data.data.gridId) { this.globalData.gridId = data.data.gridId; this.globalData.customerId = data.data.customerId; return true; } else { if (!noSkip) { console.log("找不到最近访问网格"); this.toGuidePage(); } return false; } } else { return false; } } }, // 加入网格初始化 async enterGridData() { const { customerId, gridId } = this.globalData; const { msg, data: { data }, } = await wxRequestPost( "resi/guide/user/entergrid", { customerId, gridId, }, { isQuiet: false, } ); if (msg != "success") { console.log(msg); return false; } else { if (data.code === 0) { wx.setStorageSync("userRoleList", data.data.userRoleList); wx.setStorageSync("gridName", data.data.currentGridName); this.globalData.agencyId = data.data.agencyId; this.globalData.userRoleList = data.data.userRoleList; this.globalData.currentGridName = data.data.currentGridName; this.globalData.orgInfo.orgId = data.data.orgId; this.globalData.orgInfo.orgName = data.data.orgName; this.globalData.orgInfo.orgType = data.data.orgType; this.globalData.userInfo.realName = data.data.realName; this.globalData.userInfo.nickname = data.data.nickname; this.globalData.userInfo.userStreetName = data.data.userStreetName || ""; this.globalData.userInfo.userStreetTrueName = data.data.userStreetTrueName || ""; this.globalData.userInfo.userHeadPhoto = data.data.userHeadPhoto; this.globalData.latitude = parseFloat(data.data.latitude); this.globalData.longitude = parseFloat(data.data.longitude); console.log("加入网格成功。", data.data); return true; } else { return false; } } }, // 切换所在网格 async switchGrid(customerId, gridId) { this.globalData.customerId = customerId; this.globalData.gridId = gridId; await this.enterGridData(); }, // 登陆 async login() { if (this.globalData.landing) return 0; const pages = getCurrentPages(); console.log("当前页面", pages); if (pages.length > 0) { const { route } = pages[pages.length - 1]; if ( route == "pages/guide/guide" || route == "pages/guide/novice/index" || route == "pages/guide/chooseGrid/chooseGrid" ) return 0; } this.globalData.landing = true; // 是否第一次打开小程序或退出过 if (!this.globalData.isLogined) { this.toGuidePage(); return 0; } // 获取网格、个人信息 if ( (await this.loginGetToken()) && (await this.getLatestGridData()) && (await this.enterGridData()) ) { this.globalData.landing = false; return 1; } else { this.globalData.landing = false; return 0; } }, // 前往切换标签页面 switchTab(tab) { const pages = getCurrentPages(); const mainPageIndex = pages.findIndex( (item) => item.route == "pages/main/index" ); if (mainPageIndex !== -1) { let mainPageItem = pages.find((item) => item.route == "pages/main/index"); mainPageItem.switchTab(tab); wx.navigateBack({ delta: pages.length - mainPageIndex - 1, }); } else { wx.reLaunch({ url: `/pages/main/index?tab=${tab}` }); } }, });