liufumingren 8f1a8b7ddb feat(miniprogram): 新增 uni-app + Vue3 + TS 微信小程序
- 登录:微信一键登录(code2Session)+ 账号登录双 tab;个人中心为整页登录,其它页走抽屉
- 协议:登录前必须阅读并同意,版本号随登录上报落库
- 不强制登录:启动不弹窗,点功能或遇到 401 才提示(符合小程序审核)
- 页面:首页统计 / 扫码记录 / 个人中心;H5 预览端口 7304

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-21 00:47:16 +08:00

375 lines
9.6 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup lang="ts">
/**
* 首页:当前主体的统计 + 扫码记录(只保留「首页 / 我的」两个 tab)
*
* 游客:只展示功能介绍与登录入口(不强制登录);
* 登录后:设备 / 通道 / 记录数量 + 扫码记录列表(按条码筛选、上拉加载更多)。
*/
import { computed, ref, watch } from 'vue';
import { onPullDownRefresh, onReachBottom, onShow } from '@dcloudio/uni-app';
import { ApiError } from '@/api/client';
import { fetchRecords, fetchStats } from '@/api/web';
import type { ScanRecord, Stats } from '@/api/types';
import LoginDrawer from '@/components/LoginDrawer.vue';
import { authVersion, currentUser, displayName, isLoggedIn } from '@/composables/useAuth';
import { openLoginDrawer } from '@/composables/useLoginDrawer';
import {
currentSubject,
currentSubjectId,
ensureSubjects,
hasSubject,
loadSubjects,
setSubjectId,
subjects,
} from '@/composables/useSubject';
import { formatTime, fromNow } from '@/utils/format';
const PAGE_SIZE = 20;
const stats = ref<Stats | null>(null);
const loading = ref(false);
const error = ref('');
const records = ref<ScanRecord[]>([]);
const total = ref(0);
const nextPage = ref(1);
const listLoading = ref(false);
const finished = ref(false);
const barcode = ref('');
const listError = ref('');
const loggedIn = computed(() => Boolean(currentUser.value));
const subjectNames = computed(() => subjects.value.map(item => item.name));
const subjectIndex = computed(() => {
const index = subjects.value.findIndex(item => item.id === currentSubjectId.value);
return index >= 0 ? index : 0;
});
async function loadStats() {
stats.value = hasSubject.value ? await fetchStats() : null;
}
async function loadRecords(reset = false) {
if (!hasSubject.value || listLoading.value) return;
if (reset) {
nextPage.value = 1;
finished.value = false;
}
listLoading.value = true;
listError.value = '';
try {
const data = await fetchRecords({
page: nextPage.value,
pageSize: PAGE_SIZE,
barcode: barcode.value.trim(),
});
records.value = reset ? data.list : [...records.value, ...data.list];
total.value = data.total;
nextPage.value = data.page + 1;
finished.value = records.value.length >= data.total;
} catch (err) {
listError.value = err instanceof ApiError ? err.message : '加载失败';
} finally {
listLoading.value = false;
}
}
async function load() {
if (!isLoggedIn()) {
stats.value = null;
records.value = [];
total.value = 0;
return;
}
loading.value = true;
error.value = '';
try {
await ensureSubjects();
await loadStats();
await loadRecords(true);
} catch (err) {
error.value = err instanceof ApiError ? err.message : '加载失败';
} finally {
loading.value = false;
}
}
async function reloadSubjects() {
try {
await loadSubjects();
await load();
} catch (err) {
error.value = err instanceof ApiError ? err.message : '主体列表加载失败';
}
}
function onSubjectChange(event: { detail: { value: string | number } }) {
const target = subjects.value[Number(event.detail.value)];
if (!target || target.id === currentSubjectId.value) return;
setSubjectId(target.id);
load();
}
function search() {
loadRecords(true);
}
onShow(load);
watch(authVersion, () => load());
onPullDownRefresh(async () => {
await load();
uni.stopPullDownRefresh();
});
onReachBottom(() => {
if (!finished.value) loadRecords();
});
</script>
<template>
<view class="page">
<view class="row-between hero">
<view>
<view class="hello">{{ loggedIn ? `你好,${displayName(currentUser)}` : '欢迎使用云仓溯源' }}</view>
<view v-if="loggedIn" class="hint">当前仓库:{{ currentSubject ? currentSubject.name : '未选择' }}</view>
<view v-else class="hint">仓储打包视频溯源 · 扫码即录像</view>
</view>
<picker v-if="loggedIn && subjects.length > 1" :range="subjectNames" :value="subjectIndex" @change="onSubjectChange">
<view class="subject-chip">切换仓库</view>
</picker>
</view>
<view v-if="error" class="alert alert-error">{{ error }}</view>
<!-- 游客:先介绍能做什么(不强制登录),要用功能时再点登录 -->
<view v-if="!loggedIn" class="card">
<view class="card-title">你可以用它做什么</view>
<view class="feature">
<text class="feature-dot" />
<text>扫码记录:查看每件包裹的打包时间与操作流水</text>
</view>
<view class="feature">
<text class="feature-dot" />
<text>视频溯源:按运单号回看对应的打包录像</text>
</view>
<view class="feature">
<text class="feature-dot" />
<text>设备管理:统一管理扫码中控与摄像头通道</text>
</view>
<button class="btn guest-btn" @click="openLoginDrawer">登录 / 注册</button>
<view class="hint guest-tip">不登录也可以先浏览;登录后才可查看你自己的数据。</view>
</view>
<view v-else-if="!hasSubject && !loading" class="card">
<view class="card-title">还没有可用仓库</view>
<view class="hint">仓库(主体)是设备与扫码记录的归属单位,请先在 Web 端创建,再回来查看。</view>
<button class="btn" style="margin-top: 24rpx" @click="reloadSubjects">重新获取</button>
</view>
<template v-else>
<view class="stat-grid">
<view class="card stat">
<text class="stat-label">我的设备</text>
<text class="stat-value">{{ stats ? stats.deviceCount : '—' }}</text>
<text class="stat-foot">台(中控)</text>
</view>
<view class="card stat">
<text class="stat-label">已登记通道</text>
<text class="stat-value">{{ stats ? stats.channelCount : '—' }}</text>
<text class="stat-foot">个</text>
</view>
<view class="card stat">
<text class="stat-label">扫码记录</text>
<text class="stat-value">{{ stats ? stats.recordCount : '—' }}</text>
<text class="stat-foot">条</text>
</view>
</view>
<view class="card">
<view class="row-between card-title-row">
<text class="card-title">扫码记录</text>
<text class="hint">共 {{ total }} 条</text>
</view>
<view class="search-row">
<input
v-model="barcode"
class="input search-input"
placeholder="输入条码(支持模糊)"
confirm-type="search"
@confirm="search"
/>
<button class="btn btn-ghost search-btn" :disabled="listLoading" @click="search">查询</button>
</view>
<view v-if="listError" class="alert alert-error">{{ listError }}</view>
<view v-for="row in records" :key="row.id" class="record">
<view class="row-between">
<text class="barcode">{{ row.barcode }}</text>
<text class="channel">通道 {{ row.channelId }}</text>
</view>
<view class="record-meta">
<text>{{ row.deviceSn || row.scannerId }}{{ row.deviceName ? ` · ${row.deviceName}` : '' }}</text>
<text class="muted">{{ formatTime(row.scanTime) }}</text>
</view>
<view class="record-meta muted">
<text>上报 {{ formatTime(row.reportedAt) }}</text>
<text>{{ fromNow(row.scanTime) }}</text>
</view>
</view>
<view v-if="!records.length" class="hint list-empty">{{ listLoading ? '加载中…' : '还没有扫码记录。' }}</view>
<view v-else-if="listLoading" class="hint list-foot">加载中…</view>
<view v-else-if="finished" class="hint list-foot">没有更多了</view>
</view>
</template>
<login-drawer />
</view>
</template>
<style scoped>
.hero {
padding: 8rpx 4rpx 32rpx;
}
.hello {
font-size: 38rpx;
font-weight: 600;
}
.subject-chip {
padding: 10rpx 20rpx;
border-radius: 999rpx;
background: #eef2ff;
color: var(--brand);
font-size: 24rpx;
}
.guest-btn {
margin-top: 24rpx;
}
.guest-tip {
margin-top: 16rpx;
text-align: center;
}
.feature {
display: flex;
align-items: flex-start;
margin-top: 14rpx;
color: #4b5563;
font-size: 26rpx;
line-height: 1.6;
}
.feature-dot {
width: 10rpx;
height: 10rpx;
margin: 14rpx 14rpx 0 0;
border-radius: 50%;
background: var(--brand);
flex-shrink: 0;
}
.stat-grid {
display: flex;
gap: 16rpx;
}
.stat {
flex: 1;
display: flex;
flex-direction: column;
padding: 24rpx 20rpx;
margin-bottom: 24rpx;
}
.stat-label {
color: var(--text-muted);
font-size: 22rpx;
}
.stat-value {
margin-top: 8rpx;
font-size: 44rpx;
font-weight: 600;
line-height: 1.2;
}
.stat-foot {
color: var(--text-muted);
font-size: 20rpx;
}
.card-title {
font-size: 30rpx;
font-weight: 600;
}
.card-title-row {
margin-bottom: 16rpx;
}
.search-row {
display: flex;
align-items: center;
gap: 16rpx;
margin-bottom: 8rpx;
}
.search-input {
flex: 1;
}
.search-btn {
flex-shrink: 0;
width: 140rpx;
height: 84rpx;
}
.record {
padding: 20rpx 0;
border-top: 1rpx solid #f1f3f7;
}
.barcode {
font-family: Menlo, Consolas, monospace;
font-size: 28rpx;
font-weight: 600;
word-break: break-all;
}
.channel {
flex-shrink: 0;
margin-left: 16rpx;
padding: 2rpx 14rpx;
border-radius: 999rpx;
background: #eef2ff;
color: var(--brand);
font-size: 22rpx;
}
.record-meta {
display: flex;
justify-content: space-between;
gap: 16rpx;
margin-top: 8rpx;
font-size: 24rpx;
}
.list-empty {
padding: 24rpx 0;
text-align: center;
}
.list-foot {
padding: 16rpx 0 0;
text-align: center;
}
</style>