user-detail.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. <template>
  2. <view class="user-detail-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 scroll-y class="page-content" v-if="userInfo">
  12. <!-- 用户头像和基本信息 -->
  13. <view class="user-header">
  14. <image
  15. :src="userInfo.avatar || userInfo.avatarUrl || '/static/close.png'"
  16. class="user-avatar"
  17. mode="aspectFill"
  18. @error="onAvatarError"
  19. />
  20. <view class="user-basic">
  21. <view class="user-name-row">
  22. <text class="user-name">{{ userInfo.nickname || '未设置' }}</text>
  23. <view class="score-badge" v-if="compatibilityScore">
  24. <text>匹配度 {{ fmtScore(compatibilityScore) }}</text>
  25. </view>
  26. </view>
  27. <view class="user-meta">
  28. <text v-if="userInfo.genderText">{{ userInfo.genderText }}</text>
  29. <text v-if="userInfo.age">{{ userInfo.age }}岁</text>
  30. <text v-if="userInfo.height">{{ userInfo.height }}cm</text>
  31. <text v-if="userInfo.educationText">{{ userInfo.educationText }}</text>
  32. <text v-if="userInfo.salaryText">{{ userInfo.salaryText }}</text>
  33. </view>
  34. <view class="user-tags" v-if="hasTags">
  35. <text v-if="userInfo.star" class="tag">{{ userInfo.star }}</text>
  36. <text v-if="userInfo.animal" class="tag">{{ userInfo.animal }}</text>
  37. <text v-if="userInfo.jobTitle" class="tag">{{ userInfo.jobTitle }}</text>
  38. <text v-for="t in parseHobby(userInfo.hobby)" :key="t" class="tag">{{ t }}</text>
  39. </view>
  40. </view>
  41. </view>
  42. <!-- 详细信息 -->
  43. <view class="detail-section">
  44. <view class="section-title">详细信息</view>
  45. <view class="detail-grid">
  46. <view class="detail-item" v-if="userInfo.schoolName">
  47. <text class="detail-label">毕业院校</text>
  48. <text class="detail-value">{{ userInfo.schoolName }}</text>
  49. </view>
  50. <view class="detail-item" v-if="userInfo.company">
  51. <text class="detail-label">工作单位</text>
  52. <text class="detail-value">{{ userInfo.company }}</text>
  53. </view>
  54. <view class="detail-item" v-if="userInfo.maritalText">
  55. <text class="detail-label">婚姻状况</text>
  56. <text class="detail-value">{{ userInfo.maritalText }}</text>
  57. </view>
  58. <view class="detail-item" v-if="userInfo.houseText">
  59. <text class="detail-label">房产</text>
  60. <text class="detail-value">{{ userInfo.houseText }}</text>
  61. </view>
  62. <view class="detail-item" v-if="userInfo.carText">
  63. <text class="detail-label">车产</text>
  64. <text class="detail-value">{{ userInfo.carText }}</text>
  65. </view>
  66. <view class="detail-item" v-if="userInfo.weight">
  67. <text class="detail-label">体重</text>
  68. <text class="detail-value">{{ userInfo.weight }}kg</text>
  69. </view>
  70. </view>
  71. </view>
  72. <!-- 自我简介 -->
  73. <view class="detail-section" v-if="userInfo.introduction">
  74. <view class="section-title">自我简介</view>
  75. <view class="introduction-content">
  76. <text>{{ userInfo.introduction }}</text>
  77. </view>
  78. </view>
  79. <!-- 动态区域 -->
  80. <view class="dynamic-section">
  81. <view class="section-header">
  82. <text class="section-title">动态</text>
  83. <text class="section-more" @click="viewAllDynamics" v-if="dynamicList.length > 0">
  84. 查看全部({{ totalDynamics }}) >
  85. </text>
  86. </view>
  87. <!-- 动态列表 -->
  88. <view v-if="loadingDynamics" class="loading-tip">
  89. <text>加载动态中...</text>
  90. </view>
  91. <view v-else-if="dynamicList.length === 0" class="empty-dynamics">
  92. <text class="empty-icon">📝</text>
  93. <text class="empty-text">该用户还没有发布动态</text>
  94. </view>
  95. <view v-else class="dynamic-list">
  96. <view
  97. v-for="(item, index) in dynamicList"
  98. :key="item.dynamicId || index"
  99. class="dynamic-item"
  100. @click="viewDynamicDetail(item)"
  101. >
  102. <!-- 动态内容 -->
  103. <view class="dynamic-content" v-if="item.content">
  104. <text class="dynamic-text">{{ item.content }}</text>
  105. </view>
  106. <!-- 动态媒体(照片/视频) -->
  107. <view class="dynamic-media" v-if="item.mediaUrls && item.mediaUrls.length > 0">
  108. <view :class="['media-grid', getGridClass(item)]">
  109. <image
  110. v-for="(url, idx) in item.mediaUrls.slice(0, 9)"
  111. :key="idx"
  112. :src="url"
  113. class="media-image"
  114. mode="aspectFill"
  115. @click.stop="previewMedia(item.mediaUrls, idx)"
  116. />
  117. </view>
  118. </view>
  119. <!-- 动态信息 -->
  120. <view class="dynamic-info">
  121. <text class="dynamic-time">{{ formatTime(item.createdAt) }}</text>
  122. <view class="dynamic-stats">
  123. <text class="stat-item">❤️ {{ item.likeCount || 0 }}</text>
  124. <text class="stat-item comment-stat" @click.stop="viewDynamicComments(item)">💬 {{ item.commentCount || 0 }}</text>
  125. <text class="stat-item">💬 {{ item.commentCount || 0 }}</text>
  126. </view>
  127. </view>
  128. </view>
  129. </view>
  130. </view>
  131. </scroll-view>
  132. <!-- 加载中 -->
  133. <view v-else class="loading-container">
  134. <text>加载中...</text>
  135. </view>
  136. </view>
  137. </template>
  138. <script>
  139. import api from '@/utils/api.js'
  140. export default {
  141. data() {
  142. return {
  143. userId: null,
  144. userInfo: null,
  145. compatibilityScore: null,
  146. dynamicList: [],
  147. loadingDynamics: false,
  148. totalDynamics: 0,
  149. pageNum: 1,
  150. pageSize: 6
  151. }
  152. },
  153. computed: {
  154. hasTags() {
  155. return this.userInfo && (
  156. this.userInfo.star ||
  157. this.userInfo.animal ||
  158. this.userInfo.jobTitle ||
  159. this.userInfo.hobby
  160. )
  161. }
  162. },
  163. onLoad(options) {
  164. if (options.userId) {
  165. this.userId = parseInt(options.userId)
  166. if (options.score) {
  167. this.compatibilityScore = parseFloat(options.score)
  168. }
  169. this.loadUserInfo()
  170. this.loadUserDynamics()
  171. } else {
  172. uni.showToast({
  173. title: '用户ID无效',
  174. icon: 'none'
  175. })
  176. setTimeout(() => {
  177. uni.navigateBack()
  178. }, 1500)
  179. }
  180. },
  181. methods: {
  182. // 加载用户信息
  183. async loadUserInfo() {
  184. try {
  185. uni.showLoading({ title: '加载中...' })
  186. const detail = await api.user.getDetailInfo(this.userId)
  187. if (detail) {
  188. // 确保photos是数组
  189. if (!detail.photos || !Array.isArray(detail.photos)) {
  190. detail.photos = []
  191. if (detail.avatar) {
  192. detail.photos.push(detail.avatar)
  193. }
  194. }
  195. this.userInfo = detail
  196. } else {
  197. uni.showToast({
  198. title: '用户信息不存在',
  199. icon: 'none'
  200. })
  201. setTimeout(() => {
  202. uni.navigateBack()
  203. }, 1500)
  204. }
  205. } catch (e) {
  206. console.error('获取用户信息失败:', e)
  207. uni.showToast({
  208. title: '获取用户信息失败',
  209. icon: 'none'
  210. })
  211. } finally {
  212. uni.hideLoading()
  213. }
  214. },
  215. // 加载用户动态
  216. async loadUserDynamics() {
  217. if (!this.userId) return
  218. this.loadingDynamics = true
  219. try {
  220. const currentUserId = uni.getStorageSync('userId')
  221. const params = {
  222. pageNum: this.pageNum,
  223. pageSize: this.pageSize,
  224. currentUserId: currentUserId ? parseInt(currentUserId) : null
  225. }
  226. const result = await api.dynamic.getUserDynamics(this.userId, params)
  227. console.log('用户动态API返回:', result)
  228. // 处理PageResult格式:{ records: [], total: 0, current: 1, size: 10 }
  229. let list = []
  230. let total = 0
  231. if (result) {
  232. // PageResult格式:{ records: [], total: 0 }
  233. if (result.records && Array.isArray(result.records)) {
  234. list = result.records
  235. total = result.total || 0
  236. }
  237. // 兼容格式:{ list: [], total: 0 }
  238. else if (result.list && Array.isArray(result.list)) {
  239. list = result.list
  240. total = result.total || result.list.length
  241. }
  242. // 直接是数组
  243. else if (Array.isArray(result)) {
  244. list = result
  245. total = result.length
  246. }
  247. }
  248. // 处理mediaUrls字段(可能是字符串需要解析)
  249. list = list.map(item => {
  250. if (item.mediaUrls && typeof item.mediaUrls === 'string') {
  251. try {
  252. item.mediaUrls = JSON.parse(item.mediaUrls)
  253. } catch (e) {
  254. console.error('解析mediaUrls失败:', e)
  255. item.mediaUrls = []
  256. }
  257. }
  258. if (!item.mediaUrls || !Array.isArray(item.mediaUrls)) {
  259. item.mediaUrls = []
  260. }
  261. return item
  262. })
  263. this.dynamicList = list
  264. this.totalDynamics = total
  265. console.log('处理后的动态列表:', this.dynamicList)
  266. } catch (e) {
  267. console.error('获取用户动态失败:', e)
  268. uni.showToast({
  269. title: '加载动态失败',
  270. icon: 'none'
  271. })
  272. } finally {
  273. this.loadingDynamics = false
  274. }
  275. },
  276. // 查看全部动态
  277. viewAllDynamics() {
  278. uni.navigateTo({
  279. url: `/pages/recommend/user-dynamics?userId=${this.userId}`
  280. })
  281. },
  282. // 查看动态详情
  283. viewDynamicDetail(item) {
  284. if (!item || !item.dynamicId) {
  285. console.error('动态项无效:', item)
  286. uni.showToast({
  287. title: '动态信息无效',
  288. icon: 'none'
  289. })
  290. return
  291. }
  292. uni.navigateTo({
  293. url: `/pages/plaza/detail?dynamicId=${item.dynamicId}`
  294. })
  295. },
  296. // 查看动态评论
  297. viewDynamicComments(item) {
  298. if (!item || !item.dynamicId) {
  299. console.error('动态项无效:', item)
  300. uni.showToast({
  301. title: '动态信息无效',
  302. icon: 'none'
  303. })
  304. return
  305. }
  306. uni.navigateTo({
  307. url: `/pages/plaza/detail?dynamicId=${item.dynamicId}&scrollToComment=true`
  308. })
  309. },
  310. // 预览媒体
  311. previewMedia(urls, index) {
  312. if (!urls || urls.length === 0) return
  313. uni.previewImage({
  314. urls: urls,
  315. current: index
  316. })
  317. },
  318. // 获取网格类名
  319. getGridClass(item) {
  320. const count = item.mediaUrls ? item.mediaUrls.length : 0
  321. if (count === 1) return 'grid-1'
  322. if (count === 2 || count === 4) return 'grid-2'
  323. return 'grid-3'
  324. },
  325. // 格式化时间
  326. formatTime(timeStr) {
  327. if (!timeStr) return ''
  328. const date = new Date(timeStr)
  329. const now = new Date()
  330. const diff = now - date
  331. const minutes = Math.floor(diff / 60000)
  332. const hours = Math.floor(diff / 3600000)
  333. const days = Math.floor(diff / 86400000)
  334. if (minutes < 1) return '刚刚'
  335. if (minutes < 60) return `${minutes}分钟前`
  336. if (hours < 24) return `${hours}小时前`
  337. if (days < 7) return `${days}天前`
  338. return date.toLocaleDateString()
  339. },
  340. // 格式化匹配度
  341. fmtScore(s) {
  342. return s ? Number(s).toFixed(1) : '0.0'
  343. },
  344. // 解析兴趣爱好
  345. parseHobby(h) {
  346. try {
  347. const arr = typeof h === 'string' ? JSON.parse(h) : h
  348. return Array.isArray(arr) ? arr.slice(0, 6) : []
  349. } catch {
  350. return []
  351. }
  352. },
  353. // 头像加载错误
  354. onAvatarError() {
  355. if (this.userInfo) {
  356. this.userInfo.avatar = '/static/close.png'
  357. this.userInfo.avatarUrl = '/static/close.png'
  358. }
  359. },
  360. // 返回
  361. goBack() {
  362. uni.navigateBack()
  363. }
  364. }
  365. }
  366. </script>
  367. <style lang="scss" scoped>
  368. .user-detail-page {
  369. min-height: 100vh;
  370. background: #F5F5F5;
  371. padding-top: 90rpx;
  372. }
  373. /* 自定义导航栏 */
  374. .custom-navbar {
  375. position: fixed;
  376. top: 0;
  377. left: 0;
  378. right: 0;
  379. height: 90rpx;
  380. display: flex;
  381. align-items: center;
  382. justify-content: space-between;
  383. padding: 0 20rpx;
  384. background: #FFFFFF;
  385. border-bottom: 2rpx solid #E0E0E0;
  386. z-index: 999;
  387. }
  388. .navbar-left,
  389. .navbar-right {
  390. width: 80rpx;
  391. }
  392. .back-icon {
  393. font-size: 40rpx;
  394. color: #333;
  395. font-weight: bold;
  396. }
  397. .navbar-title {
  398. flex: 1;
  399. text-align: center;
  400. font-size: 32rpx;
  401. font-weight: bold;
  402. color: #333;
  403. }
  404. /* 页面内容 */
  405. .page-content {
  406. height: calc(100vh - 90rpx);
  407. padding: 20rpx;
  408. }
  409. /* 用户头部 */
  410. .user-header {
  411. background: #FFFFFF;
  412. border-radius: 16rpx;
  413. padding: 30rpx;
  414. margin-bottom: 20rpx;
  415. display: flex;
  416. align-items: flex-start;
  417. border: 2rpx solid #E0E0E0;
  418. }
  419. .user-avatar {
  420. width: 160rpx;
  421. height: 160rpx;
  422. border-radius: 80rpx;
  423. margin-right: 24rpx;
  424. border: 4rpx solid #FFE5F1;
  425. background: #F5F5F5;
  426. }
  427. .user-basic {
  428. flex: 1;
  429. }
  430. .user-name-row {
  431. display: flex;
  432. align-items: center;
  433. margin-bottom: 16rpx;
  434. flex-wrap: wrap;
  435. gap: 12rpx;
  436. }
  437. .user-name {
  438. font-size: 36rpx;
  439. font-weight: bold;
  440. color: #333;
  441. }
  442. .score-badge {
  443. background: #FFE5F1;
  444. color: #E91E63;
  445. padding: 6rpx 16rpx;
  446. border-radius: 20rpx;
  447. font-size: 24rpx;
  448. font-weight: 600;
  449. }
  450. .user-meta {
  451. display: flex;
  452. flex-wrap: wrap;
  453. gap: 16rpx;
  454. margin-bottom: 16rpx;
  455. font-size: 26rpx;
  456. color: #666;
  457. }
  458. .user-tags {
  459. display: flex;
  460. flex-wrap: wrap;
  461. gap: 12rpx;
  462. }
  463. .tag {
  464. background: #F5F5F5;
  465. color: #666;
  466. padding: 8rpx 16rpx;
  467. border-radius: 20rpx;
  468. font-size: 24rpx;
  469. border: 1rpx solid #E0E0E0;
  470. }
  471. /* 详细信息区域 */
  472. .detail-section {
  473. background: #FFFFFF;
  474. border-radius: 16rpx;
  475. padding: 24rpx;
  476. margin-bottom: 20rpx;
  477. border: 2rpx solid #E0E0E0;
  478. }
  479. .section-title {
  480. font-size: 28rpx;
  481. font-weight: 600;
  482. color: #333;
  483. margin-bottom: 20rpx;
  484. padding-bottom: 12rpx;
  485. border-bottom: 2rpx solid #F0F0F0;
  486. }
  487. .detail-grid {
  488. display: grid;
  489. grid-template-columns: repeat(2, 1fr);
  490. gap: 20rpx;
  491. }
  492. .detail-item {
  493. display: flex;
  494. flex-direction: column;
  495. }
  496. .detail-label {
  497. font-size: 24rpx;
  498. color: #999;
  499. margin-bottom: 8rpx;
  500. }
  501. .detail-value {
  502. font-size: 26rpx;
  503. color: #333;
  504. font-weight: 500;
  505. }
  506. .introduction-content {
  507. background: #F8F9FA;
  508. padding: 20rpx;
  509. border-radius: 8rpx;
  510. border-left: 4rpx solid #E91E63;
  511. line-height: 1.8;
  512. font-size: 26rpx;
  513. color: #666;
  514. }
  515. /* 动态区域 */
  516. .dynamic-section {
  517. background: #FFFFFF;
  518. border-radius: 16rpx;
  519. padding: 24rpx;
  520. margin-bottom: 20rpx;
  521. border: 2rpx solid #E0E0E0;
  522. }
  523. .section-header {
  524. display: flex;
  525. align-items: center;
  526. justify-content: space-between;
  527. margin-bottom: 20rpx;
  528. padding-bottom: 12rpx;
  529. border-bottom: 2rpx solid #F0F0F0;
  530. }
  531. .section-more {
  532. font-size: 24rpx;
  533. color: #E91E63;
  534. }
  535. .dynamic-list {
  536. display: flex;
  537. flex-direction: column;
  538. gap: 20rpx;
  539. }
  540. .dynamic-item {
  541. padding: 20rpx;
  542. background: #F8F9FA;
  543. border-radius: 12rpx;
  544. border: 1rpx solid #E0E0E0;
  545. }
  546. .dynamic-content {
  547. margin-bottom: 16rpx;
  548. }
  549. .dynamic-text {
  550. font-size: 26rpx;
  551. color: #333;
  552. line-height: 1.6;
  553. display: -webkit-box;
  554. -webkit-box-orient: vertical;
  555. -webkit-line-clamp: 3;
  556. overflow: hidden;
  557. }
  558. .dynamic-media {
  559. margin-bottom: 16rpx;
  560. }
  561. .media-grid {
  562. display: grid;
  563. gap: 8rpx;
  564. }
  565. .grid-1 {
  566. grid-template-columns: 1fr;
  567. }
  568. .grid-2 {
  569. grid-template-columns: repeat(2, 1fr);
  570. }
  571. .grid-3 {
  572. grid-template-columns: repeat(3, 1fr);
  573. }
  574. .media-image {
  575. width: 100%;
  576. height: 200rpx;
  577. border-radius: 8rpx;
  578. background: #F5F5F5;
  579. }
  580. .dynamic-info {
  581. display: flex;
  582. justify-content: space-between;
  583. align-items: center;
  584. font-size: 24rpx;
  585. color: #999;
  586. }
  587. .dynamic-stats {
  588. display: flex;
  589. gap: 20rpx;
  590. }
  591. .stat-item {
  592. font-size: 24rpx;
  593. color: #999;
  594. }
  595. .comment-stat {
  596. cursor: pointer;
  597. transition: opacity 0.2s;
  598. }
  599. .comment-stat:active {
  600. opacity: 0.6;
  601. }
  602. .empty-dynamics {
  603. text-align: center;
  604. padding: 60rpx 20rpx;
  605. }
  606. .empty-icon {
  607. font-size: 80rpx;
  608. display: block;
  609. margin-bottom: 20rpx;
  610. }
  611. .empty-text {
  612. font-size: 26rpx;
  613. color: #999;
  614. }
  615. .loading-tip {
  616. text-align: center;
  617. padding: 40rpx;
  618. color: #999;
  619. font-size: 26rpx;
  620. }
  621. .loading-container {
  622. display: flex;
  623. align-items: center;
  624. justify-content: center;
  625. height: 100vh;
  626. font-size: 28rpx;
  627. color: #999;
  628. }
  629. </style>