diff --git a/JC-Salon-WeChat/app-login-flow.test.js b/JC-Salon-WeChat/app-login-flow.test.js new file mode 100644 index 00000000..7349984d --- /dev/null +++ b/JC-Salon-WeChat/app-login-flow.test.js @@ -0,0 +1,71 @@ +const assert = require('assert') + +const LOGIN_PREFERENCE_KEY = 'jayucloud_salon_login_preference' +const storage = {} +const navigations = [] +let codeSequence = 0 +let appDefinition + +global.wx = { + getAccountInfoSync: () => ({ miniProgram: { envVersion: 'develop' } }), + getDeviceInfo: () => ({ platform: 'devtools' }), + getStorageSync: key => storage[key], + setStorageSync: (key, value) => { storage[key] = value }, + removeStorageSync: key => { delete storage[key] }, + login: ({ success }) => success({ code: `code-${++codeSequence}` }), + reLaunch: ({ url }) => { navigations.push(url) } +} +global.getCurrentPages = () => [{ route: 'pages/launch/index' }] +global.App = definition => { appDefinition = definition } + +require('./app') +const authApi = require('./api/auth') + +function reset(preference) { + Object.keys(storage).forEach(key => delete storage[key]) + if (preference) storage[LOGIN_PREFERENCE_KEY] = preference + navigations.length = 0 + appDefinition.globalData.userInfo = null + appDefinition.globalData.wechatLoginState = null +} + +async function run() { + reset({ rememberMe: true, agreementVersions: { USER_TERMS: '1', PRIVACY_POLICY: '1' } }) + authApi.wechatStatus = async () => ({ status: 'READY' }) + authApi.wechatQuickLogin = async () => ({ status: 'SUCCESS', accessToken: 'token', userInfo: { id: 1 } }) + await appDefinition.runWechatBootstrap() + assert.deepStrictEqual(navigations, ['/pages/planning/index']) + assert.strictEqual(appDefinition.globalData.userInfo.id, 1) + + reset({ rememberMe: false }) + let quickLoginCalled = false + authApi.wechatStatus = async () => ({ status: 'READY' }) + authApi.wechatQuickLogin = async () => { quickLoginCalled = true } + await appDefinition.runWechatBootstrap() + assert.deepStrictEqual(navigations, ['/pages/login/index']) + assert.strictEqual(quickLoginCalled, false) + + reset({ rememberMe: true, agreementVersions: { USER_TERMS: '1', PRIVACY_POLICY: '1' } }) + authApi.wechatStatus = async () => ({ status: 'READY' }) + authApi.wechatQuickLogin = async () => { throw new Error('网络异常') } + await appDefinition.runWechatBootstrap() + assert.deepStrictEqual(navigations, ['/pages/login/index']) + assert.strictEqual(storage[LOGIN_PREFERENCE_KEY].rememberMe, true) + assert.strictEqual(appDefinition.globalData.wechatLoginState.status, 'ERROR') + + reset({ rememberMe: true, agreementVersions: { USER_TERMS: '1', PRIVACY_POLICY: '1' } }) + authApi.wechatStatus = async () => ({ status: 'READY' }) + authApi.wechatQuickLogin = async () => ({ status: 'SELECT_SHOP', ticket: 'ticket', shops: [{ id: 2 }] }) + await appDefinition.runWechatBootstrap() + assert.deepStrictEqual(navigations, ['/pages/login/index']) + assert.strictEqual(appDefinition.globalData.wechatLoginState.status, 'SELECT_SHOP') + + appDefinition.logoutDevice() + assert.strictEqual(storage[LOGIN_PREFERENCE_KEY], undefined) + assert.strictEqual(appDefinition.globalData.userInfo, null) +} + +run().catch(error => { + console.error(error) + process.exitCode = 1 +}) diff --git a/JC-Salon-WeChat/app.js b/JC-Salon-WeChat/app.js index fa70f2c8..8541909a 100644 --- a/JC-Salon-WeChat/app.js +++ b/JC-Salon-WeChat/app.js @@ -1,6 +1,7 @@ const auth = require('./utils/auth') const authApi = require('./api/auth') const { BRAND_CONFIG } = require('./utils/config') +const loginFlow = require('./utils/login-flow') App({ globalData: { @@ -13,7 +14,6 @@ App({ onLaunch() { this.bootstrapSession() - this.bootstrapWechatLogin() }, getWechatCode() { @@ -26,14 +26,24 @@ App({ }, async bootstrapWechatLogin() { + if (this._wechatBootstrapPromise) return this._wechatBootstrapPromise + this._wechatBootstrapPromise = this.runWechatBootstrap() + try { + return await this._wechatBootstrapPromise + } finally { + this._wechatBootstrapPromise = null + } + }, + + async runWechatBootstrap() { try { const status = await authApi.wechatStatus(await this.getWechatCode()) this.publishWechatLoginState(status) const preference = auth.getLoginPreference() - if (!preference.rememberMe || status.status !== 'READY') { + if (!loginFlow.shouldAutoLogin(preference, status)) { auth.clearSession() this.globalData.userInfo = null - this.goLoginIfNeeded() + this.goLogin() return } const result = await authApi.wechatQuickLogin({ @@ -49,7 +59,9 @@ App({ wx.reLaunch({ url: '/pages/planning/index' }) return } - wx.reLaunch({ url: '/pages/login/index' }) + auth.clearSession() + this.globalData.userInfo = null + this.goLogin() } catch (error) { auth.clearSession() this.globalData.userInfo = null @@ -57,11 +69,11 @@ App({ status: 'ERROR', message: error.message || '自动登录失败,请点击重试' }) - this.goLoginIfNeeded() + this.goLogin() } }, - goLoginIfNeeded() { + goLogin() { const pages = getCurrentPages() const current = pages.length ? pages[pages.length - 1].route : '' if (current !== 'pages/login/index') wx.reLaunch({ url: '/pages/login/index' }) diff --git a/JC-Salon-WeChat/app.json b/JC-Salon-WeChat/app.json index 637d57b0..9b532a96 100644 --- a/JC-Salon-WeChat/app.json +++ b/JC-Salon-WeChat/app.json @@ -1,5 +1,6 @@ { "pages": [ + "pages/launch/index", "pages/login/index", "pages/register/index", "pages/planning/index", diff --git a/JC-Salon-WeChat/pages/launch/index.js b/JC-Salon-WeChat/pages/launch/index.js new file mode 100644 index 00000000..afcdb24e --- /dev/null +++ b/JC-Salon-WeChat/pages/launch/index.js @@ -0,0 +1,20 @@ +const { BRAND_CONFIG } = require('../../utils/config') + +Page({ + data: { + appName: BRAND_CONFIG.appName, + message: '正在安全登录…' + }, + + onLoad() { + this.startLogin() + }, + + async startLogin() { + try { + await getApp().bootstrapWechatLogin() + } catch (error) { + this.setData({ message: '登录状态检查失败,正在返回登录页…' }) + } + } +}) diff --git a/JC-Salon-WeChat/pages/launch/index.json b/JC-Salon-WeChat/pages/launch/index.json new file mode 100644 index 00000000..ffad58ed --- /dev/null +++ b/JC-Salon-WeChat/pages/launch/index.json @@ -0,0 +1,4 @@ +{ + "navigationStyle": "custom", + "backgroundColor": "#FFFFFF" +} diff --git a/JC-Salon-WeChat/pages/launch/index.wxml b/JC-Salon-WeChat/pages/launch/index.wxml new file mode 100644 index 00000000..971e3175 --- /dev/null +++ b/JC-Salon-WeChat/pages/launch/index.wxml @@ -0,0 +1,5 @@ + + + {{appName}} + {{message}} + diff --git a/JC-Salon-WeChat/pages/launch/index.wxss b/JC-Salon-WeChat/pages/launch/index.wxss new file mode 100644 index 00000000..1d6d1a7b --- /dev/null +++ b/JC-Salon-WeChat/pages/launch/index.wxss @@ -0,0 +1,6 @@ +.launch-page { display:flex; min-height:100vh; box-sizing:border-box; flex-direction:column; align-items:center; justify-content:center; padding-bottom:100rpx; background:#fff; } +.launch-logo { width:196rpx; height:196rpx; border-radius:46rpx; } +.launch-name { margin-top:26rpx; color:#202522; font-size:40rpx; font-weight:800; letter-spacing:3rpx; } +.launch-status { display:flex; align-items:center; margin-top:42rpx; color:#8a918d; font-size:23rpx; } +.loading-dot { width:14rpx; height:14rpx; margin-right:14rpx; border-radius:50%; background:#2f8f78; animation:pulse 1.2s ease-in-out infinite; } +@keyframes pulse { 0%, 100% { opacity:.25; transform:scale(.75); } 50% { opacity:1; transform:scale(1); } } diff --git a/JC-Salon-WeChat/pages/login/index.js b/JC-Salon-WeChat/pages/login/index.js index 90459fb5..302d4f39 100644 --- a/JC-Salon-WeChat/pages/login/index.js +++ b/JC-Salon-WeChat/pages/login/index.js @@ -1,6 +1,7 @@ const authApi = require('../../api/auth') const auth = require('../../utils/auth') const { BRAND_CONFIG } = require('../../utils/config') +const loginFlow = require('../../utils/login-flow') function wxLoginCode() { return new Promise((resolve, reject) => wx.login({ @@ -44,7 +45,17 @@ Page({ consumeAppState() { const state = getApp().globalData.wechatLoginState - if (state) this.setData({ loginState: state }) + if (!state) return + if (state.status === 'SELECT_SHOP' && state.ticket) { + this.setData({ + loginState: state, + shopOptions: shopOptions(state), + loginTicket: state.ticket, + shopSelectVisible: true + }) + return + } + this.setData({ loginState: state }) }, async loadAgreements() { @@ -105,22 +116,26 @@ Page({ wx.showToast({ title: this.data.agreementError || '协议版本加载中,请稍后重试', icon: 'none' }) return false } - if (!this.data.rememberMe) { - const confirmed = await new Promise(resolve => wx.showModal({ - title: '仅本次登录', - content: '未勾选“记住我”,下次打开小程序时需要重新点击快捷登录。', - confirmText: '继续登录', - success: result => resolve(result.confirm), - fail: () => resolve(false) - })) - if (!confirmed) return false - } return true }, + async chooseRememberMode() { + if (this.data.rememberMe) return true + return new Promise(resolve => wx.showModal({ + title: '记住登录', + content: loginFlow.REMEMBER_LOGIN_PROMPT, + confirmText: '记住', + cancelText: '不记住', + success: result => resolve(loginFlow.resolveRememberChoice(result)), + fail: () => resolve(null) + })) + }, + async quickLogin() { if (this.data.loading || !(await this.ensureCanLogin())) return - this.setData({ loading: true }) + const rememberMe = await this.chooseRememberMode() + if (rememberMe === null) return + this.setData({ rememberMe, loading: true }) try { const result = await authApi.wechatQuickLogin({ wxCode: await wxLoginCode(), ...this.agreementPayload() }) this.setData({ loginState: result }) diff --git a/JC-Salon-WeChat/pages/login/index.wxml b/JC-Salon-WeChat/pages/login/index.wxml index 3bb046c4..e338dc94 100644 --- a/JC-Salon-WeChat/pages/login/index.wxml +++ b/JC-Salon-WeChat/pages/login/index.wxml @@ -4,7 +4,7 @@ {{loginState.status === 'PENDING_REVIEW' ? '申请审核中' : loginState.status === 'REJECTED' ? '申请需要修改' : loginState.status === 'DISABLED' ? '账号暂不可用' : loginState.status === 'ERROR' ? '连接失败' : '账号状态'}}{{loginState.registrationRejectReason || loginState.message}} - {{rememberMe ? '✓' : ''}}记住我,下次自动登录 + {{rememberMe ? '✓' : ''}}记住登录,后续可快速登录 diff --git a/JC-Salon-WeChat/utils/login-flow.js b/JC-Salon-WeChat/utils/login-flow.js new file mode 100644 index 00000000..f4de34a2 --- /dev/null +++ b/JC-Salon-WeChat/utils/login-flow.js @@ -0,0 +1,17 @@ +const REMEMBER_LOGIN_PROMPT = '勾选记住登录之后,后续可快速登录。' + +function shouldAutoLogin(preference, status) { + return Boolean(preference && preference.rememberMe && status && status.status === 'READY') +} + +function resolveRememberChoice(modalResult) { + if (modalResult && modalResult.confirm) return true + if (modalResult && modalResult.cancel) return false + return null +} + +module.exports = { + REMEMBER_LOGIN_PROMPT, + shouldAutoLogin, + resolveRememberChoice +} diff --git a/JC-Salon-WeChat/utils/login-flow.test.js b/JC-Salon-WeChat/utils/login-flow.test.js new file mode 100644 index 00000000..ee5d3ef0 --- /dev/null +++ b/JC-Salon-WeChat/utils/login-flow.test.js @@ -0,0 +1,13 @@ +const assert = require('assert') + +const loginFlow = require('./login-flow') + +assert.strictEqual(loginFlow.REMEMBER_LOGIN_PROMPT, '勾选记住登录之后,后续可快速登录。') +assert.strictEqual(loginFlow.shouldAutoLogin({ rememberMe: true }, { status: 'READY' }), true) +assert.strictEqual(loginFlow.shouldAutoLogin({ rememberMe: false }, { status: 'READY' }), false) +assert.strictEqual(loginFlow.shouldAutoLogin({ rememberMe: true }, { status: 'PENDING_REVIEW' }), false) +assert.strictEqual(loginFlow.shouldAutoLogin({}, null), false) +assert.strictEqual(loginFlow.resolveRememberChoice({ confirm: true, cancel: false }), true) +assert.strictEqual(loginFlow.resolveRememberChoice({ confirm: false, cancel: true }), false) +assert.strictEqual(loginFlow.resolveRememberChoice({ confirm: false, cancel: false }), null) +assert.strictEqual(loginFlow.resolveRememberChoice(null), null) diff --git a/docs/scripts/Salon小程序OpenID快捷登录说明_20261010.md b/docs/scripts/Salon小程序OpenID快捷登录说明_20261010.md index 630a53ed..a8de4ced 100644 --- a/docs/scripts/Salon小程序OpenID快捷登录说明_20261010.md +++ b/docs/scripts/Salon小程序OpenID快捷登录说明_20261010.md @@ -24,9 +24,11 @@ ## 小程序行为 - 每次启动先获取新的微信 code 探测状态;探测 code 不复用于登录。 -- 本机开启“记住我”且状态为 `READY` 时,再获取新 code 自动登录。 +- 小程序以独立启动鉴权页作为首屏,避免已记住用户启动时短暂显示登录页或提前触发规划页业务请求。 +- 本机开启“记住登录”且状态为 `READY` 时,再获取新 code 自动登录并直接进入规划页;自动登录失败则回到登录页,且保留本机记住偏好以便重试。 +- 未勾选“记住登录”时点击快捷登录,会弹出“记住 / 不记住”二选一提示;选择后立即按对应策略登录,不需要再次点击登录按钮。 - 登录后可在“账号设置”使用微信一键绑定或短信验证码绑定手机号。 -- 规划页未绑定手机号横幅可关闭到当天;主动退出会清除会话、记住我、当天横幅关闭状态和资料提示跳过状态。 +- 规划页未绑定手机号横幅可关闭到当天;主动退出会清除会话、记住登录偏好、当天横幅关闭状态和资料提示跳过状态,并回到登录页。 - 昵称和头像缺失时使用 `chooseAvatar` 与 `input type="nickname"` 引导完善,可在本机跳过。 ## 部署前检查 diff --git a/docs/项目变更记录.md b/docs/项目变更记录.md index 78124cca..12b5278f 100644 --- a/docs/项目变更记录.md +++ b/docs/项目变更记录.md @@ -1,5 +1,12 @@ # 项目变更记录 +## 2026-10-10 Salon 小程序记住登录体验优化 + +- 新增独立启动鉴权页;已记住且账号状态正常的用户启动后自动登录并直接进入规划页,不再短暂显示登录页。 +- 登录页文案调整为“记住登录,后续可快速登录”;未勾选时弹窗提供“记住 / 不记住”,选择任一项后立即按对应策略登录。 +- 自动登录失败统一回到登录页且保留记住偏好;主动退出继续清除本机会话和记住偏好并回到登录页。 +- 新增登录决策纯函数测试;GitNexus 当前无可调用服务,本次以静态调用链检查和小程序 Node 测试替代影响分析。 + ## 2026-10-10 Salon 后台注册审核与退出修复 - 将注册审核列表、通过和驳回接口拆分到 `/admin/user/workbench/registration/**`,与 Gateway 后台路径重写保持一致,修复审核页面 404。