analysis.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  2. import { upload } from "../../../../lib/request/upload";
  3. // module/chats/pages/analysis/analysis.ts
  4. Component({
  5. behaviors: [PageContainerBehavior],
  6. options: {},
  7. properties: {},
  8. data: {
  9. uploadList: [
  10. { target: 'tongueImgUrl', required: true, label: '舌面图', src: '../../assets/tongue-1.png' },
  11. { target: 'tongueBackImgUrl', required: true, label: '舌下图', src: '../../assets/tongue-2.png' },
  12. { target: 'faceImgUrl', required: false, label: '面部图', src: '../../assets/face-1.png' },
  13. ],
  14. thumbnail: [] as string[],
  15. original: [
  16. // 'http://121.43.162.141:9300/statics/2024/07/13/1_20240713152314A029.JPG',
  17. // 'http://121.43.162.141:9300/statics/2024/07/13/2_20240713152711A030.JPG',
  18. // 'http://121.43.162.141:9300/statics/2024/07/13/3_20240713152819A031.png',
  19. ] as string[],
  20. status: [false, false, false],
  21. _queue: {} as AnyObject,
  22. },
  23. methods: {
  24. handle(event: WechatMiniprogram.TouchEvent) {
  25. const { handle, index } = event.mark as AnyObject;
  26. console.log(handle, index, handle === 'upload:delete', event.mark);
  27. switch (handle) {
  28. case 'preview':
  29. break;
  30. case 'upload':
  31. this._chooseMedia(index).then(src => src && this._uploadMedia(index, src))
  32. break;
  33. case 'upload:delete':
  34. this._deleteMedia(index);
  35. break;
  36. }
  37. },
  38. _chooseMedia(index: number) {
  39. return wx.chooseMedia({ count: 1, mediaType: ['image'], sourceType: ['album', 'camera'], camera: 'front' })
  40. .then(res => {
  41. const src = res.tempFiles[0].tempFilePath;
  42. this.setData({ [`thumbnail.${index}`]: src });
  43. return src;
  44. })
  45. .catch(() => null);
  46. },
  47. _deleteMedia(index: number) {
  48. this.setData({
  49. [`thumbnail.${index}`]: '',
  50. [`original.${index}`]: '',
  51. })
  52. },
  53. _uploadMedia(index: number, src?: string) {
  54. this.setData({ [`_queue.${index}`]: true });
  55. src ??= this.data.thumbnail[index];
  56. upload({
  57. params: { name: 'file', file: src! },
  58. transform({ data }) { return (<any>data).url; }
  59. }).then(src => {
  60. this.setData({ [`original.${index}`]: src })
  61. }, (error) => {
  62. wx.showToast({ title: error?.errMsg ?? '上传失败', icon: 'error' })
  63. this.setData({
  64. [`thumbnail.${index}`]: '',
  65. [`original.${index}`]: '',
  66. })
  67. }).then(() => {
  68. this.setData({ [`_queue.${index}`]: false });
  69. })
  70. },
  71. onSubmit() {
  72. const data = [];
  73. for (let index = 0; index < this.data.uploadList.length; index++) {
  74. const item = this.data.uploadList[index];
  75. if (this.data._queue[index]) {
  76. wx.showToast({ title: `请等待图片上传完毕`, icon: 'none' });
  77. return;
  78. } else if (item.required && !this.data.original[index]) {
  79. wx.showToast({ title: `请上传${item.label}`, icon: 'none' });
  80. return;
  81. }
  82. if (this.data.original[index]) data.push({ target: item.target, src: this.data.original[index], })
  83. }
  84. this.getOpenerEventChannel().emit('update', data)
  85. wx.navigateBack()
  86. }
  87. }
  88. })