diff --git a/app.js b/app.js index 6c11e23..12a028a 100644 --- a/app.js +++ b/app.js @@ -2,6 +2,44 @@ import config from './config'; import createBus from './utils/eventBus'; const API = require('./utils/api.js'); +const role = require('./utils/role.js'); + +// ==================== 全局页面守卫 ==================== +// 无身份用户(role=none)只能停留在入驻申请页,其余页面在 onShow 时统一重定向。 +// 包装全局 Page 构造器集中处理,避免在 10 个页面各写一遍、漏掉某个入口就绕过去了。 +const ROLE_WHITELIST = ['pages/apply/index']; +let redirecting = false; + +function guardPage(route) { + if (!route || ROLE_WHITELIST.indexOf(route) > -1) return; + + role + .ensureRoleSafe() + .then((detail) => { + // 解析失败(用户服务抖动 / 断网)不跳转: + // 把解析失败当成"无身份"会将在职陪诊师误踢到申请页 + if (!detail.ok || detail.role !== 'none') return; + if (redirecting) return; + + redirecting = true; + wx.reLaunch({ + url: '/pages/apply/index', + complete: () => setTimeout(() => { redirecting = false; }, 800), + }); + }) + .catch(() => {}); +} + +const rawPage = Page; +Page = function (options = {}) { + const rawOnShow = options.onShow; + options.onShow = function (...args) { + const result = rawOnShow && rawOnShow.apply(this, args); + guardPage(this.route); + return result; + }; + return rawPage(options); +}; App({ onLaunch() { diff --git a/app.json b/app.json index c5484a0..5d727a5 100644 --- a/app.json +++ b/app.json @@ -9,7 +9,8 @@ "pages/order/orderEdit", "pages/healthprofile/index", "pages/healthprofile/profileInfo", - "pages/me/index" + "pages/me/index", + "pages/apply/index" ], "usingComponents": { "t-toast": "tdesign-miniprogram/toast/toast" @@ -21,11 +22,11 @@ "navigationBarBackgroundColor": "#ffffff", "navigationBarTitleText": "Weixin", "navigationBarTextStyle": "black", - "backgroundColor": "#0f1535" + "backgroundColor": "#FAF6F1" }, "tabBar": { - "color": "#8a8a8a", - "selectedColor": "#1a8a63", + "color": "#8A8078", + "selectedColor": "#C26A2E", "backgroundColor": "#ffffff", "borderStyle": "black", "list": [ @@ -33,19 +34,19 @@ "pagePath": "pages/home/index", "text": "首页", "iconPath": "images/home.png", - "selectedIconPath": "images/home-green.png" + "selectedIconPath": "images/home-warm.png" }, { "pagePath": "pages/ai/index", "text": "AI", "iconPath": "images/chat.png", - "selectedIconPath": "images/chat-green.png" + "selectedIconPath": "images/chat-warm.png" }, { "pagePath": "pages/set/index", "text": "设置", "iconPath": "images/set.png", - "selectedIconPath": "images/set-green.png" + "selectedIconPath": "images/set-warm.png" } ] }, diff --git a/components/order-stats/index.js b/components/order-stats/index.js index db34e64..25ae76d 100644 --- a/components/order-stats/index.js +++ b/components/order-stats/index.js @@ -1,6 +1,6 @@ // components/order-stats/index.js // 首页订单统计卡片:全部完成 / 本月完成 / 未完成 -const API = require('../../utils/api.js') +const role = require('../../utils/role.js') Component({ data: { @@ -27,7 +27,7 @@ Component({ * 费用取 payment.totalFee 合计 */ load() { - API.escort.getAllRecords({ page: 1, pageSize: 200 }) + role.queryOrders({ page: 1, pageSize: 200 }) .then(res => { if (res.code !== 0) return const records = res.data.records || [] diff --git a/components/patient-info/index.js b/components/patient-info/index.js index 5866aa7..0404963 100644 --- a/components/patient-info/index.js +++ b/components/patient-info/index.js @@ -3,10 +3,12 @@ // 第一行:姓名 + 性别(左) · 年龄(右) // 第二行:电话(左,可选拨打) · 所在地(右) // -// 两种用法: +// 三种用法: // 1. 直接传展示字段(name/sexLabel/ageText/mobile/locationText),适用于列表批量渲染 // 2. 只传 profile-id,组件内部加载档案:优先读全局缓存(utils/store.js),未命中再请求 // 加载完成后触发 loaded 事件(携带档案原始文档),展示字段以加载结果为准 +// 3. 传 snapshot(订单 patient 快照):档案不可读(陪诊员无权限 / 档案已删除)时兜底展示, +// 避免患者姓名电话为空 const store = require('../../utils/store.js') const { calcAge } = require('../../utils/format.js') @@ -19,18 +21,42 @@ Component({ locationText: { type: String, value: '' }, // 是否显示"拨打"并触发 call 事件 dial: { type: Boolean, value: false }, + // 订单患者快照(escort_record.patient):档案加载失败时的兜底 + snapshot: { + type: Object, + value: null, + observer(newVal) { + if (newVal) this.applySnapshot(newVal) + } + }, + // 是否允许读取完整档案:陪诊员传 false,避免必然的 403 与信息越权 + canLoadProfile: { + type: Boolean, + value: true + }, // 健康档案ID:传入后组件内部加载,展示字段以加载结果为准 profileId: { type: String, value: '', observer(newVal) { - if (newVal) this.loadProfile(newVal) + if (!newVal) return + if (this.data.canLoadProfile) this.loadProfile(newVal) + else if (this.data.snapshot) this.applySnapshot(this.data.snapshot) } }, // 是否读取缓存:true 缓存优先(默认),false 直接请求最新数据(请求成功后仍回填缓存) useCache: { type: Boolean, value: true } }, + lifetimes: { + attached() { + if (this.data.snapshot) this.applySnapshot(this.data.snapshot) + if (this.data.profileId && this.data.canLoadProfile) { + this.loadProfile(this.data.profileId) + } + } + }, + methods: { onCallTap() { if (!this.data.mobile) return @@ -38,9 +64,14 @@ Component({ }, /** - * 按档案ID加载:缓存优先,未命中再请求 + * 按档案ID加载:缓存优先,未命中再请求;失败时回落到订单快照 */ loadProfile(id) { + // 陪诊员没有档案读取权限:即使 observer 早于属性赋值触发也直接走快照 + if (!this.data.canLoadProfile) { + this.applySnapshot(this.data.snapshot) + return + } const cached = store.getProfile(id) if (cached) { this.applyProfile(cached) @@ -49,13 +80,17 @@ Component({ const API = require('../../utils/api.js') API.healthProfile.getProfileById(id) .then(res => { - if (res.code !== 0 || !res.data || !res.data.profile) return + if (res.code !== 0 || !res.data || !res.data.profile) { + this.applySnapshot(this.data.snapshot) + return + } const profile = res.data.profile store.cacheProfile(profile) this.applyProfile(profile) }) .catch(err => { console.error('获取健康档案失败', err) + this.applySnapshot(this.data.snapshot) }) }, @@ -75,6 +110,22 @@ Component({ locationText: [loc.province, loc.city].filter(Boolean).join(' ') }) this.triggerEvent('loaded', { profile }) + }, + + /** + * 订单快照兜底:只在档案不可读时使用,因此不触发 loaded 事件 + */ + applySnapshot(snapshot) { + const p = snapshot || {} + if (!p.name && !p.mobile) return + const age = calcAge(p.birth) + const sexLabel = p.sex === 'male' ? '男' : (p.sex === 'female' ? '女' : '') + this.setData({ + name: p.name || '', + sexLabel, + ageText: age ? `${age}岁` : '', + mobile: p.mobile || '' + }) } } }) diff --git a/images/chat-warm.png b/images/chat-warm.png new file mode 100644 index 0000000..8dbbe55 Binary files /dev/null and b/images/chat-warm.png differ diff --git a/images/chat.png b/images/chat.png index 18249fc..5cf68d1 100644 Binary files a/images/chat.png and b/images/chat.png differ diff --git a/images/home-warm.png b/images/home-warm.png new file mode 100644 index 0000000..92fa803 Binary files /dev/null and b/images/home-warm.png differ diff --git a/images/home.png b/images/home.png index 066d20b..03cf903 100644 Binary files a/images/home.png and b/images/home.png differ diff --git a/images/set-warm.png b/images/set-warm.png new file mode 100644 index 0000000..5cdf5b5 Binary files /dev/null and b/images/set-warm.png differ diff --git a/images/set.png b/images/set.png index 7db68b9..f5b8f29 100644 Binary files a/images/set.png and b/images/set.png differ diff --git a/pages/apply/index.js b/pages/apply/index.js new file mode 100644 index 0000000..c84a07a --- /dev/null +++ b/pages/apply/index.js @@ -0,0 +1,303 @@ +// pages/apply/index.js +// 陪诊师入驻申请页 +// +// 无身份用户(role=none)被全局守卫拦住后落到这里;提交后进入待审核视图。 +// 审核通过后本页 onShow 会强刷角色,直接切回首页,不需要用户手动重新登录。 +const API = require('../../utils/api.js') +const role = require('../../utils/role.js') + +const SEX_OPTIONS = [ + { value: 'female', label: '女' }, + { value: 'male', label: '男' } +] +const SEX_LABELS = SEX_OPTIONS.map((item) => item.label) +const MOBILE_REGEX = /^1[3-9]\d{9}$/ +const ID_NUMBER_REGEX = /^\d{17}[\dXx]$/ +// 服务端单张限 5MB,但 wx.request 请求体上限 10MB: +// base64 膨胀 4/3,两张 5MB 图 ≈ 13MB 会在发送前就失败,端上按 3MB/张收紧 +const MAX_IMAGE_BYTES = 3 * 1024 * 1024 + +Page({ + data: { + loading: true, + view: 'form', // form 填写 / pending 待审核 / approved 已通过但角色未刷新 + submitting: false, + rejectReason: '', + application: null, + serviceAreaText: '', + submitTimeText: '', + accountName: '微信用户', + today: '', + sexLabels: SEX_LABELS, + sexIndex: 0, + form: { + name: '', + mobile: '', + idNumber: '', + sex: '', + serviceAreas: '', + introduction: '' + }, + healthExpireDate: '', + photos: { + qualification: '', + healthCertificate: '' + } + }, + + onLoad() { + const now = new Date() + const pad = (n) => String(n).padStart(2, '0') + this.setData({ today: `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}` }) + }, + + onShow() { + this.refresh() + }, + + /** + * 先确认身份,再决定展示哪个视图 + * force=true:审批可能刚刚通过,必须绕过角色缓存 + */ + refresh() { + this.setData({ loading: true }) + role + .ensureRoleSafe(true) + .then((detail) => { + if (detail.ok && detail.role !== 'none') { + wx.switchTab({ url: '/pages/home/index' }) + return + } + this.loadApplication() + }) + .catch(() => this.loadApplication()) + }, + + loadApplication() { + const app = getApp() + const user = app && app.globalData ? app.globalData.user : null + const accountName = user && user.profile && user.profile.name ? user.profile.name : '微信用户' + + API.attendantApplication + .getMine() + .then((res) => { + if (res.code !== 0) { + wx.showToast({ title: res.msg || '加载失败', icon: 'none' }) + this.setData({ loading: false, accountName }) + return + } + + const application = res.data ? res.data.application : null + if (!application) { + this.setData({ loading: false, view: 'form', accountName }) + return + } + + if (application.status === 'pending') { + this.setData({ + loading: false, + view: 'pending', + application, + accountName, + serviceAreaText: (application.applicant.serviceAreas || []).join('、'), + submitTimeText: this.formatTime(application.createtime) + }) + return + } + + if (application.status === 'approved') { + // 走到这里说明员工档案还没查到(接口抖动或数据延迟),给一个手动重试入口 + this.setData({ loading: false, view: 'approved', application, accountName }) + return + } + + // 已驳回:回填上次填写的内容,证件照不必重复上传 + const applicant = application.applicant || {} + const sexIndex = SEX_OPTIONS.findIndex((item) => item.value === applicant.sex) + this.setData({ + loading: false, + view: 'form', + application, + accountName, + rejectReason: application.rejectReason || '', + sexIndex: sexIndex > -1 ? sexIndex : 0, + healthExpireDate: application.healthExpireDate || '', + form: { + name: applicant.name || '', + mobile: applicant.mobile || '', + idNumber: applicant.idNumber || '', + sex: applicant.sex || '', + serviceAreas: (applicant.serviceAreas || []).join(','), + introduction: applicant.introduction || '' + } + }) + }) + .catch(() => { + this.setData({ loading: false, accountName }) + wx.showToast({ title: '网络错误,请重试', icon: 'none' }) + }) + }, + + formatTime(value) { + if (!value) return '' + const date = new Date(value) + if (isNaN(date.getTime())) return '' + const pad = (n) => String(n).padStart(2, '0') + return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}` + }, + + onInput(e) { + const key = e.currentTarget.dataset.key + if (!key) return + this.setData({ [`form.${key}`]: e.detail.value }) + }, + + onSexChange(e) { + const index = Number(e.detail.value) + const option = SEX_OPTIONS[index] + this.setData({ sexIndex: index, 'form.sex': option ? option.value : '' }) + }, + + onExpireChange(e) { + this.setData({ healthExpireDate: e.detail.value }) + }, + + pickImage(e) { + const field = e.currentTarget.dataset.field + if (!field) return + + wx.chooseMedia({ + count: 1, + mediaType: ['image'], + sourceType: ['album', 'camera'], + sizeType: ['compressed'], + success: (res) => { + const file = res.tempFiles && res.tempFiles[0] + if (!file) return + if (file.size && file.size > 10 * 1024 * 1024) { + wx.showToast({ title: '请选择 10MB 以内的图片', icon: 'none' }) + return + } + // 先压再读:原图直传很容易把请求体顶到小程序 10MB 上限 + wx.compressImage({ + src: file.tempFilePath, + quality: 60, + success: (compressed) => this.readAsDataUrl(compressed.tempFilePath, field), + fail: () => this.readAsDataUrl(file.tempFilePath, field) + }) + }, + fail: () => {} + }) + }, + + readAsDataUrl(path, field) { + wx.getFileSystemManager().readFile({ + filePath: path, + encoding: 'base64', + success: (res) => { + const bytes = Math.floor((res.data.length * 3) / 4) + if (bytes > MAX_IMAGE_BYTES) { + wx.showToast({ title: '图片过大,请重新拍摄或裁剪到 3MB 以内', icon: 'none' }) + return + } + const mime = /\.png$/i.test(path) ? 'png' : 'jpeg' + this.setData({ [`photos.${field}`]: `data:image/${mime};base64,${res.data}` }) + }, + fail: () => { + wx.showToast({ title: '读取图片失败', icon: 'none' }) + } + }) + }, + + submit() { + if (this.data.submitting) return + + const { form, photos, healthExpireDate, application } = this.data + const name = form.name.trim() + const mobile = form.mobile.trim() + const idNumber = form.idNumber.trim().toUpperCase() + const areas = form.serviceAreas + .split(/[,,、\s]+/) + .map((item) => item.trim()) + .filter(Boolean) + + if (!name) return this.toast('请填写姓名') + if (!MOBILE_REGEX.test(mobile)) return this.toast('请填写正确的手机号') + if (!ID_NUMBER_REGEX.test(idNumber)) return this.toast('请填写 18 位身份证号') + if (!areas.length) return this.toast('请填写服务城市') + if (!healthExpireDate) return this.toast('请选择健康证明有效期') + + // 驳回后重新提交时允许沿用上次上传的证件照 + const isResubmit = !!application && application.status === 'rejected' + if (!photos.qualification && !isResubmit) return this.toast('请上传资质证书照片') + if (!photos.healthCertificate && !isResubmit) return this.toast('请上传健康证明照片') + + const payload = { + applicant: { + name, + mobile, + idNumber, + sex: form.sex, + serviceAreas: areas, + introduction: form.introduction.trim() + }, + healthExpireDate + } + + const materials = {} + if (photos.qualification) materials.qualification = photos.qualification + if (photos.healthCertificate) materials.healthCertificate = photos.healthCertificate + if (Object.keys(materials).length) payload.materials = materials + + this.setData({ submitting: true }) + API.attendantApplication + .create(payload) + .then((res) => { + if (res.code === 0) { + const next = res.data ? res.data.application : null + wx.showToast({ title: '申请已提交', icon: 'success' }) + this.setData({ + submitting: false, + view: 'pending', + application: next, + rejectReason: '', + serviceAreaText: next ? (next.applicant.serviceAreas || []).join('、') : areas.join('、'), + submitTimeText: next ? this.formatTime(next.createtime) : '' + }) + return + } + + // 409 分两种:已是陪诊师 → 刷新角色直接进首页;身份证号被绑/账号冲突 → 弹窗说明,留在表单 + if (res.code === 409) { + const isAttendant = res.data && res.data.isAttendant + if (isAttendant) { + wx.showToast({ title: res.msg || '你已是陪诊师', icon: 'none' }) + setTimeout(() => this.refresh(), 800) + return + } + wx.showModal({ + title: '无法提交', + content: res.msg || '提交失败,请联系客服', + showCancel: false + }) + this.setData({ submitting: false }) + return + } + + wx.showToast({ title: res.msg || '提交失败', icon: 'none' }) + this.setData({ submitting: false }) + }) + .catch(() => { + wx.showToast({ title: '网络错误,请重试', icon: 'none' }) + this.setData({ submitting: false }) + }) + }, + + toast(title) { + wx.showToast({ title, icon: 'none' }) + }, + + onRetry() { + this.refresh() + } +}) diff --git a/pages/apply/index.json b/pages/apply/index.json new file mode 100644 index 0000000..1d8be39 --- /dev/null +++ b/pages/apply/index.json @@ -0,0 +1,4 @@ +{ + "navigationBarTitleText": "成为陪诊师", + "usingComponents": {} +} diff --git a/pages/apply/index.wxml b/pages/apply/index.wxml new file mode 100644 index 0000000..2d7c8a3 --- /dev/null +++ b/pages/apply/index.wxml @@ -0,0 +1,205 @@ + + + + 加载中… + + + + + + 待审核 + 申请已提交 + 审核通过后重新进入小程序即可接单。期间无需重复提交。 + + + + + 申请人 + {{application.applicant.name}} + + + 手机号 + {{application.applicant.mobile}} + + + 服务城市 + {{serviceAreaText}} + + + 提交时间 + {{submitTimeText}} + + + + 当前微信账号:{{accountName}} + + + + + + 已通过 + 审核已通过 + 权限正在同步,点击下方按钮重新进入。 + + + 当前微信账号:{{accountName}} + + + + + + 成为陪诊师 + 提交资料并通过审核后,即可查看订单、接单并使用 AI 办公。 + + + + 上次申请未通过 + 原因:{{rejectReason}} + 修改后可重新提交,已上传的证件照无需重复上传。 + + + + 基本信息 + + + 姓名 + + + + + 手机号 + + + + + 身份证号 + + + + + 性别 + + + {{form.sex ? sexLabels[sexIndex] : '请选择性别'}} + › + + + + + + 服务城市 + + + + + 个人简介 +