analysis.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  2. import { upload } from "../../../../lib/request/upload";
  3. import { Post } from "../../../../lib/request/method";
  4. // module/chats/pages/analysis/analysis.ts
  5. interface IAnalysisData {
  6. _lastResetTime: number;
  7. uploadList: Array<{
  8. target: string;
  9. required: boolean;
  10. label: string;
  11. src: string;
  12. }>;
  13. thumbnail: string[];
  14. original: string[];
  15. status: boolean[];
  16. _queue: Record<string, any>;
  17. activeObj: Record<string, any>;
  18. followObj: Record<string, any>;
  19. workId: number;
  20. }
  21. interface IAnalysisProperties {
  22. messageType: number;
  23. }
  24. type IAnalysisInstance = WechatMiniprogram.Component.Instance<
  25. IAnalysisData,
  26. IAnalysisProperties,
  27. {
  28. handle(event: WechatMiniprogram.TouchEvent): void;
  29. _chooseMedia(index: number): Promise<string | null>;
  30. _deleteMedia(index: number): void;
  31. _uploadMedia(index: number, src?: string): void;
  32. onSubmit(): Promise<void>;
  33. }
  34. >;
  35. Component({
  36. behaviors: [PageContainerBehavior],
  37. options: {},
  38. properties: {
  39. messageType: { type: Number, value: 0 },
  40. },
  41. data: {
  42. _lastResetTime: 0,
  43. uploadList: [
  44. {
  45. target: "tongueImgUrl",
  46. required: true,
  47. label: "舌面图",
  48. src: "../../assets/tongue-1.png",
  49. },
  50. {
  51. target: "tongueBackImgUrl",
  52. required: true,
  53. label: "舌下图",
  54. src: "../../assets/tongue-2.png",
  55. },
  56. {
  57. target: "faceImgUrl",
  58. required: false,
  59. label: "正面面部图",
  60. src: "../../assets/face-1.png",
  61. },
  62. ],
  63. thumbnail: [] as string[],
  64. original: [] as string[],
  65. status: [false, false, false],
  66. _queue: {} as AnyObject,
  67. // 随访上传参数
  68. activeObj: {},
  69. followObj: {},
  70. workId: 0,
  71. },
  72. attached() {
  73. // 从存储中拿到之前的对话信息
  74. this.setData({
  75. followObj: wx.getStorageSync("followObj"),
  76. workId: wx.getStorageSync("workId"),
  77. });
  78. // 确保按钮初始状态可用
  79. setTimeout(() => {
  80. console.log('[Analysis] Initializing button state...');
  81. const submitBtn = this.selectComponent('#submitBtn');
  82. console.log('[Analysis] Found submit button:', submitBtn);
  83. if (submitBtn) {
  84. console.log('[Analysis] Resetting button state');
  85. submitBtn.resetState();
  86. }
  87. }, 100);
  88. },
  89. methods: {
  90. handle(event: WechatMiniprogram.TouchEvent) {
  91. const { handle, index } = event.mark as AnyObject;
  92. console.log(handle, index, handle === "upload:delete", event.mark);
  93. switch (handle) {
  94. case "preview":
  95. break;
  96. case "upload":
  97. this._chooseMedia(index).then(
  98. (src) => src && this._uploadMedia(index, src)
  99. );
  100. break;
  101. case "upload:delete":
  102. this._deleteMedia(index);
  103. break;
  104. }
  105. },
  106. _chooseMedia(index: number) {
  107. return wx
  108. .chooseMedia({
  109. count: 1,
  110. mediaType: ["image"],
  111. sourceType: ["album", "camera"],
  112. camera: "front",
  113. })
  114. .then((res) => {
  115. const src = res.tempFiles[0].tempFilePath;
  116. this.setData({ [`thumbnail.${index}`]: src });
  117. return src;
  118. })
  119. .catch(() => null);
  120. },
  121. _deleteMedia(index: number) {
  122. this.setData({
  123. [`thumbnail.${index}`]: "",
  124. [`original.${index}`]: "",
  125. });
  126. },
  127. _uploadMedia(index: number, src?: string) {
  128. this.setData({ [`_queue.${index}`]: true });
  129. src ??= this.data.thumbnail[index];
  130. upload({
  131. params: { name: "file", file: src! },
  132. transform({ data }) {
  133. return (<any>data).url;
  134. },
  135. })
  136. .then(
  137. (src) => {
  138. this.setData({ [`original.${index}`]: src });
  139. },
  140. (error) => {
  141. wx.showToast({ title: error?.errMsg ?? "上传失败", icon: "error" });
  142. this.setData({
  143. [`thumbnail.${index}`]: "",
  144. [`original.${index}`]: "",
  145. });
  146. }
  147. )
  148. .then(() => {
  149. this.setData({ [`_queue.${index}`]: false });
  150. });
  151. },
  152. async onSubmit() {
  153. console.log('[Analysis] Submit started');
  154. const submitBtn = this.selectComponent('#submitBtn');
  155. console.log('[Analysis] Found submit button:', submitBtn);
  156. // Check if enough time has passed since the last reset
  157. const now = Date.now();
  158. if (now - this.data._lastResetTime < 100) {
  159. console.log('[Analysis] Skipping disable - too soon after reset');
  160. return;
  161. }
  162. // 开始提交时禁用按钮
  163. if (submitBtn) {
  164. console.log('[Analysis] Disabling button');
  165. submitBtn.setData({ isDisabled: true });
  166. }
  167. const data = {
  168. thumbnail: [] as any,
  169. source: [] as any,
  170. };
  171. // 验证前重置按钮状态
  172. const resetButton = () => {
  173. console.log('[Analysis] Resetting button state');
  174. const btn = this.selectComponent('#submitBtn');
  175. console.log('[Analysis] Found submit button:', btn);
  176. if (btn) {
  177. this.setData({ _lastResetTime: Date.now() });
  178. console.log('[Analysis] Calling resetState');
  179. btn.resetState();
  180. }
  181. };
  182. for (let index = 0; index < this.data.uploadList.length; index++) {
  183. const item = this.data.uploadList[index];
  184. if (this.data._queue[index]) {
  185. console.log('[Analysis] Upload in progress, resetting button');
  186. wx.showToast({ title: `请等待图片上传完毕`, icon: "none" });
  187. resetButton();
  188. return;
  189. } else if (item.required && !this.data.original[index]) {
  190. console.log('[Analysis] Missing required image, resetting button');
  191. wx.showToast({ title: `请上传${item.label}`, icon: "none" });
  192. resetButton();
  193. return;
  194. }
  195. if (this.data.original[index])
  196. data.source.push({
  197. target: item.target,
  198. src: this.data.original[index],
  199. });
  200. if (this.data.thumbnail[index])
  201. data.thumbnail.push({
  202. target: item.target,
  203. src: this.data.thumbnail[index],
  204. });
  205. }
  206. let imageObj: any = {
  207. upImg: this.data.thumbnail[0],
  208. downImg: this.data.thumbnail[1],
  209. faceImg: this.data.thumbnail[2],
  210. };
  211. this.setData({
  212. activeObj: { ...imageObj, ...this.data.followObj },
  213. });
  214. console.log({ ...this.data.activeObj }, "activeObj");
  215. let isAnalysis: number;
  216. if (this.data.messageType === 2) {
  217. isAnalysis = wx.getStorageSync("isAnalysis");
  218. }
  219. console.log(this.data.messageType, "messageType", isAnalysis);
  220. if (isAnalysis === 2) {
  221. // 提交随访提醒
  222. try {
  223. const res = await Post(
  224. `/followupTaskManage/updateFollowupTaskFillin/${this.data?.workId}`,
  225. { ...this.data.activeObj },
  226. {
  227. transform({ data }: any) {
  228. return data;
  229. },
  230. }
  231. );
  232. // 存储舌面象分析报告id,用于跳转页面时最后产生结果查看
  233. wx.setStorageSync(
  234. "tonguefaceAnalysisReportId",
  235. res.tonguefaceAnalysisReportId
  236. );
  237. this.getOpenerEventChannel().emit("update", data);
  238. wx.navigateBack();
  239. } catch (error) {
  240. console.log('[Analysis] Submit failed, resetting button');
  241. wx.showToast({ title: error?.errMsg ?? "提交失败", icon: "none" });
  242. resetButton();
  243. }
  244. } else {
  245. // 对话管家
  246. this.getOpenerEventChannel().emit("update", data);
  247. wx.navigateBack();
  248. }
  249. },
  250. },
  251. });