refund-proof-popup.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. if (!this.data.inputDesc.trim() && this.data.inputImages.length === 0) {
  89. wx.showToast({ title: "请填写描述或上传凭证", icon: "none" });
  90. return;
  91. }
  92. this.triggerEvent("confirm", {
  93. desc: this.data.inputDesc,
  94. images: this.data.inputImages,
  95. });
  96. },
  97. },
  98. });