my-dynamics.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. <template>
  2. <view class="my-dynamics-page">
  3. <!-- 顶部导航栏 -->
  4. <view class="top-nav">
  5. <view class="back-btn" @click="goBack">
  6. <text class="back-icon">‹</text>
  7. </view>
  8. <view class="nav-title">我的动态</view>
  9. <view class="nav-right"></view>
  10. </view>
  11. <!-- 用户信息区域 -->
  12. <view class="user-info-section">
  13. <image class="avatar" :src="userInfo.avatar" mode="aspectFill"></image>
  14. <view class="user-details">
  15. <text class="nickname">{{ userInfo.nickname }}</text>
  16. <text class="dynamic-count">{{ userInfo.dynamicCount }} 动态</text>
  17. </view>
  18. </view>
  19. <!-- 标签页导航 -->
  20. <view class="tab-nav">
  21. <view
  22. class="tab-item"
  23. :class="{ active: activeTab === 'dynamic' }"
  24. @click="switchTab('dynamic')"
  25. >
  26. <text>动态</text>
  27. </view>
  28. <view
  29. class="tab-item"
  30. :class="{ active: activeTab === 'interaction' }"
  31. @click="switchTab('interaction')"
  32. >
  33. <text>互动</text>
  34. </view>
  35. <view
  36. class="tab-item"
  37. :class="{ active: activeTab === 'browse' }"
  38. @click="switchTab('browse')"
  39. >
  40. <text>浏览记录</text>
  41. </view>
  42. </view>
  43. <!-- 互动子标签 -->
  44. <view class="sub-tab-nav" v-if="activeTab === 'interaction'">
  45. <view
  46. class="sub-tab-item"
  47. :class="{ active: activeSubTab === 'like' }"
  48. @click="switchSubTab('like')"
  49. >
  50. <text>点赞</text>
  51. </view>
  52. <view
  53. class="sub-tab-item"
  54. :class="{ active: activeSubTab === 'collect' }"
  55. @click="switchSubTab('collect')"
  56. >
  57. <text>收藏</text>
  58. </view>
  59. </view>
  60. <!-- 内容区域 -->
  61. <view class="content-area">
  62. <!-- 动态列表 -->
  63. <view v-if="activeTab === 'dynamic'" class="dynamic-list">
  64. <view class="empty-tip" v-if="dynamicList.length === 0">
  65. <text>暂无动态</text>
  66. </view>
  67. <view class="dynamic-item" v-for="item in dynamicList" :key="item.dynamicId" @click="goToDetail(item.dynamicId)">
  68. <!-- 动态内容 -->
  69. <view class="dynamic-content">
  70. <text class="dynamic-text">{{ item.content }}</text>
  71. </view>
  72. <!-- 动态图片 -->
  73. <view class="dynamic-images" v-if="item.mediaUrls">
  74. <image
  75. class="dynamic-image"
  76. v-for="(img, index) in getMediaUrls(item.mediaUrls)"
  77. :key="index"
  78. :src="img"
  79. mode="aspectFill"
  80. @click.stop="previewImage(getMediaUrls(item.mediaUrls), index)"
  81. ></image>
  82. </view>
  83. <!-- 动态信息 -->
  84. <view class="dynamic-info">
  85. <text class="dynamic-time">{{ formatTime(item.createdAt) }}</text>
  86. <view class="dynamic-stats">
  87. <text class="stat-item">{{ item.likeCount || 0 }} 赞</text>
  88. <text class="stat-item">{{ item.commentCount || 0 }} 评论</text>
  89. </view>
  90. </view>
  91. </view>
  92. </view>
  93. <!-- 互动列表 -->
  94. <view v-else-if="activeTab === 'interaction'" class="interaction-list">
  95. <view class="empty-tip" v-if="interactionList.length === 0">
  96. <text>暂无互动</text>
  97. </view>
  98. <view class="interaction-item" v-for="item in interactionList" :key="item.dynamicId" @click="goToDetail(item.dynamicId)">
  99. <!-- 用户信息 -->
  100. <view class="user-info">
  101. <image class="avatar" :src="getAvatar(item)" mode="aspectFill"></image>
  102. <view class="user-details">
  103. <text class="nickname">{{ getNickname(item) }}</text>
  104. </view>
  105. </view>
  106. <!-- 互动内容 -->
  107. <view class="interaction-content">
  108. <text class="interaction-text">{{ item.content }}</text>
  109. </view>
  110. <!-- 互动图片 -->
  111. <view class="interaction-images" v-if="item.mediaUrls">
  112. <image
  113. class="interaction-image"
  114. v-for="(img, index) in getMediaUrls(item.mediaUrls)"
  115. :key="index"
  116. :src="img"
  117. mode="aspectFill"
  118. @click.stop="previewImage(getMediaUrls(item.mediaUrls), index)"
  119. ></image>
  120. </view>
  121. <!-- 互动信息 -->
  122. <view class="interaction-info">
  123. <text class="interaction-time">{{ formatTime(item.createdAt) }}</text>
  124. <view class="interaction-stats">
  125. <text class="stat-item">{{ item.likeCount || 0 }} 赞</text>
  126. <text class="stat-item">{{ item.commentCount || 0 }} 评论</text>
  127. </view>
  128. </view>
  129. </view>
  130. </view>
  131. <!-- 浏览记录列表 -->
  132. <view v-else-if="activeTab === 'browse'" class="browse-list">
  133. <view class="empty-tip" v-if="browseList.length === 0">
  134. <text>暂无浏览记录</text>
  135. </view>
  136. <view class="browse-item" v-for="item in browseList" :key="item.dynamicId" @click="goToDetail(item.dynamicId)">
  137. <!-- 用户信息 -->
  138. <view class="user-info">
  139. <image class="avatar" :src="getAvatar(item)" mode="aspectFill"></image>
  140. <view class="user-details">
  141. <text class="nickname">{{ getNickname(item) }}</text>
  142. </view>
  143. </view>
  144. <!-- 浏览内容 -->
  145. <view class="browse-content">
  146. <text class="browse-text">{{ item.content }}</text>
  147. </view>
  148. <!-- 浏览图片 -->
  149. <view class="browse-images" v-if="item.mediaUrls">
  150. <image
  151. class="browse-image"
  152. v-for="(img, index) in getMediaUrls(item.mediaUrls)"
  153. :key="index"
  154. :src="img"
  155. mode="aspectFill"
  156. @click.stop="previewImage(getMediaUrls(item.mediaUrls), index)"
  157. ></image>
  158. </view>
  159. <!-- 浏览信息 -->
  160. <view class="browse-info">
  161. <text class="browse-time">{{ formatTime(item.createdAt) }}</text>
  162. <view class="browse-stats">
  163. <text class="stat-item">{{ item.likeCount || 0 }} 赞</text>
  164. <text class="stat-item">{{ item.commentCount || 0 }} 评论</text>
  165. </view>
  166. </view>
  167. </view>
  168. </view>
  169. </view>
  170. <!-- 发布动态按钮 -->
  171. <view class="publish-btn" @click="goPublish">
  172. <text class="publish-icon">+</text>
  173. </view>
  174. </view>
  175. </template>
  176. <script>
  177. import api from '../../utils/api.js'
  178. export default {
  179. data() {
  180. return {
  181. // 用户信息
  182. userInfo: {
  183. avatar: 'https://via.placeholder.com/100',
  184. nickname: '用户',
  185. dynamicCount: 0
  186. },
  187. // 活跃标签页
  188. activeTab: 'dynamic',
  189. // 活跃子标签(互动下的点赞/收藏)
  190. activeSubTab: 'like',
  191. // 动态列表数据
  192. dynamicList: [],
  193. // 互动列表数据
  194. interactionList: [],
  195. // 浏览记录列表数据
  196. browseList: [],
  197. // 默认头像
  198. defaultAvatar: 'https://via.placeholder.com/100x100.png?text=头像'
  199. }
  200. },
  201. onLoad() {
  202. // 页面加载时获取用户信息和动态数据
  203. this.loadUserInfo();
  204. this.loadDynamicData();
  205. },
  206. methods: {
  207. // 返回上一页
  208. goBack() {
  209. uni.navigateBack();
  210. },
  211. // 获取头像
  212. getAvatar(item) {
  213. if (item && item.user && item.user.avatarUrl) {
  214. return item.user.avatarUrl;
  215. }
  216. return this.defaultAvatar;
  217. },
  218. // 获取昵称
  219. getNickname(item) {
  220. if (item && item.user && item.user.nickname) {
  221. return item.user.nickname;
  222. }
  223. return '匿名用户';
  224. },
  225. // 切换标签页
  226. switchTab(tab) {
  227. this.activeTab = tab;
  228. // 根据标签页加载对应数据
  229. if (tab === 'dynamic') {
  230. this.loadDynamicData();
  231. } else if (tab === 'interaction') {
  232. this.loadInteractionData();
  233. } else if (tab === 'browse') {
  234. this.loadBrowseData();
  235. }
  236. },
  237. // 切换互动子标签
  238. switchSubTab(subTab) {
  239. this.activeSubTab = subTab;
  240. // 根据子标签加载对应数据
  241. this.loadInteractionData();
  242. },
  243. // 加载用户信息
  244. loadUserInfo() {
  245. // 从本地存储获取用户信息
  246. const userInfo = uni.getStorageSync('userInfo');
  247. if (userInfo) {
  248. this.userInfo = {
  249. avatar: userInfo.avatarUrl || userInfo.avatar || 'https://via.placeholder.com/100',
  250. nickname: userInfo.nickname || '用户',
  251. dynamicCount: 0 // 后续从接口获取
  252. };
  253. // 加载用户动态数量
  254. this.loadUserDynamicCount();
  255. }
  256. },
  257. // 加载用户动态数量
  258. loadUserDynamicCount() {
  259. const userInfo = uni.getStorageSync('userInfo');
  260. if (userInfo && userInfo.userId) {
  261. // 获取用户动态数量
  262. api.dynamic.getUserDynamics(userInfo.userId, { pageNum: 1, pageSize: 1 }).then(res => {
  263. if (res && res.total) {
  264. this.userInfo.dynamicCount = res.total;
  265. }
  266. }).catch(err => {
  267. console.error('获取用户动态数量失败:', err);
  268. });
  269. }
  270. },
  271. // 加载动态数据
  272. loadDynamicData() {
  273. const userInfo = uni.getStorageSync('userInfo');
  274. if (userInfo && userInfo.userId) {
  275. // 获取用户发布的动态列表
  276. api.dynamic.getUserDynamics(userInfo.userId, {
  277. pageNum: 1,
  278. pageSize: 10
  279. }).then(res => {
  280. if (res && res.records) {
  281. this.dynamicList = res.records;
  282. }
  283. }).catch(err => {
  284. console.error('获取用户动态列表失败:', err);
  285. });
  286. }
  287. },
  288. // 加载互动数据
  289. loadInteractionData() {
  290. const userInfo = uni.getStorageSync('userInfo');
  291. if (userInfo && userInfo.userId) {
  292. if (this.activeSubTab === 'like') {
  293. // 获取用户点赞的动态列表
  294. api.dynamic.getLikedList(userInfo.userId, 1, 10).then(res => {
  295. if (res && res.records) {
  296. this.interactionList = res.records;
  297. }
  298. }).catch(err => {
  299. console.error('获取用户点赞列表失败:', err);
  300. });
  301. } else if (this.activeSubTab === 'collect') {
  302. // 获取用户收藏的动态列表
  303. api.dynamic.getFavoritesList(userInfo.userId, 1, 10).then(res => {
  304. if (res && res.records) {
  305. this.interactionList = res.records;
  306. }
  307. }).catch(err => {
  308. console.error('获取用户收藏列表失败:', err);
  309. });
  310. }
  311. }
  312. },
  313. // 加载浏览记录数据
  314. loadBrowseData() {
  315. const userInfo = uni.getStorageSync('userInfo');
  316. if (userInfo && userInfo.userId) {
  317. // 获取用户浏览记录列表
  318. api.dynamic.getBrowseHistoryList(userInfo.userId, 1, 10).then(res => {
  319. if (res && res.records) {
  320. this.browseList = res.records;
  321. }
  322. }).catch(err => {
  323. console.error('获取用户浏览记录失败:', err);
  324. });
  325. }
  326. },
  327. // 跳转到发布动态页面
  328. goPublish() {
  329. uni.navigateTo({
  330. url: '/pages/plaza/publish'
  331. });
  332. },
  333. // 跳转到动态详情页
  334. goToDetail(dynamicId) {
  335. uni.navigateTo({
  336. url: `/pages/plaza/detail?id=${dynamicId}`
  337. });
  338. },
  339. // 格式化时间
  340. formatTime(timeStr) {
  341. if (!timeStr) return '';
  342. const date = new Date(timeStr);
  343. const now = new Date();
  344. const diff = now - date;
  345. const minute = 60 * 1000;
  346. const hour = minute * 60;
  347. const day = hour * 24;
  348. const month = day * 30;
  349. const year = day * 365;
  350. if (diff < minute) {
  351. return '刚刚';
  352. } else if (diff < hour) {
  353. return Math.floor(diff / minute) + '分钟前';
  354. } else if (diff < day) {
  355. return Math.floor(diff / hour) + '小时前';
  356. } else if (diff < month) {
  357. return Math.floor(diff / day) + '天前';
  358. } else if (diff < year) {
  359. return Math.floor(diff / month) + '个月前';
  360. } else {
  361. return Math.floor(diff / year) + '年前';
  362. }
  363. },
  364. // 处理媒体URL,支持数组、JSON字符串、逗号分隔等多种格式
  365. getMediaUrls(mediaUrls) {
  366. if (!mediaUrls) return [];
  367. const isLikelyImage = (u) => {
  368. if (typeof u !== 'string' || !u.trim()) return false;
  369. const url = u.trim();
  370. const hasExt = /(\.png|\.jpg|\.jpeg|\.gif|\.webp|\.bmp)(\?|$)/i.test(url);
  371. const isHttp = /^https?:\/\//i.test(url);
  372. return hasExt || isHttp;
  373. };
  374. try {
  375. if (Array.isArray(mediaUrls)) {
  376. return mediaUrls.filter(isLikelyImage);
  377. }
  378. if (typeof mediaUrls === 'string') {
  379. const s = mediaUrls.trim();
  380. // JSON数组字符串: ["url1", "url2"]
  381. if (s.startsWith('[')) {
  382. const arr = JSON.parse(s);
  383. return Array.isArray(arr) ? arr.filter(isLikelyImage) : [];
  384. }
  385. // 逗号分隔或带引号
  386. return s.split(',')
  387. .map(x => x.trim().replace(/^\[|\]$/g, '').replace(/^['"]|['"]$/g, ''))
  388. .filter(isLikelyImage);
  389. }
  390. } catch (e) {
  391. // ignore parse error
  392. console.error('解析媒体URL失败:', e);
  393. }
  394. return [];
  395. },
  396. // 预览图片
  397. previewImage(urls, current) {
  398. uni.previewImage({
  399. urls: urls,
  400. current: current,
  401. longPressActions: {
  402. itemList: ['保存图片'],
  403. success: function(data) {
  404. console.log('长按图片操作结果:', data);
  405. },
  406. fail: function(err) {
  407. console.error('长按图片操作失败:', err);
  408. }
  409. }
  410. });
  411. }
  412. }
  413. }
  414. </script>
  415. <style lang="scss" scoped>
  416. .my-dynamics-page {
  417. min-height: 100vh;
  418. background: #F5F5F5;
  419. padding-bottom: 100rpx;
  420. }
  421. /* 顶部导航栏 */
  422. .top-nav {
  423. display: flex;
  424. align-items: center;
  425. justify-content: space-between;
  426. padding: 40rpx 30rpx 20rpx;
  427. background: #FFFFFF;
  428. position: relative;
  429. z-index: 10;
  430. .back-btn {
  431. width: 60rpx;
  432. height: 60rpx;
  433. display: flex;
  434. align-items: center;
  435. justify-content: center;
  436. .back-icon {
  437. font-size: 48rpx;
  438. color: #333333;
  439. font-weight: bold;
  440. }
  441. }
  442. .nav-title {
  443. font-size: 36rpx;
  444. font-weight: bold;
  445. color: #333333;
  446. }
  447. .nav-right {
  448. width: 60rpx;
  449. }
  450. }
  451. /* 用户信息区域 */
  452. .user-info-section {
  453. display: flex;
  454. align-items: center;
  455. padding: 30rpx;
  456. background: #FFFFFF;
  457. margin-bottom: 20rpx;
  458. .avatar {
  459. width: 120rpx;
  460. height: 120rpx;
  461. border-radius: 60rpx;
  462. margin-right: 20rpx;
  463. background-color: #E0E0E0;
  464. }
  465. .user-details {
  466. display: flex;
  467. flex-direction: column;
  468. justify-content: center;
  469. .nickname {
  470. font-size: 32rpx;
  471. font-weight: bold;
  472. color: #333333;
  473. margin-bottom: 8rpx;
  474. }
  475. .dynamic-count {
  476. font-size: 24rpx;
  477. color: #999999;
  478. }
  479. }
  480. }
  481. /* 标签页导航 */
  482. .tab-nav {
  483. display: flex;
  484. background: #FFFFFF;
  485. border-bottom: 2rpx solid #F0F0F0;
  486. margin-bottom: 20rpx;
  487. .tab-item {
  488. flex: 1;
  489. text-align: center;
  490. padding: 30rpx 0;
  491. position: relative;
  492. text {
  493. font-size: 30rpx;
  494. color: #666666;
  495. }
  496. &.active {
  497. text {
  498. color: #E91E63;
  499. font-weight: bold;
  500. }
  501. &::after {
  502. content: '';
  503. position: absolute;
  504. bottom: 0;
  505. left: 50%;
  506. transform: translateX(-50%);
  507. width: 60rpx;
  508. height: 6rpx;
  509. background: #E91E63;
  510. border-radius: 3rpx;
  511. }
  512. }
  513. }
  514. }
  515. /* 互动子标签导航 */
  516. .sub-tab-nav {
  517. display: flex;
  518. background: #FFFFFF;
  519. padding: 0 30rpx;
  520. margin-bottom: 20rpx;
  521. .sub-tab-item {
  522. padding: 20rpx 30rpx;
  523. margin-right: 40rpx;
  524. text {
  525. font-size: 26rpx;
  526. color: #999999;
  527. }
  528. &.active {
  529. text {
  530. color: #E91E63;
  531. font-weight: bold;
  532. }
  533. }
  534. }
  535. }
  536. /* 内容区域 */
  537. .content-area {
  538. padding: 0 30rpx;
  539. }
  540. /* 列表通用样式 */
  541. .dynamic-list,
  542. .interaction-list,
  543. .browse-list {
  544. background: #FFFFFF;
  545. border-radius: 12rpx;
  546. overflow: hidden;
  547. }
  548. .dynamic-item,
  549. .interaction-item,
  550. .browse-item {
  551. padding: 30rpx;
  552. border-bottom: 2rpx solid #F5F5F5;
  553. &:last-child {
  554. border-bottom: none;
  555. }
  556. }
  557. /* 用户信息 */
  558. .user-info {
  559. display: flex;
  560. align-items: center;
  561. margin-bottom: 20rpx;
  562. .avatar {
  563. width: 80rpx;
  564. height: 80rpx;
  565. border-radius: 40rpx;
  566. margin-right: 20rpx;
  567. background-color: #E0E0E0;
  568. }
  569. .user-details {
  570. display: flex;
  571. flex-direction: column;
  572. .nickname {
  573. font-size: 28rpx;
  574. font-weight: bold;
  575. color: #333333;
  576. margin-bottom: 4rpx;
  577. }
  578. }
  579. }
  580. /* 内容文本 */
  581. .dynamic-content,
  582. .interaction-content,
  583. .browse-content {
  584. margin-bottom: 20rpx;
  585. .dynamic-text,
  586. .interaction-text,
  587. .browse-text {
  588. font-size: 28rpx;
  589. color: #333333;
  590. line-height: 44rpx;
  591. }
  592. }
  593. /* 图片列表 */
  594. .dynamic-images,
  595. .interaction-images,
  596. .browse-images {
  597. display: flex;
  598. flex-wrap: wrap;
  599. gap: 15rpx;
  600. margin-bottom: 20rpx;
  601. .dynamic-image,
  602. .interaction-image,
  603. .browse-image {
  604. width: calc((100% - 30rpx) / 3);
  605. height: 200rpx;
  606. border-radius: 8rpx;
  607. background-color: #E0E0E0;
  608. }
  609. }
  610. /* 信息栏 */
  611. .dynamic-info,
  612. .interaction-info,
  613. .browse-info {
  614. display: flex;
  615. align-items: center;
  616. justify-content: space-between;
  617. .dynamic-time,
  618. .interaction-time,
  619. .browse-time {
  620. font-size: 22rpx;
  621. color: #999999;
  622. }
  623. .dynamic-stats,
  624. .interaction-stats,
  625. .browse-stats {
  626. display: flex;
  627. align-items: center;
  628. .stat-item {
  629. font-size: 22rpx;
  630. color: #999999;
  631. margin-left: 30rpx;
  632. }
  633. }
  634. }
  635. /* 空状态提示 */
  636. .empty-tip {
  637. padding: 100rpx 0;
  638. text-align: center;
  639. text {
  640. font-size: 28rpx;
  641. color: #999999;
  642. }
  643. }
  644. /* 发布动态按钮 */
  645. .publish-btn {
  646. position: fixed;
  647. bottom: 80rpx;
  648. right: 40rpx;
  649. width: 100rpx;
  650. height: 100rpx;
  651. background: #E91E63;
  652. border-radius: 50rpx;
  653. display: flex;
  654. align-items: center;
  655. justify-content: center;
  656. box-shadow: 0 8rpx 20rpx rgba(233, 30, 99, 0.3);
  657. z-index: 999;
  658. .publish-icon {
  659. font-size: 60rpx;
  660. color: #FFFFFF;
  661. font-weight: bold;
  662. line-height: 1;
  663. }
  664. }
  665. /* 页面背景色 */
  666. .my-dynamics-page {
  667. background-color: #F5F5F5;
  668. }
  669. </style>