goods-evaluate.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  2. Page({
  3. behaviors: [PageContainerBehavior],
  4. data: {
  5. orderId: "",
  6. product: {} as { name: string; image: string; description?: string },
  7. score: 0,
  8. rateColor: "#F7BA2A",
  9. content: "",
  10. mediaList: [] as { path: string; type: "image" | "video" }[],
  11. canPublish: false,
  12. videoFullscreen: false,
  13. },
  14. onLoad(options: Record<string, string>) {
  15. console.log(options, "options===");
  16. // const orderId = options.orderId || options.id || "";
  17. let product: { name: string; image: string; description?: string } = {
  18. name: "",
  19. image: "",
  20. description: "",
  21. };
  22. if (options.goodsInfo) {
  23. const goods = JSON.parse(decodeURIComponent(options.goodsInfo));
  24. product = {
  25. name: goods.name || "",
  26. image: goods.image || "",
  27. description: goods.description || "",
  28. };
  29. }
  30. this.setData({
  31. // orderId,
  32. product,
  33. canPublish: this._checkCanPublish(0, ""),
  34. });
  35. },
  36. _checkCanPublish(score: number, _content: string): boolean {
  37. return score > 0;
  38. },
  39. onScoreChange(e: WechatMiniprogram.CustomEvent<{ value: number }>) {
  40. const score = e.detail?.value ?? 0;
  41. this.setData({
  42. score,
  43. canPublish: this._checkCanPublish(score, this.data.content),
  44. });
  45. },
  46. onContentInput(e: WechatMiniprogram.Input) {
  47. const content = e.detail?.value ?? "";
  48. this.setData({
  49. content,
  50. canPublish: this._checkCanPublish(this.data.score, content),
  51. });
  52. },
  53. onChooseMedia() {
  54. const current = this.data.mediaList.length;
  55. if (current >= 9) {
  56. wx.showToast({ title: "图片和视频总数不能超过9个", icon: "none" });
  57. return;
  58. }
  59. const remain = 9 - current;
  60. wx.chooseMedia({
  61. count: remain,
  62. mediaType: ["image", "video"],
  63. sourceType: ["album", "camera"],
  64. maxDuration: 30,
  65. camera: "back",
  66. success: (res) => {
  67. const list = res.tempFiles.map((f) => ({
  68. path: f.tempFilePath,
  69. type: (f.fileType === "video" ? "video" : "image") as "image" | "video",
  70. }));
  71. const next = [...this.data.mediaList, ...list].slice(0, 9);
  72. this.setData({ mediaList: next });
  73. },
  74. });
  75. },
  76. onRemoveMedia(e: WechatMiniprogram.TouchEvent) {
  77. const index = e.currentTarget.dataset.index as number;
  78. const mediaList = this.data.mediaList.filter((_, i) => i !== index);
  79. this.setData({ mediaList });
  80. },
  81. onPreviewImage(e: WechatMiniprogram.TouchEvent) {
  82. const url = e.currentTarget.dataset.url as string;
  83. const urls = this.data.mediaList.filter((m) => m.type === "image").map((m) => m.path);
  84. if (url && urls.length) {
  85. wx.previewImage({ current: url, urls });
  86. }
  87. },
  88. onPreviewVideo(e: WechatMiniprogram.TouchEvent) {
  89. const index = e.currentTarget.dataset.index as number;
  90. const videoContext = wx.createVideoContext("goods-video-" + index, this);
  91. videoContext.requestFullScreen({});
  92. },
  93. onVideoFullscreenChange(e: WechatMiniprogram.VideoFullScreenChange) {
  94. const fullScreen = !!(e.detail && e.detail.fullScreen);
  95. this.setData({ videoFullscreen: fullScreen });
  96. },
  97. onRemoveProduct() {
  98. this.setData({
  99. product: { name: "", image: "", description: "" },
  100. });
  101. },
  102. onPublish() {
  103. if (!this.data.canPublish) {
  104. wx.showToast({ title: "请先完成评分", icon: "none" });
  105. return;
  106. }
  107. const { orderId } = this.data;
  108. // TODO: 调用评价提交接口,将 this.data.score、content、mediaList 上传
  109. wx.showLoading({ title: "发布中..." });
  110. setTimeout(() => {
  111. wx.hideLoading();
  112. wx.showToast({ title: "发布成功", icon: "success" });
  113. if (orderId) {
  114. wx.redirectTo({
  115. url: `/module/order/pages/other-detail/other-detail?id=${orderId}`,
  116. });
  117. } else {
  118. wx.navigateBack();
  119. }
  120. }, 500);
  121. },
  122. });