Files
kangyuan-mobile/pages/merchant/scanVerify.vue
T
2026-07-15 10:51:15 +08:00

297 lines
7.2 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.
<template>
<s-layout
title="扫码核销"
:showLeftButton="false"
tabbar="/pages/merchant/scanVerify"
navPreset="light"
>
<template #navRight>
<modeSwitch />
</template>
<view class="page">
<view class="seg bg24">
<view class="seg-item" :class="{ segActive: tab === 'scan' }" @tap="tab = 'scan'">
扫码核销
</view>
<view class="seg-item" :class="{ segActive: tab === 'input' }" @tap="tab = 'input'">
输入取货码
</view>
</view>
<view class="panel bg24" v-if="tab === 'scan'">
<view class="scan-box">
<view class="scan-box__inner">
<uni-icons type="scan" size="42" color="#2556EC" />
<view class="scan-title">请将用户订单二维码对准扫码框</view>
<view class="scan-tip c_999 size24">或点击下方按钮开始扫码</view>
</view>
</view>
<view class="btnScan" :class="{ btnScanDisabled: verifying }" @tap="startScan">
{{ verifying ? '核销中...' : '开始扫码' }}
</view>
</view>
<view class="panel bg24" v-else>
<view class="input-row">
<uni-easyinput
class="code-input"
v-model="manualCode"
placeholder="请输入取货码"
:inputBorder="false"
:styles="{ fontSize: '34rpx' }"
/>
</view>
<view class="btnScan" :class="{ btnScanDisabled: verifying }" @tap="confirmManual">
{{ verifying ? '核销中...' : '确认核销' }}
</view>
</view>
<view class="records bg24">
<view class="records-hd flex-center ju-between">
<view class="records-title">近期核销记录</view>
<view class="records-link" @tap="clear">清空</view>
</view>
<view v-for="row in records" :key="row.id" class="record flex-center ju-between">
<view class="record-left">
<view class="record-code">取货码:{{ row.code }}</view>
<view class="record-time c_999 size24">{{ row.time }}</view>
</view>
<view class="record-right c_999 size24">{{ row.operator }}</view>
</view>
<view v-if="records.length === 0" class="empty c_999">暂无记录</view>
</view>
</view>
</s-layout>
</template>
<script setup>
import { ref } from 'vue';
import modeSwitch from '@/components/mode_switch/mode_switch.vue';
import sheep from '@/sheep';
import OrderStoreApi from '@/sheep/api/trade/orderStore';
const tab = ref('scan');
const manualCode = ref('');
const verifying = ref(false);
const userStore = sheep.$store('user');
const STORAGE_KEY = 'merchant.verify.records';
const records = ref(uni.getStorageSync(STORAGE_KEY) || []);
const pushRecord = (code) => {
const next = [
{
id: `${Date.now()}-${Math.random()}`,
code,
time: new Date().toLocaleString(),
operator: '当前账号',
},
...records.value,
].slice(0, 20);
records.value = next;
uni.setStorageSync(STORAGE_KEY, next);
};
const getVerifyIds = async () => {
const localIds = Array.isArray(userStore?.userInfo?.ids) ? userStore.userInfo.ids : [];
if (localIds.length) return localIds;
const userInfo = await userStore.getInfo();
return Array.isArray(userInfo?.ids) ? userInfo.ids : [];
};
const verifyCode = async (code) => {
const pickUpVerifyCode = String(code || '').trim();
if (!pickUpVerifyCode) {
uni.showToast({ title: '核销码不能为空', icon: 'none' });
return false;
}
if (verifying.value) return false;
const ids = await getVerifyIds();
if (!ids.length) {
uni.showToast({ title: '缺少核销ids', icon: 'none' });
return false;
}
verifying.value = true;
try {
const { code: resCode } = await OrderStoreApi.verify(ids, pickUpVerifyCode);
if (resCode !== 0) return false;
pushRecord(pickUpVerifyCode);
return true;
} catch (error) {
uni.showToast({ title: '核销失败,请稍后重试', icon: 'none' });
return false;
} finally {
verifying.value = false;
}
};
const startScan = () => {
if (verifying.value) return;
uni.scanCode({
scanType: ['qrCode', 'barCode'],
success: async (res) => {
const code = String(res.result || '').trim();
if (!code) {
uni.showToast({ title: '未识别到内容', icon: 'none' });
return;
}
await verifyCode(code);
},
fail: () => {
uni.showToast({ title: '扫码失败', icon: 'none' });
},
});
};
const confirmManual = async () => {
const code = String(manualCode.value || '').trim();
if (!code) {
uni.showToast({ title: '请输入取货码', icon: 'none' });
return;
}
const success = await verifyCode(code);
if (success) {
manualCode.value = '';
}
};
const clear = () => {
records.value = [];
uni.setStorageSync(STORAGE_KEY, []);
};
</script>
<style scoped lang="scss">
.page {
padding: 24rpx 24rpx 170rpx;
box-sizing: border-box;
}
.seg {
border-radius: 24rpx;
padding: 10rpx;
box-sizing: border-box;
display: flex;
margin-bottom: 18rpx;
background: rgba(255, 255, 255, 0.98);
}
.seg-item {
flex: 1;
height: 76rpx;
display: flex;
align-items: center;
justify-content: center;
border-radius: 18rpx;
font-size: 28rpx;
color: #333;
}
.segActive {
background: rgba(37, 86, 236, 0.12);
color: #2556ec;
font-weight: 700;
}
.panel {
border-radius: 24rpx;
padding: 24rpx;
box-sizing: border-box;
margin-bottom: 18rpx;
}
.scan-box {
border-radius: 24rpx;
border: 2rpx dashed rgba(37, 86, 236, 0.35);
height: 360rpx;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 20rpx;
background: rgba(37, 86, 236, 0.04);
}
.scan-box__inner {
display: flex;
flex-direction: column;
align-items: center;
}
.scan-title {
margin-top: 18rpx;
font-size: 28rpx;
color: #333;
font-weight: 600;
}
.scan-tip {
margin-top: 10rpx;
}
.btnScan {
height: 92rpx;
border-radius: 24rpx;
background: #2556ec;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
font-size: 30rpx;
font-weight: 700;
}
.btnScanDisabled {
opacity: 0.7;
}
.input-row {
border-radius: 24rpx;
background: #f6f7fb;
padding: 18rpx 20rpx;
box-sizing: border-box;
margin-bottom: 20rpx;
}
:deep(.code-input .uni-easyinput__content) {
background: transparent !important;
}
.records {
border-radius: 24rpx;
padding: 18rpx;
box-sizing: border-box;
}
.records-hd {
padding-bottom: 12rpx;
border-bottom: 2rpx solid #f1f2f6;
}
.records-title {
font-size: 28rpx;
color: #222;
font-weight: 700;
}
.records-link {
font-size: 24rpx;
color: #2556ec;
}
.record {
padding: 16rpx 0;
border-bottom: 2rpx solid #f6f7fb;
}
.record-code {
font-size: 26rpx;
color: #333;
font-weight: 600;
}
.empty {
text-align: center;
padding: 70rpx 0;
}
</style>