index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import Toast from 'tdesign-miniprogram/toast/index';
  2. import { getOrderItem } from '../../../../services/order/orderItem';
  3. import { getSkuDetail } from '../../../../services/sku/sku';
  4. import { getCloudImageTempUrl } from '../../../../utils/cloudImageHandler';
  5. import { createComment } from '../../../../services/comments/comments';
  6. Page({
  7. data: {
  8. sku: null,
  9. },
  10. toast(message) {
  11. Toast({
  12. context: this,
  13. selector: '#t-toast',
  14. message,
  15. });
  16. },
  17. failedAndBack(message, e) {
  18. e && console.error(e);
  19. this.toast(message);
  20. setTimeout(() => {
  21. wx.navigateBack();
  22. }, 1000);
  23. },
  24. async onLoad(options) {
  25. const orderItemId = options.orderItemId;
  26. if (typeof orderItemId !== 'string') return this.failedAndBack('获取订单失败');
  27. const orderItem = await getOrderItem(orderItemId);
  28. const comment = orderItem?.comment;
  29. if (comment != null) return this.failedAndBack('已经评价过了');
  30. let sku = orderItem?.sku;
  31. if (sku == null) return this.failedAndBack('获取商品信息失败');
  32. try {
  33. sku = await getSkuDetail(sku._id);
  34. sku.image = (await getCloudImageTempUrl([sku.image ?? '']))[0];
  35. sku.spec = sku.attr_value.map((x) => x.value).join(',');
  36. this.setData({ sku, orderItemId });
  37. } catch (e) {
  38. return this.failedAndBack('获取商品信息失败', e);
  39. }
  40. },
  41. onRateChange(e) {
  42. const { value } = e?.detail;
  43. const item = e?.currentTarget?.dataset?.item;
  44. this.setData({ [item]: value }, () => {
  45. this.updateButtonStatus();
  46. });
  47. },
  48. onAnonymousChange(e) {
  49. const status = !!e?.detail?.checked;
  50. this.setData({ isAnonymous: status });
  51. },
  52. handleSuccess(e) {
  53. const { files } = e.detail;
  54. this.setData({
  55. uploadFiles: files,
  56. });
  57. },
  58. handleRemove(e) {
  59. const { index } = e.detail;
  60. const { uploadFiles } = this.data;
  61. uploadFiles.splice(index, 1);
  62. this.setData({
  63. uploadFiles,
  64. });
  65. },
  66. onTextAreaChange(e) {
  67. const value = e?.detail?.value;
  68. this.textAreaValue = value;
  69. this.updateButtonStatus();
  70. },
  71. updateButtonStatus() {
  72. const { goodRateValue, isAllowedSubmit } = this.data;
  73. const { textAreaValue } = this;
  74. const temp = Boolean(goodRateValue) && Boolean(textAreaValue);
  75. if (temp !== isAllowedSubmit) this.setData({ isAllowedSubmit: temp });
  76. },
  77. async onSubmitBtnClick() {
  78. const { goodRateValue, isAllowedSubmit, orderItemId, sku } = this.data;
  79. const { textAreaValue } = this;
  80. if (!isAllowedSubmit) return;
  81. try {
  82. await createComment({ orderItemId, content: textAreaValue, rating: goodRateValue, spuId: sku.spu._id });
  83. Toast({
  84. context: this,
  85. selector: '#t-toast',
  86. message: '评价提交成功',
  87. icon: 'check-circle',
  88. });
  89. setTimeout(() => {
  90. wx.navigateBack();
  91. }, 1000);
  92. } catch (e) {
  93. return this.failedAndBack('创建评论失败', e);
  94. }
  95. },
  96. });