支持打印和搜索

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
+12
View File
@@ -96,11 +96,23 @@
"navigationBarTitleText": "设置" "navigationBarTitleText": "设置"
} }
}, },
{
"path": "pages/printer-test/printer-test",
"style": {
"navigationBarTitleText": "打印机测试"
}
},
{ {
"path": "pages/message/message", "path": "pages/message/message",
"style": { "style": {
"navigationBarTitleText": "消息" "navigationBarTitleText": "消息"
} }
},
{
"path": "pages/search/search",
"style": {
"navigationBarTitleText": "搜索"
}
} }
], ],
"globalStyle": { "globalStyle": {
+21 -2
View File
@@ -2,7 +2,10 @@
<view class="container"> <view class="container">
<!-- 欢迎区域 --> <!-- 欢迎区域 -->
<view class="welcome-section"> <view class="welcome-section">
<view class="welcome-header">
<text class="welcome-title">{{ welcomeText }}</text> <text class="welcome-title">{{ welcomeText }}</text>
<text class="search-icon" @click="goSearch">🔍</text>
</view>
<text class="welcome-date">{{ todayDisplay }}</text> <text class="welcome-date">{{ todayDisplay }}</text>
</view> </view>
@@ -188,6 +191,10 @@ function switchToTab(url) {
uni.switchTab({ url }) uni.switchTab({ url })
} }
function goSearch() {
uni.navigateTo({ url: '/pages/search/search' })
}
// 定时刷新 // 定时刷新
let refreshTimer = null let refreshTimer = null
const REFRESH_INTERVAL = 5000 // 5秒 const REFRESH_INTERVAL = 5000 // 5秒
@@ -247,12 +254,24 @@ onHide(() => {
padding: 30rpx 0; padding: 30rpx 0;
} }
.welcome-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10rpx;
}
.welcome-title { .welcome-title {
display: block;
font-size: 44rpx; font-size: 44rpx;
font-weight: bold; font-weight: bold;
color: #333; color: #333;
margin-bottom: 10rpx; }
.search-icon {
font-size: 40rpx;
padding: 10rpx 20rpx;
background-color: #f0f0f0;
border-radius: 50%;
} }
.welcome-date { .welcome-date {
+338
View File
@@ -0,0 +1,338 @@
<template>
<view class="page">
<view class="group">
<text class="group-title">打印机控制</text>
<view class="group-body">
<view class="cell" @tap="initPrinter">
<text class="cell-label">初始化打印</text>
</view>
<view class="cell" @tap="closePrinter">
<text class="cell-label">关闭打印</text>
</view>
</view>
</view>
<view class="group">
<text class="group-title">打印测试</text>
<view class="group-body">
<view class="cell" @tap="printerText">
<text class="cell-label">打印小票</text>
</view>
<view class="cell" @tap="printerLabel">
<text class="cell-label">测试打印文字+二维码</text>
</view>
<view class="cell" @tap="printLabel">
<text class="cell-label">打印标签条形码</text>
</view>
<view class="cell" @tap="myprinttest">
<text class="cell-label">我的demo</text>
</view>
</view>
</view>
<!-- 日志区域 -->
<view class="log-box" v-if="logs.length">
<text class="group-title">日志</text>
<view class="log-content">
<text class="log-line" v-for="(log, i) in logs" :key="i">{{ log }}</text>
</view>
<view class="btn-wrapper">
<button class="clear-btn" @tap="logs = []">清空日志</button>
</view>
</view>
</view>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
const printer = uni.requireNativePlugin('LcPrinter')
const modal = uni.requireNativePlugin('modal')
const globalEvent = uni.requireNativePlugin('globalEvent')
const logs = ref([])
function log(msg) {
logs.value.push(msg)
}
onLoad(() => {
// 添加打印状态监听
globalEvent.addEventListener('onPrintCallback', (e) => {
log('state: ' + JSON.stringify(e))
uni.showToast({
title: 'state: ' + JSON.stringify(e),
duration: 2000
})
if (e.key == 0) {
uni.showToast({
title: '打印成功',
duration: 2000
})
} else if (e.key == 3) {
uni.showToast({
title: '缺纸',
duration: 2000
})
}
})
// 打印机版本获取回调
globalEvent.addEventListener('onVersion', (e) => {
log('version: ' + JSON.stringify(e))
uni.showToast({
title: 'version: ' + JSON.stringify(e),
duration: 2000
})
})
globalEvent.addEventListener('getsupportprint', (e) => {
log('getsupportprint: ' + JSON.stringify(e))
uni.showToast({
title: 'key: ' + JSON.stringify(e),
duration: 2000
})
})
})
function initPrinter() {
console.log('初始化')
log('初始化打印机...')
const ret = printer.initPrinter({})
modal.toast({
message: ret,
duration: 1.5
})
printer.setConcentration({
level: 39
})
printer.setLineSpacing({
spacing: 1
})
printer.getsupportprint()
log('初始化完成')
}
function closePrinter() {
console.log('关闭')
log('关闭打印机')
printer.closePrinter()
}
function printerLabel() {
console.log('测试打印')
log('测试打印(文字+二维码)...')
// 标签打印,使用黑标
printer.printEnableMark({
enable: true
})
printer.setFontSize({
fontSize: 0
})
printer.setTextBold({
bold: true
})
printer.printText({
content: '垃圾收运小票凭证'
})
printer.printLine({
line_length: 1
})
printer.printText({
content: 'asdads'
})
printer.printLine({
line_length: 1
})
console.log('测试打印QR')
log('打印二维码...')
printer.printQR({
text: 'title',
height: 400,
offset: 1
})
printer.printLine({
line_length: 2
})
printer.start()
console.log('测试打印QR结束')
log('打印完成')
}
function printerText() {
console.log('打印小票')
log('打印小票...')
// 普通打印(小票),不使用黑标
printer.printEnableMark({
enable: false
})
printer.setFontSize({
fontSize: 0
})
printer.setTextBold({
bold: true
})
printer.printText({
content: '这是一张测试小票'
})
printer.setTextBold({
bold: false
})
printer.printLine({
line_length: 1
})
printer.printText({
content: 'commodity'
})
printer.printLine({
line_length: 1
})
printer.printText({
content: 'Quantity'
})
printer.printLine({
line_length: 1
})
printer.printText({
content: 'unit price'
})
printer.printLine({
line_length: 1
})
printer.printBarcode({
text: '123456',
height: 80,
barcodeType: 73
})
printer.printLine({
line_length: 1
})
printer.printQR({
text: '1234456'
})
printer.printLine({
line_length: 2
})
printer.start()
log('小票打印完成')
}
function printLabel() {
log('打印标签(条形码)...')
// 标签打印,使用黑标
printer.printEnableMark({
enable: true
})
printer.printBarcode({
text: '1234567890123456789',
height: 80,
barcodeType: 73
})
printer.printLine({
line_length: 5
})
printer.printGoToNextMark()
log('标签打印完成')
}
function myprinttest(){
// 标签打印,使用黑标
printer.printEnableMark({
enable: true
})
printer.printText({
content: 'type:1\n123'
})
printer.printBarcode({
text: 'abcdefg:1234',
height: 40,
barcodeType: 73,
})
printer.printGoToNextMark()
}
</script>
<style scoped>
.page {
min-height: 100vh;
background-color: #f2f2f7;
padding-top: 20rpx;
}
.group {
margin-bottom: 32rpx;
}
.group-title {
font-size: 24rpx;
color: #8e8e93;
padding: 0 32rpx 12rpx;
display: block;
}
.group-body {
background-color: #ffffff;
border-radius: 12rpx;
margin: 0 24rpx;
overflow: hidden;
}
.cell {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
padding: 0 32rpx;
height: 96rpx;
background-color: #ffffff;
border-bottom: 1rpx solid #e5e5ea;
}
.cell:last-child {
border-bottom: none;
}
.cell:active {
background-color: #f2f2f7;
}
.cell-label {
font-size: 30rpx;
color: #1c1c1e;
}
.btn-wrapper {
padding: 20rpx 32rpx;
}
.clear-btn {
background-color: #ff3b30;
color: #fff;
font-size: 28rpx;
border-radius: 12rpx;
padding: 12rpx 0;
}
.log-box {
margin: 0 24rpx 32rpx;
background: #1c1c1e;
border-radius: 12rpx;
padding: 20rpx;
}
.log-content {
max-height: 400rpx;
overflow-y: auto;
}
.log-line {
display: block;
font-size: 22rpx;
color: #32d74b;
line-height: 1.6;
word-break: break-all;
}
</style>
+454
View File
@@ -0,0 +1,454 @@
<template>
<view class="container">
<!-- 搜索栏 -->
<view class="search-bar">
<text class="search-back" @click="goBack"></text>
<input
class="search-input"
v-model="keyword"
placeholder="输入关键词搜索..."
confirm-type="search"
@input="onInput"
@confirm="doSearch"
/>
<text class="search-scan" @click="onScan">📷</text>
<text class="search-clear" v-if="keyword" @click="clearKeyword">×</text>
</view>
<!-- 分类Tab -->
<view class="tabs">
<view
class="tab"
v-for="cat in categories"
:key="cat.key"
:class="{ active: currentCat === cat.key }"
@click="switchCat(cat.key)"
>
{{ cat.label }}
</view>
</view>
<!-- 加载状态 -->
<view v-if="loading" class="loading">
<text>搜索中...</text>
</view>
<!-- 空状态 -->
<view v-else-if="results.length === 0 && searched" class="empty">
<text>未找到相关内容</text>
</view>
<!-- 搜索结果列表 -->
<scroll-view v-else scroll-y class="result-list">
<view
v-for="item in results"
:key="item._type + ':' + item.ID"
class="result-card"
@click="goDetail(item)"
>
<view class="result-icon">
<text>{{ getIcon(item._type) }}</text>
</view>
<view class="result-info">
<text class="result-title">{{ getTitle(item) }}</text>
<text class="result-desc">{{ getDesc(item) }}</text>
</view>
<view class="result-arrow"></view>
</view>
</scroll-view>
</view>
</template>
<script setup>
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { purchaseApi } from '@/api/purchase.js'
import { warehouseApi } from '@/api/warehouse.js'
import { workOrderApi } from '@/api/work_order.js'
const keyword = ref('')
const currentCat = ref('all')
const loading = ref(false)
const results = ref([])
const searched = ref(false)
let searchTimer = null
const categories = [
{ key: 'all', label: '全部' },
{ key: 'order', label: '订单' },
{ key: 'workorder', label: '工单' },
{ key: 'item', label: '物品' },
{ key: 'container', label: '容器' },
]
function onInput() {
if (searchTimer) clearTimeout(searchTimer)
searchTimer = setTimeout(() => {
if (keyword.value.trim()) {
doSearch()
} else {
results.value = []
searched.value = false
}
}, 500)
}
function doSearch() {
if (!keyword.value.trim()) {
results.value = []
searched.value = false
return
}
// 条码识别:wo:ID / item:ID / warehouse:ID
const val = keyword.value.trim().toLowerCase()
let match
// wo:ID → 工单
match = val.match(/^wo:(\d+)$/)
if (match) {
uni.navigateTo({ url: '/pages/workorder/show-workorder?id=' + match[1] })
return
}
// item:ID → 物品
match = val.match(/^item:(\d+)$/)
if (match) {
uni.navigateTo({ url: '/pages/warehouse/item-detail?id=' + match[1] })
return
}
// warehouse:ID → 容器(tabBar页面,用事件传参)
match = val.match(/^warehouse:(\d+)$/)
if (match) {
uni.$emit('barcode-navigate-container', { id: match[1] })
uni.switchTab({ url: '/pages/warehouse/warehouse' })
return
}
searched.value = true
loading.value = true
const cat = currentCat.value
const tasks = []
if (cat === 'all' || cat === 'order') {
tasks.push(
purchaseApi.getOrders({ search: keyword.value, entries: 50, page: 1 }).then(res => {
if (res.errCode === 0 && res.data && res.data.all_orders) {
return (res.data.all_orders || []).map(o => ({ ...o, _type: 'order' }))
}
return []
}).catch(() => [])
)
}
if (cat === 'all' || cat === 'workorder') {
tasks.push(
workOrderApi.list({ search: keyword.value, entries: 50, page: 1 }).then(res => {
if (res.errCode === 0 && res.data && res.data.all_orders) {
return (res.data.all_orders || []).map(o => ({ ...o, _type: 'workorder' }))
}
return []
}).catch(() => [])
)
}
if (cat === 'all' || cat === 'item') {
tasks.push(
warehouseApi.listItem({ search: keyword.value, entries: 50, page: 1 }).then(res => {
if (res.errCode === 0 && res.data && res.data.items) {
return (res.data.items || []).map(i => ({ ...i, _type: 'item' }))
}
return []
}).catch(() => [])
)
}
if (cat === 'all' || cat === 'container') {
tasks.push(
warehouseApi.listContainer({ search: keyword.value, entries: 50, page: 1 }).then(res => {
if (res.errCode === 0 && res.data && res.data.containers) {
return (res.data.containers || []).map(c => ({ ...c, _type: 'container' }))
}
return []
}).catch(() => [])
)
}
Promise.all(tasks).then(allResults => {
const merged = allResults.flat()
results.value = merged
}).finally(() => {
loading.value = false
})
}
function switchCat(cat) {
currentCat.value = cat
if (keyword.value.trim()) {
doSearch()
}
}
function clearKeyword() {
keyword.value = ''
results.value = []
searched.value = false
}
function getIcon(type) {
const icons = {
order: '📦',
workorder: '🔧',
item: '📦',
container: '📁',
}
return icons[type] || '📄'
}
function getTitle(item) {
switch (item._type) {
case 'order':
return item.Title || '订单'
case 'workorder':
return item.Title || '工单'
case 'item':
return item.Name || '物品'
case 'container':
return item.Title || '容器'
default:
return ''
}
}
function getDesc(item) {
switch (item._type) {
case 'order':
return '状态: ' + (item.Status || '') + ' | ' + (item.Vendor || '')
case 'workorder':
return '状态: ' + (item.Status || '')
case 'item':
return item.SerialNumber || (item.Remark ? item.Remark.substring(0, 30) : '')
case 'container':
return '子容器: ' + (item.ChildCount || 0) + ' | 物品: ' + (item.ItemCount || 0)
default:
return ''
}
}
function goDetail(item) {
switch (item._type) {
case 'order':
uni.navigateTo({ url: '/pages/order/order-detail?id=' + item.ID })
break
case 'workorder':
uni.navigateTo({ url: '/pages/workorder/show-workorder?id=' + item.ID })
break
case 'item':
uni.navigateTo({ url: '/pages/warehouse/item-detail?id=' + item.ID })
break
case 'container':
uni.switchTab({ url: '/pages/warehouse/warehouse?container_id=' + item.ID })
break
}
}
function goBack() {
uni.navigateBack({ delta: 1 })
}
// 扫码
function onScan() {
// #ifdef H5
uni.showModal({
title: '提示',
content: 'H5 端不支持摄像头扫码,请手动输入条码(如 wo:1、item:1、warehouse:1',
showCancel: false
})
// #endif
// #ifdef APP-PLUS
// 使用 5+ API 直接调用原生扫码
const barcode = plus.barcode.create('barcode', [plus.barcode.CODE128, plus.barcode.CODE39, plus.barcode.EAN13, plus.barcode.QR], {
top: '0',
left: '0',
width: '100%',
height: '100%',
position: 'static',
frameColor: '#007AFF',
scanbarColor: '#007AFF'
})
barcode.onmarked = (type, result) => {
console.log('扫码结果:', type, result)
barcode.close()
keyword.value = result
doSearch()
}
barcode.onerror = (err) => {
console.error('扫码错误:', err)
barcode.close()
uni.showToast({ title: '扫码失败', icon: 'none' })
}
// 创建并显示扫码页面
const page = plus.webview.currentWebview()
page.append(barcode)
// #endif
// #ifdef MP-WEIXIN
uni.scanCode({
success: (res) => {
console.log('扫码结果:', res)
keyword.value = res.result
doSearch()
},
fail: (err) => {
console.error('扫码失败', err)
if (err.errMsg && err.errMsg.includes('cancel')) return
uni.showToast({ title: '扫码失败', icon: 'none' })
}
})
// #endif
}
</script>
<style scoped>
.container {
min-height: 100vh;
background-color: #f5f5f5;
display: flex;
flex-direction: column;
}
/* 搜索栏 */
.search-bar {
display: flex;
align-items: center;
padding: 20rpx 24rpx;
background-color: #007AFF;
gap: 16rpx;
}
.search-back {
font-size: 48rpx;
color: #fff;
width: 60rpx;
text-align: center;
}
.search-input {
flex: 1;
height: 72rpx;
background-color: #fff;
border-radius: 36rpx;
padding: 0 30rpx;
font-size: 28rpx;
}
.search-clear {
font-size: 40rpx;
color: #fff;
width: 60rpx;
text-align: center;
}
.search-scan {
font-size: 40rpx;
color: #fff;
width: 60rpx;
text-align: center;
}
/* 分类Tab */
.tabs {
display: flex;
background-color: #fff;
padding: 0 10rpx;
border-bottom: 1rpx solid #eee;
}
.tab {
flex: 1;
text-align: center;
padding: 24rpx 0;
font-size: 26rpx;
color: #666;
border-bottom: 4rpx solid transparent;
}
.tab.active {
color: #007AFF;
border-bottom-color: #007AFF;
font-weight: bold;
}
/* 加载/空状态 */
.loading,
.empty {
text-align: center;
padding: 120rpx 0;
color: #999;
font-size: 28rpx;
}
/* 结果列表 */
.result-list {
flex: 1;
height: 0;
padding: 20rpx;
}
.result-card {
display: flex;
align-items: center;
background-color: #fff;
padding: 28rpx 24rpx;
border-radius: 12rpx;
margin-bottom: 16rpx;
}
.result-icon {
width: 80rpx;
height: 80rpx;
background-color: #f0f0f0;
border-radius: 12rpx;
display: flex;
align-items: center;
justify-content: center;
font-size: 36rpx;
margin-right: 20rpx;
flex-shrink: 0;
}
.result-info {
flex: 1;
overflow: hidden;
}
.result-title {
display: block;
font-size: 30rpx;
color: #333;
font-weight: 500;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.result-desc {
display: block;
font-size: 24rpx;
color: #999;
margin-top: 8rpx;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.result-arrow {
font-size: 36rpx;
color: #ccc;
margin-left: 16rpx;
}
</style>
+15 -4
View File
@@ -22,10 +22,16 @@
</view> </view>
</view> </view>
<!-- ====================== 这里是居中的按钮 ====================== --> <!-- ===== 分组打印机 ===== -->
<view class="group">
<text class="group-title">打印机</text>
<view class="group-body">
<view class="cell" @tap="goPrinterTest">
<text class="cell-label">打印机测试</text>
<text class="cell-arrow"></text>
</view>
</view>
</view>
<!-- ===== 分组关于 ===== --> <!-- ===== 分组关于 ===== -->
<view class="group"> <view class="group">
@@ -113,6 +119,11 @@ function onEditApiUrl() {
}) })
} }
// 跳转打印机测试页
function goPrinterTest() {
uni.navigateTo({ url: '/pages/printer-test/printer-test' })
}
onMounted(() => { onMounted(() => {
// 真机/打包环境:通过 plus.runtime 获取 // 真机/打包环境:通过 plus.runtime 获取
+88 -5
View File
@@ -3,6 +3,9 @@
<view class="header"> <view class="header">
<text class="back-btn" @click="goBack"> 返回</text> <text class="back-btn" @click="goBack"> 返回</text>
<text class="title">物品详情</text> <text class="title">物品详情</text>
<view class="header-right">
<text class="print-btn" @click="printItem">打印</text>
</view>
</view> </view>
<scroll-view scroll-y class="content" refresher-enabled @refresherrefresh="onRefresh" :refresher-triggered="refreshing"> <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 { warehouseApi } from '@/api/warehouse.js'
import { useConfigStore } from '@/stores/config.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 configStore = useConfigStore()
const itemId = ref(0) const itemId = ref(0)
const loading = ref(false) const loading = ref(false)
@@ -318,6 +331,73 @@ function goBack() {
uni.navigateBack({ delta: 1 }) 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() { async function onRefresh() {
refreshing.value = true refreshing.value = true
await fetchDetail() await fetchDetail()
@@ -372,13 +452,16 @@ onShow(() => {
color: #333; color: #333;
flex: 1; flex: 1;
text-align: center; text-align: center;
padding-right: 60rpx;
} }
.title { .header-right {
font-size: 36rpx; display: flex;
font-weight: bold; align-items: center;
color: #333; }
.print-btn {
font-size: 28rpx;
color: #007AFF;
} }
.content { .content {
+117
View File
@@ -40,6 +40,7 @@
<text class="breadcrumb-sep" v-if="index < breadcrumbs.length - 1"> / </text> <text class="breadcrumb-sep" v-if="index < breadcrumbs.length - 1"> / </text>
</text> </text>
</view> </view>
<text class="print-btn" @click="printContainer">打印</text>
<text class="edit-btn" @click="editCurrentContainer"> 编辑</text> <text class="edit-btn" @click="editCurrentContainer"> 编辑</text>
</view> </view>
@@ -236,6 +237,16 @@ import { onShow } from '@dcloudio/uni-app'
import { warehouseApi } from '@/api/warehouse.js' import { warehouseApi } from '@/api/warehouse.js'
import { useConfigStore } from '@/stores/config.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 configStore = useConfigStore()
function getPhotoUrl(sha256) { 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(() => { onMounted(() => {
fetchContainers() fetchContainers()
}) })
// 每次页面显示时刷新数据(如从新增物品页面返回时) // 每次页面显示时刷新数据(如从新增物品页面返回时)
onShow(() => { onShow(() => {
// 检查是否有条码导航事件
uni.$once('barcode-navigate-container', (data) => {
if (data && data.id) {
currentContainerId.value = parseInt(data.id)
// 清空搜索,确保显示容器内容
searchText.value = ''
}
})
fetchContainers() fetchContainers()
}) })
</script> </script>
@@ -696,6 +805,14 @@ onShow(() => {
font-size: 28rpx; font-size: 28rpx;
color: #007AFF; color: #007AFF;
white-space: nowrap; white-space: nowrap;
margin-left: 20rpx;
}
.print-btn {
font-size: 28rpx;
color: #007AFF;
white-space: nowrap;
margin-left: 20rpx;
} }
.item-card { .item-card {
+74 -2
View File
@@ -3,8 +3,10 @@
<view class="header"> <view class="header">
<text class="back-btn" @click="goBack"> 返回</text> <text class="back-btn" @click="goBack"> 返回</text>
<text class="title">工单详情</text> <text class="title">工单详情</text>
<view class="header-right">
<text v-if="canModify" class="edit-btn" @click="goEdit">编辑</text> <text v-if="canModify" class="edit-btn" @click="goEdit">编辑</text>
<view v-else class="header-right"></view> <text class="print-btn" @click="printWorkOrder">打印</text>
</view>
</view> </view>
<scroll-view scroll-y class="content" refresher-enabled @refresherrefresh="onRefresh" :refresher-triggered="refreshing"> <scroll-view scroll-y class="content" refresher-enabled @refresherrefresh="onRefresh" :refresher-triggered="refreshing">
@@ -258,6 +260,16 @@ import { workOrderApi } from '@/api/work_order.js'
import api from '@/api/index.js' import api from '@/api/index.js'
import { useConfigStore } from '@/stores/config.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 configStore = useConfigStore()
const goBack = () => uni.navigateBack() const goBack = () => uni.navigateBack()
@@ -342,6 +354,59 @@ async function fetchOrderDetail() {
} }
} }
// 打印工单标签
function printWorkOrder() {
if (!printer) {
uni.showToast({ title: '打印机插件未加载(仅在 App 环境可用)', icon: 'none' })
return
}
// 标签打印,使用黑标
printer.printEnableMark({
enable: true
})
// 第一行:标题
printer.setFontSize({ fontSize: 0 })
printer.setTextBold({ bold: true })
printer.printText({
content: (order.value.Title || '工单')+'\n'
})
printer.setTextBold({ bold: false })
//printer.printLine({ line_length: 1 })
// 第二行:描述
if (order.value.Description) {
//printer.setFontSize({ fontSize: 1 })
printer.printText({
content: order.value.Description+'\n'
})
//printer.printLine({ line_length: 1 })
}
// 第三行:创建时间
if (order.value.CreatedAt) {
printer.setFontSize({ fontSize: 1 })
printer.printText({
content: formatDate(order.value.CreatedAt)
})
//printer.printLine({ line_length: 1 })
}
// 条形码(高40
printer.printBarcode({
text: 'wo:' + orderId.value,
height: 40,
barcodeType: 73
})
//printer.printLine({ line_length: 2 })
// 提交打印
printer.start()
uni.showToast({ title: '打印成功', icon: 'success' })
}
// 新增进度 // 新增进度
const selectedStatus = ref({}) const selectedStatus = ref({})
const commitComment = ref('') const commitComment = ref('')
@@ -621,7 +686,14 @@ async function onRefresh() {
} }
.header-right { .header-right {
width: 60rpx; display: flex;
align-items: center;
gap: 40rpx;
}
.print-btn {
font-size: 28rpx;
color: #007AFF;
} }
.content { .content {