liked-me.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <template>
  2. <view class="like-page">
  3. <!-- 数据加载状态 -->
  4. <view class="loading-container" v-if="loading">
  5. <uni-loading type="circle" color="#E91E63"></uni-loading>
  6. <text class="loading-text">加载中...</text>
  7. </view>
  8. <!-- 空数据状态 -->
  9. <view class="empty-container" v-else-if="users.length === 0">
  10. <text class="empty-icon">😊</text>
  11. <text class="empty-text">暂无用户喜欢您</text>
  12. <view class="empty-action">
  13. <button class="action-btn" @click="goRecommend">完善资料增加曝光</button>
  14. </view>
  15. </view>
  16. <!-- 用户列表 -->
  17. <scroll-view class="user-list" scroll-y="true">
  18. <view class="user-item" v-for="user in users" :key="user.userId" @click="goUserDetail(user.userId)">
  19. <image class="user-avatar" :src="user.avatar" mode="aspectFill"></image>
  20. <view class="user-info">
  21. <view class="user-basic">
  22. <text class="user-nickname">{{ user.nickname }}</text>
  23. <text class="user-age">{{ user.age }}岁</text>
  24. </view>
  25. <view class="user-details">
  26. <text class="detail-item" v-if="user.height">{{ user.height }}cm</text>
  27. <text class="detail-item" v-if="user.weight">{{ user.weight }}kg</text>
  28. <text class="detail-item" v-if="user.educationText">{{ user.educationText }}</text>
  29. <text class="detail-item" v-if="user.salaryText">{{ user.salaryText }}</text>
  30. </view>
  31. <view class="user-location" v-if="user.location">
  32. <text class="location-text">{{ user.location }}</text>
  33. </view>
  34. </view>
  35. </view>
  36. </scroll-view>
  37. </view>
  38. </template>
  39. <script>
  40. import api from '@/utils/api.js'
  41. export default {
  42. data() {
  43. return {
  44. users: [],
  45. loading: true,
  46. pageNum: 1,
  47. pageSize: 20,
  48. hasMore: true
  49. }
  50. },
  51. onLoad() {
  52. this.loadUsers()
  53. },
  54. methods: {
  55. // 计算年龄
  56. calculateAge(birthDate) {
  57. console.log('calculateAge方法被调用,birthDate:', birthDate, '类型:', typeof birthDate)
  58. if (!birthDate) {
  59. console.log('birthDate为空,返回0')
  60. return 0
  61. }
  62. try {
  63. const birth = new Date(birthDate)
  64. console.log('转换后的Date对象:', birth, '时间戳:', birth.getTime())
  65. // 检查日期是否有效
  66. if (isNaN(birth.getTime())) {
  67. console.log('日期无效,返回0')
  68. return 0
  69. }
  70. const now = new Date()
  71. let age = now.getFullYear() - birth.getFullYear()
  72. const monthDiff = now.getMonth() - birth.getMonth()
  73. if (monthDiff < 0 || (monthDiff === 0 && now.getDate() < birth.getDate())) {
  74. age--
  75. }
  76. console.log('计算出的年龄:', age)
  77. return age
  78. } catch (error) {
  79. console.error('计算年龄失败:', error)
  80. return 0
  81. }
  82. },
  83. // 获取教育程度文本
  84. getEducationText(level) {
  85. const educationMap = {
  86. 1: '小学',
  87. 2: '初中',
  88. 3: '高中/中专',
  89. 4: '大专',
  90. 5: '本科',
  91. 6: '硕士',
  92. 7: '博士',
  93. 8: '博士后'
  94. }
  95. return educationMap[level] || ''
  96. },
  97. // 获取薪资范围文本
  98. getSalaryText(range) {
  99. const salaryMap = {
  100. 1: '3k以下',
  101. 2: '3k-5k',
  102. 3: '5k-8k',
  103. 4: '8k-12k',
  104. 5: '12k-15k',
  105. 6: '15k-20k',
  106. 7: '20k-30k',
  107. 8: '30k-50k',
  108. 9: '50k以上'
  109. }
  110. return salaryMap[range] || ''
  111. },
  112. // 加载喜欢我的用户列表
  113. async loadUsers() {
  114. try {
  115. this.loading = true
  116. // 获取当前用户ID
  117. const userInfo = uni.getStorageSync('userInfo')
  118. if (!userInfo || !userInfo.userId) {
  119. uni.showToast({
  120. title: '请先登录',
  121. icon: 'none'
  122. })
  123. return
  124. }
  125. // 调用API获取喜欢我的用户列表
  126. const res = await api.recommend.getLikedMeUsers(
  127. userInfo.userId,
  128. this.pageSize,
  129. (this.pageNum - 1) * this.pageSize
  130. )
  131. // 处理返回数据,转换为前端需要的格式
  132. if (res && Array.isArray(res)) {
  133. this.users = res.map(user => {
  134. console.log('原始用户数据:', user)
  135. return {
  136. userId: user.userId,
  137. nickname: user.nickname || '',
  138. avatar: user.avatarUrl || '',
  139. age: this.calculateAge(user.birthDate || user.birth_date),
  140. height: user.height,
  141. weight: user.weight,
  142. educationText: this.getEducationText(user.educationLevel),
  143. salaryText: this.getSalaryText(user.salaryRange),
  144. location: `${user.provinceName || ''}${user.cityName || ''}${user.areaName || ''}`,
  145. // 保留原始数据用于其他用途
  146. originalData: user
  147. }
  148. })
  149. } else {
  150. this.users = []
  151. }
  152. console.log('加载喜欢我的用户成功:', this.users)
  153. } catch (error) {
  154. console.error('加载喜欢我的用户失败:', error)
  155. uni.showToast({
  156. title: '加载失败,请重试',
  157. icon: 'none'
  158. })
  159. this.users = []
  160. } finally {
  161. this.loading = false
  162. }
  163. },
  164. // 跳转到用户详情页
  165. goUserDetail(userId) {
  166. uni.navigateTo({
  167. url: `/pages/recommend/user-detail?userId=${userId}`,
  168. fail: (err) => {
  169. console.error('跳转用户详情失败:', err)
  170. uni.showToast({
  171. title: '页面不存在',
  172. icon: 'none'
  173. })
  174. }
  175. })
  176. },
  177. // 跳转到推荐页面
  178. goRecommend() {
  179. uni.navigateTo({
  180. url: '/pages/profile/index'
  181. })
  182. }
  183. }
  184. }
  185. </script>
  186. <style lang="scss" scoped>
  187. .like-page {
  188. min-height: 100vh;
  189. background-color: #F5F5F5;
  190. padding: 0 5rpx;
  191. box-sizing: border-box;
  192. overflow-x: hidden;
  193. }
  194. .loading-container {
  195. display: flex;
  196. flex-direction: column;
  197. align-items: center;
  198. justify-content: center;
  199. padding: 100rpx 0;
  200. .loading-text {
  201. margin-top: 20rpx;
  202. font-size: 28rpx;
  203. color: #999999;
  204. }
  205. }
  206. .empty-container {
  207. display: flex;
  208. flex-direction: column;
  209. align-items: center;
  210. justify-content: center;
  211. padding: 150rpx 0;
  212. .empty-icon {
  213. font-size: 120rpx;
  214. margin-bottom: 30rpx;
  215. }
  216. .empty-text {
  217. font-size: 32rpx;
  218. color: #999999;
  219. margin-bottom: 40rpx;
  220. }
  221. .action-btn {
  222. background: linear-gradient(135deg, #FF6B9D 0%, #E91E63 100%);
  223. color: #FFFFFF;
  224. border: none;
  225. padding: 18rpx 70rpx;
  226. border-radius: 45rpx;
  227. font-size: 28rpx;
  228. font-weight: 600;
  229. box-shadow: 0 6rpx 20rpx rgba(233, 30, 99, 0.3);
  230. transition: all 0.3s ease;
  231. letter-spacing: 1rpx;
  232. &:active {
  233. transform: translateY(2rpx);
  234. box-shadow: 0 3rpx 12rpx rgba(233, 30, 99, 0.4);
  235. background: linear-gradient(135deg, #E91E63 0%, #C2185B 100%);
  236. }
  237. }
  238. }
  239. .user-list {
  240. padding: 20rpx;
  241. height: 100vh;
  242. box-sizing: border-box;
  243. overflow-x: hidden;
  244. }
  245. .user-item {
  246. display: flex;
  247. align-items: center;
  248. background-color: #FFFFFF;
  249. border-radius: 20rpx;
  250. padding: 20rpx;
  251. margin-bottom: 20rpx;
  252. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  253. transition: all 0.3s ease;
  254. width: 100%;
  255. box-sizing: border-box;
  256. overflow: hidden;
  257. &:active {
  258. transform: translateY(2rpx);
  259. box-shadow: 0 1rpx 5rpx rgba(0, 0, 0, 0.05);
  260. }
  261. .user-avatar {
  262. width: 120rpx;
  263. height: 120rpx;
  264. border-radius: 50%;
  265. margin-right: 20rpx;
  266. border: 4rpx solid #FFF9F9;
  267. box-shadow: 0 4rpx 15rpx rgba(233, 30, 99, 0.2);
  268. flex-shrink: 0;
  269. }
  270. .user-info {
  271. flex: 1;
  272. min-width: 0;
  273. .user-basic {
  274. display: flex;
  275. align-items: center;
  276. margin-bottom: 10rpx;
  277. .user-nickname {
  278. font-size: 32rpx;
  279. font-weight: bold;
  280. color: #333333;
  281. margin-right: 15rpx;
  282. white-space: nowrap;
  283. overflow: hidden;
  284. text-overflow: ellipsis;
  285. }
  286. .user-age {
  287. font-size: 28rpx;
  288. color: #666666;
  289. white-space: nowrap;
  290. }
  291. }
  292. .user-details {
  293. display: flex;
  294. flex-wrap: wrap;
  295. gap: 15rpx;
  296. margin-bottom: 10rpx;
  297. .detail-item {
  298. font-size: 24rpx;
  299. color: #999999;
  300. background-color: #F5F5F5;
  301. padding: 6rpx 15rpx;
  302. border-radius: 15rpx;
  303. white-space: nowrap;
  304. }
  305. }
  306. .user-location {
  307. .location-text {
  308. font-size: 24rpx;
  309. color: #999999;
  310. white-space: nowrap;
  311. overflow: hidden;
  312. text-overflow: ellipsis;
  313. }
  314. }
  315. }
  316. }
  317. </style>