user-detail.vue 15 KB

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