liked-me.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <template>
  2. <view class="like-page">
  3. <view v-if="loading && users.length === 0" class="loading-container">
  4. <uni-loading type="circle" color="#E91E63"></uni-loading>
  5. <text class="loading-text">加载中...</text>
  6. </view>
  7. <view v-else-if="users.length === 0" class="empty-container">
  8. <text class="empty-icon">😊</text>
  9. <text class="empty-text">暂无用户喜欢您</text>
  10. <view class="empty-action">
  11. <button class="action-btn" @click="goRecommend">完善资料增加曝光</button>
  12. </view>
  13. </view>
  14. <scroll-view v-else class="user-list" scroll-y="true" @scrolltolower="loadMoreUsers">
  15. <view class="user-item" v-for="user in users" :key="user.userId" @click="goUserDetail(user.userId)">
  16. <image class="user-avatar" :src="user.avatar" mode="aspectFill" @error="handleAvatarError(user)"></image>
  17. <view class="user-info">
  18. <view class="user-basic">
  19. <text class="user-nickname">{{ user.nickname }}</text>
  20. <text class="user-age">{{ user.age }}岁</text>
  21. </view>
  22. <view class="user-details">
  23. <text class="detail-item" v-if="user.height">{{ user.height }}cm</text>
  24. <text class="detail-item" v-if="user.weight">{{ user.weight }}kg</text>
  25. <text class="detail-item" v-if="user.educationText">{{ user.educationText }}</text>
  26. <text class="detail-item" v-if="user.salaryText">{{ user.salaryText }}</text>
  27. </view>
  28. <view class="user-location" v-if="user.location">
  29. <text class="location-text">{{ user.location }}</text>
  30. </view>
  31. </view>
  32. <view class="like-status">
  33. <text class="like-icon">💖</text>
  34. </view>
  35. </view>
  36. <view v-if="loadingMore" class="loading-more">
  37. <uni-loading type="circle" color="#E91E63" size="20"></uni-loading>
  38. <text class="loading-more-text">加载更多...</text>
  39. </view>
  40. <view v-else-if="!hasMore && users.length > 0" class="no-more">
  41. <text class="no-more-text">没有更多了</text>
  42. </view>
  43. </scroll-view>
  44. </view>
  45. </template>
  46. <script>
  47. import api from '@/utils/api.js'
  48. const EDUCATION_MAP = {
  49. 1: '初中',
  50. 2: '高中',
  51. 3: '大专',
  52. 4: '本科',
  53. 5: '硕士',
  54. 6: '博士'
  55. }
  56. const SALARY_MAP = {
  57. 1: '3k以下',
  58. 2: '3k-5k',
  59. 3: '5k-8k',
  60. 4: '8k-12k',
  61. 5: '12k-20k',
  62. 6: '20k-50k',
  63. 7: '50k以上'
  64. }
  65. export default {
  66. data() {
  67. return {
  68. users: [],
  69. loading: true,
  70. loadingMore: false,
  71. pageNum: 1,
  72. pageSize: 20,
  73. hasMore: true,
  74. currentUserId: null
  75. }
  76. },
  77. onLoad() {
  78. this.getCurrentUserId()
  79. },
  80. onShow() {
  81. if (!this.users.length || this.users.length === 0) {
  82. this.loadUsers()
  83. }
  84. },
  85. methods: {
  86. getCurrentUserId() {
  87. try {
  88. const userInfo = uni.getStorageSync('userInfo')
  89. if (userInfo) {
  90. this.currentUserId = userInfo.userId || userInfo.id
  91. }
  92. if (!this.currentUserId) {
  93. const token = uni.getStorageSync('token')
  94. if (token) {
  95. console.warn('无法获取当前用户ID,请检查登录状态')
  96. }
  97. }
  98. } catch (e) {
  99. console.error('获取用户信息失败:', e)
  100. }
  101. },
  102. transformUserData(user) {
  103. if (!user) return null
  104. const birthDate = user.birthDate || user.birth_date
  105. let age = ''
  106. if (birthDate) {
  107. try {
  108. const birth = new Date(birthDate)
  109. if (!isNaN(birth.getTime())) {
  110. const today = new Date()
  111. let userAge = today.getFullYear() - birth.getFullYear()
  112. const monthDiff = today.getMonth() - birth.getMonth()
  113. if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
  114. userAge--
  115. }
  116. age = userAge > 0 ? userAge : ''
  117. }
  118. } catch (e) {
  119. console.error('计算年龄失败:', e)
  120. }
  121. }
  122. let educationText = ''
  123. if (user.educationLevel !== undefined && user.educationLevel !== null) {
  124. educationText = EDUCATION_MAP[user.educationLevel] || ''
  125. }
  126. let salaryText = ''
  127. if (user.salaryRange !== undefined && user.salaryRange !== null) {
  128. salaryText = SALARY_MAP[user.salaryRange] || ''
  129. }
  130. let location = ''
  131. if (user.cityId || user.provinceId) {
  132. location = this.formatLocation(user.provinceId, user.cityId)
  133. }
  134. return {
  135. userId: user.userId || user.user_id,
  136. nickname: user.nickname || '匿名用户',
  137. avatar: user.avatarUrl || user.avatar_url || user.avatar || '/static/default-avatar.png',
  138. age: age,
  139. height: user.height || '',
  140. weight: user.weight || '',
  141. educationText: educationText,
  142. salaryText: salaryText,
  143. location: location,
  144. gender: user.gender
  145. }
  146. },
  147. formatLocation(provinceId, cityId) {
  148. if (!provinceId && !cityId) return ''
  149. const locationMap = this.$const.LOCATION_MAP
  150. if (locationMap && locationMap.length > 0) {
  151. try {
  152. const province = locationMap.find(p => p.id === provinceId)
  153. const city = province && province.children ? province.children.find(c => c.id === cityId) : null
  154. if (city) {
  155. return city.name || ''
  156. }
  157. } catch (e) {
  158. console.error('获取位置信息失败:', e)
  159. }
  160. }
  161. return ''
  162. },
  163. async loadUsers() {
  164. if (!this.currentUserId) {
  165. this.getCurrentUserId()
  166. if (!this.currentUserId) {
  167. uni.showToast({
  168. title: '请先登录',
  169. icon: 'none'
  170. })
  171. this.loading = false
  172. return
  173. }
  174. }
  175. try {
  176. this.loading = true
  177. const res = await api.recommend.getLikedMeUsers(this.currentUserId, this.pageSize, 0)
  178. if (res && Array.isArray(res)) {
  179. this.users = res.map(user => this.transformUserData(user))
  180. this.hasMore = res.length >= this.pageSize
  181. } else {
  182. this.users = []
  183. this.hasMore = false
  184. }
  185. if (this.users.length === 0) {
  186. console.log('没有喜欢我的用户')
  187. }
  188. } catch (error) {
  189. console.error('加载喜欢我的用户失败:', error)
  190. this.users = []
  191. uni.showToast({
  192. title: '加载失败,请重试',
  193. icon: 'none'
  194. })
  195. } finally {
  196. this.loading = false
  197. }
  198. },
  199. async loadMoreUsers() {
  200. if (this.loadingMore || !this.hasMore) {
  201. return
  202. }
  203. if (!this.currentUserId) {
  204. return
  205. }
  206. try {
  207. this.loadingMore = true
  208. const offset = (this.pageNum) * this.pageSize
  209. const res = await api.recommend.getLikedMeUsers(this.currentUserId, this.pageSize, offset)
  210. if (res && Array.isArray(res) && res.length > 0) {
  211. const newUsers = res.map(user => this.transformUserData(user))
  212. this.users = this.users.concat(newUsers)
  213. this.pageNum++
  214. this.hasMore = res.length >= this.pageSize
  215. } else {
  216. this.hasMore = false
  217. }
  218. } catch (error) {
  219. console.error('加载更多用户失败:', error)
  220. uni.showToast({
  221. title: '加载更多失败',
  222. icon: 'none'
  223. })
  224. } finally {
  225. this.loadingMore = false
  226. }
  227. },
  228. handleAvatarError(user) {
  229. if (user) {
  230. user.avatar = '/static/default-avatar.png'
  231. }
  232. },
  233. goUserDetail(userId) {
  234. if (!userId) {
  235. uni.showToast({
  236. title: '用户信息不完整',
  237. icon: 'none'
  238. })
  239. return
  240. }
  241. uni.navigateTo({
  242. url: `/pages/recommend/index?userId=${userId}`,
  243. fail: (err) => {
  244. console.error('跳转用户详情失败:', err)
  245. uni.showToast({
  246. title: '页面不存在',
  247. icon: 'none'
  248. })
  249. }
  250. })
  251. },
  252. goRecommend() {
  253. uni.navigateTo({
  254. url: '/pages/profile/index'
  255. })
  256. }
  257. }
  258. }
  259. </script>
  260. <style lang="scss" scoped>
  261. .like-page {
  262. min-height: 100vh;
  263. background-color: #F5F5F5;
  264. padding: 0 5rpx;
  265. box-sizing: border-box;
  266. overflow-x: hidden;
  267. }
  268. .loading-container {
  269. display: flex;
  270. flex-direction: column;
  271. align-items: center;
  272. justify-content: center;
  273. padding: 100rpx 0;
  274. .loading-text {
  275. margin-top: 20rpx;
  276. font-size: 28rpx;
  277. color: #999999;
  278. }
  279. }
  280. .empty-container {
  281. display: flex;
  282. flex-direction: column;
  283. align-items: center;
  284. justify-content: center;
  285. padding: 150rpx 0;
  286. .empty-icon {
  287. font-size: 120rpx;
  288. margin-bottom: 30rpx;
  289. }
  290. .empty-text {
  291. font-size: 32rpx;
  292. color: #999999;
  293. margin-bottom: 40rpx;
  294. }
  295. .action-btn {
  296. background: linear-gradient(135deg, #FF6B9D 0%, #E91E63 100%);
  297. color: #FFFFFF;
  298. border: none;
  299. padding: 18rpx 70rpx;
  300. border-radius: 45rpx;
  301. font-size: 28rpx;
  302. font-weight: 600;
  303. box-shadow: 0 6rpx 20rpx rgba(233, 30, 99, 0.3);
  304. transition: all 0.3s ease;
  305. letter-spacing: 1rpx;
  306. &:active {
  307. transform: translateY(2rpx);
  308. box-shadow: 0 3rpx 12rpx rgba(233, 30, 99, 0.4);
  309. background: linear-gradient(135deg, #E91E63 0%, #C2185B 100%);
  310. }
  311. }
  312. }
  313. .user-list {
  314. padding: 20rpx;
  315. height: calc(100vh - 88rpx);
  316. }
  317. .user-item {
  318. display: flex;
  319. align-items: center;
  320. background-color: #FFFFFF;
  321. border-radius: 20rpx;
  322. padding: 20rpx;
  323. margin-bottom: 20rpx;
  324. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  325. transition: all 0.3s ease;
  326. &:active {
  327. transform: translateY(2rpx);
  328. box-shadow: 0 1rpx 5rpx rgba(0, 0, 0, 0.05);
  329. }
  330. .user-avatar {
  331. width: 120rpx;
  332. height: 120rpx;
  333. border-radius: 50%;
  334. margin-right: 20rpx;
  335. border: 4rpx solid #FFF9F9;
  336. box-shadow: 0 4rpx 15rpx rgba(233, 30, 99, 0.2);
  337. }
  338. .user-info {
  339. flex: 1;
  340. .user-basic {
  341. display: flex;
  342. align-items: center;
  343. margin-bottom: 10rpx;
  344. .user-nickname {
  345. font-size: 32rpx;
  346. font-weight: bold;
  347. color: #333333;
  348. margin-right: 15rpx;
  349. }
  350. .user-age {
  351. font-size: 28rpx;
  352. color: #666666;
  353. }
  354. }
  355. .user-details {
  356. display: flex;
  357. flex-wrap: wrap;
  358. gap: 15rpx;
  359. margin-bottom: 10rpx;
  360. .detail-item {
  361. font-size: 24rpx;
  362. color: #999999;
  363. background-color: #F5F5F5;
  364. padding: 6rpx 15rpx;
  365. border-radius: 15rpx;
  366. }
  367. }
  368. .user-location {
  369. .location-text {
  370. font-size: 24rpx;
  371. color: #999999;
  372. }
  373. }
  374. }
  375. .like-status {
  376. .like-icon {
  377. font-size: 40rpx;
  378. color: #E91E63;
  379. }
  380. }
  381. }
  382. .loading-more {
  383. display: flex;
  384. align-items: center;
  385. justify-content: center;
  386. padding: 30rpx 0;
  387. .loading-more-text {
  388. margin-left: 16rpx;
  389. font-size: 26rpx;
  390. color: #999999;
  391. }
  392. }
  393. .no-more {
  394. text-align: center;
  395. padding: 30rpx 0;
  396. .no-more-text {
  397. font-size: 26rpx;
  398. color: #999999;
  399. }
  400. }
  401. </style>