liked-by-me.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. return ''
  146. },
  147. // 取消喜欢
  148. async cancelLike(targetUserId) {
  149. try {
  150. // 从本地存储获取当前用户ID
  151. const userId = parseInt(uni.getStorageSync('userId') || 1)
  152. // 调用后端API取消喜欢
  153. await api.recommend.feedback({ userId, targetUserId, type: 'dislike' })
  154. // 刷新页面,重新加载喜欢的用户列表
  155. await this.onRefresh()
  156. // 显示操作成功提示
  157. uni.showToast({ title: '已取消喜欢', icon: 'success' })
  158. } catch (error) {
  159. console.error('取消喜欢失败:', error)
  160. uni.showToast({ title: '取消喜欢失败', icon: 'none' })
  161. }
  162. },
  163. // 跳转到用户详情页
  164. goUserDetail(userId) {
  165. uni.navigateTo({
  166. url: `/pages/recommend/index?userId=${userId}`,
  167. fail: (err) => {
  168. console.error('跳转用户详情失败:', err)
  169. uni.showToast({
  170. title: '页面不存在',
  171. icon: 'none'
  172. })
  173. }
  174. })
  175. },
  176. // 跳转到推荐页面
  177. goRecommend() {
  178. uni.navigateTo({
  179. url: '/pages/recommend/index'
  180. })
  181. }
  182. }
  183. }
  184. </script>
  185. <style lang="scss" scoped>
  186. .like-page {
  187. min-height: 100vh;
  188. background-color: #F5F5F5;
  189. padding: 0 5rpx;
  190. box-sizing: border-box;
  191. overflow-x: hidden;
  192. }
  193. .loading-container {
  194. display: flex;
  195. flex-direction: column;
  196. align-items: center;
  197. justify-content: center;
  198. padding: 100rpx 0;
  199. .loading-text {
  200. margin-top: 20rpx;
  201. font-size: 28rpx;
  202. color: #999999;
  203. }
  204. }
  205. .empty-container {
  206. display: flex;
  207. flex-direction: column;
  208. align-items: center;
  209. justify-content: center;
  210. padding: 150rpx 0;
  211. .empty-icon {
  212. font-size: 120rpx;
  213. margin-bottom: 30rpx;
  214. }
  215. .empty-text {
  216. font-size: 32rpx;
  217. color: #999999;
  218. margin-bottom: 40rpx;
  219. }
  220. .action-btn {
  221. background: linear-gradient(135deg, #FF6B9D 0%, #E91E63 100%);
  222. color: #FFFFFF;
  223. border: none;
  224. padding: 18rpx 70rpx;
  225. border-radius: 45rpx;
  226. font-size: 28rpx;
  227. font-weight: 600;
  228. box-shadow: 0 6rpx 20rpx rgba(233, 30, 99, 0.3);
  229. transition: all 0.3s ease;
  230. letter-spacing: 1rpx;
  231. &:active {
  232. transform: translateY(2rpx);
  233. box-shadow: 0 3rpx 12rpx rgba(233, 30, 99, 0.4);
  234. background: linear-gradient(135deg, #E91E63 0%, #C2185B 100%);
  235. }
  236. }
  237. }
  238. .user-list {
  239. padding: 20rpx;
  240. height: 100vh;
  241. box-sizing: border-box;
  242. overflow-x: hidden;
  243. }
  244. .load-more {
  245. text-align: center;
  246. padding: 30rpx 0;
  247. font-size: 28rpx;
  248. color: #999999;
  249. }
  250. .user-item {
  251. display: flex;
  252. align-items: center;
  253. background-color: #FFFFFF;
  254. border-radius: 20rpx;
  255. padding: 20rpx;
  256. margin-bottom: 20rpx;
  257. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  258. transition: all 0.3s ease;
  259. &:active {
  260. transform: translateY(2rpx);
  261. box-shadow: 0 1rpx 5rpx rgba(0, 0, 0, 0.05);
  262. }
  263. .user-avatar {
  264. width: 120rpx;
  265. height: 120rpx;
  266. border-radius: 50%;
  267. margin-right: 20rpx;
  268. border: 4rpx solid #FFF9F9;
  269. box-shadow: 0 4rpx 15rpx rgba(233, 30, 99, 0.2);
  270. }
  271. .user-info {
  272. flex: 1;
  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. }
  283. .user-age {
  284. font-size: 28rpx;
  285. color: #666666;
  286. }
  287. }
  288. .user-details {
  289. display: flex;
  290. flex-wrap: wrap;
  291. gap: 15rpx;
  292. margin-bottom: 10rpx;
  293. .detail-item {
  294. font-size: 24rpx;
  295. color: #999999;
  296. background-color: #F5F5F5;
  297. padding: 6rpx 15rpx;
  298. border-radius: 15rpx;
  299. }
  300. }
  301. .user-location {
  302. .location-text {
  303. font-size: 24rpx;
  304. color: #999999;
  305. }
  306. }
  307. }
  308. .like-status {
  309. .like-icon {
  310. font-size: 40rpx;
  311. color: #E91E63;
  312. }
  313. }
  314. }
  315. </style>