circle-feed.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. <template>
  2. <view class="feed-page">
  3. <!-- 导航栏 -->
  4. <view class="nav-bar">
  5. <view class="nav-back" @tap="goBack">
  6. <text class="nav-back-icon">‹</text>
  7. </view>
  8. <text class="nav-title">{{ circleName }}</text>
  9. <view class="nav-placeholder"></view>
  10. </view>
  11. <!-- 圈子信息卡 -->
  12. <view class="circle-header-card" @tap="showMemberList">
  13. <view class="ch-avatar">{{ (circleName || '?').substring(0,1) }}</view>
  14. <view class="ch-info">
  15. <text class="ch-name">{{ circleName }}</text>
  16. <text class="ch-meta">{{ memberCount }} 位成员 · 点击查看详情</text>
  17. </view>
  18. <text class="ch-arrow">›</text>
  19. </view>
  20. <!-- 发布框 -->
  21. <view class="post-input-card" @tap="openPostEditor">
  22. <text class="pi-icon">✍️</text>
  23. <text class="pi-placeholder">分享今天的打卡或想法...</text>
  24. </view>
  25. <!-- 动态列表 -->
  26. <scroll-view class="feed-scroll" scroll-y :refresher-enabled="true" :refresher-triggered="refreshing" @refresherrefresh="onRefresh">
  27. <view v-if="posts.length === 0 && !loading" class="empty-state">
  28. <text class="empty-icon">📭</text>
  29. <text class="empty-text">暂无动态,快来发第一条吧</text>
  30. </view>
  31. <view v-for="post in posts" :key="post.id" class="feed-item">
  32. <!-- 作者信息 -->
  33. <view class="feed-author">
  34. <view class="fa-avatar">{{ getAvatarText(post) }}</view>
  35. <view class="fa-info">
  36. <text class="fa-name">{{ getMemberName(post) }}</text>
  37. <text class="fa-time">{{ formatTime(post.createdAt) }}</text>
  38. </view>
  39. <text class="fa-type-badge" :class="'type-' + post.type">{{ typeLabel(post.type) }}</text>
  40. </view>
  41. <!-- 文字内容 -->
  42. <view class="feed-content" v-if="post.content">
  43. <text class="fc-text">{{ post.content }}</text>
  44. </view>
  45. <!-- 图片 -->
  46. <view class="feed-images" v-if="post.imageUrls && post.imageUrls.length > 0">
  47. <image
  48. v-for="(img, idx) in post.imageUrls"
  49. :key="idx"
  50. :src="img"
  51. mode="aspectFill"
  52. class="feed-img"
  53. @tap="previewImage(post.imageUrls, idx)"
  54. />
  55. </view>
  56. <!-- 操作栏 -->
  57. <view class="feed-actions">
  58. <view class="fa-item" :class="{ 'fa-active': post.liked }" @tap="toggleLike(post)">
  59. <text class="fa-icon">{{ post.liked ? '❤️' : '🤍' }}</text>
  60. <text class="fa-count">{{ post.likeCount || 0 }}</text>
  61. </view>
  62. <view class="fa-item" @tap="openCommentInput(post)">
  63. <text class="fa-icon">💬</text>
  64. <text class="fa-count">{{ post.commentCount || 0 }}</text>
  65. </view>
  66. <view class="fa-item" @tap="sharePost(post)">
  67. <text class="fa-icon">↗️</text>
  68. <text class="fa-count">分享</text>
  69. </view>
  70. </view>
  71. <!-- 评论列表 -->
  72. <view class="feed-comments" v-if="post.comments && post.comments.length > 0">
  73. <view class="comment-item" v-for="(c, idx) in post.comments.slice(0, 3)" :key="idx">
  74. <text class="c-name">{{ getMemberNameById(c.memberId) }}</text>
  75. <text class="c-text">{{ c.content }}</text>
  76. </view>
  77. <text class="more-comments" v-if="post.commentCount > 3" @tap="loadMoreComments(post)">查看全部 {{ post.commentCount }} 条评论</text>
  78. </view>
  79. <!-- 评论输入框(展开时) -->
  80. <view class="comment-input-area" v-if="post.showCommentInput">
  81. <input class="ci-input" placeholder="写评论..." v-model="post.commentText" @confirm="submitComment(post)" />
  82. <text class="ci-send" @tap="submitComment(post)">发送</text>
  83. </view>
  84. </view>
  85. <view class="loading-more" v-if="loadingMore">
  86. <text class="lm-text">加载中...</text>
  87. </view>
  88. <view class="end-hint" v-if="!hasMore && posts.length > 0">
  89. <text class="eh-text">— 没有更多了 —</text>
  90. </view>
  91. <view class="bottom-space"></view>
  92. </scroll-view>
  93. <!-- 发帖弹窗 -->
  94. <view class="modal-mask" v-if="showPostModal" @tap="closePostModal">
  95. <view class="modal-content" @tap.stop>
  96. <view class="modal-header">
  97. <text class="modal-title">发布动态</text>
  98. <text class="modal-close" @tap="closePostModal">✕</text>
  99. </view>
  100. <view class="type-selector">
  101. <view class="type-opt" :class="{ 'type-active': postType === t.value }" v-for="t in postTypes" :key="t.value" @tap="postType = t.value">
  102. <text class="to-icon">{{ t.icon }}</text>
  103. <text class="to-label">{{ t.label }}</text>
  104. </view>
  105. </view>
  106. <textarea class="post-textarea" placeholder="分享你的想法..." v-model="postContent" maxlength="500" />
  107. <view class="modal-footer">
  108. <button class="btn-publish" :disabled="!postContent.trim()" @tap="publishPost">发布</button>
  109. </view>
  110. </view>
  111. </view>
  112. <!-- 成员列表弹窗 -->
  113. <view class="modal-mask" v-if="showMemberModal" @tap="showMemberModal = false">
  114. <view class="modal-content modal-members" @tap.stop>
  115. <view class="modal-header">
  116. <text class="modal-title">圈子成员</text>
  117. <text class="modal-close" @tap="showMemberModal = false">✕</text>
  118. </view>
  119. <view class="member-list">
  120. <view class="member-item" v-for="m in members" :key="m.id">
  121. <view class="mi-avatar">{{ (m.nickname || '?').substring(0,1) }}</view>
  122. <text class="mi-name">{{ m.nickname || '未知成员' }}</text>
  123. <text class="mi-role" v-if="m.role === 'admin'">管理员</text>
  124. </view>
  125. </view>
  126. </view>
  127. </view>
  128. </view>
  129. </template>
  130. <script>
  131. import { getCircleFeed, getCircleMembers, createCirclePost, toggleCircleLike, addCircleComment, getCircleComments } from '@/utils/api.js'
  132. import { parseDate } from '@/utils/format.js'
  133. export default {
  134. data: function() {
  135. return {
  136. circleId: null,
  137. circleName: '',
  138. memberCount: 0,
  139. posts: [],
  140. loading: true,
  141. refreshing: false,
  142. loadingMore: false,
  143. hasMore: true,
  144. page: 1,
  145. pageSize: 10,
  146. showPostModal: false,
  147. showMemberModal: false,
  148. members: [],
  149. postType: 'text',
  150. postContent: '',
  151. postTypes: [
  152. { value: 'text', label: '文字', icon: '📝' },
  153. { value: 'checkin', label: '打卡', icon: '✅' },
  154. { value: 'diet', label: '饮食', icon: '🥗' },
  155. { value: 'exercise', label: '运动', icon: '🏃' },
  156. { value: 'sleep', label: '睡眠', icon: '😴' },
  157. { value: 'water', label: '饮水', icon: '💧' },
  158. { value: 'mood', label: '心情', icon: '😊' }
  159. ]
  160. }
  161. },
  162. onLoad: function(options) {
  163. this.circleId = options.circleId ? parseInt(options.circleId) : null
  164. this.circleName = options.name || '圈子'
  165. if (options.memberCount) this.memberCount = parseInt(options.memberCount)
  166. if (this.circleId) {
  167. this.loadFeed()
  168. this.loadMembers()
  169. }
  170. },
  171. methods: {
  172. goBack: function() {
  173. uni.navigateBack()
  174. },
  175. loadFeed: function() {
  176. var self = this
  177. self.loading = true
  178. getCircleFeed({ circleId: self.circleId, page: self.page, size: self.pageSize }).then(function(res) {
  179. if (res.code === 200 && Array.isArray(res.data)) {
  180. if (self.page === 1) {
  181. self.posts = res.data
  182. } else {
  183. self.posts = self.posts.concat(res.data)
  184. }
  185. self.hasMore = res.data.length >= self.pageSize
  186. }
  187. }).catch(function() {}).finally(function() {
  188. self.loading = false
  189. self.refreshing = false
  190. self.loadingMore = false
  191. })
  192. },
  193. loadMembers: function() {
  194. var self = this
  195. var memberId = uni.getStorageSync('currentChildId') || ''
  196. getCircleMembers(self.circleId, memberId).then(function(res) {
  197. if (res.code === 200 && Array.isArray(res.data)) {
  198. self.members = res.data
  199. }
  200. }).catch(function() {})
  201. },
  202. onRefresh: function() {
  203. this.page = 1
  204. this.hasMore = true
  205. this.loadFeed()
  206. },
  207. loadMore: function() {
  208. if (this.loadingMore || !this.hasMore) return
  209. this.page++
  210. this.loadingMore = true
  211. this.loadFeed()
  212. },
  213. openPostEditor: function() {
  214. var memberId = uni.getStorageSync('currentChildId') || ''
  215. if (!memberId) {
  216. uni.showToast({ title: '请先登录', icon: 'none' })
  217. return
  218. }
  219. this.postContent = ''
  220. this.postType = 'text'
  221. this.showPostModal = true
  222. },
  223. closePostModal: function() {
  224. this.showPostModal = false
  225. },
  226. publishPost: function() {
  227. var self = this
  228. var memberId = uni.getStorageSync('currentChildId') || ''
  229. if (!memberId) return
  230. if (!self.postContent.trim()) return
  231. createCirclePost({
  232. circleId: self.circleId,
  233. memberId: memberId,
  234. memberType: 'child',
  235. type: self.postType,
  236. content: self.postContent.trim()
  237. }).then(function(res) {
  238. if (res.code === 200) {
  239. self.showPostModal = false
  240. self.page = 1
  241. self.hasMore = true
  242. self.loadFeed()
  243. uni.showToast({ title: '发布成功', icon: 'success' })
  244. } else {
  245. uni.showToast({ title: res.message || '发布失败', icon: 'none' })
  246. }
  247. }).catch(function() {
  248. uni.showToast({ title: '发布失败,请重试', icon: 'none' })
  249. })
  250. },
  251. toggleLike: function(post) {
  252. var self = this
  253. var memberId = uni.getStorageSync('currentChildId') || ''
  254. if (!memberId) return
  255. toggleCircleLike({
  256. circleId: self.circleId,
  257. postId: post.id,
  258. memberId: memberId,
  259. memberType: 'child'
  260. }).then(function(res) {
  261. if (res.code === 200) {
  262. post.liked = res.data && res.data.liked
  263. post.likeCount = res.data && res.data.likeCount
  264. }
  265. }).catch(function() {})
  266. },
  267. openCommentInput: function(post) {
  268. post.showCommentInput = !post.showCommentInput
  269. if (post.showCommentInput && !post.comments) {
  270. var self = this
  271. var memberId = uni.getStorageSync('currentChildId') || ''
  272. if (!memberId) return
  273. getCircleComments({ postId: post.id }).then(function(res) {
  274. if (res.code === 200) {
  275. post.comments = res.data || []
  276. }
  277. }).catch(function() {})
  278. }
  279. },
  280. submitComment: function(post) {
  281. var self = this
  282. var memberId = uni.getStorageSync('currentChildId') || ''
  283. if (!memberId || !post.commentText.trim()) return
  284. addCircleComment({
  285. circleId: self.circleId,
  286. postId: post.id,
  287. memberId: memberId,
  288. memberType: 'child',
  289. content: post.commentText.trim()
  290. }).then(function(res) {
  291. if (res.code === 200) {
  292. post.commentText = ''
  293. post.commentCount = (post.commentCount || 0) + 1
  294. if (!post.comments) post.comments = []
  295. post.comments.push(res.data)
  296. }
  297. }).catch(function() {})
  298. },
  299. loadMoreComments: function(post) {
  300. // TODO: load all comments
  301. },
  302. sharePost: function(post) {
  303. uni.showToast({ title: '分享功能开发中', icon: 'none' })
  304. },
  305. showMemberList: function() {
  306. this.showMemberModal = true
  307. },
  308. previewImage: function(images, index) {
  309. uni.previewImage({ current: images[index], urls: images })
  310. },
  311. getAvatarText: function(post) {
  312. return 'A'
  313. },
  314. getMemberName: function(post) {
  315. return '我'
  316. },
  317. getMemberNameById: function(memberId) {
  318. for (var i = 0; i < this.members.length; i++) {
  319. if (this.members[i].memberId === memberId) {
  320. return this.members[i].nickname || '未知'
  321. }
  322. }
  323. return '未知成员'
  324. },
  325. formatTime: function(timeStr) {
  326. if (!timeStr) return ''
  327. var d = parseDate(timeStr)
  328. if (!d) return ''
  329. var h = d.getHours().toString()
  330. if (h.length < 2) h = '0' + h
  331. var m = d.getMinutes().toString()
  332. if (m.length < 2) m = '0' + m
  333. return h + ':' + m
  334. },
  335. typeLabel: function(type) {
  336. var map = {
  337. text: '文字', checkin: '打卡', diet: '饮食',
  338. exercise: '运动', sleep: '睡眠', water: '饮水', mood: '心情'
  339. }
  340. return map[type] || type
  341. },
  342. onScrollToLower: function() {
  343. this.loadMore()
  344. }
  345. }
  346. }
  347. </script>
  348. <style scoped>
  349. .feed-page {
  350. min-height: 100vh;
  351. background: #F5F7FA;
  352. }
  353. .nav-bar {
  354. display: flex;
  355. align-items: center;
  356. justify-content: space-between;
  357. height: 88rpx;
  358. padding-top: env(safe-area-inset-top);
  359. background: #FFFFFF;
  360. position: sticky;
  361. top: 0;
  362. z-index: 100;
  363. border-bottom: 1rpx solid #f0f0f0;
  364. }
  365. .nav-back {
  366. width: 80rpx;
  367. display: flex;
  368. align-items: center;
  369. padding-left: 24rpx;
  370. }
  371. .nav-back-icon {
  372. font-size: 48rpx;
  373. color: #333;
  374. }
  375. .nav-title {
  376. font-size: 32rpx;
  377. font-weight: 600;
  378. color: #333;
  379. }
  380. .nav-placeholder { width: 80rpx; }
  381. /* 圈子信息卡 */
  382. .circle-header-card {
  383. display: flex;
  384. align-items: center;
  385. background: #FFFFFF;
  386. padding: 24rpx 32rpx;
  387. margin-bottom: 16rpx;
  388. }
  389. .ch-avatar {
  390. width: 72rpx;
  391. height: 72rpx;
  392. border-radius: 50%;
  393. background: linear-gradient(135deg, #FF8C42, #FFB366);
  394. color: #fff;
  395. font-size: 32rpx;
  396. font-weight: 700;
  397. display: flex;
  398. align-items: center;
  399. justify-content: center;
  400. margin-right: 20rpx;
  401. flex-shrink: 0;
  402. }
  403. .ch-info { flex: 1; }
  404. .ch-name {
  405. font-size: 30rpx;
  406. font-weight: 600;
  407. color: #333;
  408. display: block;
  409. }
  410. .ch-meta {
  411. font-size: 22rpx;
  412. color: #999;
  413. margin-top: 4rpx;
  414. display: block;
  415. }
  416. .ch-arrow { font-size: 36rpx; color: #ccc; }
  417. /* 发布框 */
  418. .post-input-card {
  419. display: flex;
  420. align-items: center;
  421. background: #FFFFFF;
  422. padding: 24rpx 32rpx;
  423. margin-bottom: 16rpx;
  424. }
  425. .pi-icon { font-size: 40rpx; margin-right: 20rpx; }
  426. .pi-placeholder { font-size: 28rpx; color: #bbb; }
  427. /* 滚动区域 */
  428. .feed-scroll {
  429. height: calc(100vh - 400rpx);
  430. }
  431. /* 动态项 */
  432. .feed-item {
  433. background: #FFFFFF;
  434. margin-bottom: 16rpx;
  435. padding: 24rpx 32rpx;
  436. }
  437. .feed-author {
  438. display: flex;
  439. align-items: center;
  440. margin-bottom: 16rpx;
  441. }
  442. .fa-avatar {
  443. width: 56rpx;
  444. height: 56rpx;
  445. border-radius: 50%;
  446. background: #E3F2FD;
  447. color: #1976D2;
  448. font-size: 24rpx;
  449. font-weight: 700;
  450. display: flex;
  451. align-items: center;
  452. justify-content: center;
  453. margin-right: 16rpx;
  454. flex-shrink: 0;
  455. }
  456. .fa-info { flex: 1; display: flex; flex-direction: column; }
  457. .fa-name { font-size: 26rpx; font-weight: 600; color: #333; }
  458. .fa-time { font-size: 20rpx; color: #999; margin-top: 2rpx; }
  459. .fa-type-badge {
  460. font-size: 20rpx;
  461. padding: 4rpx 12rpx;
  462. border-radius: 12rpx;
  463. background: #FFF0E0;
  464. color: #FF8C42;
  465. }
  466. /* 内容 */
  467. .feed-content { margin-bottom: 16rpx; }
  468. .fc-text { font-size: 28rpx; color: #333; line-height: 1.7; }
  469. .feed-images { display: flex; flex-wrap: wrap; margin-bottom: 16rpx; }
  470. .feed-img {
  471. width: 220rpx;
  472. height: 220rpx;
  473. border-radius: 12rpx;
  474. margin-right: 12rpx;
  475. margin-bottom: 12rpx;
  476. }
  477. /* 操作栏 */
  478. .feed-actions {
  479. display: flex;
  480. border-top: 1rpx solid #f5f5f5;
  481. padding-top: 16rpx;
  482. }
  483. .fa-item {
  484. display: flex;
  485. align-items: center;
  486. margin-right: 40rpx;
  487. }
  488. .fa-icon { font-size: 32rpx; margin-right: 6rpx; }
  489. .fa-count { font-size: 22rpx; color: #999; }
  490. .fa-active .fa-icon { transform: scale(1.1); }
  491. /* 评论 */
  492. .feed-comments { margin-top: 16rpx; padding-top: 16rpx; border-top: 1rpx solid #f5f5f5; }
  493. .comment-item { display: flex; margin-bottom: 8rpx; }
  494. .c-name { font-size: 24rpx; color: #FF8C42; font-weight: 600; margin-right: 8rpx; }
  495. .c-text { font-size: 24rpx; color: #666; }
  496. .more-comments { font-size: 22rpx; color: #999; margin-top: 8rpx; }
  497. .comment-input-area {
  498. display: flex;
  499. align-items: center;
  500. margin-top: 12rpx;
  501. padding-top: 12rpx;
  502. border-top: 1rpx solid #f5f5f5;
  503. }
  504. .ci-input {
  505. flex: 1;
  506. background: #F5F5F5;
  507. border-radius: 30rpx;
  508. padding: 12rpx 20rpx;
  509. font-size: 26rpx;
  510. margin-right: 16rpx;
  511. }
  512. .ci-send { font-size: 26rpx; color: #FF8C42; font-weight: 600; }
  513. /* 加载状态 */
  514. .loading-more, .end-hint { text-align: center; padding: 24rpx 0; }
  515. .lm-text, .eh-text { font-size: 24rpx; color: #ccc; }
  516. .empty-state { text-align: center; padding: 120rpx 0; }
  517. .empty-icon { font-size: 80rpx; display: block; margin-bottom: 16rpx; }
  518. .empty-text { font-size: 26rpx; color: #ccc; }
  519. .bottom-space { height: 40rpx; }
  520. /* 弹窗 */
  521. .modal-mask {
  522. position: fixed;
  523. top: 0; left: 0; right: 0; bottom: 0;
  524. background: rgba(0,0,0,0.5);
  525. z-index: 1000;
  526. display: flex;
  527. align-items: flex-end;
  528. justify-content: center;
  529. }
  530. .modal-content {
  531. width: 100%;
  532. max-height: 80vh;
  533. background: #FFFFFF;
  534. border-radius: 32rpx 32rpx 0 0;
  535. padding: 32rpx;
  536. overflow-y: auto;
  537. }
  538. .modal-header {
  539. display: flex;
  540. justify-content: space-between;
  541. align-items: center;
  542. margin-bottom: 24rpx;
  543. }
  544. .modal-title { font-size: 32rpx; font-weight: 600; color: #333; }
  545. .modal-close { font-size: 32rpx; color: #999; }
  546. .type-selector {
  547. display: flex;
  548. flex-wrap: wrap;
  549. margin-bottom: 20rpx;
  550. }
  551. .type-opt {
  552. display: flex;
  553. flex-direction: column;
  554. align-items: center;
  555. padding: 16rpx 24rpx;
  556. margin-right: 16rpx;
  557. margin-bottom: 16rpx;
  558. background: #F5F5F5;
  559. border-radius: 16rpx;
  560. border: 2rpx solid transparent;
  561. }
  562. .type-active { border-color: #FF8C42; background: #FFF0E0; }
  563. .to-icon { font-size: 36rpx; margin-bottom: 6rpx; }
  564. .to-label { font-size: 22rpx; color: #666; }
  565. .post-textarea {
  566. width: 100%;
  567. height: 200rpx;
  568. background: #F8F8F8;
  569. border-radius: 16rpx;
  570. padding: 20rpx;
  571. font-size: 28rpx;
  572. line-height: 1.6;
  573. box-sizing: border-box;
  574. }
  575. .modal-footer { margin-top: 20rpx; }
  576. .btn-publish {
  577. background: linear-gradient(135deg, #FF8C42, #FFB366);
  578. color: #fff;
  579. border: none;
  580. border-radius: 40rpx;
  581. font-size: 30rpx;
  582. font-weight: 600;
  583. padding: 24rpx 0;
  584. }
  585. .btn-publish[disabled] { opacity: 0.4; }
  586. /* 成员列表 */
  587. .modal-members { max-height: 60vh; }
  588. .member-list { }
  589. .member-item {
  590. display: flex;
  591. align-items: center;
  592. padding: 20rpx 0;
  593. border-bottom: 1rpx solid #f5f5f5;
  594. }
  595. .mi-avatar {
  596. width: 56rpx;
  597. height: 56rpx;
  598. border-radius: 50%;
  599. background: #E3F2FD;
  600. color: #1976D2;
  601. font-size: 24rpx;
  602. font-weight: 700;
  603. display: flex;
  604. align-items: center;
  605. justify-content: center;
  606. margin-right: 16rpx;
  607. }
  608. .mi-name { font-size: 26rpx; color: #333; flex: 1; }
  609. .mi-role { font-size: 20rpx; color: #FF8C42; background: #FFF0E0; padding: 4rpx 12rpx; border-radius: 12rpx; }
  610. </style>