Parcourir la source

feat(recommend): 添加用户浏览记录功能

- 实现保存用户浏览记录的API接口
- 添加获取用户浏览历史列表功能
- 添加获取用户浏览记录数量统计
- 在用户详情页自动保存浏览记录
- 在我的页面显示浏览记录统计数据
- 实现浏览历史页面的数据加载和展示
- 添加年龄、学历、收入等信息的格式化处理
李思佳 il y a 3 jours
Parent
commit
045b8f9ac4

+ 3 - 0
LiangZhiYUMao/pages/mine/index.vue

@@ -419,6 +419,9 @@ export default {
         
         // 获取喜欢我的用户真实数量
         this.getLikedMeUsersCount()
+        
+        // 获取我浏览的用户真实数量
+        this.getBrowseHistoryCount()
 
         // 立即加载最新用户信息(不延迟)
         this.loadUserInfo()

+ 67 - 2
LiangZhiYUMao/pages/mine/visited-by-me.vue

@@ -63,8 +63,35 @@ export default {
     async loadBrowseHistory() {
       try {
         this.loading = true
-        // 不调用真实API,显示空数据
-        this.browseHistory = []
+        // 获取当前登录用户ID
+        const userId = uni.getStorageSync('userId')
+        if (!userId) {
+          uni.showToast({
+            title: '请先登录',
+            icon: 'none'
+          })
+          return
+        }
+        
+        // 调用API获取浏览记录
+        const history = await api.recommend.getBrowseHistory(parseInt(userId), this.pageSize, (this.pageNum - 1) * this.pageSize)
+        if (history && Array.isArray(history)) {
+          // 处理API返回的数据,转换为页面需要的格式
+          this.browseHistory = history.map(item => ({
+            id: item.userId,
+            userId: item.userId,
+            avatar: item.avatarUrl || '/static/close.png',
+            nickname: item.nickname || '未设置',
+            age: this.calculateAge(item.birthDate),
+            height: item.height,
+            weight: item.weight,
+            educationText: this.getEducationText(item.educationLevel),
+            salaryText: this.getSalaryText(item.salaryRange),
+            viewTime: item.createTime || new Date().toISOString()
+          }))
+        } else {
+          this.browseHistory = []
+        }
       } catch (error) {
         console.error('加载浏览记录失败:', error)
         uni.showToast({
@@ -76,6 +103,44 @@ export default {
       }
     },
     
+    // 计算年龄
+    calculateAge(birthDate) {
+      if (!birthDate) return ''
+      const birth = new Date(birthDate)
+      const now = new Date()
+      let age = now.getFullYear() - birth.getFullYear()
+      if (now.getMonth() < birth.getMonth() || (now.getMonth() === birth.getMonth() && now.getDate() < birth.getDate())) {
+        age--
+      }
+      return age
+    },
+    
+    // 获取学历文本
+    getEducationText(educationLevel) {
+      if (educationLevel === undefined || educationLevel === null) return ''
+      const educationMap = {
+        1: '高中及以下',
+        2: '大专',
+        3: '本科',
+        4: '硕士',
+        5: '博士'
+      }
+      return educationMap[educationLevel] || ''
+    },
+    
+    // 获取收入文本
+    getSalaryText(salaryRange) {
+      if (salaryRange === undefined || salaryRange === null) return ''
+      const salaryMap = {
+        1: '<5k',
+        2: '5-10k',
+        3: '10-20k',
+        4: '20-50k',
+        5: '50k+'
+      }
+      return salaryMap[salaryRange] || ''
+    },
+    
     // 格式化时间
     formatTime(time) {
       if (!time) return ''

+ 2 - 0
LiangZhiYUMao/pages/recommend/user-detail.vue

@@ -181,6 +181,8 @@ export default {
 			}
 			this.loadUserInfo()
 			this.loadUserDynamics()
+			// 保存用户浏览记录
+			this.saveBrowseHistory()
 		} else {
 			uni.showToast({
 				title: '用户ID无效',

+ 13 - 0
LiangZhiYUMao/utils/api.js

@@ -549,6 +549,19 @@ export default {
     // 获取喜欢我的用户数量
     getLikedMeUsersCount: (userId) => request({
       url: `/recommend/liked-me-users-count?userId=${userId}`
+    }),
+    // 保存用户浏览记录
+    saveUserLook: (userId, lookUserId) => request({
+      url: `/recommend/save-look?userId=${userId}&lookUserId=${lookUserId}`,
+      method: 'POST'
+    }),
+    // 获取用户浏览记录列表
+    getBrowseHistory: (userId, limit, offset) => request({
+      url: `/recommend/browse-history?userId=${userId}${limit ? `&limit=${limit}` : ''}${offset ? `&offset=${offset}` : ''}`
+    }),
+    // 获取用户浏览记录数量
+    getBrowseHistoryCount: (userId) => request({
+      url: `/recommend/browse-history-count?userId=${userId}`
     })
   },
 

+ 14 - 0
service/Recommend/src/main/java/com/zhentao/controller/RecommendController.java

@@ -307,6 +307,20 @@ public class RecommendController {
         }
     }
     
+    // 获取喜欢我的用户数量
+    @GetMapping("/liked-me-users-count")
+    public Result<Integer> getUsersWhoLikedMeCount(
+            @RequestParam("userId") Integer userId
+    ) {
+        try {
+            Integer count = recommendService.countUsersWhoLikedMe(userId);
+            return Result.success(count);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return Result.error("获取喜欢我的用户数量失败: " + e.getMessage());
+        }
+    }
+    
     // 保存用户浏览记录
     @PostMapping("/save-look")
     public Result<Boolean> saveUserLook(