| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- <template>
- <view class="like-page">
- <view v-if="loading && users.length === 0" class="loading-container">
- <uni-loading type="circle" color="#E91E63"></uni-loading>
- <text class="loading-text">加载中...</text>
- </view>
-
- <view v-else-if="users.length === 0" class="empty-container">
- <text class="empty-icon">😊</text>
- <text class="empty-text">暂无用户喜欢您</text>
- <view class="empty-action">
- <button class="action-btn" @click="goRecommend">完善资料增加曝光</button>
- </view>
- </view>
-
- <scroll-view v-else class="user-list" scroll-y="true" @scrolltolower="loadMoreUsers">
- <view class="user-item" v-for="user in users" :key="user.userId" @click="goUserDetail(user.userId)">
- <image class="user-avatar" :src="user.avatar" mode="aspectFill" @error="handleAvatarError(user)"></image>
- <view class="user-info">
- <view class="user-basic">
- <text class="user-nickname">{{ user.nickname }}</text>
- <text class="user-age">{{ user.age }}岁</text>
- </view>
- <view class="user-details">
- <text class="detail-item" v-if="user.height">{{ user.height }}cm</text>
- <text class="detail-item" v-if="user.weight">{{ user.weight }}kg</text>
- <text class="detail-item" v-if="user.educationText">{{ user.educationText }}</text>
- <text class="detail-item" v-if="user.salaryText">{{ user.salaryText }}</text>
- </view>
- <view class="user-location" v-if="user.location">
- <text class="location-text">{{ user.location }}</text>
- </view>
- </view>
- <view class="like-status">
- <text class="like-icon">💖</text>
- </view>
- </view>
-
- <view v-if="loadingMore" class="loading-more">
- <uni-loading type="circle" color="#E91E63" size="20"></uni-loading>
- <text class="loading-more-text">加载更多...</text>
- </view>
-
- <view v-else-if="!hasMore && users.length > 0" class="no-more">
- <text class="no-more-text">没有更多了</text>
- </view>
- </scroll-view>
- </view>
- </template>
- <script>
- import api from '@/utils/api.js'
- const EDUCATION_MAP = {
- 1: '初中',
- 2: '高中',
- 3: '大专',
- 4: '本科',
- 5: '硕士',
- 6: '博士'
- }
- const SALARY_MAP = {
- 1: '3k以下',
- 2: '3k-5k',
- 3: '5k-8k',
- 4: '8k-12k',
- 5: '12k-20k',
- 6: '20k-50k',
- 7: '50k以上'
- }
- export default {
- data() {
- return {
- users: [],
- loading: true,
- loadingMore: false,
- pageNum: 1,
- pageSize: 20,
- hasMore: true,
- currentUserId: null
- }
- },
- onLoad() {
- this.getCurrentUserId()
- },
- onShow() {
- if (!this.users.length || this.users.length === 0) {
- this.loadUsers()
- }
- },
- methods: {
- getCurrentUserId() {
- try {
- const userInfo = uni.getStorageSync('userInfo')
- if (userInfo) {
- this.currentUserId = userInfo.userId || userInfo.id
- }
- if (!this.currentUserId) {
- const token = uni.getStorageSync('token')
- if (token) {
- console.warn('无法获取当前用户ID,请检查登录状态')
- }
- }
- } catch (e) {
- console.error('获取用户信息失败:', e)
- }
- },
-
- transformUserData(user) {
- if (!user) return null
-
- const birthDate = user.birthDate || user.birth_date
- let age = ''
- if (birthDate) {
- try {
- const birth = new Date(birthDate)
- if (!isNaN(birth.getTime())) {
- const today = new Date()
- let userAge = today.getFullYear() - birth.getFullYear()
- const monthDiff = today.getMonth() - birth.getMonth()
- if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
- userAge--
- }
- age = userAge > 0 ? userAge : ''
- }
- } catch (e) {
- console.error('计算年龄失败:', e)
- }
- }
-
- let educationText = ''
- if (user.educationLevel !== undefined && user.educationLevel !== null) {
- educationText = EDUCATION_MAP[user.educationLevel] || ''
- }
-
- let salaryText = ''
- if (user.salaryRange !== undefined && user.salaryRange !== null) {
- salaryText = SALARY_MAP[user.salaryRange] || ''
- }
-
- let location = ''
- if (user.cityId || user.provinceId) {
- location = this.formatLocation(user.provinceId, user.cityId)
- }
-
- return {
- userId: user.userId || user.user_id,
- nickname: user.nickname || '匿名用户',
- avatar: user.avatarUrl || user.avatar_url || user.avatar || '/static/default-avatar.png',
- age: age,
- height: user.height || '',
- weight: user.weight || '',
- educationText: educationText,
- salaryText: salaryText,
- location: location,
- gender: user.gender
- }
- },
-
- formatLocation(provinceId, cityId) {
- if (!provinceId && !cityId) return ''
- const locationMap = this.$const.LOCATION_MAP
- if (locationMap && locationMap.length > 0) {
- try {
- const province = locationMap.find(p => p.id === provinceId)
- const city = province && province.children ? province.children.find(c => c.id === cityId) : null
- if (city) {
- return city.name || ''
- }
- } catch (e) {
- console.error('获取位置信息失败:', e)
- }
- }
- return ''
- },
-
- async loadUsers() {
- if (!this.currentUserId) {
- this.getCurrentUserId()
- if (!this.currentUserId) {
- uni.showToast({
- title: '请先登录',
- icon: 'none'
- })
- this.loading = false
- return
- }
- }
-
- try {
- this.loading = true
- const res = await api.recommend.getLikedMeUsers(this.currentUserId, this.pageSize, 0)
-
- if (res && Array.isArray(res)) {
- this.users = res.map(user => this.transformUserData(user))
- this.hasMore = res.length >= this.pageSize
- } else {
- this.users = []
- this.hasMore = false
- }
-
- if (this.users.length === 0) {
- console.log('没有喜欢我的用户')
- }
- } catch (error) {
- console.error('加载喜欢我的用户失败:', error)
- this.users = []
- uni.showToast({
- title: '加载失败,请重试',
- icon: 'none'
- })
- } finally {
- this.loading = false
- }
- },
-
- async loadMoreUsers() {
- if (this.loadingMore || !this.hasMore) {
- return
- }
-
- if (!this.currentUserId) {
- return
- }
-
- try {
- this.loadingMore = true
- const offset = (this.pageNum) * this.pageSize
- const res = await api.recommend.getLikedMeUsers(this.currentUserId, this.pageSize, offset)
-
- if (res && Array.isArray(res) && res.length > 0) {
- const newUsers = res.map(user => this.transformUserData(user))
- this.users = this.users.concat(newUsers)
- this.pageNum++
- this.hasMore = res.length >= this.pageSize
- } else {
- this.hasMore = false
- }
- } catch (error) {
- console.error('加载更多用户失败:', error)
- uni.showToast({
- title: '加载更多失败',
- icon: 'none'
- })
- } finally {
- this.loadingMore = false
- }
- },
-
- handleAvatarError(user) {
- if (user) {
- user.avatar = '/static/default-avatar.png'
- }
- },
-
- goUserDetail(userId) {
- if (!userId) {
- uni.showToast({
- title: '用户信息不完整',
- icon: 'none'
- })
- return
- }
- uni.navigateTo({
- url: `/pages/recommend/index?userId=${userId}`,
- fail: (err) => {
- console.error('跳转用户详情失败:', err)
- uni.showToast({
- title: '页面不存在',
- icon: 'none'
- })
- }
- })
- },
-
- goRecommend() {
- uni.navigateTo({
- url: '/pages/profile/index'
- })
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .like-page {
- min-height: 100vh;
- background-color: #F5F5F5;
- padding: 0 5rpx;
- box-sizing: border-box;
- overflow-x: hidden;
- }
- .loading-container {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- padding: 100rpx 0;
-
- .loading-text {
- margin-top: 20rpx;
- font-size: 28rpx;
- color: #999999;
- }
- }
- .empty-container {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- padding: 150rpx 0;
-
- .empty-icon {
- font-size: 120rpx;
- margin-bottom: 30rpx;
- }
-
- .empty-text {
- font-size: 32rpx;
- color: #999999;
- margin-bottom: 40rpx;
- }
-
- .action-btn {
- background: linear-gradient(135deg, #FF6B9D 0%, #E91E63 100%);
- color: #FFFFFF;
- border: none;
- padding: 18rpx 70rpx;
- border-radius: 45rpx;
- font-size: 28rpx;
- font-weight: 600;
- box-shadow: 0 6rpx 20rpx rgba(233, 30, 99, 0.3);
- transition: all 0.3s ease;
- letter-spacing: 1rpx;
-
- &:active {
- transform: translateY(2rpx);
- box-shadow: 0 3rpx 12rpx rgba(233, 30, 99, 0.4);
- background: linear-gradient(135deg, #E91E63 0%, #C2185B 100%);
- }
- }
- }
- .user-list {
- padding: 20rpx;
- height: calc(100vh - 88rpx);
- }
- .user-item {
- display: flex;
- align-items: center;
- background-color: #FFFFFF;
- border-radius: 20rpx;
- padding: 20rpx;
- margin-bottom: 20rpx;
- box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
- transition: all 0.3s ease;
-
- &:active {
- transform: translateY(2rpx);
- box-shadow: 0 1rpx 5rpx rgba(0, 0, 0, 0.05);
- }
-
- .user-avatar {
- width: 120rpx;
- height: 120rpx;
- border-radius: 50%;
- margin-right: 20rpx;
- border: 4rpx solid #FFF9F9;
- box-shadow: 0 4rpx 15rpx rgba(233, 30, 99, 0.2);
- }
-
- .user-info {
- flex: 1;
-
- .user-basic {
- display: flex;
- align-items: center;
- margin-bottom: 10rpx;
-
- .user-nickname {
- font-size: 32rpx;
- font-weight: bold;
- color: #333333;
- margin-right: 15rpx;
- }
-
- .user-age {
- font-size: 28rpx;
- color: #666666;
- }
- }
-
- .user-details {
- display: flex;
- flex-wrap: wrap;
- gap: 15rpx;
- margin-bottom: 10rpx;
-
- .detail-item {
- font-size: 24rpx;
- color: #999999;
- background-color: #F5F5F5;
- padding: 6rpx 15rpx;
- border-radius: 15rpx;
- }
- }
-
- .user-location {
- .location-text {
- font-size: 24rpx;
- color: #999999;
- }
- }
- }
-
- .like-status {
- .like-icon {
- font-size: 40rpx;
- color: #E91E63;
- }
- }
- }
- .loading-more {
- display: flex;
- align-items: center;
- justify-content: center;
- padding: 30rpx 0;
-
- .loading-more-text {
- margin-left: 16rpx;
- font-size: 26rpx;
- color: #999999;
- }
- }
- .no-more {
- text-align: center;
- padding: 30rpx 0;
-
- .no-more-text {
- font-size: 26rpx;
- color: #999999;
- }
- }
- </style>
|