diff --git a/app.js b/app.js index 3bd213a..8b6c74a 100644 --- a/app.js +++ b/app.js @@ -4,6 +4,18 @@ const request = require('./utils/request.js') // 服务列表缓存:只服务首屏秒开,命中后仍会请求最新数据覆盖 const SERVICES_CACHE_KEY = 'servicesCacheV1' +// 医院列表缓存:全量裁剪后落本地,冷启动先用缓存,网络成功后再覆盖 +const HOSPITALS_CACHE_KEY = 'hospitalsCacheV1' +const HOSPITAL_PAGE_SIZE = 200 +const HOSPITAL_MAX_PAGES = 20 + +// 省份归一:兼容医院库「北京」与服务目录「北京市」等不同写法 +function normalizeProvince(value) { + return String(value || '') + .trim() + .replace(/(特别行政区|自治区|省|市)$/, '') + .replace(/(壮族|回族|维吾尔)$/, '') +} App({ onLaunch() { @@ -11,7 +23,9 @@ App({ // 登录逻辑只在 app 里有一份,request 层不碰 request.setUnauthorizedHandler(() => this.silentSignin()) + this.globalData.hospitals = this.getCachedHospitals() this.loadServices() + this.loadHospitals() // 服务协议只有协议页用,改成该页首次进入时再拉(页面里已有 loadAgreement), // 启动期少一次并发请求 @@ -67,6 +81,86 @@ App({ } }, + // 医院列表:启动异步预取,页面下拉直接复用;失败时保留缓存并允许重试 + loadHospitals() { + if (this.globalData.hospitalsReady) return this.globalData.hospitalsReady + this.globalData.hospitalsReady = this.fetchHospitals() + .then((hospitals) => { + this.globalData.hospitals = hospitals + this.cacheHospitals(hospitals) + return hospitals + }) + .catch((err) => { + console.error('获取医院列表失败', err) + this.globalData.hospitalsReady = null + return this.globalData.hospitals + }) + return this.globalData.hospitalsReady + }, + + // 接口单页上限 200:按 total 逐页拉全量,避免下拉只看到第一页 + async fetchHospitals() { + const hospitals = [] + let page = 1 + let total = 0 + + while (page <= HOSPITAL_MAX_PAGES) { + const res = await API.hospital.getHospitals({ page, pageSize: HOSPITAL_PAGE_SIZE }) + if (res.code !== 0) throw new Error(res.msg || '医院列表加载失败') + const list = Array.isArray(res.data?.list) ? res.data.list : [] + total = Number(res.data?.total) || 0 + hospitals.push(...list) + if (list.length === 0 || hospitals.length >= total) break + page += 1 + } + + return hospitals + .map((item) => ({ + _id: item?._id ? String(item._id) : '', + name: String(item?.name || '').trim(), + entityName: String(item?.entityName || '').trim(), + pinyin: String(item?.pinyin || '').trim(), + pinyinFL: String(item?.pinyinFL || '').trim(), + province: String(item?.province || '').trim(), + address: String(item?.address || '').trim(), + })) + .filter((item) => item.name) + }, + + // 缓存只存下拉需要的字段,避免整份医院文档占满本地存储 + cacheHospitals(hospitals) { + try { + wx.setStorageSync(HOSPITALS_CACHE_KEY, hospitals) + } catch (err) { + console.warn('缓存医院列表失败', err) + } + }, + + // 冷启动先用缓存占位,网络成功后覆盖;缓存异常时不阻塞页面 + getCachedHospitals() { + try { + const cached = wx.getStorageSync(HOSPITALS_CACHE_KEY) + if (!Array.isArray(cached)) return [] + return cached.filter((item) => item && item.name) + } catch (err) { + return [] + } + }, + + // 省份是否同口径:医院库可能写「北京」,服务目录写「北京市」 + isSameProvince(a, b) { + const left = normalizeProvince(a) + const right = normalizeProvince(b) + return !!left && left === right + }, + + // 按服务目录里的省份筛医院 + getHospitalsByProvince(province) { + const hospitals = this.globalData.hospitals || [] + if (!province) return hospitals + return hospitals.filter((item) => this.isSameProvince(item.province, province)) + }, + loadAgreement() { if (this.globalData.agreementReady) return this.globalData.agreementReady this.globalData.agreementReady = API.resource.getAgreement() @@ -210,6 +304,8 @@ App({ chatSocket: null, services: [], servicesReady: null, + hospitals: [], + hospitalsReady: null, agreement: null, agreementReady: null, authReady: null, diff --git a/utils/api.js b/utils/api.js index b01a3ab..8569acf 100644 --- a/utils/api.js +++ b/utils/api.js @@ -12,6 +12,11 @@ const API = { update: (data) => requestUser.post('/user/update', data), }, + // 医院基础信息(公开接口):启动预取与下单页医院下拉共用 + hospital: { + getHospitals: (params) => request.get('/health/hospital', params), + }, + escort: { getMyRecords: (params) => request.get('/health/escort-record/my', params), getAttendantRecords: (params) => request.get('/health/escort-record/attendant', params),