analysis.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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: false, 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 => 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(({ errMsg }) => {
  46. const message = (<AnyObject>{ 'chooseMedia:fail cancel': '取消上传' })[errMsg] ?? errMsg;
  47. wx.showToast({ title: message, icon: 'none' });
  48. throw { errMsg };
  49. })
  50. },
  51. _deleteMedia(index: number) {
  52. this.setData({
  53. [`thumbnail.${index}`]: '',
  54. [`original.${index}`]: '',
  55. })
  56. },
  57. _uploadMedia(index: number, src?: string) {
  58. src ??= this.data.thumbnail[index];
  59. upload({
  60. params: { name: 'file', file: src! },
  61. transform({ data }) { return (<any>data).url; }
  62. }).then(src => {
  63. this.setData({ [`original.${index}`]: src })
  64. }, () => {
  65. wx.showToast({ title: '上传失败', icon: 'error' })
  66. this.setData({
  67. [`thumbnail.${index}`]: '',
  68. [`original.${index}`]: '',
  69. })
  70. })
  71. },
  72. onSubmit() {
  73. const data = [];
  74. for (let index = 0; index < this.data.uploadList.length; index++) {
  75. const item = this.data.uploadList[index];
  76. if (item.required) {
  77. if (this.data._queue[index]) {
  78. wx.showToast({ title: `请等待${item.label}上传完成`, icon: 'loading' });
  79. return;
  80. } else if (!this.data.original[index]) {
  81. wx.showToast({ title: `请上传${item.label}`, icon: 'none' });
  82. return;
  83. }
  84. }
  85. if (this.data.original[index]) data.push({ target: item.target, src: this.data.original[index], })
  86. }
  87. this.getOpenerEventChannel().emit('update', data)
  88. wx.navigateBack()
  89. }
  90. }
  91. })