513 lines
10 KiB
Vue
513 lines
10 KiB
Vue
<template>
|
||
<main class="search-page">
|
||
<section class="search-card">
|
||
<header class="page-header">
|
||
<div>
|
||
<p class="eyebrow">CUSTOMER MANAGEMENT</p>
|
||
<h1>查询用户</h1>
|
||
<p class="subtitle">根据车牌号快速查找客户服务账号</p>
|
||
</div>
|
||
<div class="header-icon" aria-hidden="true">
|
||
<svg viewBox="0 0 24 24">
|
||
<circle cx="10.8" cy="10.8" r="6.8" />
|
||
<path d="m16 16 5 5" />
|
||
</svg>
|
||
</div>
|
||
</header>
|
||
|
||
<form class="search-form" @submit.prevent="handleSearch">
|
||
<div class="search-input">
|
||
<svg viewBox="0 0 24 24" aria-hidden="true">
|
||
<circle cx="10.8" cy="10.8" r="6.8" />
|
||
<path d="m16 16 5 5" />
|
||
</svg>
|
||
<input v-model.trim="keyword" type="search" placeholder="请输入车牌号,如:粤B12345" aria-label="输入车牌号" />
|
||
<button v-if="keyword" type="button" class="clear-button" aria-label="清空搜索" @click="clearSearch">×</button>
|
||
</div>
|
||
<button class="search-button" type="submit">查询</button>
|
||
</form>
|
||
</section>
|
||
|
||
<section class="result-section" aria-live="polite">
|
||
<div class="result-heading">
|
||
<div>
|
||
<h2>{{ keyword ? '查询结果' : '最近更改的用户' }}</h2>
|
||
<p>{{ keyword ? `包含“${keyword}”的用户` : '展示最近更新的客户账号' }}</p>
|
||
</div>
|
||
<span class="result-count">共 {{ total }} 位</span>
|
||
</div>
|
||
|
||
<div class="user-list">
|
||
<article @click="toUser(user.plateNumber)" v-for="user in users" :key="user.id" class="user-item">
|
||
<div class="car-avatar">{{ user.plateNumber.slice(-1) }}</div>
|
||
<div class="user-info">
|
||
<strong>{{ user.plateNumber }}</strong>
|
||
<span>客户账号 · {{ user.updatedAt }}</span>
|
||
</div>
|
||
<span class="user-status" :class="user.status === '正常' ? 'normal' : 'disabled'">
|
||
<i></i>{{ user.status }}
|
||
</span>
|
||
<button class="more-button" type="button" :aria-label="`查看 ${user.plateNumber} 操作`">···</button>
|
||
</article>
|
||
|
||
<div v-if="loading" class="list-state">
|
||
<span class="loading-spinner"></span>正在加载用户...
|
||
</div>
|
||
<div v-else-if="errorMessage" class="list-state error-state">
|
||
{{ errorMessage }}
|
||
<button type="button" @click="loadUsers">重新加载</button>
|
||
</div>
|
||
<div v-else-if="!users.length" class="list-state">没有找到匹配的用户</div>
|
||
<div v-else-if="finished" class="list-state">已加载全部用户</div>
|
||
<div ref="loadMoreTarget" class="load-more-target" aria-hidden="true"></div>
|
||
</div>
|
||
</section>
|
||
</main>
|
||
</template>
|
||
|
||
<script setup>
|
||
import { nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
|
||
import { useRouter } from 'vue-router'
|
||
import apiClient from '../../apiClient/apiClient'
|
||
|
||
// 后续修改每页请求数量时只需要调整这里。
|
||
const PAGE_SIZE = 6
|
||
const keyword = ref('')
|
||
const users = ref([])
|
||
const total = ref(0)
|
||
const page = ref(1)
|
||
const loading = ref(false)
|
||
const finished = ref(false)
|
||
const errorMessage = ref('')
|
||
const loadMoreTarget = ref(null)
|
||
const router = useRouter()
|
||
let observer
|
||
|
||
const mockUsers = Array.from({ length: 24 }, (_, index) => {
|
||
const plates = ['粤B12345', '粤B8236K', '粤A1902M', '粤B5618T', '粤B7801V', '粤C29481', '粤A67832', '粤B40916']
|
||
return {
|
||
id: index + 1,
|
||
plate: plates[index % plates.length],
|
||
updatedAt: `${index + 1}分钟前更新`,
|
||
status: index === 7 || index === 16 ? '停用' : '正常',
|
||
}
|
||
})
|
||
|
||
/**
|
||
* 模拟用户接口。
|
||
* 后续接入真实接口时,保持返回 { list, total } 即可替换此方法。
|
||
*/
|
||
function fetchUsers({ page: currentPage, pageSize, keyword: searchKeyword }) {
|
||
|
||
/* return response.then(res => {
|
||
return {
|
||
list: res.data.list,
|
||
total: res.data.total,
|
||
};
|
||
}).catch(error => {
|
||
console.error('获取用户失败', error);
|
||
throw error;
|
||
}); */
|
||
return new Promise((resolve) => {
|
||
window.setTimeout(() => {
|
||
const normalizedKeyword = searchKeyword.toUpperCase()
|
||
const filteredUsers = normalizedKeyword
|
||
? mockUsers.filter((user) => user.plate.includes(normalizedKeyword))
|
||
: mockUsers
|
||
const start = (currentPage - 1) * pageSize
|
||
resolve({
|
||
list: filteredUsers.slice(start, start + pageSize),
|
||
total: filteredUsers.length,
|
||
})
|
||
}, 500)
|
||
})
|
||
}
|
||
|
||
async function loadUsers(reset = false) {
|
||
if (loading.value || (!reset && finished.value)) return
|
||
if (reset) {
|
||
page.value = 1
|
||
finished.value = false
|
||
users.value = []
|
||
errorMessage.value = ''
|
||
}
|
||
|
||
loading.value = true
|
||
try {
|
||
const result = await apiClient.post('/users', {
|
||
page: page.value - 1,
|
||
pageSize: PAGE_SIZE,
|
||
keyword: keyword.value,
|
||
});
|
||
const list = result.data?.list || []
|
||
console.log('获取用户列表', result.data.list);
|
||
users.value = reset ? list : users.value.concat(list)
|
||
total.value = result.data?.total ?? users.value.length
|
||
finished.value = list.length < PAGE_SIZE || users.value.length >= total.value
|
||
if (!finished.value) page.value += 1
|
||
} catch (error) {
|
||
console.error('获取用户失败', error)
|
||
errorMessage.value = '用户加载失败,请稍后重试'
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
function handleSearch() {
|
||
loadUsers(true)
|
||
}
|
||
|
||
function clearSearch() {
|
||
keyword.value = ''
|
||
loadUsers(true)
|
||
}
|
||
|
||
function toUser(plateNumber) {
|
||
router.push({
|
||
path: '/adminusers',
|
||
query: { plateNumber: plateNumber },
|
||
}) // 点击用户跳转到用户详情页
|
||
}
|
||
|
||
onMounted(async () => {
|
||
await loadUsers(true)
|
||
await nextTick()
|
||
observer = new IntersectionObserver(
|
||
([entry]) => entry.isIntersecting && loadUsers(),
|
||
{ rootMargin: '160px 0px' },
|
||
)
|
||
if (loadMoreTarget.value) observer.observe(loadMoreTarget.value)
|
||
})
|
||
|
||
onBeforeUnmount(() => observer?.disconnect())
|
||
</script>
|
||
|
||
<style scoped>
|
||
.search-page {
|
||
min-height: calc(100vh - 74px);
|
||
padding: 38px 24px 60px;
|
||
background: #f5f7fb;
|
||
color: #1d2b45;
|
||
}
|
||
|
||
.search-card,
|
||
.result-section {
|
||
width: 100%;
|
||
max-width: 860px;
|
||
margin: 0 auto;
|
||
}
|
||
|
||
.search-card {
|
||
padding: 28px 32px 30px;
|
||
background: #fff;
|
||
border: 1px solid #e8edf5;
|
||
border-radius: 12px;
|
||
box-shadow: 0 8px 26px #29466b0d;
|
||
}
|
||
|
||
.page-header,
|
||
.result-heading {
|
||
display: flex;
|
||
align-items: flex-start;
|
||
justify-content: space-between;
|
||
}
|
||
|
||
.eyebrow {
|
||
margin: 0 0 7px;
|
||
color: #1769e1;
|
||
font-size: 10px;
|
||
font-weight: 700;
|
||
letter-spacing: 1.5px;
|
||
}
|
||
|
||
h1,
|
||
h2 {
|
||
margin: 0;
|
||
color: #1b2942;
|
||
}
|
||
|
||
h1 {
|
||
font-size: 24px;
|
||
}
|
||
|
||
.subtitle,
|
||
.result-heading p {
|
||
margin: 7px 0 0;
|
||
color: #8d98aa;
|
||
font-size: 12px;
|
||
}
|
||
|
||
.header-icon {
|
||
display: grid;
|
||
width: 44px;
|
||
height: 44px;
|
||
place-items: center;
|
||
color: #1769e1;
|
||
background: #eaf2ff;
|
||
border-radius: 10px;
|
||
}
|
||
|
||
.header-icon svg,
|
||
.search-input>svg {
|
||
width: 21px;
|
||
fill: none;
|
||
stroke: currentColor;
|
||
stroke-linecap: round;
|
||
stroke-linejoin: round;
|
||
stroke-width: 1.7;
|
||
}
|
||
|
||
.search-form {
|
||
display: flex;
|
||
margin-top: 25px;
|
||
gap: 10px;
|
||
}
|
||
|
||
.search-input {
|
||
display: flex;
|
||
height: 46px;
|
||
flex: 1;
|
||
align-items: center;
|
||
padding: 0 13px;
|
||
gap: 9px;
|
||
border: 1px solid #dfe6ef;
|
||
border-radius: 7px;
|
||
transition: border-color .2s, box-shadow .2s;
|
||
}
|
||
|
||
.search-input:focus-within {
|
||
border-color: #1769e1;
|
||
box-shadow: 0 0 0 3px #1769e11a;
|
||
}
|
||
|
||
.search-input input {
|
||
width: 100%;
|
||
min-width: 0;
|
||
padding: 0;
|
||
outline: 0;
|
||
border: 0;
|
||
color: #263650;
|
||
background: transparent;
|
||
font: inherit;
|
||
font-size: 13px;
|
||
}
|
||
|
||
.search-input input::-webkit-search-cancel-button {
|
||
display: none;
|
||
}
|
||
|
||
.clear-button,
|
||
.more-button {
|
||
padding: 0;
|
||
color: #99a5b4;
|
||
background: none;
|
||
border: 0;
|
||
cursor: pointer;
|
||
}
|
||
|
||
.clear-button {
|
||
font-size: 21px;
|
||
}
|
||
|
||
.search-button {
|
||
width: 90px;
|
||
color: #fff;
|
||
background: #1769e1;
|
||
border: 0;
|
||
border-radius: 7px;
|
||
box-shadow: 0 5px 12px #1769e133;
|
||
cursor: pointer;
|
||
font: inherit;
|
||
font-size: 13px;
|
||
}
|
||
|
||
.search-button:hover {
|
||
background: #0e5ac7;
|
||
}
|
||
|
||
.result-section {
|
||
margin-top: 28px;
|
||
}
|
||
|
||
.result-heading {
|
||
align-items: center;
|
||
margin-bottom: 13px;
|
||
}
|
||
|
||
h2 {
|
||
font-size: 17px;
|
||
}
|
||
|
||
.result-count {
|
||
color: #8d98aa;
|
||
font-size: 12px;
|
||
}
|
||
|
||
.user-list {
|
||
overflow: hidden;
|
||
background: #fff;
|
||
border: 1px solid #e8edf5;
|
||
border-radius: 10px;
|
||
}
|
||
|
||
.user-item {
|
||
display: flex;
|
||
min-height: 76px;
|
||
align-items: center;
|
||
padding: 0 20px;
|
||
border-bottom: 1px solid #edf0f5;
|
||
}
|
||
|
||
.car-avatar {
|
||
display: grid;
|
||
width: 38px;
|
||
height: 38px;
|
||
margin-right: 13px;
|
||
place-items: center;
|
||
color: #1769e1;
|
||
background: #eaf2ff;
|
||
border-radius: 9px;
|
||
font-size: 16px;
|
||
font-weight: 700;
|
||
}
|
||
|
||
.user-info {
|
||
display: flex;
|
||
min-width: 0;
|
||
flex: 1;
|
||
flex-direction: column;
|
||
gap: 5px;
|
||
}
|
||
|
||
.user-info strong {
|
||
color: #354158;
|
||
font-size: 13px;
|
||
}
|
||
|
||
.user-info span {
|
||
color: #9aa4b3;
|
||
font-size: 11px;
|
||
}
|
||
|
||
.user-status {
|
||
display: flex;
|
||
align-items: center;
|
||
margin-right: 18px;
|
||
gap: 5px;
|
||
font-size: 11px;
|
||
}
|
||
|
||
.user-status i {
|
||
width: 6px;
|
||
height: 6px;
|
||
border-radius: 50%;
|
||
}
|
||
|
||
.user-status.normal {
|
||
color: #239366;
|
||
}
|
||
|
||
.user-status.normal i {
|
||
background: #38b982;
|
||
}
|
||
|
||
.user-status.disabled {
|
||
color: #d78a37;
|
||
}
|
||
|
||
.user-status.disabled i {
|
||
background: #e8a24d;
|
||
}
|
||
|
||
.more-button {
|
||
width: 28px;
|
||
font-size: 17px;
|
||
letter-spacing: 2px;
|
||
}
|
||
|
||
.list-state {
|
||
padding: 24px 0;
|
||
color: #99a5b4;
|
||
font-size: 12px;
|
||
text-align: center;
|
||
}
|
||
|
||
.list-state button {
|
||
margin-left: 8px;
|
||
padding: 0;
|
||
color: #1769e1;
|
||
background: none;
|
||
border: 0;
|
||
cursor: pointer;
|
||
font: inherit;
|
||
}
|
||
|
||
.error-state {
|
||
color: #d47725;
|
||
}
|
||
|
||
.loading-spinner {
|
||
display: inline-block;
|
||
width: 13px;
|
||
height: 13px;
|
||
margin-right: 7px;
|
||
vertical-align: -2px;
|
||
border: 2px solid #dce9ff;
|
||
border-top-color: #1769e1;
|
||
border-radius: 50%;
|
||
animation: spin .7s linear infinite;
|
||
}
|
||
|
||
.load-more-target {
|
||
height: 1px;
|
||
}
|
||
|
||
@keyframes spin {
|
||
to {
|
||
transform: rotate(360deg);
|
||
}
|
||
}
|
||
|
||
@media (max-width: 760px) {
|
||
.search-page {
|
||
min-height: calc(100vh - 116px);
|
||
padding: 22px 14px 40px;
|
||
}
|
||
|
||
.search-card {
|
||
padding: 22px 18px 24px;
|
||
}
|
||
|
||
h1 {
|
||
font-size: 21px;
|
||
}
|
||
|
||
.header-icon {
|
||
width: 38px;
|
||
height: 38px;
|
||
}
|
||
|
||
.search-form {
|
||
gap: 8px;
|
||
}
|
||
|
||
.search-button {
|
||
width: 68px;
|
||
}
|
||
|
||
.user-item {
|
||
min-height: 70px;
|
||
padding: 0 13px;
|
||
}
|
||
|
||
.user-status {
|
||
margin-right: 7px;
|
||
}
|
||
|
||
.user-info span {
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
}
|
||
</style> |