careDetail.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import { Post } from "../../../../lib/request/method";
  2. import { fromHealthReportSymptom } from "../../../../utils/util";
  3. Page({
  4. onLoad(options) {
  5. if (options.id) {
  6. wx.setStorageSync("careId", Number(options.id));
  7. this.getCareDetail(Number(options.id));
  8. }
  9. // 设置页面标题
  10. if (options.name && options.name !== "null") {
  11. this.setData({
  12. name: options.name,
  13. });
  14. }
  15. },
  16. data: {
  17. address: "",
  18. name: "服务包详情",
  19. // patientName: "",
  20. showAddress: false,
  21. careDetail: {},
  22. healthReports: [] as any[],
  23. symptomCellWidth: "300rpx",
  24. statusText: "未知状态",
  25. loading: true,
  26. isShowDelivery: false,
  27. carouselLoading: {} as Record<string | number, boolean>, // 用于跟踪每个轮播图的加载状态
  28. },
  29. // 处理症状数据,按症状名称分组,使相同症状在同一列显示
  30. processSymptomData(healthReports: any[]) {
  31. // 收集所有唯一的症状名称
  32. const symptomSet = new Set<string>();
  33. healthReports.forEach((report) => {
  34. if (report.symptoms && Array.isArray(report.symptoms)) {
  35. report.symptoms.forEach((symptom: any) => {
  36. symptomSet.add(symptom.name);
  37. });
  38. }
  39. });
  40. const allSymptomNames = Array.from(symptomSet);
  41. // 重新组织每个报告的症状数据,确保症状顺序一致
  42. const processedReports = healthReports.map((report) => {
  43. const orderedSymptoms: any[] = [];
  44. // 按照所有症状名称的顺序,为每个报告创建症状数组
  45. allSymptomNames.forEach((symptomName) => {
  46. // 查找当前报告中是否有这个症状
  47. const existingSymptom = report.symptoms?.find(
  48. (s: any) => s.name === symptomName
  49. );
  50. if (existingSymptom) {
  51. // 如果存在,添加症状信息
  52. orderedSymptoms.push({
  53. name: existingSymptom.name,
  54. label: existingSymptom.label ? `(${existingSymptom.label})` : "",
  55. value: existingSymptom.value,
  56. });
  57. } else {
  58. // 如果不存在,添加空占位符
  59. orderedSymptoms.push({
  60. name: "",
  61. label: "",
  62. value: 0,
  63. });
  64. }
  65. });
  66. return {
  67. ...report,
  68. symptoms: orderedSymptoms,
  69. };
  70. });
  71. // 计算每条报告相对于上一条的趋势(上升/下降)
  72. for (let i = 0; i < processedReports.length; i++) {
  73. const previousReport = i > 0 ? processedReports[i - 1] : null;
  74. const currentReport = processedReports[i];
  75. currentReport.symptoms = currentReport.symptoms.map(
  76. (sym: any, idx: number) => {
  77. const previousSymptom = previousReport
  78. ? previousReport.symptoms[idx]
  79. : null;
  80. let trend: "" | "up" | "down" = "";
  81. if (previousReport && sym && sym.name) {
  82. const currentValue = Number(sym.value) || 0;
  83. const previousValue = previousSymptom
  84. ? Number(previousSymptom.value) || 0
  85. : 0;
  86. if (currentValue > previousValue) trend = "up";
  87. else if (currentValue < previousValue) trend = "down";
  88. }
  89. return { ...sym, trend };
  90. }
  91. );
  92. }
  93. return processedReports;
  94. },
  95. onCardRecord(e: any) {
  96. const id = e.currentTarget.dataset.id;
  97. wx.navigateTo({
  98. url: `/module/article/pages/punch-card/punch-card?id=${id}`,
  99. });
  100. },
  101. // 轮播图加载完成
  102. onCarouselLoaded(e: any) {
  103. const { itemId } = e.detail;
  104. if (itemId) {
  105. this.setData({
  106. [`carouselLoading.${itemId}`]: false,
  107. });
  108. }
  109. },
  110. // 图片加载失败
  111. onImageError(e: any) {
  112. const { itemId } = e.detail;
  113. // 图片加载失败时也要隐藏加载状态
  114. if (itemId) {
  115. this.setData({
  116. [`carouselLoading.${itemId}`]: false,
  117. });
  118. }
  119. },
  120. // 视频播放错误
  121. onVideoError(e: any) {
  122. const { itemId } = e.detail;
  123. // 视频播放错误时也要隐藏加载状态
  124. if (itemId) {
  125. this.setData({
  126. [`carouselLoading.${itemId}`]: false,
  127. });
  128. }
  129. },
  130. // 测试图片加载
  131. testImageLoading(mediaList: any[], itemId: string | number) {
  132. mediaList.forEach((media) => {
  133. if (media.type === "image") {
  134. wx.getImageInfo({
  135. src: media.src,
  136. success: () => {
  137. // 图片加载成功,隐藏加载状态
  138. this.setData({
  139. [`carouselLoading.${itemId}`]: false,
  140. });
  141. },
  142. fail: () => {
  143. // 图片加载失败,也要隐藏加载状态
  144. this.setData({
  145. [`carouselLoading.${itemId}`]: false,
  146. });
  147. },
  148. });
  149. } else if (media.type === "video") {
  150. // 视频不进行预加载测试
  151. }
  152. });
  153. },
  154. // 获取调理详情
  155. async getCareDetail(id: number) {
  156. wx.showLoading({
  157. title: "加载中",
  158. mask: true,
  159. });
  160. // 获取调理详情
  161. try {
  162. const res = await Post(
  163. `/patientCrManage/getPcrProcessById/${id}`,
  164. {},
  165. {
  166. transform({ data }: any) {
  167. return data;
  168. },
  169. }
  170. );
  171. if (res) {
  172. wx.hideLoading();
  173. this.setData({
  174. address: `${res?.provinceName ?? ""}${res?.cityName ?? ""}${
  175. res?.areaName ?? ""
  176. }${res?.detailAddress ?? ""}`,
  177. });
  178. if (
  179. !res?.provinceName &&
  180. !res?.cityName &&
  181. !res?.areaName &&
  182. !res?.detailAddress &&
  183. (!res?.liaison || !res.patientName) &&
  184. !res?.phone
  185. ) {
  186. this.setData({
  187. showAddress: false,
  188. });
  189. } else {
  190. this.setData({
  191. showAddress: true,
  192. });
  193. }
  194. // 设置状态文本
  195. const statusMap: Record<string, string> = {
  196. "0": "待付款",
  197. "1": "已作废",
  198. "2": "用户取消",
  199. "3": "未开始",
  200. "4": "调理中",
  201. "5": "已完结",
  202. };
  203. const statusText = statusMap[res.progress] || "未知状态";
  204. let isShowDelivery = false;
  205. if (res && res.items && res.items.length > 0) {
  206. res.items.forEach((item: any) => {
  207. item.carouselMediaList = [];
  208. // 添加photo
  209. if (item.conditioningProgramDetail?.photo) {
  210. item.carouselMediaList.push({
  211. type: "image",
  212. src: item.conditioningProgramDetail?.photo,
  213. });
  214. }
  215. // 添加itemImgFirst
  216. if (item?.conditioningProgramDetail?.itemImgFirst) {
  217. item.carouselMediaList.push({
  218. type: "image",
  219. src: item.conditioningProgramDetail?.itemImgFirst,
  220. });
  221. }
  222. // 添加itemVideoFirst
  223. if (item?.conditioningProgramDetail?.itemVideoFirst) {
  224. item.carouselMediaList.push({
  225. type: "video",
  226. src: item.conditioningProgramDetail?.itemVideoFirst,
  227. });
  228. }
  229. // 如果有轮播图内容,设置加载状态
  230. if (item.carouselMediaList.length > 0) {
  231. this.setData({
  232. [`carouselLoading.${item.id}`]: true,
  233. });
  234. // 测试图片是否可以正常加载
  235. this.testImageLoading(item.carouselMediaList, item.id);
  236. // 设置超时,防止一直显示加载中
  237. setTimeout(() => {
  238. // 检查是否还在加载状态,如果是则自动隐藏
  239. const currentLoading = this.data.carouselLoading;
  240. if (currentLoading && currentLoading[item.id]) {
  241. this.setData({
  242. [`carouselLoading.${item.id}`]: false,
  243. });
  244. }
  245. }, 10000); // 10秒后自动隐藏加载状态
  246. } else {
  247. // 如果没有轮播图内容,确保不显示加载状态
  248. this.setData({
  249. [`carouselLoading.${item.id}`]: false,
  250. });
  251. }
  252. });
  253. // 是否显示配送
  254. isShowDelivery = res.items.some((item: any) => {
  255. return item.conditioningProgramDetail?.isDelivery === "Y";
  256. });
  257. var data = res.items.find(
  258. (item: any) =>
  259. Array.isArray(item.healthAnalysisReports) &&
  260. item.healthAnalysisReports.length > 0
  261. );
  262. if (data) {
  263. const items = data.healthAnalysisReports.map((report: any) => {
  264. return {
  265. ...report,
  266. symptoms: fromHealthReportSymptom(report).items,
  267. };
  268. });
  269. // 处理症状数据,按症状名称分组,使相同症状在同一列显示
  270. const processedReports = this.processSymptomData(items);
  271. this.setData({
  272. healthReports: processedReports,
  273. });
  274. }
  275. }
  276. this.setData({
  277. loading: false,
  278. careDetail: res,
  279. statusText,
  280. isShowDelivery,
  281. });
  282. // 设置页面标题
  283. if (res.conditioningWrapName) {
  284. wx.setNavigationBarTitle({
  285. title: res.conditioningWrapName,
  286. });
  287. }
  288. } else {
  289. wx.hideLoading();
  290. this.setData({
  291. loading: false,
  292. careDetail: {},
  293. });
  294. }
  295. } catch (error: any) {
  296. wx.showToast({
  297. title: error.message,
  298. icon: "none",
  299. });
  300. wx.hideLoading();
  301. this.setData({
  302. isShow: false,
  303. loading: false,
  304. });
  305. }
  306. },
  307. onRecord(e: any) {
  308. const id = e.currentTarget.dataset.id;
  309. // 跳转核销记录
  310. wx.navigateTo({
  311. url: `/module/care/pages/care/verifyRecord?id=${id}`,
  312. success: () => {},
  313. fail: (error) => {
  314. console.error("跳转失败:", error);
  315. },
  316. });
  317. },
  318. onOffline(e: any) {
  319. // 跳转线下操作
  320. },
  321. onReport(e: WechatMiniprogram.TouchEvent) {
  322. wx.navigateTo({
  323. url: "/module/care/pages/reportRecord/reportRecord",
  324. });
  325. },
  326. onSeeReport(e: WechatMiniprogram.TouchEvent) {
  327. const id = e.currentTarget.dataset.id;
  328. if (id) {
  329. wx.navigateTo({ url: `/module/health/pages/report/report?id=${id}` });
  330. } else {
  331. wx.showToast({
  332. title: "暂无数据",
  333. icon: "none",
  334. });
  335. }
  336. },
  337. onPreviewImage(e: WechatMiniprogram.TouchEvent) {
  338. const url = e.currentTarget.dataset.url;
  339. wx.previewImage({
  340. current: url,
  341. urls: [url],
  342. showmenu: true,
  343. });
  344. },
  345. onHide() {
  346. wx.removeStorageSync("careId");
  347. },
  348. });