analysis.ts 6.5 KB

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