From 5fdcf8ea4c347066256bf2e35c692527677a77f4 Mon Sep 17 00:00:00 2001 From: lik Date: Tue, 10 Nov 2026 15:13:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=A6=96=E9=A1=B5=E6=8E=A5=E5=8D=95?= =?UTF-8?q?=E5=BC=80=E5=85=B3=E6=8E=A5=E5=85=A5=E6=9C=8D=E5=8A=A1=E7=AB=AF?= =?UTF-8?q?=E8=90=BD=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/home/index.js | 39 ++++++++++++++++++++++++++++++++------- pages/home/index.wxml | 2 +- utils/api.js | 2 ++ utils/role.js | 7 +++++++ 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/pages/home/index.js b/pages/home/index.js index 7fd46f7..e64e7ab 100644 --- a/pages/home/index.js +++ b/pages/home/index.js @@ -1,5 +1,6 @@ // pages/home/index.js const role = require('../../utils/role.js') +const API = require('../../utils/api.js') // 取消与 status 解耦:展示状态优先取 cancelStatus function resolveDisplayStatus(order) { @@ -38,7 +39,9 @@ Page({ isIntern: false, levelText: '', quickEntries: [], - online: true, + online: false, + showOnlineSwitch: false, + onlineSaving: false, profile: { name: '', avatar: '' @@ -89,13 +92,18 @@ Page({ .then((detail) => { const r = detail.role const attendant = detail.attendant || null + // 接单开关只对正式陪诊师展示:管理员无接单身份,实习陪诊师不可接单 + const isIntern = !!(attendant && attendant.stage === 'intern') + const showOnlineSwitch = r === 'attendant' && !isIntern this.setData({ role: r, isAdmin: r === 'admin', // 实习陪诊师可进工作台查看,但不能接单:首页给一条只读提示 - isIntern: !!(attendant && attendant.stage === 'intern'), + isIntern, levelText: r === 'admin' ? '管理员' : (attendant ? attendant.text : ''), - quickEntries: r === 'admin' ? ADMIN_ENTRIES : (r === 'attendant' ? ATTENDANT_ENTRIES : []) + quickEntries: r === 'admin' ? ADMIN_ENTRIES : (r === 'attendant' ? ATTENDANT_ENTRIES : []), + showOnlineSwitch, + online: showOnlineSwitch && attendant ? attendant.isOnline === true : false }) this.loadUser() this.getRecentOrders() @@ -118,10 +126,27 @@ Page({ .catch(() => {}) }, - toggleOnline() { - this.setData({ - online: !this.data.online - }) + async toggleOnline() { + if (!this.data.showOnlineSwitch || this.data.onlineSaving) return + const next = !this.data.online + // 先乐观更新,请求失败再回滚 + this.setData({ online: next, onlineSaving: true }) + try { + const res = await API.employee.setOnline(next) + if (!res || res.code !== 0) { + throw new Error((res && res.msg) || '切换失败,请重试') + } + const info = res.data && res.data.employee ? res.data.employee.attendantInfo : null + const actual = info && typeof info.isOnline === 'boolean' ? info.isOnline : next + this.setData({ online: actual }) + role.setAttendantOnline(actual) + wx.showToast({ title: actual ? '已开启接单' : '已暂停接单', icon: 'none' }) + } catch (e) { + this.setData({ online: !next }) + wx.showToast({ title: (e && e.message) || '切换失败,请重试', icon: 'none' }) + } finally { + this.setData({ onlineSaving: false }) + } }, async getStats() { diff --git a/pages/home/index.wxml b/pages/home/index.wxml index 2ca880a..0e4a922 100644 --- a/pages/home/index.wxml +++ b/pages/home/index.wxml @@ -18,7 +18,7 @@ {{greeting}},{{todayText}} - + {{online ? '接单中' : '休息中'}} diff --git a/utils/api.js b/utils/api.js index 7685a05..03c0daa 100644 --- a/utils/api.js +++ b/utils/api.js @@ -42,6 +42,8 @@ const API = { // 当前账号的员工档案:登录即可访问,用于判定在职陪诊员身份 employee: { getMe: () => request.get('/health/employee/me'), + // 首页接单开关:仅在职正式陪诊师可切换 + setOnline: (isOnline) => request.patch('/health/employee/me/online', { isOnline }), // 共享订单时的陪诊师候选(在职陪诊师,含实习) getShareableAttendants: () => request.get('/health/employee/shareable-attendants'), }, diff --git a/utils/role.js b/utils/role.js index 8f7b491..696edbb 100644 --- a/utils/role.js +++ b/utils/role.js @@ -39,6 +39,7 @@ function normalizeAttendant(employee) { grade, star, canAcceptOrder: stage === 'certified', + isOnline: info.isOnline === true, text: stage === 'intern' ? '实习陪诊师' : `正式陪诊师 · ${gradeText}` } } @@ -189,6 +190,11 @@ function getAttendantSync() { return cachedAttendant } +// 接单开关切换成功后同步快照,避免 onShow 复用旧值把开关弹回去 +function setAttendantOnline(isOnline) { + if (cachedAttendant) cachedAttendant.isOnline = !!isOnline +} + /** * 退出登录 / 清除缓存后重置,避免沿用上一个账号的角色 */ @@ -242,6 +248,7 @@ module.exports = { ensureRoleSafe, getRoleSync, getAttendantSync, + setAttendantOnline, resetRole, queryOrders, guardAdminPage