guide-analysis.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // module/chats/components/guide-analysis/guide-analysis.ts
  2. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  3. import { upload } from "../../../../lib/request/upload";
  4. Component({
  5. behaviors: [PageContainerBehavior],
  6. properties: {
  7. visible: { type: Boolean, value: false },
  8. },
  9. data: {
  10. uploadList: [
  11. {
  12. target: "tongueImgUrl",
  13. required: true,
  14. label: "舌面图",
  15. src: "../../assets/tongue-1.png",
  16. },
  17. {
  18. target: "tongueBackImgUrl",
  19. required: true,
  20. label: "舌下图",
  21. src: "../../assets/tongue-2.png",
  22. },
  23. {
  24. target: "faceImgUrl",
  25. required: false,
  26. label: "正面面部图",
  27. src: "../../assets/face-1.png",
  28. },
  29. ],
  30. thumbnail: [] as string[],
  31. original: [] as string[],
  32. _queue: {} as AnyObject,
  33. },
  34. methods: {
  35. handle(event: WechatMiniprogram.TouchEvent) {
  36. const { handle, index } = event.mark as AnyObject;
  37. switch (handle) {
  38. case "preview":
  39. break;
  40. case "upload":
  41. this._chooseMedia(index).then((src) => src && this._uploadMedia(index, src));
  42. break;
  43. case "upload:delete":
  44. this._deleteMedia(index);
  45. break;
  46. }
  47. },
  48. _chooseMedia(index: number) {
  49. return wx.chooseMedia({
  50. count: 1,
  51. mediaType: ["image"],
  52. sourceType: ["album", "camera"],
  53. camera: "front",
  54. }).then((res) => {
  55. const src = res.tempFiles[0].tempFilePath;
  56. this.setData({ [`thumbnail.${index}`]: src });
  57. return src;
  58. }).catch(() => null);
  59. },
  60. _deleteMedia(index: number) {
  61. this.setData({
  62. [`thumbnail.${index}`]: "",
  63. [`original.${index}`]: "",
  64. });
  65. },
  66. _uploadMedia(index: number, src?: string) {
  67. this.setData({ [`_queue.${index}`]: true });
  68. src ??= this.data.thumbnail[index];
  69. upload({
  70. params: { name: "file", file: src! },
  71. transform({ data }) {
  72. return (<any>data).url;
  73. },
  74. }).then(
  75. (src) => {
  76. this.setData({ [`original.${index}`]: src });
  77. },
  78. (error) => {
  79. wx.showToast({ title: error?.errMsg ?? "上传失败", icon: "error" });
  80. this.setData({
  81. [`thumbnail.${index}`]: "",
  82. [`original.${index}`]: "",
  83. });
  84. }
  85. ).then(() => {
  86. this.setData({ [`_queue.${index}`]: false });
  87. });
  88. },
  89. onCancel() {
  90. this.triggerEvent('stop');
  91. },
  92. onSubmit() {
  93. const data = {
  94. thumbnail: [] as any,
  95. source: [] as any,
  96. };
  97. for (let index = 0; index < this.data.uploadList.length; index++) {
  98. const item = this.data.uploadList[index];
  99. if (this.data._queue[index]) return wx.showToast({ title: `请等待图片上传完毕`, icon: 'none' });
  100. if (item.required && !this.data.original[index]) return wx.showToast({ title: `请上传${item.label}`, icon: 'none' });
  101. if (this.data.original[index]) data.source.push({
  102. target: item.target,
  103. src: this.data.original[index],
  104. });
  105. if (this.data.thumbnail[index]) data.thumbnail.push({
  106. target: item.target,
  107. src: this.data.thumbnail[index],
  108. });
  109. }
  110. this.triggerEvent('stop', data);
  111. }
  112. },
  113. })