home.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. import PageContainerBehavior from "../../core/behavior/page-container.behavior";
  2. import {
  3. DraggableSheetBehavior,
  4. getDraggableSheetContext,
  5. } from "../../core/behavior/draggableSheet.behavior";
  6. import { login } from "../../lib/logic";
  7. import { useRouteQuery } from "../../utils/route-query";
  8. import { appUpdate } from "../../lib/wx/update";
  9. import { addPatientOnlineRecordClockIn } from "./request";
  10. const { shared, Easing, timing } = wx.worklet;
  11. const offset = shared(0);
  12. const menus = ["shareAppMessage", "shareTimeline"];
  13. // pages/home/home.ts
  14. import {
  15. getPatients,
  16. healthReportMethod,
  17. healthIndexMethod,
  18. getSolarTerms,
  19. getShortScienceList,
  20. getPatientDescription,
  21. getNotDealLists,
  22. getCareList,
  23. } from "./request";
  24. import { toCertificationPage, toChats } from "./router";
  25. import { useLocation } from "../../lib/use/use-location";
  26. Page({
  27. data: {
  28. isShowComplete: true,
  29. careList: [],
  30. displayList: [] as {
  31. id: number;
  32. conditioningProgramType: string;
  33. operateBy?: string;
  34. operateTime: string;
  35. frequencyType: string; // 频次类型
  36. frequencyMeasure: string; // 总用量
  37. arrangeDate: string; // 下一次开始日期
  38. finishCount: number; // 完成量
  39. conditioningProgramName: string; // 调养计划名称
  40. conditioningProgramSupplierName: string; // 调养计划供应商名称
  41. expanded: boolean;
  42. }[],
  43. allExpanded: false,
  44. fixedHeight: "370rpx",
  45. tabbarValue: "/pages/home/home",
  46. pageHeight: "100vh", // 默认值
  47. popupList: [] as AnyArray,
  48. isShowPopup: false,
  49. patients: [] as (App.Patient.Model & { isDefault: "Y" | "N" })[],
  50. patient: null as App.Patient.Model | null,
  51. patientDescription: "",
  52. healthId: "",
  53. healthReport: { data: null, message: "", loading: true },
  54. healthIndex: { data: [], message: "" },
  55. position: {} as AnyObject,
  56. location: {} as AnyObject,
  57. solarTerms: {} as AnyObject,
  58. sheet: false,
  59. scienceList: [] as AnyArray,
  60. _loaded: false,
  61. statusList: [] as AnyArray,
  62. switchType: "",
  63. carouselLoading: {} as Record<string | number, boolean>, // 用于跟踪每个轮播图的加载状态
  64. // 轮播媒体列表
  65. carouselMediaList: [] as Array<{
  66. type: "image" | "video";
  67. src: string;
  68. poster?: string;
  69. title?: string;
  70. }>,
  71. },
  72. behaviors: [
  73. PageContainerBehavior,
  74. DraggableSheetBehavior(".draggable-sheet-wrapper"),
  75. ],
  76. // 获取调养计划
  77. async getCareLists() {
  78. // 保存当前的展开状态
  79. const currentExpandedStates = this.data.displayList.map((item: any) => ({
  80. id: item.id,
  81. expanded: item.expanded,
  82. }));
  83. const res = await getCareList();
  84. if (res && res.length > 0) {
  85. res.forEach((item: any, index: number) => {
  86. item.carouselMediaList = [];
  87. // 确保 item.id 存在且为数字类型
  88. if (!item.id && item.id !== 0) {
  89. item.id = index;
  90. }
  91. // 确保 id 是数字类型
  92. item.id = Number(item.id);
  93. // 添加photo
  94. if (item.photo) {
  95. item.carouselMediaList.push({
  96. type: "image",
  97. src: item.photo,
  98. });
  99. }
  100. // 添加itemImgFirst
  101. if (item?.itemImgFirst) {
  102. item.carouselMediaList.push({
  103. type: "image",
  104. src: item.itemImgFirst,
  105. });
  106. }
  107. // 添加itemVideoFirst
  108. if (item?.itemVideoFirst) {
  109. item.carouselMediaList.push({
  110. type: "video",
  111. src: item.itemVideoFirst,
  112. });
  113. }
  114. // 如果有轮播图内容,设置加载状态
  115. if (item.carouselMediaList.length > 0) {
  116. this.setData({
  117. [`carouselLoading.${item.id}`]: true,
  118. });
  119. // 设置超时,防止一直显示加载中
  120. setTimeout(() => {
  121. // 检查是否还在加载状态,如果是则自动隐藏
  122. const currentLoading = this.data.carouselLoading;
  123. if (currentLoading && currentLoading[item.id]) {
  124. this.setData({
  125. [`carouselLoading.${item.id}`]: false,
  126. });
  127. }
  128. }, 10000); // 10秒后自动隐藏加载状态
  129. } else {
  130. // 如果没有轮播图内容,确保不显示加载状态
  131. this.setData({
  132. [`carouselLoading.${item.id}`]: false,
  133. });
  134. }
  135. });
  136. // 先设置 careList
  137. this.setData({
  138. careList: res,
  139. });
  140. await this.updateDisplayList(currentExpandedStates);
  141. setTimeout(() => {
  142. res.forEach((item: any, index: number) => {
  143. if (item.carouselMediaList && item.carouselMediaList.length > 0) {
  144. this.setData({
  145. [`careList[${index}].carouselMediaList`]: [
  146. ...item.carouselMediaList,
  147. ],
  148. });
  149. // 同时更新 displayList
  150. const displayIndex = this.data.displayList.findIndex(
  151. (displayItem: any) => displayItem.id === item.id
  152. );
  153. if (displayIndex !== -1) {
  154. this.setData({
  155. [`displayList[${displayIndex}].carouselMediaList`]: [
  156. ...item.carouselMediaList,
  157. ],
  158. });
  159. }
  160. }
  161. });
  162. }, 100);
  163. }
  164. },
  165. async onLoad(options) {
  166. appUpdate();
  167. const query = useRouteQuery(options.scene!);
  168. if (query.ys) wx.setStorageSync("doctorId", query.ys);
  169. this.initFabAnimated();
  170. if (options.switchType) {
  171. this.setData({
  172. switchType: options.switchType,
  173. });
  174. }
  175. },
  176. async onShow() {
  177. wx.showShareMenu({ withShareTicket: true, menus }).then();
  178. await this.load();
  179. console.log(
  180. this.data.switchType,
  181. "home.ts中的switchType",
  182. this.data.patient
  183. );
  184. // 如果是从一体机扫码进来的 有switchType值就直接跳转到注册页面
  185. if (
  186. this.data.switchType &&
  187. ((this.data.patient as any)?.isPerfectInfo ?? true)
  188. ) {
  189. wx.navigateTo({
  190. url: "/module/user/pages/user-certification/user-certification?type=home",
  191. });
  192. }
  193. // 如果用户没有手机号每次进入页面都提示 点击跳到注册页补充
  194. // todo 要先判断用户有没有手机号 isPerfectInfo是true 就出来弹窗提示用户
  195. // if ((this.data.patient as any)?.isPerfectInfo ?? true) {
  196. // wx.showModal({
  197. // title: "提示",
  198. // content: "手机号为空,请补充",
  199. // success: (res) => {
  200. // if (res.confirm) {
  201. // wx.navigateTo({
  202. // url: "/module/user/pages/user-certification/user-certification?type=home",
  203. // });
  204. // }
  205. // },
  206. // });
  207. // }
  208. },
  209. onHide() {
  210. wx.hideShareMenu({ menus }).then();
  211. offset.value = timing(
  212. 0,
  213. { duration: 100, easing: (<any>Easing).linear },
  214. () => {
  215. "worklet";
  216. }
  217. );
  218. },
  219. onShareAppMessage(_opts): WechatMiniprogram.Page.ICustomShareContent {
  220. return {
  221. title: `健康为基,从容赏生活之美`,
  222. imageUrl: `https://wx.hzliuzhi.com/media/healthManager/wx/share.jpg`,
  223. path: `/pages/home/home`,
  224. };
  225. },
  226. onShareTimeline() {
  227. return {
  228. title: `健康为基,从容赏生活之美`,
  229. };
  230. },
  231. async updateDisplayList(
  232. preserveExpandedStates?: Array<{
  233. id: number;
  234. expanded: boolean;
  235. }>
  236. ) {
  237. const { careList, allExpanded } = this.data;
  238. let newDisplayList: any[] =
  239. allExpanded || careList.length <= 4 ? careList : careList.slice(0, 4);
  240. // 如果有保存的展开状态,则恢复它们
  241. if (preserveExpandedStates && preserveExpandedStates.length > 0) {
  242. newDisplayList = newDisplayList.map((item: any) => {
  243. const savedState = preserveExpandedStates.find(
  244. (state) => state.id === item.id
  245. );
  246. if (savedState) {
  247. return {
  248. ...item,
  249. expanded: savedState.expanded,
  250. };
  251. }
  252. return item;
  253. });
  254. }
  255. this.setData({
  256. displayList: newDisplayList,
  257. });
  258. },
  259. toggleAll() {
  260. this.setData(
  261. { allExpanded: !this.data.allExpanded },
  262. this.updateDisplayList
  263. );
  264. },
  265. toggleItem(e: any) {
  266. const index = e.currentTarget.dataset.index;
  267. const key = `displayList[${index}].expanded`;
  268. this.setData({ [key]: !this.data.displayList[index].expanded });
  269. },
  270. // 核销记录
  271. onRecord(e: any) {
  272. const id = e.currentTarget.dataset.id;
  273. if (id) {
  274. wx.navigateTo({
  275. url: `/module/care/pages/care/verifyRecord?id=${id}`,
  276. });
  277. } else {
  278. wx.showToast({
  279. title: "暂无核销记录",
  280. icon: "none",
  281. });
  282. }
  283. },
  284. onClose() {
  285. this.setData({
  286. isShowPopup: false,
  287. });
  288. },
  289. // 打卡
  290. pushCard(e: any) {
  291. const id = e.currentTarget.dataset.id;
  292. wx.navigateTo({
  293. url: `/module/article/pages/punch-card/punch-card?id=${id}`,
  294. });
  295. },
  296. calculatePageHeight() {
  297. const systemInfo = wx.getSystemInfoSync();
  298. const windowHeight = systemInfo.windowHeight; // 屏幕可用高度
  299. // 获取 tabbar 高度
  300. const query = wx.createSelectorQuery();
  301. query
  302. .select(".t-tabbar")
  303. .boundingClientRect((rect) => {
  304. if (rect) {
  305. const tabbarHeight = rect.height;
  306. const contentHeight = windowHeight - tabbarHeight;
  307. this.setData({
  308. pageHeight: `${contentHeight}px`,
  309. });
  310. }
  311. })
  312. .exec();
  313. },
  314. getNotDealList() {
  315. getNotDealLists().then((res) => {
  316. if (res.length > 0) {
  317. this.setData({
  318. popupList: res,
  319. });
  320. this.setData({
  321. isShowPopup: true,
  322. });
  323. }
  324. });
  325. },
  326. showFollowPopup() {
  327. if (this.data.popupList.length > 0) {
  328. this.setData({
  329. isShowPopup: true,
  330. });
  331. }
  332. },
  333. onVisibleChange(e: { detail: { visible: any } }) {
  334. this.setData({
  335. isShowPopup: e.detail.visible,
  336. });
  337. },
  338. // 诊疗随访提醒
  339. goComplete(e: {
  340. currentTarget: { dataset: { page: string; id: number; title: string } };
  341. }) {
  342. const { title } = e.currentTarget.dataset;
  343. let page = e.currentTarget.dataset.page;
  344. let id = e.currentTarget.dataset.id;
  345. // let page = '/module/chats/pages/index/index';
  346. // let id = 1;
  347. if (page === "/module/chats/pages/index/index") {
  348. if (title === "健康评估") {
  349. wx.setStorageSync("isAnalysis", 4);
  350. toChats("questionnaire", 2);
  351. } else {
  352. wx.setStorageSync("isAnalysis", 2);
  353. wx.setStorageSync("workId", id);
  354. wx.navigateTo({
  355. url: `${page}?component=questionnaire&messageType=1&id=${id}`,
  356. });
  357. }
  358. } else {
  359. wx.navigateTo({ url: `${page}?id=${id}` });
  360. // wx.navigateTo({ url: `/module/follow/pages/evaluation/report?id=${id}` });
  361. }
  362. },
  363. // 随访评估报告已出
  364. goSeeFollowReport() {
  365. wx.navigateTo({ url: "/module/follow/pages/evaluation/report" });
  366. },
  367. async load(forceLogin = false) {
  368. try {
  369. await login(forceLogin);
  370. wx.showLoading({ title: "加载中" });
  371. const { patient } = await getPatients(/*this.data.patientId*/);
  372. // if (!patient) await toCertificationPage();
  373. if (!patient) {
  374. if (wx.getStorageSync("doctorId")) {
  375. toCertificationPage();
  376. }
  377. this.setData({
  378. "healthReport.loading": false,
  379. isShowComplete: true,
  380. });
  381. } else {
  382. this.setData({ patient });
  383. this.observerPatient(patient);
  384. }
  385. wx.hideLoading();
  386. } catch (error: any) {
  387. await wx
  388. .showModal({
  389. title: `加载失败`,
  390. content: `${error?.errMsg ?? error?.message ?? ""}`,
  391. showCancel: false,
  392. confirmText: `重新加载`,
  393. })
  394. .catch(() => {});
  395. await this.load(true);
  396. return;
  397. }
  398. if (!this.data._loaded) {
  399. this.loadScienceList();
  400. useLocation()
  401. .then((location) => {
  402. this.setData({ location });
  403. })
  404. .catch(() => {});
  405. getSolarTerms()
  406. .then((solarTerms) => {
  407. this.setData({ solarTerms });
  408. })
  409. .catch(() => {});
  410. this.setData({ _loaded: true });
  411. }
  412. },
  413. async _getHealthReport() {
  414. wx.showLoading({ title: "加载中" });
  415. this.setData({ "healthReport.loading": true });
  416. try {
  417. const data = await healthReportMethod();
  418. if (!data) {
  419. this.setData({
  420. isShowComplete: true,
  421. });
  422. } else {
  423. this.setData({
  424. isShowComplete: false,
  425. });
  426. }
  427. this.setData({
  428. "healthReport.data": data,
  429. "healthReport.loading": false,
  430. healthId: data?.healthAnalysisReportId,
  431. });
  432. let arr2 = [
  433. [
  434. {
  435. title: "健康状态",
  436. value: data?.willillStateName ? `${data?.willillStateName}` : "",
  437. },
  438. {
  439. title: "程度",
  440. value: data?.willillDegreeName ? `${data?.willillDegreeName}` : "",
  441. },
  442. {
  443. title: "表现",
  444. value: data?.willillFunctionName
  445. ? `${data?.willillFunctionName}`
  446. : "",
  447. },
  448. ],
  449. [
  450. {
  451. title: "中医证素",
  452. value: data?.factorItemSummary ? `${data?.factorItemSummary}` : "",
  453. },
  454. ],
  455. [
  456. {
  457. title: "体质",
  458. value: data?.constitutionGroupName
  459. ? `${data?.constitutionGroupName}`
  460. : "",
  461. },
  462. ],
  463. ];
  464. this.setData({
  465. statusList: arr2,
  466. });
  467. // if (!data?.healthAnalysisReportId) {
  468. // this.setData({
  469. // isShowComplete: true,
  470. // });
  471. // }
  472. } catch (error: any) {
  473. wx.showToast({
  474. title: error.errMsg || "加载失败",
  475. icon: "none",
  476. });
  477. this.setData({
  478. "healthReport.data": [],
  479. "healthReport.loading": false,
  480. "healthReport.message": error.errMsg,
  481. healthId: "",
  482. });
  483. }
  484. wx.hideLoading();
  485. },
  486. async _getAbnormalHealthIndex() {
  487. this.setData({ "healthIndex.loading": true });
  488. try {
  489. const data = await healthIndexMethod();
  490. this.setData({
  491. "healthIndex.data": data
  492. .map((item: AnyObject) => item.abnormalDesc)
  493. .filter(Boolean),
  494. "healthIndex.loading": false,
  495. });
  496. } catch (error: any) {
  497. this.setData({
  498. "healthIndex.data": [],
  499. "healthIndex.loading": false,
  500. "healthIndex.message": error.errMsg,
  501. });
  502. }
  503. },
  504. onBodyModel(event: WechatMiniprogram.TouchEvent) {
  505. if (event.detail?.position === "LB") {
  506. this.toReportPage();
  507. } else if (event.detail?.position === "item0") {
  508. const report = this.data.healthReport.data as unknown as AnyObject;
  509. this.setData({
  510. position: {
  511. LT: [
  512. "willillState",
  513. "willillDegree",
  514. "willillSocial",
  515. "willillFunction",
  516. ]
  517. .map((key) => {
  518. const title = report[`${key}Name`];
  519. const description = report[`${key}Description`];
  520. return title || description ? { title, description } : null;
  521. })
  522. .filter(Boolean),
  523. },
  524. });
  525. this.showDraggableSheet();
  526. } else if (event.detail?.position === "item2") {
  527. const report = this.data.healthReport.data as unknown as AnyObject;
  528. const get = (key: string) => ({ [key]: report[key] });
  529. this.setData({
  530. position: {
  531. RT: {
  532. ...get("constitutionGroupName"),
  533. ...get("constitutionGroupDefinition"),
  534. ...get("faceImg"),
  535. ...get("faceAnalysisResult"),
  536. ...get("tongueAnalysisResult"),
  537. ...get("upImg"),
  538. ...get("downImg"),
  539. },
  540. },
  541. });
  542. this.showDraggableSheet();
  543. } else if (event.detail?.position === "item1") {
  544. const report = this.data.healthReport.data as unknown as AnyObject;
  545. const get = (key: string) => ({ [key]: report[key] });
  546. this.setData({
  547. position: {
  548. RB: {
  549. ...get("diagnoseSyndromeSummary"),
  550. ...get("diagnoseSyndromes"),
  551. ...get("factorItemSummary"),
  552. ...get("factorItems"),
  553. },
  554. },
  555. });
  556. this.showDraggableSheet();
  557. } else if (event.detail?.position === "CT") {
  558. this.setData({
  559. position: {
  560. CT: this.data.healthIndex.data.map(
  561. (item, index) => `${index + 1}、${item}`
  562. ),
  563. },
  564. });
  565. this.showDraggableSheet();
  566. }
  567. },
  568. tabValue() {
  569. this.setData({
  570. position: {
  571. CT: this.data.healthIndex.data.map(
  572. (item, index) => `${index + 1}、${item}`
  573. ),
  574. },
  575. });
  576. this.showDraggableSheet();
  577. },
  578. initFabAnimated() {
  579. (<any>this).applyAnimatedStyle(".fab-wrapper", () => {
  580. "worklet";
  581. return { right: `${Math.min(offset.value, 36) - 36}px` };
  582. });
  583. (<any>this).applyAnimatedStyle(".fab-1", () => {
  584. "worklet";
  585. return { transform: `translateY(${-offset.value}px)` };
  586. });
  587. (<any>this).applyAnimatedStyle(".fab-2", () => {
  588. "worklet";
  589. return {
  590. transform: `translateX(${-offset.value}px) translateY(${
  591. -offset.value / 2
  592. }px)`,
  593. };
  594. });
  595. (<any>this).applyAnimatedStyle(".fab-3", () => {
  596. "worklet";
  597. return {
  598. transform: `translateX(${-offset.value}px) translateY(${
  599. offset.value / 2
  600. }px)`,
  601. };
  602. });
  603. (<any>this).applyAnimatedStyle(".fab-4", () => {
  604. "worklet";
  605. return { transform: `translateY(${offset.value}px)` };
  606. });
  607. },
  608. onFabTap() {
  609. const value = Math.abs(offset.value - 72);
  610. offset.value = timing(
  611. value,
  612. { duration: 500, easing: (<any>Easing).linear },
  613. () => {
  614. "worklet";
  615. if (offset.value > 0) offset.value = 72;
  616. }
  617. );
  618. },
  619. async toChatsPage() {
  620. if (!this.data.patient?.patientId) {
  621. try {
  622. await this.load();
  623. } catch (error: any) {
  624. wx.showModal({
  625. title: "出错了",
  626. content: error?.errMsg ?? error?.message ?? "错误,请重试",
  627. showCancel: false,
  628. });
  629. return;
  630. }
  631. }
  632. const page = "/module/chats/pages/index/index";
  633. wx.navigateTo({
  634. url: `${page}?component=guide&isShowGuide=true`,
  635. });
  636. wx.setStorageSync("isAnalysis", 3);
  637. },
  638. toHealthPage() {
  639. wx.navigateTo({ url: `/module/health/pages/home/home` });
  640. },
  641. toDietTonicPage() {
  642. wx.navigateTo({
  643. url: `/module/article/pages/diet-list/diet-list?classify=tonic`,
  644. });
  645. },
  646. toDietTeaPage() {
  647. wx.navigateTo({
  648. url: `/module/article/pages/diet-list/diet-list?classify=tea`,
  649. });
  650. },
  651. toSciencePage() {
  652. wx.navigateTo({ url: `/module/article/pages/science-list/science-list` });
  653. },
  654. toSchemePage() {
  655. const id = this.data.healthId;
  656. if (id)
  657. wx.navigateTo({ url: `/module/health/pages/scheme/scheme?id=${id}` });
  658. else wx.showToast({ title: "暂无调理方案", icon: "none" });
  659. },
  660. async toReportPage() {
  661. // const { patient } = await getPatients(/*this.data.patientId*/);
  662. if (!this.data.patient) await toCertificationPage();
  663. else {
  664. const id = this.data.healthId;
  665. if (id)
  666. wx.navigateTo({ url: `/module/health/pages/report/report?id=${id}` });
  667. else wx.showToast({ title: "暂无分析报告", icon: "none" });
  668. }
  669. },
  670. onDraggableSizeUpdate(e: any) {
  671. "worklet";
  672. if (e.pixels < 1) {
  673. wx.worklet.runOnJS(this.hideDraggableSheet.bind(this))();
  674. }
  675. },
  676. showDraggableSheet() {
  677. getDraggableSheetContext.call(this).scrollTo({
  678. size: 0.5,
  679. pixels: 600,
  680. animated: true,
  681. duration: 300,
  682. easingFunction: "ease",
  683. });
  684. this.setData({ sheet: true });
  685. },
  686. hideDraggableSheet(event?: any) {
  687. if (event) {
  688. getDraggableSheetContext.call(this).scrollTo({
  689. size: 0,
  690. animated: true,
  691. duration: 300,
  692. easingFunction: "ease",
  693. });
  694. }
  695. this.setData({ position: {}, sheet: false });
  696. },
  697. async loadScienceList() {
  698. try {
  699. const { data } = await getShortScienceList();
  700. this.setData({ scienceList: data });
  701. } catch (error) {}
  702. },
  703. observerPatient(model: { patientId: string; sex: "0" | "1" }) {
  704. wx.setStorageSync("patientId", model.patientId);
  705. this._getHealthReport();
  706. this._getAbnormalHealthIndex();
  707. const patientIcon = { 0: "gender-male", 1: "gender-female" }[model.sex];
  708. const patientIconColor = { 0: "#0f40f5", 1: "#E560B3" }[model.sex];
  709. this.setData({ patientIcon, patientIconColor });
  710. getPatientDescription(model).then((patientDescription) => {
  711. this.setData({ patientDescription });
  712. // wx.setStorageSync("patientPhone", patientDescription.phone);
  713. });
  714. // 获取未处理随访列表
  715. this.getNotDealList();
  716. // 获取调养计划
  717. this.getCareLists();
  718. },
  719. // 轮播图加载完成
  720. onCarouselLoaded(e: any) {
  721. const { itemId } = e.detail;
  722. if (itemId) {
  723. this.setData({
  724. [`carouselLoading.${itemId}`]: false,
  725. });
  726. }
  727. },
  728. // 图片加载失败
  729. onImageError(e: any) {
  730. const { itemId } = e.detail;
  731. // 图片加载失败时也要隐藏加载状态
  732. if (itemId) {
  733. this.setData({
  734. [`carouselLoading.${itemId}`]: false,
  735. });
  736. }
  737. },
  738. // 视频播放错误
  739. onVideoError(e: any) {
  740. const { itemId } = e.detail;
  741. // 视频播放错误时也要隐藏加载状态
  742. if (itemId) {
  743. this.setData({
  744. [`carouselLoading.${itemId}`]: false,
  745. });
  746. }
  747. },
  748. // 没有健康分析去做健康分析
  749. goHealthAnalyze() {
  750. wx.redirectTo({
  751. url: `/module/chats/pages/index/index?component=guide&isShowGuide=true`,
  752. });
  753. wx.setStorageSync("showGuideActive", 1);
  754. wx.setStorageSync("isAnalysis", 3);
  755. },
  756. async isGoPunchcard(e: any) {
  757. const { id, signintime } = e.currentTarget.dataset;
  758. // 已经打卡了
  759. if (signintime) {
  760. // 已打卡 跳转到打卡页面
  761. wx.navigateTo({
  762. url: `/module/article/pages/punch-card/punch-card?id=${id}`,
  763. });
  764. } else {
  765. // todo 打卡
  766. const cardId = e.currentTarget.dataset.id;
  767. await addPatientOnlineRecordClockIn(cardId);
  768. wx.showToast({
  769. title: "打卡成功",
  770. icon: "success",
  771. duration: 1500,
  772. });
  773. // 无感刷新调养计划,保持展开状态
  774. await this.refreshCareListsWithState();
  775. }
  776. },
  777. // 无感刷新调养计划,保持展开状态和媒体加载状态
  778. async refreshCareListsWithState() {
  779. // 保存当前的展开状态
  780. const currentExpandedStates = this.data.displayList.map((item: any) => ({
  781. id: item.id,
  782. expanded: item.expanded,
  783. carouselMediaList: item.carouselMediaList, // 保存媒体列表,避免重新加载
  784. }));
  785. // 获取新数据
  786. const res = await getCareList();
  787. if (res && res.length > 0) {
  788. // 处理新数据,但保持媒体加载状态
  789. res.forEach((item: any) => {
  790. // 查找对应的旧数据
  791. const oldItem = currentExpandedStates.find(
  792. (state) => state.id === item.id
  793. );
  794. // 如果找到旧数据且媒体已加载,保持媒体状态
  795. if (oldItem && oldItem.carouselMediaList) {
  796. // 保持原有的媒体列表,避免重新加载
  797. item.carouselMediaList = oldItem.carouselMediaList;
  798. } else {
  799. // 否则重新构建媒体列表
  800. item.carouselMediaList = [];
  801. // 添加photo
  802. if (item.photo) {
  803. item.carouselMediaList.push({
  804. type: "image",
  805. src: item.photo,
  806. });
  807. }
  808. // 添加itemImgFirst
  809. if (item?.itemImgFirst) {
  810. item.carouselMediaList.push({
  811. type: "image",
  812. src: item.itemImgFirst,
  813. });
  814. }
  815. // 添加itemVideoFirst
  816. if (item?.itemVideoFirst) {
  817. item.carouselMediaList.push({
  818. type: "video",
  819. src: item.itemVideoFirst,
  820. });
  821. }
  822. // 如果有轮播图内容,设置加载状态
  823. if (item.carouselMediaList.length > 0) {
  824. this.setData({
  825. [`carouselLoading.${item.id}`]: true,
  826. });
  827. // 设置超时,防止一直显示加载中
  828. setTimeout(() => {
  829. // 检查是否还在加载状态,如果是则自动隐藏
  830. const currentLoading = this.data.carouselLoading;
  831. if (currentLoading && currentLoading[item.id]) {
  832. this.setData({
  833. [`carouselLoading.${item.id}`]: false,
  834. });
  835. }
  836. }, 10000); // 10秒后自动隐藏加载状态
  837. } else {
  838. // 如果没有轮播图内容,确保不显示加载状态
  839. this.setData({
  840. [`carouselLoading.${item.id}`]: false,
  841. });
  842. }
  843. }
  844. // 确保 id 是数字类型
  845. item.id = Number(item.id);
  846. });
  847. // 更新 careList
  848. this.setData({
  849. careList: res,
  850. });
  851. // 更新 displayList 并恢复展开状态
  852. const { allExpanded } = this.data;
  853. let newDisplayList: any[] =
  854. allExpanded || res.length <= 4 ? res : res.slice(0, 4);
  855. // 恢复展开状态
  856. newDisplayList = newDisplayList.map((item: any) => {
  857. const savedState = currentExpandedStates.find(
  858. (state) => state.id === item.id
  859. );
  860. if (savedState) {
  861. return {
  862. ...item,
  863. expanded: savedState.expanded,
  864. carouselMediaList:
  865. savedState.carouselMediaList || item.carouselMediaList,
  866. };
  867. }
  868. return item;
  869. });
  870. this.setData({
  871. displayList: newDisplayList,
  872. });
  873. }
  874. },
  875. });