feat: 首页接单开关接入服务端落库

This commit is contained in:
lik
2026-11-10 15:13:53 +08:00
parent 5ae8a62475
commit 5fdcf8ea4c
4 changed files with 42 additions and 8 deletions
+32 -7
View File
@@ -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() {
+1 -1
View File
@@ -18,7 +18,7 @@
<text class="greeting">{{greeting}},{{todayText}}</text>
</view>
</view>
<view class="online-switch" bindtap="toggleOnline">
<view class="online-switch" wx:if="{{showOnlineSwitch}}" bindtap="toggleOnline">
<text class="switch-text">{{online ? '接单中' : '休息中'}}</text>
<view class="switch-track {{online ? 'on' : 'off'}}">
<view class="switch-thumb"></view>
+2
View File
@@ -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'),
},
+7
View File
@@ -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