user-dynamics.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <template>
  2. <view class="user-dynamics-page">
  3. <!-- 自定义导航栏 -->
  4. <view class="custom-navbar">
  5. <view class="navbar-left" @click="goBack">
  6. <text class="back-icon">←</text>
  7. </view>
  8. <view class="navbar-title">用户动态</view>
  9. <view class="navbar-right"></view>
  10. </view>
  11. <scroll-view
  12. scroll-y
  13. class="page-content"
  14. @scrolltolower="loadMore"
  15. refresher-enabled
  16. :refresher-triggered="refreshing"
  17. @refresherrefresh="onRefresh"
  18. >
  19. <!-- 动态列表 -->
  20. <view v-if="loading && dynamicList.length === 0" class="loading-container">
  21. <text>加载中...</text>
  22. </view>
  23. <view v-else-if="dynamicList.length === 0" class="empty-container">
  24. <text class="empty-icon">📝</text>
  25. <text class="empty-text">该用户还没有发布动态</text>
  26. </view>
  27. <view v-else class="dynamic-list">
  28. <view
  29. v-for="(item, index) in dynamicList"
  30. :key="item.dynamicId || index"
  31. class="dynamic-card"
  32. @tap="viewDynamicDetail(item, index)"
  33. >
  34. <!-- 动态内容 -->
  35. <view class="dynamic-content" v-if="item.content">
  36. <text class="dynamic-text">{{ item.content }}</text>
  37. </view>
  38. <!-- 动态媒体(照片/视频) -->
  39. <view class="dynamic-media" v-if="item.mediaUrls && item.mediaUrls.length > 0">
  40. <view :class="['media-grid', getGridClass(item)]">
  41. <image
  42. v-for="(url, idx) in item.mediaUrls.slice(0, 9)"
  43. :key="idx"
  44. :src="url"
  45. class="media-image"
  46. mode="aspectFill"
  47. />
  48. </view>
  49. </view>
  50. <!-- 动态信息 -->
  51. <view class="dynamic-info">
  52. <text class="dynamic-time">{{ formatTime(item.createdAt) }}</text>
  53. <view class="dynamic-stats">
  54. <text class="stat-item">❤️ {{ item.likeCount || 0 }}</text>
  55. <text class="stat-item">💬 {{ item.commentCount || 0 }}</text>
  56. <text class="stat-item">⭐ {{ item.favoriteCount || 0 }}</text>
  57. </view>
  58. </view>
  59. </view>
  60. </view>
  61. <!-- 加载更多 -->
  62. <view v-if="loading && dynamicList.length > 0" class="loading-tip">
  63. <text>加载中...</text>
  64. </view>
  65. <view v-if="noMore && dynamicList.length > 0" class="loading-tip">
  66. <text>没有更多了~</text>
  67. </view>
  68. </scroll-view>
  69. </view>
  70. </template>
  71. <script>
  72. import api from '@/utils/api.js'
  73. export default {
  74. data() {
  75. return {
  76. userId: null,
  77. dynamicList: [],
  78. loading: false,
  79. refreshing: false,
  80. noMore: false,
  81. pageNum: 1,
  82. pageSize: 10
  83. }
  84. },
  85. onLoad(options) {
  86. if (options.userId) {
  87. this.userId = parseInt(options.userId)
  88. this.loadDynamics()
  89. } else {
  90. uni.showToast({
  91. title: '用户ID无效',
  92. icon: 'none'
  93. })
  94. setTimeout(() => {
  95. uni.navigateBack()
  96. }, 1500)
  97. }
  98. },
  99. methods: {
  100. // 加载动态
  101. async loadDynamics(isRefresh = false) {
  102. if (this.loading) return
  103. if (isRefresh) {
  104. this.pageNum = 1
  105. this.noMore = false
  106. }
  107. this.loading = true
  108. try {
  109. const currentUserId = uni.getStorageSync('userId')
  110. const params = {
  111. pageNum: this.pageNum,
  112. pageSize: this.pageSize,
  113. currentUserId: currentUserId ? parseInt(currentUserId) : null
  114. }
  115. const result = await api.dynamic.getUserDynamics(this.userId, params)
  116. // 处理PageResult格式:{ records: [], total: 0, current: 1, size: 10 }
  117. let list = []
  118. if (result) {
  119. // PageResult格式:{ records: [], total: 0 }
  120. if (result.records && Array.isArray(result.records)) {
  121. list = result.records
  122. }
  123. // 兼容格式:{ list: [], total: 0 }
  124. else if (result.list && Array.isArray(result.list)) {
  125. list = result.list
  126. }
  127. // 直接是数组
  128. else if (Array.isArray(result)) {
  129. list = result
  130. }
  131. }
  132. // 处理mediaUrls字段(可能是字符串需要解析)
  133. list = list.map(item => {
  134. // 确保 dynamicId 存在
  135. if (!item.dynamicId && item.id) {
  136. item.dynamicId = item.id
  137. }
  138. if (!item.dynamicId && item.dynamic_id) {
  139. item.dynamicId = item.dynamic_id
  140. }
  141. if (item.mediaUrls && typeof item.mediaUrls === 'string') {
  142. try {
  143. item.mediaUrls = JSON.parse(item.mediaUrls)
  144. } catch (e) {
  145. console.error('解析mediaUrls失败:', e)
  146. item.mediaUrls = []
  147. }
  148. }
  149. if (!item.mediaUrls || !Array.isArray(item.mediaUrls)) {
  150. item.mediaUrls = []
  151. }
  152. return item
  153. })
  154. this.noMore = list.length < this.pageSize
  155. if (isRefresh) {
  156. this.dynamicList = list
  157. } else {
  158. this.dynamicList = [...this.dynamicList, ...list]
  159. }
  160. if (!this.noMore) {
  161. this.pageNum++
  162. }
  163. } catch (e) {
  164. console.error('获取用户动态失败:', e)
  165. uni.showToast({
  166. title: '加载失败',
  167. icon: 'none'
  168. })
  169. } finally {
  170. this.loading = false
  171. this.refreshing = false
  172. }
  173. },
  174. // 加载更多
  175. loadMore() {
  176. if (!this.noMore && !this.loading) {
  177. this.loadDynamics()
  178. }
  179. },
  180. // 下拉刷新
  181. onRefresh() {
  182. this.refreshing = true
  183. this.loadDynamics(true)
  184. },
  185. // 查看动态详情
  186. viewDynamicDetail(item, index) {
  187. // 如果 item 无效,尝试从列表中根据索引获取
  188. if (!item && typeof index === 'number' && this.dynamicList && this.dynamicList[index]) {
  189. item = this.dynamicList[index]
  190. }
  191. if (!item) {
  192. console.error('动态项为空,无法获取')
  193. uni.showToast({
  194. title: '动态信息为空',
  195. icon: 'none'
  196. })
  197. return
  198. }
  199. // 尝试多种可能的字段名
  200. const dynamicId = item.dynamicId || item.id || item.dynamic_id
  201. if (!dynamicId) {
  202. console.error('动态ID无效,item 内容:', item)
  203. uni.showToast({
  204. title: '动态ID无效',
  205. icon: 'none'
  206. })
  207. return
  208. }
  209. const url = `/pages/plaza/detail?dynamicId=${dynamicId}`
  210. uni.navigateTo({
  211. url: url,
  212. success: (res) => {
  213. },
  214. fail: (err) => {
  215. console.error('跳转失败:', err)
  216. uni.showToast({
  217. title: '跳转失败: ' + (err.errMsg || '未知错误'),
  218. icon: 'none',
  219. duration: 2000
  220. })
  221. }
  222. })
  223. },
  224. // 预览媒体
  225. previewMedia(urls, index) {
  226. if (!urls || urls.length === 0) return
  227. uni.previewImage({
  228. urls: urls,
  229. current: index
  230. })
  231. },
  232. // 获取网格类名
  233. getGridClass(item) {
  234. const count = item.mediaUrls ? item.mediaUrls.length : 0
  235. if (count === 1) return 'grid-1'
  236. if (count === 2 || count === 4) return 'grid-2'
  237. return 'grid-3'
  238. },
  239. // 格式化时间
  240. formatTime(timeStr) {
  241. if (!timeStr) return ''
  242. const date = new Date(timeStr)
  243. const now = new Date()
  244. const diff = now - date
  245. const minutes = Math.floor(diff / 60000)
  246. const hours = Math.floor(diff / 3600000)
  247. const days = Math.floor(diff / 86400000)
  248. if (minutes < 1) return '刚刚'
  249. if (minutes < 60) return `${minutes}分钟前`
  250. if (hours < 24) return `${hours}小时前`
  251. if (days < 7) return `${days}天前`
  252. return date.toLocaleDateString()
  253. },
  254. // 返回
  255. goBack() {
  256. uni.navigateBack()
  257. }
  258. }
  259. }
  260. </script>
  261. <style lang="scss" scoped>
  262. .user-dynamics-page {
  263. min-height: 100vh;
  264. background: #F5F5F5;
  265. padding-top: 90rpx;
  266. }
  267. /* 自定义导航栏 */
  268. .custom-navbar {
  269. position: fixed;
  270. top: 0;
  271. left: 0;
  272. right: 0;
  273. height: 90rpx;
  274. display: flex;
  275. align-items: center;
  276. justify-content: space-between;
  277. padding: 0 20rpx;
  278. background: #FFFFFF;
  279. border-bottom: 2rpx solid #E0E0E0;
  280. z-index: 999;
  281. }
  282. .navbar-left,
  283. .navbar-right {
  284. width: 80rpx;
  285. }
  286. .back-icon {
  287. font-size: 40rpx;
  288. color: #333;
  289. font-weight: bold;
  290. }
  291. .navbar-title {
  292. flex: 1;
  293. text-align: center;
  294. font-size: 32rpx;
  295. font-weight: bold;
  296. color: #333;
  297. }
  298. /* 页面内容 */
  299. .page-content {
  300. height: calc(100vh - 90rpx);
  301. padding: 20rpx;
  302. }
  303. .dynamic-list {
  304. display: flex;
  305. flex-direction: column;
  306. gap: 20rpx;
  307. }
  308. .dynamic-card {
  309. background: #FFFFFF;
  310. border-radius: 16rpx;
  311. padding: 24rpx;
  312. border: 2rpx solid #E0E0E0;
  313. cursor: pointer;
  314. transition: background-color 0.2s;
  315. position: relative;
  316. z-index: 1;
  317. -webkit-tap-highlight-color: transparent;
  318. }
  319. .dynamic-card:active {
  320. background: #F8F9FA;
  321. opacity: 0.8;
  322. }
  323. .dynamic-content {
  324. margin-bottom: 20rpx;
  325. }
  326. .dynamic-text {
  327. font-size: 28rpx;
  328. color: #333;
  329. line-height: 1.8;
  330. }
  331. .dynamic-media {
  332. margin-bottom: 20rpx;
  333. }
  334. .media-grid {
  335. display: grid;
  336. gap: 12rpx;
  337. }
  338. .grid-1 {
  339. grid-template-columns: 1fr;
  340. }
  341. .grid-2 {
  342. grid-template-columns: repeat(2, 1fr);
  343. }
  344. .grid-3 {
  345. grid-template-columns: repeat(3, 1fr);
  346. }
  347. .media-image {
  348. width: 100%;
  349. height: 300rpx;
  350. border-radius: 12rpx;
  351. background: #F5F5F5;
  352. pointer-events: none;
  353. -webkit-user-select: none;
  354. user-select: none;
  355. }
  356. .dynamic-info {
  357. display: flex;
  358. justify-content: space-between;
  359. align-items: center;
  360. padding-top: 16rpx;
  361. border-top: 1rpx solid #F0F0F0;
  362. }
  363. .dynamic-time {
  364. font-size: 24rpx;
  365. color: #999;
  366. }
  367. .dynamic-stats {
  368. display: flex;
  369. gap: 24rpx;
  370. }
  371. .stat-item {
  372. font-size: 24rpx;
  373. color: #666;
  374. }
  375. .comment-stat {
  376. cursor: pointer;
  377. transition: opacity 0.2s;
  378. }
  379. .comment-stat:active {
  380. opacity: 0.6;
  381. }
  382. .empty-container {
  383. display: flex;
  384. flex-direction: column;
  385. align-items: center;
  386. justify-content: center;
  387. padding: 120rpx 40rpx;
  388. }
  389. .empty-icon {
  390. font-size: 120rpx;
  391. margin-bottom: 30rpx;
  392. }
  393. .empty-text {
  394. font-size: 28rpx;
  395. color: #999;
  396. }
  397. .loading-container {
  398. display: flex;
  399. align-items: center;
  400. justify-content: center;
  401. padding: 100rpx;
  402. font-size: 28rpx;
  403. color: #999;
  404. }
  405. .loading-tip {
  406. text-align: center;
  407. padding: 40rpx;
  408. color: #999;
  409. font-size: 26rpx;
  410. }
  411. </style>