my-posts.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <view class="posts-container" :style="{ '--font-scale': fontScale }">
  3. <scroll-view scroll-y class="posts-list" @scrolltolower="onLoadMore">
  4. <view v-if="loading && posts.length === 0" class="loading-wrap">
  5. <text class="loading-text">加载中...</text>
  6. </view>
  7. <view v-else-if="posts.length === 0" class="empty-wrap">
  8. <text class="empty-icon">📝</text>
  9. <text class="empty-text">还没有发布过记录</text>
  10. <button class="write-btn" @click="goEdit">写一篇</button>
  11. </view>
  12. <view v-else class="post-list">
  13. <view v-for="item in posts" :key="item.id" class="post-card">
  14. <image
  15. v-if="item.coverImage"
  16. class="post-cover"
  17. :src="getImageUrl(item.coverImage)"
  18. mode="aspectFill"
  19. lazy-load />
  20. <view class="post-body">
  21. <text class="post-title">{{ item.title }}</text>
  22. <view class="post-meta">
  23. <text class="post-date">{{ formatDate(item.publishedAt || item.createdAt) }}</text>
  24. <text
  25. :class="['post-status', getStatusClass(item)]"
  26. >
  27. {{ getStatusText(item) }}
  28. </text>
  29. </view>
  30. <text v-if="item.auditStatus === 'rejected' && item.auditReason" class="post-reason">
  31. 驳回原因:{{ item.auditReason }}
  32. </text>
  33. </view>
  34. </view>
  35. </view>
  36. <view v-if="loadingMore" class="loading-more">
  37. <text class="loading-text">加载中...</text>
  38. </view>
  39. <view v-if="noMore && posts.length > 0" class="no-more">
  40. <text class="no-more-text">— 没有更多了 —</text>
  41. </view>
  42. </scroll-view>
  43. </view>
  44. </template>
  45. <script>
  46. import { getMyPosts } from '@/utils/api.js'
  47. import config from '@/config.js'
  48. import fontScaleMixin from '@/mixins/font-scale.js'
  49. export default {
  50. mixins: [fontScaleMixin],
  51. data() {
  52. return {
  53. posts: [],
  54. page: 1,
  55. size: 20,
  56. total: 0,
  57. loading: false,
  58. loadingMore: false,
  59. noMore: false
  60. }
  61. },
  62. onLoad() {
  63. this.loadPosts()
  64. },
  65. methods: {
  66. async loadPosts(isLoadMore) {
  67. if (!isLoadMore) {
  68. if (this.loading) return
  69. this.loading = true
  70. } else {
  71. if (this.loadingMore || this.noMore) return
  72. this.loadingMore = true
  73. }
  74. try {
  75. var res = await getMyPosts({ page: this.page, size: this.size })
  76. if (res.code === 200 && res.data) {
  77. var list = res.data.records || []
  78. if (this.page === 1) {
  79. this.posts = list
  80. } else {
  81. this.posts = this.posts.concat(list)
  82. }
  83. this.total = res.data.total || 0
  84. this.noMore = this.posts.length >= this.total
  85. }
  86. } catch (e) {
  87. uni.showToast({ title: '加载失败', icon: 'none' })
  88. } finally {
  89. this.loading = false
  90. this.loadingMore = false
  91. }
  92. },
  93. onLoadMore() {
  94. if (this.noMore || this.loading || this.loadingMore) return
  95. this.page++
  96. this.loadPosts(true)
  97. },
  98. goEdit() {
  99. uni.navigateTo({ url: '/pages/article-center/article-edit' })
  100. },
  101. formatDate(dateStr) {
  102. if (!dateStr) return ''
  103. return dateStr.slice(0, 10)
  104. },
  105. getStatusText(item) {
  106. if (item.status === 'draft' && (!item.auditStatus || item.auditStatus === 'pending')) {
  107. return '审核中'
  108. }
  109. if (item.auditStatus === 'rejected') return '已驳回'
  110. if (item.status === 'published') return '已发布'
  111. return '草稿'
  112. },
  113. getStatusClass(item) {
  114. if (item.auditStatus === 'approved' || item.status === 'published') return 'status-approved'
  115. if (item.auditStatus === 'pending') return 'status-pending'
  116. if (item.auditStatus === 'rejected') return 'status-rejected'
  117. return 'status-draft'
  118. },
  119. getImageUrl: function(path) {
  120. if (!path || path === 'null' || path === 'undefined' || path === '/null') return ''
  121. var s = String(path).trim()
  122. if (!s || s === 'null' || s === '/null') return ''
  123. if (s.indexOf('http://') === 0 || s.indexOf('https://') === 0) return s
  124. return config.API_BASE_URL + (s.charAt(0) === '/' ? s : '/' + s)
  125. }
  126. }
  127. }
  128. </script>
  129. <style scoped lang="scss">
  130. .posts-container { min-height: 100vh; background: $app-bg; }
  131. .posts-list { height: 100vh; }
  132. .loading-wrap, .empty-wrap { display: flex; flex-direction: column; align-items: center; padding-top: 200rpx; }
  133. .loading-text { font-size: calc(26rpx * var(--font-scale, 1)); color: $facade-text-muted; }
  134. .empty-icon { font-size: calc($uni-font-size-6xl * var(--font-scale, 1)); margin-bottom: 24rpx; }
  135. .empty-text { font-size: calc(28rpx * var(--font-scale, 1)); color: $facade-text-muted; margin-bottom: $uni-space-lg; }
  136. .write-btn { width: 240rpx; height: 72rpx; line-height: 72rpx; background: $brand-primary; color: $app-surface; font-size: calc(28rpx * var(--font-scale, 1)); border-radius: 24rpx; border: none; }
  137. .write-btn::after { border: none; }
  138. .post-list { padding: 20rpx 24rpx; }
  139. .post-card {
  140. display: flex;
  141. background: $app-surface;
  142. border-radius: 16rpx;
  143. overflow: hidden;
  144. margin-bottom: 20rpx;
  145. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  146. }
  147. .post-cover { width: 200rpx; height: 160rpx; flex-shrink: 0; }
  148. .post-body { flex: 1; padding: 20rpx; display: flex; flex-direction: column; justify-content: center; }
  149. .post-title { font-size: calc(28rpx * var(--font-scale, 1)); font-weight: bold; color: $facade-text-strong; line-height: 1.4; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-bottom: 8rpx; }
  150. .post-meta { display: flex; align-items: center; font-size: calc(22rpx * var(--font-scale, 1)); }
  151. .post-date { color: $facade-text-muted; margin-right: 16rpx; }
  152. .post-status { padding: 2rpx 12rpx; border-radius: 8rpx; }
  153. .status-approved { color: $status-success-2; background: rgba(34,197,94,0.1); }
  154. .status-pending { color: $dim-wealth; background: rgba(245,158,11,0.1); }
  155. .status-rejected { color: $status-danger; background: rgba(239,68,68,0.1); }
  156. .status-draft { color: $facade-text-muted; background: rgba(153,153,153,0.1); }
  157. .post-reason { font-size: calc(20rpx * var(--font-scale, 1)); color: $status-danger; margin-top: 6rpx; display: block; }
  158. .loading-more, .no-more { text-align: center; padding: $uni-space-page; }
  159. .no-more-text { font-size: calc(24rpx * var(--font-scale, 1)); color: $facade-text-ghost; }
  160. </style>