index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <view class="page_container">
  3. <custom-nav-bar title="详情" />
  4. <u-loading-page :loading="loading"></u-loading-page>
  5. <moments-item
  6. v-if="!loading"
  7. :moments="moments"
  8. showDetails
  9. @startComent="startComent"
  10. />
  11. <comment-input
  12. v-show="commenting"
  13. ref="commentInputRef"
  14. inDetails
  15. :loading="commentLoading"
  16. :commenting="commenting"
  17. :placeholder="placeholder"
  18. @completeInput="sendComment"
  19. @hiddenInput="commenting = false"
  20. />
  21. </view>
  22. </template>
  23. <script>
  24. import { v4 as uuidv4 } from "uuid";
  25. import { PageEvents } from "../../../constant";
  26. import CustomNavBar from "../../../components/CustomNavBar/index.vue";
  27. import CommentInput from "../index/components/CommentInput.vue";
  28. import MomentsItem from "../index/components/MomentsItem.vue";
  29. import { commentMoments, getMomentsByID } from "../../../api/moments";
  30. export default {
  31. components: {
  32. CustomNavBar,
  33. CommentInput,
  34. MomentsItem,
  35. },
  36. data() {
  37. return {
  38. commenting: false,
  39. moments: {},
  40. commentLoading: false,
  41. commentState: {
  42. workMomentID: "",
  43. comment: {},
  44. },
  45. loading: false,
  46. };
  47. },
  48. computed: {
  49. placeholder() {
  50. if (this.commentState.comment.userID) {
  51. return `回复${this.commentState.comment.nickname}`;
  52. }
  53. return "评论";
  54. },
  55. },
  56. onLoad(options) {
  57. this.fetchMoments(options);
  58. this.$nextTick(() => this.$refs.commentInputRef.setKeyboardListener());
  59. },
  60. onUnload() {
  61. this.$refs.commentInputRef.disposeKeyboardListener();
  62. },
  63. methods: {
  64. fetchMoments(options) {
  65. this.loading = true;
  66. getMomentsByID(options.workMomentID)
  67. .then(({ workMoment }) => {
  68. this.moments = {
  69. ...workMoment,
  70. };
  71. })
  72. .catch(() => uni.$u.toast("获取详情失败!"))
  73. .finally(() => (this.loading = false));
  74. },
  75. startComent(workMomentID, comment = {}) {
  76. this.commentState.workMomentID = workMomentID;
  77. this.commentState.comment = comment;
  78. this.commenting = true;
  79. },
  80. sendComment(content) {
  81. this.commentLoading = true;
  82. commentMoments(
  83. this.commentState.workMomentID,
  84. content,
  85. this.commentState.comment.userID,
  86. )
  87. .then(() => {
  88. const newComment = {
  89. userID: this.$store.getters.storeCurrentUserID,
  90. nickname: this.$store.getters.storeSelfInfo.nickname,
  91. replyUserID: this.commentState.comment.userID,
  92. replyNickname: this.commentState.comment.nickname,
  93. commentID: uuidv4(),
  94. content,
  95. };
  96. this.moments.comments = [...this.moments.comments, newComment];
  97. uni.$emit(
  98. PageEvents.PushNewComment,
  99. newComment,
  100. this.commentState.workMomentID,
  101. );
  102. })
  103. .catch((err) => uni.$u.toast("评论失败!"))
  104. .finally(() => {
  105. this.commentLoading = false;
  106. this.commentState.workMomentID = "";
  107. this.commentState.comment = {};
  108. this.commenting = false;
  109. });
  110. },
  111. },
  112. onBackPress() {
  113. if (this.commenting) {
  114. this.commenting = false;
  115. return true;
  116. }
  117. return false;
  118. },
  119. };
  120. </script>
  121. <style lang="scss" scoped>
  122. .page_container {
  123. .moments_item {
  124. flex: 1;
  125. padding: 0 32rpx;
  126. /deep/.moments_text {
  127. -webkit-line-clamp: unset;
  128. }
  129. }
  130. .input_bar {
  131. display: flex;
  132. padding: 18rpx 24rpx;
  133. background-color: #e9f4ff;
  134. .u-input {
  135. flex: 1;
  136. height: 64rpx;
  137. background-color: #fff;
  138. }
  139. .u-button {
  140. height: 64rpx;
  141. width: 108rpx;
  142. margin-left: 24rpx;
  143. }
  144. }
  145. }
  146. </style>