comment-thread.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <template>
  2. <view class="ct-root">
  3. <!-- 评论列表 -->
  4. <scroll-view
  5. v-if="rows.length > 0"
  6. class="ct-list"
  7. :style="{ height: listHeight + 'px' }"
  8. scroll-y
  9. >
  10. <view
  11. v-for="(item, index) in rows"
  12. :key="(item && item.id) || index"
  13. class="ct-row"
  14. >
  15. <image
  16. class="ct-avatar"
  17. :src="avatarSrc(item && item.user && item.user.avatar)"
  18. @click.stop="onUser(item)"
  19. />
  20. <view class="ct-body">
  21. <!-- 父评论:昵称 + 点赞 -->
  22. <view class="ct-head">
  23. <view class="ct-head-left">
  24. <view v-if="index === 0" class="ct-rank ct-rank-1">
  25. <text class="ct-rank-text">第一</text>
  26. </view>
  27. <view v-else-if="index === 1" class="ct-rank ct-rank-2">
  28. <text class="ct-rank-text">第二</text>
  29. </view>
  30. <view v-else-if="index === 2" class="ct-rank ct-rank-3">
  31. <text class="ct-rank-text">第三</text>
  32. </view>
  33. <text class="ct-nick">{{ (item.user && item.user.nickname) || '游客' }}</text>
  34. </view>
  35. <view class="ct-like" @click.stop="onLike(index)">
  36. <text :class="item.hasLike ? 'ct-like-num ct-like-num-on' : 'ct-like-num'">{{ item.like || 0 }}</text>
  37. <text :class="item.hasLike ? 'ct-like-icon ct-like-icon-on' : 'ct-like-icon'">{{ item.hasLike ? '♥' : '♡' }}</text>
  38. </view>
  39. </view>
  40. <!-- 父评论:内容 -->
  41. <text class="ct-content">{{ plain(item.content) }}</text>
  42. <!-- 父评论:时间 + 回复 + 删除 -->
  43. <view class="ct-meta">
  44. <text class="ct-time">{{ item.createtime_text || '' }}</text>
  45. <view
  46. class="ct-action"
  47. @click.stop="onReply(item.user && item.user.nickname, item.user && item.user.nickname, item.id)"
  48. >
  49. <text class="ct-action-reply">回复</text>
  50. </view>
  51. <view v-if="item.owner" class="ct-action" @click.stop="onDelete(index)">
  52. <text class="ct-action-del">删除</text>
  53. </view>
  54. </view>
  55. <!-- 子评论列表 -->
  56. <view v-if="item.childlist && item.childlist.length > 0" class="ct-sublist">
  57. <view
  58. v-for="(child, k) in item.childlist"
  59. :key="(child && child.id) || k"
  60. class="ct-row ct-row-sub"
  61. >
  62. <image
  63. class="ct-avatar ct-avatar-sub"
  64. :src="avatarSrc(child && child.user && child.user.avatar)"
  65. @click.stop="onUser(child)"
  66. />
  67. <view class="ct-body">
  68. <view class="ct-head">
  69. <view class="ct-head-left">
  70. <text class="ct-nick ct-nick-sub">{{ (child.user && child.user.nickname) || '游客' }}</text>
  71. </view>
  72. <view class="ct-like" @click.stop="onLike(index, k)">
  73. <text :class="child.hasLike ? 'ct-like-num ct-like-num-on' : 'ct-like-num'">{{ child.like || 0 }}</text>
  74. <text :class="child.hasLike ? 'ct-like-icon ct-like-icon-on' : 'ct-like-icon'">{{ child.hasLike ? '♥' : '♡' }}</text>
  75. </view>
  76. </view>
  77. <text class="ct-content ct-content-sub">{{ plain(child.content) }}</text>
  78. <view class="ct-meta">
  79. <text class="ct-time">{{ child.createtime_text || '' }}</text>
  80. <view
  81. class="ct-action"
  82. @click.stop="onReply(item.user && item.user.nickname, child.user && child.user.nickname, item.id)"
  83. >
  84. <text class="ct-action-reply">回复</text>
  85. </view>
  86. <view v-if="child.owner" class="ct-action" @click.stop="onDelete(index, k)">
  87. <text class="ct-action-del">删除</text>
  88. </view>
  89. </view>
  90. </view>
  91. </view>
  92. </view>
  93. </view>
  94. </view>
  95. </scroll-view>
  96. <!-- 空状态 -->
  97. <view v-else class="ct-empty" :style="{ height: listHeight + 'px' }">
  98. <text class="ct-empty-title">暂无评论,</text>
  99. <text class="ct-empty-cta" @click.stop="focusInput">来写第一条 →</text>
  100. </view>
  101. <!-- 底部输入栏:fixed + 键盘跟随(照搬 HKLive comment-nvue.vue) -->
  102. <view class="ct-inputbar" :style="inputBarStyle" @click.stop>
  103. <input
  104. ref="commentInput"
  105. class="ct-input"
  106. v-model="form.content"
  107. :placeholder="placeholder"
  108. placeholder-style="color: #999;"
  109. :maxlength="200"
  110. adjust-position="false"
  111. :cursor-spacing="0"
  112. confirm-type="send"
  113. @blur="onInputBlur"
  114. @confirm="onSubmit"
  115. />
  116. <view class="ct-send" @click.stop="onSubmit">
  117. <text class="ct-send-text">发送</text>
  118. </view>
  119. </view>
  120. <view :style="{ height: spacerHeight + 'px' }" />
  121. </view>
  122. </template>
  123. <script>
  124. /**
  125. * 视频评论弹层主体
  126. *
  127. * 移植自参考工程 HKLive\components\comment\comment-nvue.vue,做了精简:
  128. * - 移除 i18n(直接中文文案)
  129. * - 移除举报跳转(NewHKLive 暂无 complaint 路由)
  130. * - 移除 $appKit / $wanlshop 等私有插件依赖
  131. * - 头像加载用 NewHKLive 的 buildOssUrl
  132. * - 接口走 features/feed/feed-gateway.js 的封装
  133. *
  134. * 接口:
  135. * - 列表已由父组件传入 props.list(父组件用 fetchClipComments 拉好)
  136. * - 点赞 / 回复 / 删除 / 新增 自行调用 gateway
  137. *
  138. * Events:
  139. * - complete { type: 'add' | 'del', count: number }
  140. * 父组件用来同步外层 likes/comments 计数
  141. */
  142. import { stripHtml } from '@/features/feed/clip-normalize'
  143. import { buildOssUrl } from '@/core/media/oss-url'
  144. import {
  145. addClipComment,
  146. deleteClipComment,
  147. fetchClipComments,
  148. toggleClipCommentLike
  149. } from '@/features/feed/feed-gateway'
  150. import { openUserProfile } from '@/core/nav/navigation'
  151. export default {
  152. name: 'CommentThread',
  153. props: {
  154. findId: { type: [Number, String], default: 0 },
  155. list: { type: Array, default: () => [] },
  156. count: { type: Number, default: 0 },
  157. listHeight: { type: Number, default: 400 },
  158. safeBottomInset: { type: Number, default: 0 },
  159. tabbarHeight: { type: Number, default: 0 }
  160. },
  161. data() {
  162. return {
  163. rows: [],
  164. form: { pid: null, content: '' },
  165. keyboardHeight: 0,
  166. safeBottom: 0,
  167. inputRowPx: 0,
  168. inputInnerPx: 0,
  169. inputBarPadV: 0,
  170. inputBottomFineTune: 0,
  171. _kbHandler: null,
  172. _kbClearTimer: null
  173. }
  174. },
  175. computed: {
  176. signedIn() {
  177. return !!this.$store.state.user.isLogin
  178. },
  179. placeholder() {
  180. return this.form.pid ? '回复评论…' : '说点什么~'
  181. },
  182. /** 照搬 HKLive comment-nvue.vue#inputBarStyle,键盘弹起时再下移 inputBottomFineTune */
  183. inputBarStyle() {
  184. const tabH = this.tabbarHeight || 0
  185. const safeB = this.safeBottom || 0
  186. const kb = this.keyboardHeight > 0 ? this.keyboardHeight : 0
  187. const fine = this.inputBottomFineTune || 0
  188. const inner = this.inputInnerPx || uni.upx2px(70)
  189. const padV = this.inputBarPadV || uni.upx2px(16)
  190. const padH = uni.upx2px(24)
  191. const barH = inner + padV * 2
  192. if (kb > 0) {
  193. const lift = Math.max(0, kb - tabH - fine)
  194. return {
  195. height: `${barH}px`,
  196. paddingTop: `${padV}px`,
  197. paddingBottom: `${padV}px`,
  198. paddingLeft: `${padH}px`,
  199. paddingRight: `${padH}px`,
  200. bottom: `${lift}px`
  201. }
  202. }
  203. return {
  204. height: `${barH + safeB}px`,
  205. paddingTop: `${padV}px`,
  206. paddingBottom: `${safeB + padV}px`,
  207. paddingLeft: `${padH}px`,
  208. paddingRight: `${padH}px`,
  209. bottom: `${tabH}px`
  210. }
  211. },
  212. spacerHeight() {
  213. if (this.keyboardHeight > 0) return 0
  214. const inner = this.inputInnerPx || uni.upx2px(70)
  215. const padV = this.inputBarPadV || uni.upx2px(16)
  216. return inner + padV * 2 + (this.safeBottom || 0) + (this.tabbarHeight || 0) + 8
  217. }
  218. },
  219. watch: {
  220. list: {
  221. immediate: true,
  222. handler(next) {
  223. this.rows = Array.isArray(next) ? next.slice() : []
  224. }
  225. }
  226. },
  227. mounted() {
  228. const sys = uni.getSystemInfoSync() || {}
  229. const sysSafe = (sys.safeAreaInsets && sys.safeAreaInsets.bottom) || 0
  230. this.safeBottom = (this.safeBottomInset && this.safeBottomInset > 0) ? this.safeBottomInset : sysSafe
  231. this.inputInnerPx = uni.upx2px(70)
  232. this.inputBarPadV = uni.upx2px(16)
  233. this.inputRowPx = this.inputInnerPx + this.inputBarPadV * 2
  234. this.inputBottomFineTune = uni.upx2px(60)
  235. if (uni.onKeyboardHeightChange) {
  236. this._kbHandler = (res) => {
  237. this.keyboardHeight = (res && res.height) || 0
  238. }
  239. uni.onKeyboardHeightChange(this._kbHandler)
  240. }
  241. },
  242. beforeDestroy() {
  243. if (this._kbClearTimer) clearTimeout(this._kbClearTimer)
  244. if (this._kbHandler && uni.offKeyboardHeightChange) {
  245. uni.offKeyboardHeightChange(this._kbHandler)
  246. this._kbHandler = null
  247. }
  248. },
  249. methods: {
  250. plain(content) {
  251. return stripHtml(content || '')
  252. },
  253. avatarSrc(path) {
  254. return buildOssUrl(path, 80, 80, 2, 'avatar')
  255. },
  256. requireSignIn() {
  257. if (this.signedIn) return true
  258. uni.showModal({
  259. title: '请先登录',
  260. content: '登录后才能评论、点赞',
  261. confirmText: '去登录',
  262. cancelText: '取消',
  263. success: ({ confirm }) => {
  264. if (confirm) uni.navigateTo({ url: '/pages/passport/lobby' })
  265. }
  266. })
  267. return false
  268. },
  269. onUser(row) {
  270. openUserProfile(row)
  271. },
  272. /**
  273. * 评论 / 子评论点赞 toggle
  274. * @param {number} index 父评论索引
  275. * @param {number|null} childKey 子评论索引;null 表示对父评论操作
  276. */
  277. async onLike(index, childKey = null) {
  278. if (!this.requireSignIn()) return
  279. const parent = this.rows[index]
  280. if (!parent) return
  281. const target = childKey == null ? parent : (parent.childlist && parent.childlist[childKey])
  282. if (!target || !target.id) return
  283. if (target._busy) return
  284. target._busy = true
  285. const was = !!target.hasLike
  286. const next = !was
  287. this.$set(target, 'hasLike', next)
  288. this.$set(target, 'like', Math.max(0, Number(target.like || 0) + (next ? 1 : -1)))
  289. try {
  290. const flag = await toggleClipCommentLike(target.id)
  291. if (flag === 1 || flag === 0) {
  292. const serverOn = flag === 1
  293. if (serverOn !== next) {
  294. this.$set(target, 'hasLike', serverOn)
  295. this.$set(target, 'like', Math.max(0, Number(target.like || 0) + (serverOn ? 0 : -2) /* 抵消乐观更新 */))
  296. }
  297. }
  298. } catch (err) {
  299. console.warn('[comment] like failed, rolling back', err)
  300. this.$set(target, 'hasLike', was)
  301. this.$set(target, 'like', Math.max(0, Number(target.like || 0) + (was ? 1 : -1)))
  302. uni.showToast({ title: (err && err.message) || '点赞失败', icon: 'none' })
  303. } finally {
  304. target._busy = false
  305. }
  306. },
  307. /**
  308. * 点 "回复" 时:把输入框预填 "@昵称 "、设置 pid 为父评论 id、聚焦
  309. */
  310. onReply(_pUserNick, reUserNick, pid) {
  311. if (!this.requireSignIn()) return
  312. this.form.pid = pid
  313. this.form.content = reUserNick ? `@${reUserNick} ` : ''
  314. this.focusInput()
  315. },
  316. focusInput() {
  317. this.$nextTick(() => {
  318. try {
  319. this.$refs.commentInput && this.$refs.commentInput.focus && this.$refs.commentInput.focus()
  320. } catch (_) {}
  321. })
  322. },
  323. clearForm() {
  324. this.form.pid = null
  325. this.form.content = ''
  326. },
  327. async reloadComments() {
  328. if (!this.findId) return this.count || 0
  329. const result = await fetchClipComments(this.findId)
  330. this.rows = Array.isArray(result.list) ? result.list.slice() : []
  331. return Number(result.count) || this.rows.length || 0
  332. },
  333. /**
  334. * 提交评论 / 回复
  335. */
  336. async onSubmit() {
  337. if (!this.requireSignIn()) return
  338. const content = (this.form.content || '').trim()
  339. if (content.length < 2) {
  340. uni.showToast({ title: '内容太短', icon: 'none' })
  341. return
  342. }
  343. try {
  344. await addClipComment(this.findId, content, this.form.pid)
  345. uni.showToast({ title: '已发送', icon: 'none' })
  346. this.clearForm()
  347. const nextCount = await this.reloadComments()
  348. this.$emit('complete', { type: 'add', count: nextCount, list: this.rows.slice() })
  349. } catch (err) {
  350. console.warn('[comment] add failed', err)
  351. uni.showToast({ title: (err && err.message) || '发送失败', icon: 'none' })
  352. }
  353. },
  354. /**
  355. * 删除评论(含确认弹框)
  356. * @param {number} index 父评论索引
  357. * @param {number|null} childKey 子评论索引;null 表示删除父评论
  358. */
  359. async onDelete(index, childKey = null) {
  360. if (!this.requireSignIn()) return
  361. uni.showModal({
  362. title: '删除评论',
  363. content: '删除后无法恢复,将同时删除其下所有回复。',
  364. confirmText: '确认',
  365. cancelText: '取消',
  366. success: async ({ confirm }) => {
  367. if (!confirm) return
  368. const parent = this.rows[index]
  369. if (!parent) return
  370. const target = childKey == null ? parent : (parent.childlist && parent.childlist[childKey])
  371. if (!target || !target.id) return
  372. try {
  373. const result = await deleteClipComment(target.id, target.find_id || this.findId)
  374. if (childKey == null) {
  375. this.rows.splice(index, 1)
  376. } else {
  377. parent.childlist.splice(childKey, 1)
  378. }
  379. const nextCount = result && result.count != null ? result.count : Math.max(0, (this.count || 0) - 1)
  380. this.$emit('complete', { type: 'del', count: nextCount })
  381. } catch (err) {
  382. console.warn('[comment] delete failed', err)
  383. uni.showToast({ title: (err && err.message) || '删除失败', icon: 'none' })
  384. }
  385. }
  386. })
  387. },
  388. onInputBlur() {
  389. if (this._kbClearTimer) clearTimeout(this._kbClearTimer)
  390. this._kbClearTimer = setTimeout(() => {
  391. this.keyboardHeight = 0
  392. }, 180)
  393. }
  394. }
  395. }
  396. </script>
  397. <style scoped>
  398. .ct-root {
  399. background-color: transparent;
  400. }
  401. .ct-list {
  402. padding: 0 28rpx;
  403. box-sizing: border-box;
  404. }
  405. .ct-row {
  406. display: flex;
  407. flex-direction: row;
  408. padding-top: 20rpx;
  409. padding-bottom: 20rpx;
  410. }
  411. .ct-row-sub {
  412. padding-top: 14rpx;
  413. padding-bottom: 14rpx;
  414. }
  415. .ct-avatar {
  416. width: 70rpx;
  417. height: 70rpx;
  418. border-radius: 50%;
  419. background-color: rgba(255, 255, 255, 0.08);
  420. margin-right: 20rpx;
  421. flex-shrink: 0;
  422. }
  423. .ct-avatar-sub {
  424. width: 60rpx;
  425. height: 60rpx;
  426. margin-right: 16rpx;
  427. }
  428. .ct-body {
  429. flex: 1;
  430. min-width: 0;
  431. }
  432. .ct-head {
  433. display: flex;
  434. flex-direction: row;
  435. align-items: center;
  436. justify-content: space-between;
  437. }
  438. .ct-head-left {
  439. display: flex;
  440. flex-direction: row;
  441. align-items: center;
  442. flex: 1;
  443. min-width: 0;
  444. }
  445. .ct-rank {
  446. height: 32rpx;
  447. padding: 0 10rpx;
  448. border-radius: 10rpx;
  449. margin-right: 12rpx;
  450. display: flex;
  451. align-items: center;
  452. justify-content: center;
  453. }
  454. .ct-rank-1 { background-color: #ff8c1a; }
  455. .ct-rank-2 { background-color: #aaaa00; }
  456. .ct-rank-3 { background-color: #2b6cb0; }
  457. .ct-rank-text {
  458. color: #ffffff;
  459. font-size: 20rpx;
  460. line-height: 28rpx;
  461. }
  462. .ct-nick {
  463. color: rgba(255, 255, 255, 0.66);
  464. font-size: 28rpx;
  465. font-weight: 500;
  466. overflow: hidden;
  467. text-overflow: ellipsis;
  468. white-space: nowrap;
  469. max-width: 350rpx;
  470. }
  471. .ct-nick-sub {
  472. font-size: 26rpx;
  473. }
  474. .ct-like {
  475. display: flex;
  476. flex-direction: row;
  477. align-items: center;
  478. padding-left: 16rpx;
  479. padding-right: 0;
  480. height: 50rpx;
  481. }
  482. .ct-like-num {
  483. color: rgba(255, 255, 255, 0.66);
  484. font-size: 24rpx;
  485. margin-right: 8rpx;
  486. }
  487. .ct-like-num-on {
  488. color: #ff4d6a;
  489. }
  490. .ct-like-icon {
  491. color: rgba(255, 255, 255, 0.66);
  492. font-size: 28rpx;
  493. }
  494. .ct-like-icon-on {
  495. color: #ff4d6a;
  496. }
  497. .ct-content {
  498. color: rgba(255, 255, 255, 0.9);
  499. font-size: 28rpx;
  500. line-height: 40rpx;
  501. margin-top: 6rpx;
  502. margin-bottom: 8rpx;
  503. }
  504. .ct-content-sub {
  505. font-size: 26rpx;
  506. line-height: 36rpx;
  507. }
  508. .ct-meta {
  509. display: flex;
  510. flex-direction: row;
  511. align-items: center;
  512. margin-bottom: 4rpx;
  513. }
  514. .ct-time {
  515. color: rgba(255, 255, 255, 0.5);
  516. font-size: 22rpx;
  517. margin-right: 20rpx;
  518. }
  519. .ct-action {
  520. margin-right: 20rpx;
  521. padding: 4rpx 0;
  522. }
  523. .ct-action-reply {
  524. color: #4a8cff;
  525. font-size: 22rpx;
  526. }
  527. .ct-action-del {
  528. color: #ff4d6a;
  529. font-size: 22rpx;
  530. }
  531. .ct-sublist {
  532. background-color: rgba(255, 255, 255, 0.04);
  533. border-radius: 12rpx;
  534. padding: 6rpx 16rpx;
  535. margin-top: 12rpx;
  536. }
  537. .ct-empty {
  538. display: flex;
  539. flex-direction: column;
  540. align-items: center;
  541. justify-content: center;
  542. }
  543. .ct-empty-title {
  544. color: rgba(255, 255, 255, 0.45);
  545. font-size: 26rpx;
  546. }
  547. .ct-empty-cta {
  548. color: #4a8cff;
  549. font-size: 26rpx;
  550. margin-top: 8rpx;
  551. padding: 8rpx 16rpx;
  552. }
  553. .ct-inputbar {
  554. position: fixed;
  555. left: 0;
  556. right: 0;
  557. z-index: 100;
  558. background-color: #1a1a1a;
  559. border-top: 1rpx solid rgba(255, 255, 255, 0.08);
  560. display: flex;
  561. flex-direction: row;
  562. align-items: center;
  563. box-sizing: border-box;
  564. }
  565. .ct-input {
  566. flex: 1;
  567. height: 70rpx;
  568. background-color: rgba(255, 255, 255, 0.08);
  569. color: #ffffff;
  570. border-radius: 35rpx;
  571. padding: 0 28rpx;
  572. font-size: 28rpx;
  573. }
  574. .ct-send {
  575. height: 70rpx;
  576. background-color: #4a8cff;
  577. border-radius: 35rpx;
  578. padding: 0 32rpx;
  579. display: flex;
  580. align-items: center;
  581. justify-content: center;
  582. margin-left: 20rpx;
  583. flex-shrink: 0;
  584. }
  585. .ct-send-text {
  586. color: #ffffff;
  587. font-size: 26rpx;
  588. font-weight: 500;
  589. }
  590. </style>