analysis.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. _submitting: false,
  72. },
  73. attached() {
  74. // 从存储中拿到之前的对话信息
  75. this.setData({
  76. followObj: wx.getStorageSync("followObj"),
  77. workId: wx.getStorageSync("workId"),
  78. });
  79. },
  80. methods: {
  81. handle(event: WechatMiniprogram.TouchEvent) {
  82. const { handle, index } = event.mark as AnyObject;
  83. console.log(handle, index, handle === "upload:delete", event.mark);
  84. switch (handle) {
  85. case "preview":
  86. break;
  87. case "upload":
  88. this._chooseMedia(index).then(
  89. (src) => src && this._uploadMedia(index, src)
  90. );
  91. break;
  92. case "upload:delete":
  93. this._deleteMedia(index);
  94. break;
  95. }
  96. },
  97. _chooseMedia(index: number) {
  98. return wx
  99. .chooseMedia({
  100. count: 1,
  101. mediaType: ["image"],
  102. sourceType: ["album", "camera"],
  103. camera: "front",
  104. })
  105. .then((res) => {
  106. const src = res.tempFiles[0].tempFilePath;
  107. this.setData({ [`thumbnail.${index}`]: src });
  108. return src;
  109. })
  110. .catch(() => null);
  111. },
  112. _deleteMedia(index: number) {
  113. this.setData({
  114. [`thumbnail.${index}`]: "",
  115. [`original.${index}`]: "",
  116. });
  117. },
  118. _uploadMedia(index: number, src?: string) {
  119. this.setData({ [`_queue.${index}`]: true });
  120. src ??= this.data.thumbnail[index];
  121. upload({
  122. params: { name: "file", file: src! },
  123. transform({ data }) {
  124. return (<any>data).url;
  125. },
  126. })
  127. .then(
  128. (src) => {
  129. this.setData({ [`original.${index}`]: src });
  130. },
  131. (error) => {
  132. wx.showToast({ title: error?.errMsg ?? "上传失败", icon: "error" });
  133. this.setData({
  134. [`thumbnail.${index}`]: "",
  135. [`original.${index}`]: "",
  136. });
  137. }
  138. )
  139. .then(() => {
  140. this.setData({ [`_queue.${index}`]: false });
  141. });
  142. },
  143. async onSubmit() {
  144. const submitBtn = this.selectComponent('#submitBtn');
  145. if (this.data._submitting) return;
  146. this.setData({ _submitting: true });
  147. const data = {
  148. thumbnail: [] as any,
  149. source: [] as any,
  150. };
  151. for (let index = 0; index < this.data.uploadList.length; index++) {
  152. const item = this.data.uploadList[index];
  153. if (this.data._queue[index]) {
  154. console.log('[Analysis] Upload in progress, resetting button');
  155. wx.showToast({ title: `请等待图片上传完毕`, icon: "none" });
  156. submitBtn?.resetState();
  157. this.setData({ _submitting: false });
  158. return;
  159. } else if (item.required && !this.data.original[index]) {
  160. console.log('[Analysis] Missing required image, resetting button');
  161. wx.showToast({ title: `请上传${item.label}`, icon: "none" });
  162. submitBtn?.resetState();
  163. this.setData({ _submitting: false });
  164. return;
  165. }
  166. if (this.data.original[index])
  167. data.source.push({
  168. target: item.target,
  169. src: this.data.original[index],
  170. });
  171. if (this.data.thumbnail[index])
  172. data.thumbnail.push({
  173. target: item.target,
  174. src: this.data.thumbnail[index],
  175. });
  176. }
  177. let imageObj: any = {
  178. upImg: this.data.original[0],
  179. downImg: this.data.original[1],
  180. faceImg: this.data.original[2],
  181. };
  182. // console.log(this.data.followObj, "this.data.followObj");
  183. this.setData({
  184. activeObj: { ...imageObj, ...this.data.followObj },
  185. });
  186. // console.log({ ...this.data.activeObj }, "activeObj");
  187. let isAnalysis: number;
  188. if (this.data.messageType === 2) {
  189. isAnalysis = wx.getStorageSync("isAnalysis");
  190. }
  191. // console.log(this.data, "messageType", isAnalysis);
  192. if (isAnalysis === 2) {
  193. // 提交随访提醒
  194. try {
  195. const res = await Post(
  196. `/followupTaskManage/updateFollowupTaskFillin/${this.data?.workId}`,
  197. { ...this.data.activeObj },
  198. {
  199. transform({ data }: any) {
  200. return data;
  201. },
  202. }
  203. );
  204. // 存储舌面象分析报告id,用于跳转页面时最后产生结果查看
  205. wx.setStorageSync(
  206. "tonguefaceAnalysisReportId",
  207. res.tonguefaceAnalysisReportId
  208. );
  209. this.getOpenerEventChannel().emit("update", data);
  210. wx.navigateBack();
  211. } catch (error) {
  212. console.log('[Analysis] Submit failed, resetting button');
  213. wx.showToast({ title: error?.errMsg ?? "提交失败", icon: "none" });
  214. submitBtn?.resetState();
  215. this.setData({ _submitting: false });
  216. }
  217. } else {
  218. // console.log("对话管家",data)
  219. // 对话管家
  220. this.getOpenerEventChannel().emit("update", data);
  221. wx.navigateBack();
  222. }
  223. },
  224. },
  225. });