支持打印和搜索

This commit is contained in:
2026-04-27 22:01:45 +08:00
parent 09e806c969
commit 7bcd159605
8 changed files with 1121 additions and 15 deletions
+88 -5
View File
@@ -3,6 +3,9 @@
<view class="header">
<text class="back-btn" @click="goBack"> 返回</text>
<text class="title">物品详情</text>
<view class="header-right">
<text class="print-btn" @click="printItem">打印</text>
</view>
</view>
<scroll-view scroll-y class="content" refresher-enabled @refresherrefresh="onRefresh" :refresher-triggered="refreshing">
@@ -159,6 +162,16 @@ import { ref, onMounted } from 'vue'
import { warehouseApi } from '@/api/warehouse.js'
import { useConfigStore } from '@/stores/config.js'
// 仅在 App 环境下加载原生插件
let printer = null
try {
if (uni.requireNativePlugin) {
printer = uni.requireNativePlugin('LcPrinter')
}
} catch (e) {
console.error('打印机插件加载失败:', e.message)
}
const configStore = useConfigStore()
const itemId = ref(0)
const loading = ref(false)
@@ -318,6 +331,73 @@ function goBack() {
uni.navigateBack({ delta: 1 })
}
// 打印物品标签
function printItem() {
if (!printer) {
uni.showToast({ title: '打印机插件未加载(仅在 App 环境可用)', icon: 'none' })
return
}
if (!item.value) {
uni.showToast({ title: '物品信息未加载', icon: 'none' })
return
}
// 标签打印,使用黑标
printer.printEnableMark({
enable: true
})
// 第一行:标题(名称)
printer.setFontSize({ fontSize: 0 })
printer.setTextBold({ bold: true })
printer.printText({
content: (item.value.Name || '物品')+'\n'
})
printer.setTextBold({ bold: false })
//printer.printLine({ line_length: 1 })
// 第二行:编号
if (item.value.SerialNumber) {
printer.setFontSize({ fontSize: 1 })
printer.printText({
content: '序列号:' + item.value.SerialNumber +'\n'
})
//printer.printLine({ line_length: 1 })
}
// 第三行:备注
if (item.value.Remark) {
printer.setFontSize({ fontSize: 1 })
printer.printText({
content: '备注:' + item.value.Remark+'\n'
})
//printer.printLine({ line_length: 1 })
}
// 第四行:创建日期
if (item.value.CreatedAt) {
printer.setFontSize({ fontSize: 1 })
printer.printText({
content: '创建日期:' + formatDate(item.value.CreatedAt)
})
//printer.printLine({ line_length: 1 })
}
// 条形码(高度4
printer.printBarcode({
text: 'item:' + itemId.value,
height: 40,
barcodeType: 73
})
printer.printLine({ line_length: 2 })
// 提交打印
printer.start()
uni.showToast({ title: '打印成功', icon: 'success' })
}
async function onRefresh() {
refreshing.value = true
await fetchDetail()
@@ -372,13 +452,16 @@ onShow(() => {
color: #333;
flex: 1;
text-align: center;
padding-right: 60rpx;
}
.title {
font-size: 36rpx;
font-weight: bold;
color: #333;
.header-right {
display: flex;
align-items: center;
}
.print-btn {
font-size: 28rpx;
color: #007AFF;
}
.content {
+117
View File
@@ -40,6 +40,7 @@
<text class="breadcrumb-sep" v-if="index < breadcrumbs.length - 1"> / </text>
</text>
</view>
<text class="print-btn" @click="printContainer">打印</text>
<text class="edit-btn" @click="editCurrentContainer"> 编辑</text>
</view>
@@ -236,6 +237,16 @@ import { onShow } from '@dcloudio/uni-app'
import { warehouseApi } from '@/api/warehouse.js'
import { useConfigStore } from '@/stores/config.js'
// 仅在 App 环境下加载原生插件
let printer = null
try {
if (uni.requireNativePlugin) {
printer = uni.requireNativePlugin('LcPrinter')
}
} catch (e) {
console.error('打印机插件加载失败:', e.message)
}
const configStore = useConfigStore()
function getPhotoUrl(sha256) {
@@ -569,12 +580,110 @@ function goItemDetail(id) {
})
}
// 打印容器标签
async function printContainer() {
if (!printer) {
uni.showToast({ title: '打印机插件未加载(仅在 App 环境可用)', icon: 'none' })
return
}
if (!currentContainerId.value || currentContainerId.value === 0) {
uni.showToast({ title: '容器信息未加载', icon: 'none' })
return
}
uni.showLoading({ title: '获取容器信息...' })
try {
// 获取完整容器详情(包含 Remark, CreatedAt 等字段)
const res = await warehouseApi.getContainer(currentContainerId.value)
uni.hideLoading()
if (res.errCode !== 0 || !res.data || !res.data.container) {
uni.showToast({ title: '获取容器信息失败', icon: 'none' })
return
}
const container = res.data.container
// 标签打印,使用黑标
printer.printEnableMark({
enable: true
})
// 第一行:容器名(加粗)
printer.setFontSize({ fontSize: 0 })
printer.setTextBold({ bold: true })
printer.printText({
content: (container.Title || '容器') + '\n'
})
printer.setTextBold({ bold: false })
// 第二行:完整面包屑
const breadcrumbText = breadcrumbs.value.map(b => b.title).join(' / ')
if (breadcrumbText) {
printer.setFontSize({ fontSize: 1 })
printer.printText({
content: breadcrumbText + '\n'
})
}
// 第三行:备注
if (container.Remark) {
printer.setFontSize({ fontSize: 1 })
printer.printText({
content: container.Remark + '\n'
})
}
// 第四行:创建日期
if (container.CreatedAt) {
printer.setFontSize({ fontSize: 1 })
printer.printText({
content: formatDate(container.CreatedAt)
})
}
// 条形码(高度40
printer.printBarcode({
text: 'warehouse:' + container.ID,
height: 40,
barcodeType: 73
})
printer.printLine({ line_length: 2 })
// 提交打印
printer.start()
uni.showToast({ title: '打印成功', icon: 'success' })
} catch (e) {
uni.hideLoading()
console.error('打印失败', e)
uni.showToast({ title: '打印失败', icon: 'none' })
}
}
// 格式化日期
function formatDate(dateStr) {
if (!dateStr) return ''
const date = new Date(dateStr)
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}
onMounted(() => {
fetchContainers()
})
// 每次页面显示时刷新数据(如从新增物品页面返回时)
onShow(() => {
// 检查是否有条码导航事件
uni.$once('barcode-navigate-container', (data) => {
if (data && data.id) {
currentContainerId.value = parseInt(data.id)
// 清空搜索,确保显示容器内容
searchText.value = ''
}
})
fetchContainers()
})
</script>
@@ -696,6 +805,14 @@ onShow(() => {
font-size: 28rpx;
color: #007AFF;
white-space: nowrap;
margin-left: 20rpx;
}
.print-btn {
font-size: 28rpx;
color: #007AFF;
white-space: nowrap;
margin-left: 20rpx;
}
.item-card {