liked-by-me.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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" @refresherrefresh="onRefresh" @scrolltolower="onLoadMore" refresher-enabled="true" refresher-triggered="refreshing">
  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 class="like-status" @click.stop="cancelLike(user.userId)">
  36. <text class="like-icon">❤️</text>
  37. </view>
  38. </view>
  39. <!-- 加载更多提示 -->
  40. <view class="load-more" v-if="users.length > 0">
  41. <text v-if="hasMore">加载中...</text>
  42. <text v-else>没有更多数据了</text>
  43. </view>
  44. </scroll-view>
  45. </view>
  46. </template>
  47. <script>
  48. import api from '@/utils/api.js'
  49. export default {
  50. data() {
  51. return {
  52. users: [],
  53. loading: true,
  54. refreshing: false,
  55. pageNum: 1,
  56. pageSize: 20,
  57. hasMore: true
  58. }
  59. },
  60. onLoad() {
  61. this.loadUsers()
  62. },
  63. methods: {
  64. // 加载我喜欢的用户列表
  65. async loadUsers() {
  66. try {
  67. // 从本地存储获取当前用户ID
  68. const userId = parseInt(uni.getStorageSync('userId') || 1)
  69. // 计算偏移量
  70. const offset = (this.pageNum - 1) * this.pageSize
  71. // 调用后端API获取我喜欢的用户列表
  72. const likedUsers = await api.recommend.getLikedUsers(userId, this.pageSize, offset)
  73. // 处理返回的数据,确保字段名匹配页面需要的格式
  74. const formattedUsers = likedUsers.map(user => ({
  75. userId: user.userId,
  76. nickname: user.nickname,
  77. avatar: user.avatarUrl || '',
  78. age: this.calculateAge(user.birthDate),
  79. height: user.height,
  80. weight: user.weight,
  81. educationText: this.formatEducation(user.educationLevel),
  82. salaryText: this.formatSalary(user.salaryRange),
  83. location: this.formatLocation(user.provinceId, user.cityId, user.areaId)
  84. }))
  85. // 根据是否是刷新操作来处理数据
  86. if (this.pageNum === 1) {
  87. // 首次加载或刷新,直接替换数据
  88. this.users = formattedUsers
  89. } else {
  90. // 加载更多,追加数据
  91. this.users = [...this.users, ...formattedUsers]
  92. }
  93. // 判断是否还有更多数据
  94. this.hasMore = formattedUsers.length >= this.pageSize
  95. } catch (error) {
  96. console.error('加载喜欢的用户失败:', error)
  97. uni.showToast({
  98. title: '加载失败,请重试',
  99. icon: 'none'
  100. })
  101. } finally {
  102. this.loading = false
  103. this.refreshing = false
  104. }
  105. },
  106. // 下拉刷新
  107. async onRefresh() {
  108. this.refreshing = true
  109. this.pageNum = 1
  110. this.hasMore = true
  111. await this.loadUsers()
  112. },
  113. // 上拉加载更多
  114. async onLoadMore() {
  115. if (this.loading || !this.hasMore) return
  116. this.pageNum++
  117. await this.loadUsers()
  118. },
  119. // 计算年龄
  120. calculateAge(birthDate) {
  121. if (!birthDate) return ''
  122. const birth = new Date(birthDate)
  123. const now = new Date()
  124. let age = now.getFullYear() - birth.getFullYear()
  125. if (now.getMonth() < birth.getMonth() || (now.getMonth() === birth.getMonth() && now.getDate() < birth.getDate())) {
  126. age--
  127. }
  128. return age
  129. },
  130. // 格式化教育程度
  131. formatEducation(educationLevel) {
  132. if (!educationLevel) return ''
  133. const map = {1:'高中',2:'大专',3:'本科',4:'硕士',5:'博士'}
  134. return map[educationLevel] || ''
  135. },
  136. // 格式化薪资
  137. formatSalary(salaryRange) {
  138. if (!salaryRange) return ''
  139. const map = {1:'<5k',2:'5-10k',3:'10-20k',4:'20-50k',5:'50k+'}
  140. return map[salaryRange] || ''
  141. },
  142. // 格式化地理位置
  143. formatLocation(provinceId, cityId, areaId) {
  144. // 这里简化处理,实际可以根据ID查询名称
  145. // 由于没有实时获取地区名称的API,暂时返回空字符串
  146. return ''
  147. },
  148. // 取消喜欢
  149. async cancelLike(targetUserId) {
  150. try {
  151. // 从本地存储获取当前用户ID
  152. const userId = parseInt(uni.getStorageSync('userId') || 1)
  153. // 调用后端API取消喜欢
  154. await api.recommend.feedback({ userId, targetUserId, type: 'dislike' })
  155. // 刷新页面,重新加载喜欢的用户列表
  156. await this.onRefresh()
  157. // 显示操作成功提示
  158. uni.showToast({ title: '已取消喜欢', icon: 'success' })
  159. } catch (error) {
  160. console.error('取消喜欢失败:', error)
  161. uni.showToast({ title: '取消喜欢失败', icon: 'none' })
  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/recommend/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. .load-more {
  246. text-align: center;
  247. padding: 30rpx 0;
  248. font-size: 28rpx;
  249. color: #999999;
  250. }
  251. .user-item {
  252. display: flex;
  253. align-items: center;
  254. background-color: #FFFFFF;
  255. border-radius: 20rpx;
  256. padding: 20rpx;
  257. margin-bottom: 20rpx;
  258. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  259. transition: all 0.3s ease;
  260. &:active {
  261. transform: translateY(2rpx);
  262. box-shadow: 0 1rpx 5rpx rgba(0, 0, 0, 0.05);
  263. }
  264. .user-avatar {
  265. width: 120rpx;
  266. height: 120rpx;
  267. border-radius: 50%;
  268. margin-right: 20rpx;
  269. border: 4rpx solid #FFF9F9;
  270. box-shadow: 0 4rpx 15rpx rgba(233, 30, 99, 0.2);
  271. }
  272. .user-info {
  273. flex: 1;
  274. .user-basic {
  275. display: flex;
  276. align-items: center;
  277. margin-bottom: 10rpx;
  278. .user-nickname {
  279. font-size: 32rpx;
  280. font-weight: bold;
  281. color: #333333;
  282. margin-right: 15rpx;
  283. }
  284. .user-age {
  285. font-size: 28rpx;
  286. color: #666666;
  287. }
  288. }
  289. .user-details {
  290. display: flex;
  291. flex-wrap: wrap;
  292. gap: 15rpx;
  293. margin-bottom: 10rpx;
  294. .detail-item {
  295. font-size: 24rpx;
  296. color: #999999;
  297. background-color: #F5F5F5;
  298. padding: 6rpx 15rpx;
  299. border-radius: 15rpx;
  300. }
  301. }
  302. .user-location {
  303. .location-text {
  304. font-size: 24rpx;
  305. color: #999999;
  306. }
  307. }
  308. }
  309. .like-status {
  310. .like-icon {
  311. font-size: 40rpx;
  312. color: #E91E63;
  313. }
  314. }
  315. }
  316. </style>