user-dynamics.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. console.log('用户动态API返回:', result)
  117. // 处理PageResult格式:{ records: [], total: 0, current: 1, size: 10 }
  118. let list = []
  119. if (result) {
  120. // PageResult格式:{ records: [], total: 0 }
  121. if (result.records && Array.isArray(result.records)) {
  122. list = result.records
  123. }
  124. // 兼容格式:{ list: [], total: 0 }
  125. else if (result.list && Array.isArray(result.list)) {
  126. list = result.list
  127. }
  128. // 直接是数组
  129. else if (Array.isArray(result)) {
  130. list = result
  131. }
  132. }
  133. // 处理mediaUrls字段(可能是字符串需要解析)
  134. list = list.map(item => {
  135. // 确保 dynamicId 存在
  136. if (!item.dynamicId && item.id) {
  137. item.dynamicId = item.id
  138. }
  139. if (!item.dynamicId && item.dynamic_id) {
  140. item.dynamicId = item.dynamic_id
  141. }
  142. if (item.mediaUrls && typeof item.mediaUrls === 'string') {
  143. try {
  144. item.mediaUrls = JSON.parse(item.mediaUrls)
  145. } catch (e) {
  146. console.error('解析mediaUrls失败:', e)
  147. item.mediaUrls = []
  148. }
  149. }
  150. if (!item.mediaUrls || !Array.isArray(item.mediaUrls)) {
  151. item.mediaUrls = []
  152. }
  153. return item
  154. })
  155. this.noMore = list.length < this.pageSize
  156. if (isRefresh) {
  157. this.dynamicList = list
  158. } else {
  159. this.dynamicList = [...this.dynamicList, ...list]
  160. }
  161. if (!this.noMore) {
  162. this.pageNum++
  163. }
  164. } catch (e) {
  165. console.error('获取用户动态失败:', e)
  166. uni.showToast({
  167. title: '加载失败',
  168. icon: 'none'
  169. })
  170. } finally {
  171. this.loading = false
  172. this.refreshing = false
  173. }
  174. },
  175. // 加载更多
  176. loadMore() {
  177. if (!this.noMore && !this.loading) {
  178. this.loadDynamics()
  179. }
  180. },
  181. // 下拉刷新
  182. onRefresh() {
  183. this.refreshing = true
  184. this.loadDynamics(true)
  185. },
  186. // 查看动态详情
  187. viewDynamicDetail(item, index) {
  188. console.log('=== 点击动态详情 ===')
  189. console.log('传入的 item:', item)
  190. console.log('传入的 index:', index)
  191. console.log('dynamicList:', this.dynamicList)
  192. // 如果 item 无效,尝试从列表中根据索引获取
  193. if (!item && typeof index === 'number' && this.dynamicList && this.dynamicList[index]) {
  194. item = this.dynamicList[index]
  195. console.log('从列表中获取 item:', item)
  196. }
  197. if (!item) {
  198. console.error('动态项为空,无法获取')
  199. uni.showToast({
  200. title: '动态信息为空',
  201. icon: 'none'
  202. })
  203. return
  204. }
  205. // 尝试多种可能的字段名
  206. const dynamicId = item.dynamicId || item.id || item.dynamic_id
  207. console.log('提取的 dynamicId:', dynamicId)
  208. if (!dynamicId) {
  209. console.error('动态ID无效,item 内容:', item)
  210. uni.showToast({
  211. title: '动态ID无效',
  212. icon: 'none'
  213. })
  214. return
  215. }
  216. const url = `/pages/plaza/detail?dynamicId=${dynamicId}`
  217. console.log('准备跳转, url:', url)
  218. uni.navigateTo({
  219. url: url,
  220. success: (res) => {
  221. console.log('跳转成功:', res)
  222. },
  223. fail: (err) => {
  224. console.error('跳转失败:', err)
  225. uni.showToast({
  226. title: '跳转失败: ' + (err.errMsg || '未知错误'),
  227. icon: 'none',
  228. duration: 2000
  229. })
  230. }
  231. })
  232. },
  233. // 预览媒体
  234. previewMedia(urls, index) {
  235. if (!urls || urls.length === 0) return
  236. uni.previewImage({
  237. urls: urls,
  238. current: index
  239. })
  240. },
  241. // 获取网格类名
  242. getGridClass(item) {
  243. const count = item.mediaUrls ? item.mediaUrls.length : 0
  244. if (count === 1) return 'grid-1'
  245. if (count === 2 || count === 4) return 'grid-2'
  246. return 'grid-3'
  247. },
  248. // 格式化时间
  249. formatTime(timeStr) {
  250. if (!timeStr) return ''
  251. const date = new Date(timeStr)
  252. const now = new Date()
  253. const diff = now - date
  254. const minutes = Math.floor(diff / 60000)
  255. const hours = Math.floor(diff / 3600000)
  256. const days = Math.floor(diff / 86400000)
  257. if (minutes < 1) return '刚刚'
  258. if (minutes < 60) return `${minutes}分钟前`
  259. if (hours < 24) return `${hours}小时前`
  260. if (days < 7) return `${days}天前`
  261. return date.toLocaleDateString()
  262. },
  263. // 返回
  264. goBack() {
  265. uni.navigateBack()
  266. }
  267. }
  268. }
  269. </script>
  270. <style lang="scss" scoped>
  271. .user-dynamics-page {
  272. min-height: 100vh;
  273. background: #F5F5F5;
  274. padding-top: 90rpx;
  275. }
  276. /* 自定义导航栏 */
  277. .custom-navbar {
  278. position: fixed;
  279. top: 0;
  280. left: 0;
  281. right: 0;
  282. height: 90rpx;
  283. display: flex;
  284. align-items: center;
  285. justify-content: space-between;
  286. padding: 0 20rpx;
  287. background: #FFFFFF;
  288. border-bottom: 2rpx solid #E0E0E0;
  289. z-index: 999;
  290. }
  291. .navbar-left,
  292. .navbar-right {
  293. width: 80rpx;
  294. }
  295. .back-icon {
  296. font-size: 40rpx;
  297. color: #333;
  298. font-weight: bold;
  299. }
  300. .navbar-title {
  301. flex: 1;
  302. text-align: center;
  303. font-size: 32rpx;
  304. font-weight: bold;
  305. color: #333;
  306. }
  307. /* 页面内容 */
  308. .page-content {
  309. height: calc(100vh - 90rpx);
  310. padding: 20rpx;
  311. }
  312. .dynamic-list {
  313. display: flex;
  314. flex-direction: column;
  315. gap: 20rpx;
  316. }
  317. .dynamic-card {
  318. background: #FFFFFF;
  319. border-radius: 16rpx;
  320. padding: 24rpx;
  321. border: 2rpx solid #E0E0E0;
  322. cursor: pointer;
  323. transition: background-color 0.2s;
  324. position: relative;
  325. z-index: 1;
  326. -webkit-tap-highlight-color: transparent;
  327. }
  328. .dynamic-card:active {
  329. background: #F8F9FA;
  330. opacity: 0.8;
  331. }
  332. .dynamic-content {
  333. margin-bottom: 20rpx;
  334. }
  335. .dynamic-text {
  336. font-size: 28rpx;
  337. color: #333;
  338. line-height: 1.8;
  339. }
  340. .dynamic-media {
  341. margin-bottom: 20rpx;
  342. }
  343. .media-grid {
  344. display: grid;
  345. gap: 12rpx;
  346. }
  347. .grid-1 {
  348. grid-template-columns: 1fr;
  349. }
  350. .grid-2 {
  351. grid-template-columns: repeat(2, 1fr);
  352. }
  353. .grid-3 {
  354. grid-template-columns: repeat(3, 1fr);
  355. }
  356. .media-image {
  357. width: 100%;
  358. height: 300rpx;
  359. border-radius: 12rpx;
  360. background: #F5F5F5;
  361. pointer-events: none;
  362. -webkit-user-select: none;
  363. user-select: none;
  364. }
  365. .dynamic-info {
  366. display: flex;
  367. justify-content: space-between;
  368. align-items: center;
  369. padding-top: 16rpx;
  370. border-top: 1rpx solid #F0F0F0;
  371. }
  372. .dynamic-time {
  373. font-size: 24rpx;
  374. color: #999;
  375. }
  376. .dynamic-stats {
  377. display: flex;
  378. gap: 24rpx;
  379. }
  380. .stat-item {
  381. font-size: 24rpx;
  382. color: #666;
  383. }
  384. .comment-stat {
  385. cursor: pointer;
  386. transition: opacity 0.2s;
  387. }
  388. .comment-stat:active {
  389. opacity: 0.6;
  390. }
  391. .empty-container {
  392. display: flex;
  393. flex-direction: column;
  394. align-items: center;
  395. justify-content: center;
  396. padding: 120rpx 40rpx;
  397. }
  398. .empty-icon {
  399. font-size: 120rpx;
  400. margin-bottom: 30rpx;
  401. }
  402. .empty-text {
  403. font-size: 28rpx;
  404. color: #999;
  405. }
  406. .loading-container {
  407. display: flex;
  408. align-items: center;
  409. justify-content: center;
  410. padding: 100rpx;
  411. font-size: 28rpx;
  412. color: #999;
  413. }
  414. .loading-tip {
  415. text-align: center;
  416. padding: 40rpx;
  417. color: #999;
  418. font-size: 26rpx;
  419. }
  420. </style>