Files
YwxAppThink/addon/wxchat/YwxMakeFriends/pages/real-match/index.uvue
T

465 lines
9.9 KiB
Plaintext

<template>
<view class="page">
<view class="nav-bar">
<view class="nav-back" @click="goBack">
<uni-icons type="left" size="22" color="#333"></uni-icons>
</view>
<text class="nav-title">真人匹配</text>
<view class="nav-right">
<uni-icons type="reload" size="22" color="#333" @click="reload"></uni-icons>
</view>
</view>
<view class="filter-bar">
<text class="filter-label">缘分卡片</text>
<text class="filter-sub">右滑喜欢 · 左滑跳过,遇到同频的人</text>
</view>
<!-- 滑卡区 -->
<view class="card-stack" v-if="displayCards.length > 0">
<view v-for="(card, idx) in displayCards" :key="card.uid" class="card"
:style="cardStyle(idx)">
<!-- 顶部照片 -->
<image class="card-photo" :src="card.avatar" mode="aspectFill"></image>
<!-- 喜欢 / 跳过 印章 -->
<view v-if="idx === 0" class="stamp like-stamp" :style="{ opacity: likeOpacity }">喜欢</view>
<view v-if="idx === 0" class="stamp nope-stamp" :style="{ opacity: nopeOpacity }">跳过</view>
<!-- 底部信息 -->
<view class="card-info">
<view class="card-name-row">
<text class="card-name">{{ card.name }}</text>
<text class="card-age" v-if="card.age > 0">{{ card.age }}岁</text>
<text class="card-online" v-if="card.isOnline">在线</text>
</view>
<view class="card-meta" v-if="card.location || card.distance > 0">
<text class="card-meta-i" v-if="card.location">{{ card.location }}</text>
<text class="card-meta-i" v-if="card.distance > 0">{{ card.distance }}km</text>
</view>
<text class="card-bio" v-if="card.bio">{{ card.bio }}</text>
</view>
<!-- 仅顶层卡片绑定手势 -->
<view v-if="idx === 0" class="card-gesture"
@touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd"></view>
</view>
</view>
<!-- 空态 -->
<view v-else class="empty">
<text class="empty-text">附近暂时没有更多缘分啦~</text>
<button class="empty-btn" @click="reload">换一批</button>
</view>
<!-- 操作按钮 -->
<view class="actions" v-if="displayCards.length > 0">
<view class="act-btn skip" @click="skipTop">
<uni-icons type="close" size="28" color="#FF6B6B"></uni-icons>
</view>
<view class="act-btn like" @click="likeTop">
<uni-icons type="heart" size="30" color="#fff"></uni-icons>
</view>
</view>
<view class="tip">滑动卡片或点击按钮,喜欢即关注对方</view>
</view>
</template>
<script setup lang="uts">
import { ref, computed, onMounted } from 'vue'
import { Api } from '@/common/api-service.uts'
import { getUserInfo } from '@/stores/user.uts'
const cards = ref<UTSJSONObject[]>([])
const offsetX = ref(0)
const rotate = ref(0)
const likeOpacity = ref(0)
const nopeOpacity = ref(0)
const dragging = ref(false)
let startX = 0
let startY = 0
let loading = false
const displayCards = computed<UTSJSONObject[]>(() => {
return cards.value.slice(0, 3)
})
const meUid = computed<number>(() => {
return getUserInfo()?.uid ?? 0
})
const mapCard = (it : UTSJSONObject) : UTSJSONObject => {
const city = it.getString('residecity') ?? ''
const province = it.getString('resideprovince') ?? ''
return {
uid: it.getNumber('uid') ?? 0,
name: it.getString('nickname') ?? '匿名用户',
avatar: it.getString('avatar') ?? '',
age: it.getNumber('age') ?? 0,
isOnline: it.getBoolean('is_online') ?? false,
location: city.length > 0 ? city : province,
distance: it.getNumber('distance') ?? 0,
bio: it.getString('bio') ?? ''
} as UTSJSONObject
}
const loadUsers = async () => {
if (loading) return
loading = true
try {
let data : UTSJSONObject | null = null
let list : any = null
try {
data = await Api.user.nearby({ limit: 30, radius: 100 })
list = data.getJSONArray('list')
} catch (e) {
console.warn('nearby 失败,回退 local/online', e)
}
if (list == null || list.size() == 0) {
try {
data = await Api.user.local({})
list = data.getJSONArray('list')
} catch (e) { console.warn('local 失败', e) }
}
if (list == null || list.size() == 0) {
try {
data = await Api.user.online({})
list = data.getJSONArray('list')
} catch (e) { console.warn('online 失败', e) }
}
const arr : UTSJSONObject[] = []
if (list != null) {
for (let i = 0; i < list.size(); i++) {
const it = list.get(i) as UTSJSONObject
if (it != null) arr.push(mapCard(it))
}
}
if (arr.length > 0) {
cards.value = arr
}
} catch (e) {
console.warn('加载匹配用户失败', e)
} finally {
loading = false
}
}
const reload = () => {
offsetX.value = 0
rotate.value = 0
likeOpacity.value = 0
nopeOpacity.value = 0
loadUsers()
}
const removeTop = () => {
if (cards.value.length > 0) {
cards.value.splice(0, 1)
}
offsetX.value = 0
rotate.value = 0
likeOpacity.value = 0
nopeOpacity.value = 0
if (cards.value.length === 0) {
loadUsers()
}
}
const likeTop = () => {
const top = cards.value[0]
if (top != null) {
const uid = top.getNumber('uid') ?? 0
if (uid > 0 && meUid.value > 0) {
Api.follow.toggle(meUid.value, uid, 1).then(() => {
uni.showToast({ title: '已喜欢,已关注', icon: 'none' })
}).catch((err : any) => {
console.warn('关注失败', err)
})
}
}
removeTop()
}
const skipTop = () => {
removeTop()
}
// ===== 手势 =====
const onTouchStart = (e : any) => {
dragging.value = true
const t = e.touches[0]
startX = t.clientX
startY = t.clientY
}
const onTouchMove = (e : any) => {
if (! dragging.value) return
const t = e.touches[0]
const dx = t.clientX - startX
const dy = t.clientY - startY
offsetX.value = dx
rotate.value = dx / 20
likeOpacity.value = dx > 0 ? Math.min(dx / 100, 1) : 0
nopeOpacity.value = dx < 0 ? Math.min(-dx / 100, 1) : 0
}
const onTouchEnd = (e : any) => {
if (! dragging.value) return
dragging.value = false
const dx = offsetX.value
if (dx > 100) {
likeTop()
} else if (dx < -100) {
skipTop()
} else {
offsetX.value = 0
rotate.value = 0
likeOpacity.value = 0
nopeOpacity.value = 0
}
}
const cardStyle = (idx : number) : UTSJSONObject => {
if (idx === 0) {
return {
transform: 'translateX(' + offsetX.value + 'px) rotate(' + rotate.value + 'deg)',
zIndex: 10
} as UTSJSONObject
}
return {
transform: 'scale(' + (1 - idx * 0.05) + ') translateY(' + (idx * 16) + 'px)',
zIndex: 10 - idx,
opacity: 0.92
} as UTSJSONObject
}
const goBack = () => {
uni.navigateBack()
}
onMounted(() => {
loadUsers()
})
</script>
<style>
.page {
display: flex;
flex-direction: column;
background: linear-gradient(180deg, #FFE9EC 0%, #f6f6f6 40%);
min-height: 100%;
box-sizing: border-box;
padding-bottom: 40rpx;
}
.nav-bar {
display: flex;
flex-direction: row;
align-items: center;
padding: 20rpx 24rpx;
padding-top: 60rpx;
}
.nav-back,
.nav-right {
width: 80rpx;
display: flex;
align-items: center;
}
.nav-right {
justify-content: flex-end;
}
.nav-title {
flex: 1;
text-align: center;
font-size: 34rpx;
font-weight: bold;
color: #333;
}
.filter-bar {
display: flex;
flex-direction: column;
padding: 10rpx 40rpx 20rpx;
}
.filter-label {
font-size: 38rpx;
font-weight: bold;
color: #222;
}
.filter-sub {
font-size: 24rpx;
color: #999;
margin-top: 8rpx;
}
.card-stack {
position: relative;
height: 880rpx;
margin: 20rpx 50rpx;
}
.card {
position: absolute;
left: 0;
right: 0;
top: 0;
height: 880rpx;
border-radius: 28rpx;
overflow: hidden;
background-color: #fff;
box-shadow: 0 12rpx 40rpx rgba(0, 0, 0, 0.12);
}
.card-photo {
width: 100%;
height: 700rpx;
background-color: #eee;
}
.card-gesture {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
.stamp {
position: absolute;
top: 60rpx;
padding: 10rpx 30rpx;
border: 6rpx solid;
border-radius: 16rpx;
font-size: 52rpx;
font-weight: bold;
transform: rotate(-18deg);
}
.like-stamp {
left: 40rpx;
color: #4CD964;
border-color: #4CD964;
}
.nope-stamp {
right: 40rpx;
color: #FF3B30;
border-color: #FF3B30;
transform: rotate(18deg);
}
.card-info {
position: absolute;
left: 0;
right: 0;
bottom: 0;
padding: 24rpx 30rpx 40rpx;
background: linear-gradient(180deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.55) 100%);
}
.card-name-row {
display: flex;
flex-direction: row;
align-items: center;
}
.card-name {
font-size: 40rpx;
font-weight: bold;
color: #fff;
}
.card-age {
font-size: 32rpx;
color: #fff;
margin-left: 16rpx;
}
.card-online {
font-size: 22rpx;
color: #fff;
background: #4CD964;
border-radius: 20rpx;
padding: 4rpx 14rpx;
margin-left: 16rpx;
}
.card-meta {
display: flex;
flex-direction: row;
margin-top: 12rpx;
}
.card-meta-i {
font-size: 24rpx;
color: #fff;
background: rgba(255, 255, 255, 0.25);
border-radius: 20rpx;
padding: 4rpx 16rpx;
margin-right: 12rpx;
}
.card-bio {
font-size: 24rpx;
color: #f0f0f0;
margin-top: 12rpx;
overflow: hidden;
}
.actions {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
margin-top: 30rpx;
}
.act-btn {
width: 110rpx;
height: 110rpx;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
margin: 0 40rpx;
background: #fff;
box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.12);
}
.act-btn.like {
background: linear-gradient(135deg, #FF6B6B, #FF8E53);
}
.tip {
text-align: center;
font-size: 24rpx;
color: #999;
margin-top: 30rpx;
}
.empty {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 120rpx 0;
}
.empty-text {
font-size: 28rpx;
color: #aaa;
margin-bottom: 30rpx;
}
.empty-btn {
background: linear-gradient(135deg, #FF6B6B, #FF8E53);
color: #fff;
border: none;
border-radius: 40rpx;
font-size: 26rpx;
padding: 12rpx 50rpx;
}
</style>