From 643f37b06e9051b1b47d619cbdd55bc3b8052e49 Mon Sep 17 00:00:00 2001 From: chenglijuan Date: Wed, 20 May 2026 14:24:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BA=BF=E4=B8=8A=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=88=86=E4=BA=AB=E5=8A=9F=E8=83=BD+=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=90=8E=E5=8F=B0=E7=99=BB=E5=BD=95=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E5=BD=B1=E5=93=8D=E9=A6=96=E9=A1=B5=E6=B8=B2=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 62 ++++++++++++++++++++--------- pages/appointment/appointment.js | 32 +++++++++------ pages/index/index.js | 61 +++++++++++++++-------------- pages/index/index.wxml | 15 ++----- pages/index/index.wxss | 67 ++++++-------------------------- pages/records/records.js | 58 +++++++++++++-------------- pages/scan/result/index.js | 13 +++++++ utils/config.js | 2 +- 8 files changed, 153 insertions(+), 157 deletions(-) diff --git a/app.js b/app.js index 0d7a29a..7da650b 100644 --- a/app.js +++ b/app.js @@ -4,27 +4,48 @@ const { request } = require('./utils/api') App({ onLaunch() { - // 自动静默登录:wx.login 获取 code,再请求后端接口换取 openid + // 优先从本地缓存恢复登录态,避免阻塞页面 + const cached = wx.getStorageSync('userInfo') + if (cached && cached.openid) { + this.globalData.userInfo = cached + this.globalData.isLoggedIn = true + } + // 静默登录获取最新 session,不阻塞页面 this.silentLogin() }, silentLogin() { - wx.login({ - success: (loginRes) => { - if (loginRes.code) { - this.loginWithCode(loginRes.code) - } else { - console.error('wx.login 失败', loginRes.errMsg) + this._loginPromise = new Promise((resolve) => { + this._loginResolve = resolve + wx.login({ + success: (loginRes) => { + if (loginRes.code) { + this.loginWithCode(loginRes.code) + } else { + console.error('wx.login 失败', loginRes.errMsg) + this.handleLoginFail() + } + }, + fail: (err) => { + console.error('wx.login 调用失败', err) this.handleLoginFail() } - }, - fail: (err) => { - console.error('wx.login 调用失败', err) - this.handleLoginFail() - } + }) }) }, + /** + * 返回登录完成的 Promise,供页面 await 使用 + * @param {boolean} useCache - 是否在已有缓存时立即 resolve + * @returns {Promise} + */ + waitLogin(useCache = false) { + if (useCache && this.globalData.isLoggedIn) { + return Promise.resolve(this.globalData.userInfo) + } + return this._loginPromise || Promise.resolve(this.globalData.userInfo || null) + }, + async loginWithCode(code) { try { const data = await request({ @@ -39,22 +60,25 @@ App({ } this.globalData.userInfo = userInfo this.globalData.isLoggedIn = true + this.globalData.loginFailed = false wx.setStorageSync('userInfo', userInfo) - if (this.loginReadyCallback) { - this.loginReadyCallback(userInfo) - } + if (this._loginResolve) this._loginResolve(userInfo) } catch (err) { console.error('后端登录失败', err) - this.handleLoginFail() + // 如果有缓存登录态,登录失败不立即清除,让页面继续使用缓存 + if (!this.globalData.isLoggedIn) { + this.handleLoginFail() + } else { + this.globalData.loginFailed = true + if (this._loginResolve) this._loginResolve(this.globalData.userInfo) + } } }, handleLoginFail() { this.globalData.isLoggedIn = false this.globalData.loginFailed = true - if (this.loginReadyCallback) { - this.loginReadyCallback(null) - } + if (this._loginResolve) this._loginResolve(null) }, globalData: { diff --git a/pages/appointment/appointment.js b/pages/appointment/appointment.js index ca3f2e4..b24c076 100644 --- a/pages/appointment/appointment.js +++ b/pages/appointment/appointment.js @@ -34,20 +34,15 @@ Page({ }, onLoad() { - if (app.globalData.isLoggedIn) { + this._awaitLoginAndInit() + }, + + async _awaitLoginAndInit() { + const userInfo = await app.waitLogin(true) + if (userInfo) { this.initPage() - } else if (app.globalData.loginFailed) { - this.showLoginTipAndGoBack() } else { - // 登录异步未完成,等待回调 - app.loginReadyCallback = (userInfo) => { - app.loginReadyCallback = null - if (userInfo) { - this.initPage() - } else { - this.showLoginTipAndGoBack() - } - } + this.showLoginTipAndGoBack() } }, @@ -292,5 +287,18 @@ Page({ console.error('提交预约失败', err) wx.showToast({ title: '提交失败,请重试', icon: 'none' }) } + }, + + onShareAppMessage(res) { + return { + title: '访客预约', + path: '/pages/index/index' + } + }, + + onShareTimeline() { + return { + title: '访客预约' + } } }) diff --git a/pages/index/index.js b/pages/index/index.js index 8aa845a..c5ae88b 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -11,45 +11,33 @@ Page({ }, onLoad() { + // 首页不阻塞,直接渲染;有缓存登录态时立即加载数据 if (app.globalData.isLoggedIn) { - this.onLoginReady() - } else if (app.globalData.loginFailed) { - this.onLoginFailed() - } else { - app.loginReadyCallback = (userInfo) => { - if (userInfo) { - this.onLoginReady() - } else { - this.onLoginFailed() - } - } - } - }, - - onShow() { - if (app.globalData.isLoggedIn) { + this.setData({ isLoggedIn: true }) this.loadLatestRecord() } }, - onLoginReady() { - this.setData({ isLoggedIn: true, loginFailed: false }) - this.loadLatestRecord() + async onShow() { + // 每次显示时等待登录完成,再加载最新数据 + const userInfo = await app.waitLogin(true) + if (userInfo) { + this.setData({ isLoggedIn: true, loginFailed: false }) + this.loadLatestRecord() + } else { + this.setData({ isLoggedIn: false, loginFailed: true }) + } }, - onLoginFailed() { - this.setData({ isLoggedIn: false, loginFailed: true }) - }, - - onRetry() { + async onRetry() { this.setData({ loginFailed: false }) app.silentLogin() - app.loginReadyCallback = (userInfo) => { - if (userInfo) { - this.onLoginReady() - } else { - this.onLoginFailed() - } + const userInfo = await app.waitLogin() + if (userInfo) { + this.setData({ isLoggedIn: true, loginFailed: false }) + this.loadLatestRecord() + } else { + this.setData({ loginFailed: true }) } }, @@ -85,5 +73,18 @@ Page({ showQrcode(e) { this.selectComponent('#qrcodeModal').show(e.currentTarget.dataset.id) + }, + + onShareAppMessage(res) { + return { + title: '访客预约', + path: '/pages/index/index' + } + }, + + onShareTimeline() { + return { + title: '访客预约' + } } }) diff --git a/pages/index/index.wxml b/pages/index/index.wxml index 73309b2..f3282a4 100644 --- a/pages/index/index.wxml +++ b/pages/index/index.wxml @@ -1,16 +1,9 @@ - - - - 正在获取身份信息... - - - - - ⚠️ - 网络异常,请重试 - 重新加载 + + diff --git a/pages/index/index.wxss b/pages/index/index.wxss index b962f37..96e0804 100644 --- a/pages/index/index.wxss +++ b/pages/index/index.wxss @@ -21,67 +21,24 @@ page { line-height: 1; } -/* loading 遮罩 */ -.loading-mask { - position: fixed; - top: 0; - left: 0; - right: 0; - bottom: 0; - background: #f0f5fa; +/* 登录失败提示条(不遮挡页面) */ +.login-fail-bar { display: flex; - flex-direction: column; align-items: center; - justify-content: center; - z-index: 999; -} - -.loading-spinner { - width: 64rpx; - height: 64rpx; - border: 6rpx solid #dbeafe; - border-top: 6rpx solid #5b9bd5; - border-radius: 50%; - animation: spin 0.8s linear infinite; - margin-bottom: 24rpx; -} - -@keyframes spin { - from { transform: rotate(0deg); } - to { transform: rotate(360deg); } -} - -.loading-text { - font-size: 28rpx; - color: #7f8fa6; - letter-spacing: 2rpx; -} - -/* 登录失败 */ -.fail-icon { - font-size: 80rpx; + justify-content: space-between; + background: #fff3e0; + color: #e6a23c; + font-size: 24rpx; + padding: 16rpx 24rpx; + border-radius: 12rpx; margin-bottom: 20rpx; } -.fail-text { - font-size: 28rpx; - color: #7f8fa6; - margin-bottom: 32rpx; -} - -.retry-btn { - font-size: 28rpx; - color: #fff; - background: linear-gradient(135deg, #5b9bd5, #4a8bc2); - padding: 16rpx 56rpx; - border-radius: 40rpx; +.login-fail-bar .retry-link { + color: #5b9bd5; font-weight: 600; - letter-spacing: 2rpx; - box-shadow: 0 4rpx 16rpx rgba(91, 155, 213, 0.3); -} - -.retry-btn:active { - opacity: 0.85; + flex-shrink: 0; + margin-left: 20rpx; } .header-title { diff --git a/pages/records/records.js b/pages/records/records.js index 2a1289e..3e4f75f 100644 --- a/pages/records/records.js +++ b/pages/records/records.js @@ -13,19 +13,16 @@ Page({ }, onLoad() { - if (app.globalData.isLoggedIn) { - this.onLoginReady() - } else if (app.globalData.loginFailed) { - this.onLoginFailed() + this._awaitLoginAndLoad() + }, + + async _awaitLoginAndLoad() { + const userInfo = await app.waitLogin(true) + if (userInfo) { + this.setData({ isLoggedIn: true, loginFailed: false }) + this.loadRecords() } else { - app.loginReadyCallback = (userInfo) => { - app.loginReadyCallback = null - if (userInfo) { - this.onLoginReady() - } else { - this.onLoginFailed() - } - } + this.setData({ isLoggedIn: false, loginFailed: true, loading: false }) } }, @@ -36,25 +33,15 @@ Page({ } }, - onLoginReady() { - this.setData({ isLoggedIn: true, loginFailed: false }) - this.loadRecords() - }, - - onLoginFailed() { - this.setData({ isLoggedIn: false, loginFailed: true, loading: false }) - }, - - onRetry() { + async onRetry() { this.setData({ loginFailed: false, loading: true }) app.silentLogin() - app.loginReadyCallback = (userInfo) => { - app.loginReadyCallback = null - if (userInfo) { - this.onLoginReady() - } else { - this.onLoginFailed() - } + const userInfo = await app.waitLogin() + if (userInfo) { + this.setData({ isLoggedIn: true, loginFailed: false }) + this.loadRecords() + } else { + this.setData({ loginFailed: true }) } }, @@ -122,5 +109,18 @@ Page({ showQrcode(e) { this.selectComponent('#qrcodeModal').show(e.currentTarget.dataset.id) + }, + + onShareAppMessage(res) { + return { + title: '访客预约', + path: '/pages/index/index' + } + }, + + onShareTimeline() { + return { + title: '访客预约' + } } }) diff --git a/pages/scan/result/index.js b/pages/scan/result/index.js index 03a91f4..8a5d1eb 100644 --- a/pages/scan/result/index.js +++ b/pages/scan/result/index.js @@ -94,5 +94,18 @@ Page({ this.setData({ verifying: false }) wx.showToast({ title: err.message || '核销失败,请稍后重试', icon: 'none' }) } + }, + + onShareAppMessage(res) { + return { + title: '访客预约', + path: '/pages/index/index' + } + }, + + onShareTimeline() { + return { + title: '访客预约' + } } }) \ No newline at end of file diff --git a/utils/config.js b/utils/config.js index 1b231a9..0dd538f 100644 --- a/utils/config.js +++ b/utils/config.js @@ -5,7 +5,7 @@ const ENV_CONFIG = { release: 'https://smartguest.bmser.com:8091', trial: 'https://smartguest.bmser.com:8091', // develop: 'https://192.168.123.76:8080' - develop: 'https://10.50.13.191:8091' + develop: 'https://10.50.13.185:8091' } // 自动判断当前运行环境