user-detail.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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, index)"
  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. // 处理PageResult格式:{ records: [], total: 0, current: 1, size: 10 }
  228. let list = []
  229. let total = 0
  230. if (result) {
  231. // PageResult格式:{ records: [], total: 0 }
  232. if (result.records && Array.isArray(result.records)) {
  233. list = result.records
  234. total = result.total || 0
  235. }
  236. // 兼容格式:{ list: [], total: 0 }
  237. else if (result.list && Array.isArray(result.list)) {
  238. list = result.list
  239. total = result.total || result.list.length
  240. }
  241. // 直接是数组
  242. else if (Array.isArray(result)) {
  243. list = result
  244. total = result.length
  245. }
  246. }
  247. // 处理mediaUrls字段(可能是字符串需要解析)
  248. list = list.map(item => {
  249. if (item.mediaUrls && typeof item.mediaUrls === 'string') {
  250. try {
  251. item.mediaUrls = JSON.parse(item.mediaUrls)
  252. } catch (e) {
  253. console.error('解析mediaUrls失败:', e)
  254. item.mediaUrls = []
  255. }
  256. }
  257. if (!item.mediaUrls || !Array.isArray(item.mediaUrls)) {
  258. item.mediaUrls = []
  259. }
  260. return item
  261. })
  262. this.dynamicList = list
  263. this.totalDynamics = total
  264. } catch (e) {
  265. console.error('获取用户动态失败:', e)
  266. uni.showToast({
  267. title: '加载动态失败',
  268. icon: 'none'
  269. })
  270. } finally {
  271. this.loadingDynamics = false
  272. }
  273. },
  274. // 查看全部动态
  275. viewAllDynamics() {
  276. uni.navigateTo({
  277. url: `/pages/recommend/user-dynamics?userId=${this.userId}`
  278. })
  279. },
  280. // 查看动态详情
  281. viewDynamicDetail(item,index) {
  282. if (!this.dynamicList[index] || !this.dynamicList[index].dynamicId) {
  283. uni.showToast({
  284. title: '动态信息无效',
  285. icon: 'none'
  286. })
  287. return
  288. }
  289. uni.navigateTo({
  290. url: `/pages/plaza/detail?dynamicId=${this.dynamicList[index].dynamicId}`
  291. })
  292. },
  293. // 查看动态评论
  294. viewDynamicComments(item) {
  295. if (!item || !item.dynamicId) {
  296. console.error('动态项无效:', item)
  297. uni.showToast({
  298. title: '动态信息无效',
  299. icon: 'none'
  300. })
  301. return
  302. }
  303. uni.navigateTo({
  304. url: `/pages/plaza/detail?dynamicId=${item.dynamicId}&scrollToComment=true`
  305. })
  306. },
  307. // 预览媒体
  308. previewMedia(urls, index) {
  309. if (!urls || urls.length === 0) return
  310. uni.previewImage({
  311. urls: urls,
  312. current: index
  313. })
  314. },
  315. // 获取网格类名
  316. getGridClass(item) {
  317. const count = item.mediaUrls ? item.mediaUrls.length : 0
  318. if (count === 1) return 'grid-1'
  319. if (count === 2 || count === 4) return 'grid-2'
  320. return 'grid-3'
  321. },
  322. // 格式化时间
  323. formatTime(timeStr) {
  324. if (!timeStr) return ''
  325. const date = new Date(timeStr)
  326. const now = new Date()
  327. const diff = now - date
  328. const minutes = Math.floor(diff / 60000)
  329. const hours = Math.floor(diff / 3600000)
  330. const days = Math.floor(diff / 86400000)
  331. if (minutes < 1) return '刚刚'
  332. if (minutes < 60) return `${minutes}分钟前`
  333. if (hours < 24) return `${hours}小时前`
  334. if (days < 7) return `${days}天前`
  335. return date.toLocaleDateString()
  336. },
  337. // 格式化匹配度
  338. fmtScore(s) {
  339. return s ? Number(s).toFixed(1) : '0.0'
  340. },
  341. // 解析兴趣爱好
  342. parseHobby(h) {
  343. try {
  344. const arr = typeof h === 'string' ? JSON.parse(h) : h
  345. return Array.isArray(arr) ? arr.slice(0, 6) : []
  346. } catch {
  347. return []
  348. }
  349. },
  350. // 头像加载错误
  351. onAvatarError() {
  352. if (this.userInfo) {
  353. this.userInfo.avatar = '/static/close.png'
  354. this.userInfo.avatarUrl = '/static/close.png'
  355. }
  356. },
  357. // 返回
  358. goBack() {
  359. uni.navigateBack()
  360. }
  361. }
  362. }
  363. </script>
  364. <style lang="scss" scoped>
  365. .user-detail-page {
  366. min-height: 100vh;
  367. background: #F5F5F5;
  368. padding-top: 90rpx;
  369. }
  370. /* 自定义导航栏 */
  371. .custom-navbar {
  372. position: fixed;
  373. top: 0;
  374. left: 0;
  375. right: 0;
  376. height: 90rpx;
  377. display: flex;
  378. align-items: center;
  379. justify-content: space-between;
  380. padding: 0 20rpx;
  381. background: #FFFFFF;
  382. border-bottom: 2rpx solid #E0E0E0;
  383. z-index: 999;
  384. }
  385. .navbar-left,
  386. .navbar-right {
  387. width: 80rpx;
  388. }
  389. .back-icon {
  390. font-size: 40rpx;
  391. color: #333;
  392. font-weight: bold;
  393. }
  394. .navbar-title {
  395. flex: 1;
  396. text-align: center;
  397. font-size: 32rpx;
  398. font-weight: bold;
  399. color: #333;
  400. }
  401. /* 页面内容 */
  402. .page-content {
  403. height: calc(100vh - 90rpx);
  404. padding: 20rpx;
  405. }
  406. /* 用户头部 */
  407. .user-header {
  408. background: #FFFFFF;
  409. border-radius: 16rpx;
  410. padding: 30rpx;
  411. margin-bottom: 20rpx;
  412. display: flex;
  413. align-items: flex-start;
  414. border: 2rpx solid #E0E0E0;
  415. }
  416. .user-avatar {
  417. width: 160rpx;
  418. height: 160rpx;
  419. border-radius: 80rpx;
  420. margin-right: 24rpx;
  421. border: 4rpx solid #FFE5F1;
  422. background: #F5F5F5;
  423. }
  424. .user-basic {
  425. flex: 1;
  426. }
  427. .user-name-row {
  428. display: flex;
  429. align-items: center;
  430. margin-bottom: 16rpx;
  431. flex-wrap: wrap;
  432. gap: 12rpx;
  433. }
  434. .user-name {
  435. font-size: 36rpx;
  436. font-weight: bold;
  437. color: #333;
  438. }
  439. .score-badge {
  440. background: #FFE5F1;
  441. color: #E91E63;
  442. padding: 6rpx 16rpx;
  443. border-radius: 20rpx;
  444. font-size: 24rpx;
  445. font-weight: 600;
  446. }
  447. .user-meta {
  448. display: flex;
  449. flex-wrap: wrap;
  450. gap: 16rpx;
  451. margin-bottom: 16rpx;
  452. font-size: 26rpx;
  453. color: #666;
  454. }
  455. .user-tags {
  456. display: flex;
  457. flex-wrap: wrap;
  458. gap: 12rpx;
  459. }
  460. .tag {
  461. background: #F5F5F5;
  462. color: #666;
  463. padding: 8rpx 16rpx;
  464. border-radius: 20rpx;
  465. font-size: 24rpx;
  466. border: 1rpx solid #E0E0E0;
  467. }
  468. /* 详细信息区域 */
  469. .detail-section {
  470. background: #FFFFFF;
  471. border-radius: 16rpx;
  472. padding: 24rpx;
  473. margin-bottom: 20rpx;
  474. border: 2rpx solid #E0E0E0;
  475. }
  476. .section-title {
  477. font-size: 28rpx;
  478. font-weight: 600;
  479. color: #333;
  480. margin-bottom: 20rpx;
  481. padding-bottom: 12rpx;
  482. border-bottom: 2rpx solid #F0F0F0;
  483. }
  484. .detail-grid {
  485. display: grid;
  486. grid-template-columns: repeat(2, 1fr);
  487. gap: 20rpx;
  488. }
  489. .detail-item {
  490. display: flex;
  491. flex-direction: column;
  492. }
  493. .detail-label {
  494. font-size: 24rpx;
  495. color: #999;
  496. margin-bottom: 8rpx;
  497. }
  498. .detail-value {
  499. font-size: 26rpx;
  500. color: #333;
  501. font-weight: 500;
  502. }
  503. .introduction-content {
  504. background: #F8F9FA;
  505. padding: 20rpx;
  506. border-radius: 8rpx;
  507. border-left: 4rpx solid #E91E63;
  508. line-height: 1.8;
  509. font-size: 26rpx;
  510. color: #666;
  511. }
  512. /* 动态区域 */
  513. .dynamic-section {
  514. background: #FFFFFF;
  515. border-radius: 16rpx;
  516. padding: 24rpx;
  517. margin-bottom: 20rpx;
  518. border: 2rpx solid #E0E0E0;
  519. }
  520. .section-header {
  521. display: flex;
  522. align-items: center;
  523. justify-content: space-between;
  524. margin-bottom: 20rpx;
  525. padding-bottom: 12rpx;
  526. border-bottom: 2rpx solid #F0F0F0;
  527. }
  528. .section-more {
  529. font-size: 24rpx;
  530. color: #E91E63;
  531. }
  532. .dynamic-list {
  533. display: flex;
  534. flex-direction: column;
  535. gap: 20rpx;
  536. }
  537. .dynamic-item {
  538. padding: 20rpx;
  539. background: #F8F9FA;
  540. border-radius: 12rpx;
  541. border: 1rpx solid #E0E0E0;
  542. }
  543. .dynamic-content {
  544. margin-bottom: 16rpx;
  545. }
  546. .dynamic-text {
  547. font-size: 26rpx;
  548. color: #333;
  549. line-height: 1.6;
  550. display: -webkit-box;
  551. -webkit-box-orient: vertical;
  552. -webkit-line-clamp: 3;
  553. overflow: hidden;
  554. }
  555. .dynamic-media {
  556. margin-bottom: 16rpx;
  557. }
  558. .media-grid {
  559. display: grid;
  560. gap: 8rpx;
  561. }
  562. .grid-1 {
  563. grid-template-columns: 1fr;
  564. }
  565. .grid-2 {
  566. grid-template-columns: repeat(2, 1fr);
  567. }
  568. .grid-3 {
  569. grid-template-columns: repeat(3, 1fr);
  570. }
  571. .media-image {
  572. width: 100%;
  573. height: 200rpx;
  574. border-radius: 8rpx;
  575. background: #F5F5F5;
  576. }
  577. .dynamic-info {
  578. display: flex;
  579. justify-content: space-between;
  580. align-items: center;
  581. font-size: 24rpx;
  582. color: #999;
  583. }
  584. .dynamic-stats {
  585. display: flex;
  586. gap: 20rpx;
  587. }
  588. .stat-item {
  589. font-size: 24rpx;
  590. color: #999;
  591. }
  592. .comment-stat {
  593. cursor: pointer;
  594. transition: opacity 0.2s;
  595. }
  596. .comment-stat:active {
  597. opacity: 0.6;
  598. }
  599. .empty-dynamics {
  600. text-align: center;
  601. padding: 60rpx 20rpx;
  602. }
  603. .empty-icon {
  604. font-size: 80rpx;
  605. display: block;
  606. margin-bottom: 20rpx;
  607. }
  608. .empty-text {
  609. font-size: 26rpx;
  610. color: #999;
  611. }
  612. .loading-tip {
  613. text-align: center;
  614. padding: 40rpx;
  615. color: #999;
  616. font-size: 26rpx;
  617. }
  618. .loading-container {
  619. display: flex;
  620. align-items: center;
  621. justify-content: center;
  622. height: 100vh;
  623. font-size: 28rpx;
  624. color: #999;
  625. }
  626. </style>