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