write-review.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const utils_api = require("../../utils/api.js");
  4. const _sfc_main = {
  5. data() {
  6. return {
  7. bookInfo: {
  8. id: null,
  9. title: ""
  10. },
  11. selectedRating: "recommend",
  12. // recommend, general, bad
  13. ratingOptions: [
  14. { label: "推荐", value: "recommend" },
  15. { label: "一般", value: "general" },
  16. { label: "不好", value: "bad" }
  17. ],
  18. reviewContent: "",
  19. userInfo: null,
  20. isSubmitting: false
  21. };
  22. },
  23. computed: {
  24. canPublish() {
  25. return this.reviewContent.trim().length > 0 && !this.isSubmitting;
  26. }
  27. },
  28. onLoad(options) {
  29. try {
  30. const userInfo = common_vendor.index.getStorageSync("userInfo");
  31. if (userInfo && userInfo.id) {
  32. this.userInfo = userInfo;
  33. }
  34. } catch (e) {
  35. common_vendor.index.__f__("error", "at pages/write-review/write-review.vue:82", "获取用户信息失败", e);
  36. }
  37. if (options.bookId) {
  38. this.bookInfo.id = parseInt(options.bookId);
  39. }
  40. if (options.title) {
  41. this.bookInfo.title = decodeURIComponent(options.title);
  42. }
  43. },
  44. methods: {
  45. selectRating(value) {
  46. this.selectedRating = value;
  47. },
  48. handleInput(e) {
  49. this.reviewContent = e.detail.value;
  50. },
  51. handleCancel() {
  52. common_vendor.index.showModal({
  53. title: "提示",
  54. content: "确定要取消吗?未保存的内容将丢失。",
  55. success: (res) => {
  56. if (res.confirm) {
  57. common_vendor.index.navigateBack();
  58. }
  59. }
  60. });
  61. },
  62. async handlePublish() {
  63. if (!this.canPublish) {
  64. common_vendor.index.showToast({
  65. title: "请输入书评内容",
  66. icon: "none"
  67. });
  68. return;
  69. }
  70. if (!this.userInfo || !this.userInfo.id) {
  71. common_vendor.index.showModal({
  72. title: "提示",
  73. content: "请先登录",
  74. showCancel: true,
  75. cancelText: "取消",
  76. confirmText: "去登录",
  77. success: (res) => {
  78. if (res.confirm) {
  79. common_vendor.index.navigateTo({
  80. url: "/pages/login/login"
  81. });
  82. }
  83. }
  84. });
  85. return;
  86. }
  87. if (!this.bookInfo.id) {
  88. common_vendor.index.showToast({
  89. title: "书籍信息不完整",
  90. icon: "none"
  91. });
  92. return;
  93. }
  94. try {
  95. this.isSubmitting = true;
  96. common_vendor.index.showLoading({
  97. title: "发表中...",
  98. mask: true
  99. });
  100. const res = await utils_api.addComment(
  101. this.userInfo.id,
  102. this.bookInfo.id,
  103. this.reviewContent.trim()
  104. );
  105. common_vendor.index.hideLoading();
  106. this.isSubmitting = false;
  107. if (res && res.code === 200) {
  108. common_vendor.index.showToast({
  109. title: "发表成功",
  110. icon: "success",
  111. duration: 2e3
  112. });
  113. setTimeout(() => {
  114. common_vendor.index.navigateBack();
  115. }, 2e3);
  116. } else {
  117. common_vendor.index.showToast({
  118. title: res.message || "发表失败",
  119. icon: "none"
  120. });
  121. }
  122. } catch (e) {
  123. common_vendor.index.hideLoading();
  124. this.isSubmitting = false;
  125. common_vendor.index.__f__("error", "at pages/write-review/write-review.vue:182", "发表评论失败", e);
  126. common_vendor.index.showToast({
  127. title: e.message || "发表失败,请重试",
  128. icon: "none"
  129. });
  130. }
  131. }
  132. }
  133. };
  134. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  135. return {
  136. a: common_vendor.f($data.ratingOptions, (rating, index, i0) => {
  137. return {
  138. a: common_vendor.t(rating.label),
  139. b: index,
  140. c: $data.selectedRating === rating.value ? 1 : "",
  141. d: common_vendor.o(($event) => $options.selectRating(rating.value), index)
  142. };
  143. }),
  144. b: common_vendor.o([($event) => $data.reviewContent = $event.detail.value, (...args) => $options.handleInput && $options.handleInput(...args)]),
  145. c: $data.reviewContent,
  146. d: common_vendor.t($data.reviewContent.length),
  147. e: common_vendor.o((...args) => $options.handleCancel && $options.handleCancel(...args)),
  148. f: !$options.canPublish ? 1 : "",
  149. g: common_vendor.o((...args) => $options.handlePublish && $options.handlePublish(...args))
  150. };
  151. }
  152. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-d8ce6122"]]);
  153. wx.createPage(MiniProgramPage);
  154. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/write-review/write-review.js.map