瑶都万能墙
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
6.9 KiB

  1. <template>
  2. <view>
  3. <uv-popup ref="popup" :round="30">
  4. <view class="comment-publish">
  5. <!-- 回复目标信息显示 -->
  6. <view class="reply-target" v-if="replyParams && replyParams.replyToName">
  7. <view class="reply-info">
  8. <text class="reply-text">回复 @{{ replyParams.replyToName }}:</text>
  9. <text class="cancel-reply" @click="cancelReply">×</text>
  10. </view>
  11. </view>
  12. <view class="content-input">
  13. <uv-textarea v-model="form.userValue" :maxlength="200" autoHeight count focus
  14. :placeholder="replyParams && replyParams.replyToName ? `回复 @${replyParams.replyToName}` : placeholder"></uv-textarea>
  15. </view>
  16. <view class="images box">
  17. <uv-upload :fileList="fileList" :maxCount="imageMax" multiple width="150rpx" height="150rpx"
  18. @delete="deleteImage" @afterRead="afterRead"
  19. :sizeType="['original', 'compressed']"
  20. :previewFullImage="true"></uv-upload>
  21. </view>
  22. <view class="uni-color-btn" @click="submit">
  23. 发布
  24. </view>
  25. </view>
  26. </uv-popup>
  27. <!-- 邮箱填写弹窗组件 -->
  28. <email-popup ref="emailPopup" @skip="handleEmailSkip" @confirm="handleEmailConfirm"></email-popup>
  29. </view>
  30. </template>
  31. <script>
  32. import { mapState } from 'vuex'
  33. import EmailPopup from './emailPopup.vue'
  34. import { subscribeBeforePublish } from '@/utils/subscribeMessage.js'
  35. export default {
  36. name: 'CommentPublish',
  37. components: {
  38. EmailPopup
  39. },
  40. props: {
  41. // 评论参数
  42. params: {
  43. type: Object,
  44. default: () => ({})
  45. },
  46. // 输入框占位符
  47. placeholder: {
  48. type: String,
  49. default: '说点什么吧...'
  50. }
  51. },
  52. computed: {
  53. ...mapState(['userInfo'])
  54. },
  55. data() {
  56. return {
  57. form: {
  58. userValue: ''
  59. },
  60. imageMax: 9,
  61. fileList: [],
  62. replyParams: null // 回复参数
  63. }
  64. },
  65. methods: {
  66. // 打开弹窗
  67. open() {
  68. this.$refs.popup.open('bottom')
  69. },
  70. // 关闭弹窗
  71. close() {
  72. this.$refs.popup.close()
  73. },
  74. // 检查邮箱并提交
  75. async submit() {
  76. // 检查用户是否填写了邮箱
  77. if (!this.userInfo.mail) {
  78. this.$refs.emailPopup.show()
  79. return
  80. }
  81. this.doSubmit()
  82. },
  83. // 处理邮箱跳过事件
  84. handleEmailSkip() {
  85. this.doSubmit()
  86. },
  87. // 处理邮箱确认事件
  88. handleEmailConfirm(email) {
  89. this.doSubmit()
  90. },
  91. // 执行提交评论
  92. async doSubmit() {
  93. await subscribeBeforePublish()
  94. let data = {
  95. ...this.form,
  96. ...this.params,
  97. }
  98. // 如果是回复评论,添加回复参数
  99. if (this.replyParams) {
  100. data = {
  101. ...data,
  102. pid: this.replyParams.pid, // 添加 pid 参数
  103. replyToId: this.replyParams.replyToId,
  104. replyToName: this.replyParams.replyToName, // 确保 replyToName 传递给后端
  105. replyToAvatar: this.replyParams.replyToAvatar
  106. }
  107. // 在前端也存储 replyToName,确保数据一致性
  108. console.log('回复参数:', {
  109. pid: data.pid,
  110. replyToId: data.replyToId,
  111. replyToName: data.replyToName,
  112. replyToAvatar: data.replyToAvatar
  113. });
  114. }
  115. if (this.$utils.verificationAll(data, {
  116. userValue: '说点什么吧',
  117. type: '缺少type',
  118. orderId: '缺少orderId',
  119. })) {
  120. return
  121. }
  122. data.userImage = this.fileList.map((item) => item.url).join(",")
  123. this.$api('addComment', data, res => {
  124. console.log('addComment API 响应:', res);
  125. if (res.code == 200) {
  126. this.close()
  127. this.resetForm()
  128. uni.showToast({
  129. title: '发布成功!',
  130. icon: 'none'
  131. })
  132. // 传递回复信息给父组件,用于更新子评论列表
  133. const successData = {
  134. isReply: !!data.pid,
  135. parentId: data.pid,
  136. replyData: data
  137. };
  138. console.log('准备触发success事件,数据:', successData);
  139. this.$emit('success', successData)
  140. }
  141. })
  142. },
  143. // 重置表单
  144. resetForm() {
  145. this.form.userValue = ''
  146. this.fileList = []
  147. this.replyParams = null
  148. },
  149. // 设置回复参数
  150. setReplyParams(params) {
  151. this.replyParams = params
  152. },
  153. // 取消回复
  154. cancelReply() {
  155. this.replyParams = null
  156. },
  157. deleteImage(e) {
  158. this.fileList.splice(e.index, 1)
  159. },
  160. afterRead(e) {
  161. let self = this
  162. e.file.forEach(file => {
  163. self.$Oss.ossUpload(file.url).then(url => {
  164. self.fileList.push({
  165. url
  166. })
  167. })
  168. })
  169. },
  170. }
  171. }
  172. </script>
  173. <style scoped lang="scss">
  174. .comment-publish {
  175. .reply-target {
  176. padding: 20rpx;
  177. background-color: #f5f5f5;
  178. border-bottom: 1px solid #e5e5e5;
  179. .reply-info {
  180. display: flex;
  181. justify-content: space-between;
  182. align-items: center;
  183. .reply-text {
  184. color: #666;
  185. font-size: 28rpx;
  186. }
  187. .cancel-reply {
  188. color: #999;
  189. font-size: 36rpx;
  190. font-weight: bold;
  191. padding: 0 10rpx;
  192. }
  193. }
  194. }
  195. .content-input {
  196. min-height: 400rpx;
  197. }
  198. .box {
  199. padding: 0 20rpx;
  200. }
  201. .images {
  202. display: flex;
  203. flex-wrap: wrap;
  204. padding: 20rpx;
  205. }
  206. .uni-color-btn {
  207. background-color: $uni-color-primary;
  208. color: #fff;
  209. text-align: center;
  210. padding: 20rpx;
  211. margin: 20rpx;
  212. border-radius: 10rpx;
  213. font-size: 32rpx;
  214. }
  215. }
  216. </style>