user-detail.vue 15 KB

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