home.ts 25 KB

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