analysis.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. 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. if (this.data._submitting) return;
  145. this.setData({ _submitting: true });
  146. const data = {
  147. thumbnail: [] as any,
  148. source: [] as any,
  149. };
  150. for (let index = 0; index < this.data.uploadList.length; index++) {
  151. const item = this.data.uploadList[index];
  152. if (this.data._queue[index]) {
  153. console.log('[Analysis] Upload in progress, resetting button');
  154. wx.showToast({ title: `请等待图片上传完毕`, icon: "none" });
  155. submitBtn?.resetState();
  156. this.setData({ _submitting: false });
  157. return;
  158. } else if (item.required && !this.data.original[index]) {
  159. console.log('[Analysis] Missing required image, resetting button');
  160. wx.showToast({ title: `请上传${item.label}`, icon: "none" });
  161. submitBtn?.resetState();
  162. this.setData({ _submitting: false });
  163. return;
  164. }
  165. if (this.data.original[index])
  166. data.source.push({
  167. target: item.target,
  168. src: this.data.original[index],
  169. });
  170. if (this.data.thumbnail[index])
  171. data.thumbnail.push({
  172. target: item.target,
  173. src: this.data.thumbnail[index],
  174. });
  175. }
  176. let imageObj: any = {
  177. upImg: this.data.original[0],
  178. downImg: this.data.original[1],
  179. faceImg: this.data.original[2],
  180. };
  181. // console.log(this.data.followObj, "this.data.followObj");
  182. this.setData({
  183. activeObj: { ...imageObj, ...this.data.followObj },
  184. });
  185. // console.log({ ...this.data.activeObj }, "activeObj");
  186. let isAnalysis: number;
  187. if (this.data.messageType === 2) {
  188. isAnalysis = wx.getStorageSync("isAnalysis");
  189. }
  190. // console.log(this.data, "messageType", isAnalysis);
  191. if (isAnalysis === 2) {
  192. // 提交随访提醒
  193. try {
  194. const res = await Post(
  195. `/followupTaskManage/updateFollowupTaskFillin/${this.data?.workId}`,
  196. { ...this.data.activeObj },
  197. {
  198. transform({ data }: any) {
  199. return data;
  200. },
  201. }
  202. );
  203. // 存储舌面象分析报告id,用于跳转页面时最后产生结果查看
  204. wx.setStorageSync(
  205. "tonguefaceAnalysisReportId",
  206. res.tonguefaceAnalysisReportId
  207. );
  208. this.getOpenerEventChannel().emit("update", data);
  209. wx.navigateBack();
  210. } catch (error) {
  211. console.log('[Analysis] Submit failed, resetting button');
  212. wx.showToast({ title: error?.errMsg ?? "提交失败", icon: "none" });
  213. submitBtn?.resetState();
  214. this.setData({ _submitting: false });
  215. }
  216. } else {
  217. // 对话管家
  218. this.getOpenerEventChannel().emit("update", data);
  219. wx.navigateBack();
  220. }
  221. },
  222. },
  223. });