refund-proof-popup.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { upload } from "../../../../lib/request/upload";
  2. Component({
  3. properties: {
  4. visible: { type: Boolean, value: false },
  5. desc: { type: String, value: "" },
  6. images: { type: Array, value: [] },
  7. },
  8. data: {
  9. inputDesc: "",
  10. inputImages: [] as string[],
  11. uploading: false,
  12. },
  13. observers: {
  14. visible(v: boolean) {
  15. if (v) {
  16. this.setData({
  17. inputDesc: this.data.desc || "",
  18. inputImages: [...(this.data.images as string[])],
  19. });
  20. }
  21. },
  22. },
  23. methods: {
  24. onPopupVisibleChange(e: WechatMiniprogram.CustomEvent<{ visible: boolean }>) {
  25. if (!e?.detail?.visible) this.triggerEvent("close");
  26. },
  27. onClose() {
  28. this.triggerEvent("close");
  29. },
  30. onDescInput(e: WechatMiniprogram.Input) {
  31. const value = (e.detail?.value || "").slice(0, 200);
  32. this.setData({ inputDesc: value });
  33. },
  34. onChooseImage() {
  35. const current = this.data.inputImages.length;
  36. const remain = 9 - current;
  37. if (remain <= 0) {
  38. wx.showToast({ title: "最多上传9张", icon: "none" });
  39. return;
  40. }
  41. wx.chooseMedia({
  42. count: remain,
  43. mediaType: ["image"],
  44. sourceType: ["album", "camera"],
  45. camera: "back",
  46. success: (res) => {
  47. const tempPaths = res.tempFiles.map((f) => f.tempFilePath);
  48. this.setData({ uploading: true });
  49. this._uploadSequentially(tempPaths, 0);
  50. },
  51. });
  52. },
  53. _uploadSequentially(paths: string[], index: number) {
  54. if (index >= paths.length) {
  55. this.setData({ uploading: false });
  56. return;
  57. }
  58. upload<string, any>({
  59. params: { name: "file", file: paths[index] },
  60. transform({ data }: any): string {
  61. return (data as any)?.url || (data as any);
  62. },
  63. }).then(
  64. (url) => {
  65. this.setData({
  66. inputImages: [...this.data.inputImages, url],
  67. });
  68. this._uploadSequentially(paths, index + 1);
  69. },
  70. (error: any) => {
  71. wx.showToast({
  72. title: error?.errMsg || "上传失败",
  73. icon: "none",
  74. });
  75. this.setData({ uploading: false });
  76. }
  77. );
  78. },
  79. onRemoveImage(e: WechatMiniprogram.TouchEvent) {
  80. const index = Number((e.currentTarget.dataset as { index?: number })?.index ?? -1);
  81. if (index < 0) return;
  82. const next = [...this.data.inputImages];
  83. next.splice(index, 1);
  84. this.setData({ inputImages: next });
  85. },
  86. onConfirm() {
  87. if (this.data.uploading) return;
  88. this.triggerEvent("confirm", {
  89. desc: this.data.inputDesc,
  90. images: this.data.inputImages,
  91. });
  92. },
  93. },
  94. });