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: [] as string[],
  16. status: [false, false, false],
  17. _queue: {} as AnyObject,
  18. },
  19. methods: {
  20. handle(event: WechatMiniprogram.TouchEvent) {
  21. const { handle, index } = event.mark as AnyObject;
  22. console.log(handle, index, handle === 'upload:delete', event.mark);
  23. switch (handle) {
  24. case 'preview':
  25. break;
  26. case 'upload':
  27. this._chooseMedia(index).then(src => src && this._uploadMedia(index, src))
  28. break;
  29. case 'upload:delete':
  30. this._deleteMedia(index);
  31. break;
  32. }
  33. },
  34. _chooseMedia(index: number) {
  35. return wx.chooseMedia({ count: 1, mediaType: ['image'], sourceType: ['album', 'camera'], camera: 'front' })
  36. .then(res => {
  37. const src = res.tempFiles[0].tempFilePath;
  38. this.setData({ [`thumbnail.${index}`]: src });
  39. return src;
  40. })
  41. .catch(() => null);
  42. },
  43. _deleteMedia(index: number) {
  44. this.setData({
  45. [`thumbnail.${index}`]: '',
  46. [`original.${index}`]: '',
  47. })
  48. },
  49. _uploadMedia(index: number, src?: string) {
  50. this.setData({ [`_queue.${index}`]: true });
  51. src ??= this.data.thumbnail[index];
  52. upload({
  53. params: { name: 'file', file: src! },
  54. transform({ data }) { return (<any>data).url; }
  55. }).then(src => {
  56. this.setData({ [`original.${index}`]: src })
  57. }, (error) => {
  58. wx.showToast({ title: error?.errMsg ?? '上传失败', icon: 'error' })
  59. this.setData({
  60. [`thumbnail.${index}`]: '',
  61. [`original.${index}`]: '',
  62. })
  63. }).then(() => {
  64. this.setData({ [`_queue.${index}`]: false });
  65. })
  66. },
  67. onSubmit() {
  68. const data = {
  69. thumbnail: [] as any,
  70. source: [] as any,
  71. };
  72. for (let index = 0; index < this.data.uploadList.length; index++) {
  73. const item = this.data.uploadList[index];
  74. if (this.data._queue[index]) {
  75. wx.showToast({ title: `请等待图片上传完毕`, icon: 'none' });
  76. return;
  77. } else if (item.required && !this.data.original[index]) {
  78. wx.showToast({ title: `请上传${item.label}`, icon: 'none' });
  79. return;
  80. }
  81. if (this.data.original[index]) data.source.push({ target: item.target, src: this.data.original[index], })
  82. if (this.data.thumbnail[index]) data.thumbnail.push({ target: item.target, src: this.data.thumbnail[index], })
  83. }
  84. this.getOpenerEventChannel().emit('update', data)
  85. wx.navigateBack()
  86. }
  87. }
  88. })