|
|
@ -9,6 +9,7 @@ |
|
|
|
|
|
|
|
|
<script> |
|
|
<script> |
|
|
import request from '@/utils/request' |
|
|
import request from '@/utils/request' |
|
|
|
|
|
import RFID from '@/api/RFID/RFID' |
|
|
|
|
|
|
|
|
export default { |
|
|
export default { |
|
|
name: 'ReadTidTemplatePrintRFID', |
|
|
name: 'ReadTidTemplatePrintRFID', |
|
|
@ -21,13 +22,16 @@ export default { |
|
|
data() { |
|
|
data() { |
|
|
return { |
|
|
return { |
|
|
// 固定配置 |
|
|
// 固定配置 |
|
|
elemID: 'epc-id', // 固定元素ID |
|
|
|
|
|
tidChecked: true, // 先读TID |
|
|
tidChecked: true, // 先读TID |
|
|
epcChecked: false, // 不读EPC |
|
|
epcChecked: false, // 不读EPC |
|
|
printCount: 1, // 打印1次 |
|
|
printCount: 1, // 打印1次 |
|
|
emuType: '1', // ZPL仿真类型 |
|
|
emuType: '1', // ZPL仿真类型 |
|
|
// 可由父组件传入的参数(默认值,父组件调用时可覆盖) |
|
|
|
|
|
actualData: '' // 打印的真实数据 |
|
|
|
|
|
|
|
|
// 默认模板数据(可被覆盖) |
|
|
|
|
|
defaultTemplateData: [ |
|
|
|
|
|
{ elemID: 'epc-id', actualData: '' } // 默认单条数据 |
|
|
|
|
|
], |
|
|
|
|
|
// 存储读取到的TID |
|
|
|
|
|
readTid: '' |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
methods: { |
|
|
methods: { |
|
|
@ -70,33 +74,79 @@ export default { |
|
|
return timestamp + randomNum |
|
|
return timestamp + randomNum |
|
|
}, |
|
|
}, |
|
|
// 发送POST请求 |
|
|
// 发送POST请求 |
|
|
|
|
|
// sendPostRequest(funcId, funcName, data) { |
|
|
|
|
|
// return new Promise((resolve, reject) => { |
|
|
|
|
|
// const printNJ = process.env.NODE_ENV === 'production' ? window.g.printNJ : process.env.VUE_APP_PRINTNJ |
|
|
|
|
|
// request({ |
|
|
|
|
|
// baseURL: '', |
|
|
|
|
|
// url: printNJ, |
|
|
|
|
|
// method: 'post', |
|
|
|
|
|
// timeout: 30000, |
|
|
|
|
|
// data: { |
|
|
|
|
|
// version: '1.0', |
|
|
|
|
|
// seqNo: this.generateUniqueId(), |
|
|
|
|
|
// funcName: funcId, |
|
|
|
|
|
// inParams: data |
|
|
|
|
|
// } |
|
|
|
|
|
// }).then(response => { |
|
|
|
|
|
// const res = response.data |
|
|
|
|
|
// if (res.rtnCode === '0') { |
|
|
|
|
|
// resolve(res.outParams) |
|
|
|
|
|
// this.creatSucessLog(funcId, funcName, res) |
|
|
|
|
|
// } else { |
|
|
|
|
|
// reject(res.rsltMsg) |
|
|
|
|
|
// this.creatErrLog(funcId, funcName, res) |
|
|
|
|
|
// } |
|
|
|
|
|
// }).catch(error => { |
|
|
|
|
|
// console.error('请求失败:', error) |
|
|
|
|
|
// this.$message.error('接口请求失败', { offset: 8 }) |
|
|
|
|
|
// reject(error.message) |
|
|
|
|
|
// }) |
|
|
|
|
|
// }) |
|
|
|
|
|
// }, |
|
|
|
|
|
// 发送POST请求(修复版) |
|
|
sendPostRequest(funcId, funcName, data) { |
|
|
sendPostRequest(funcId, funcName, data) { |
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
const printNJ = process.env.NODE_ENV === 'production' ? window.g.printNJ : process.env.VUE_APP_PRINTNJ |
|
|
|
|
|
|
|
|
// 第一步:先硬编码URL,对齐jQuery版本 |
|
|
|
|
|
const targetUrl = process.env.NODE_ENV === 'production' ? window.g.printNJ : process.env.VUE_APP_PRINTNJ |
|
|
|
|
|
// 第二步:明确配置请求头和数据格式 |
|
|
request({ |
|
|
request({ |
|
|
baseURL: '', |
|
|
|
|
|
url: printNJ, |
|
|
|
|
|
|
|
|
url: targetUrl, // 直接使用目标地址,无需baseURL拼接 |
|
|
method: 'post', |
|
|
method: 'post', |
|
|
timeout: 30000, |
|
|
timeout: 30000, |
|
|
data: { |
|
|
|
|
|
|
|
|
headers: { |
|
|
|
|
|
// 显式设置Content-Type,确保后端能解析JSON格式 |
|
|
|
|
|
'Content-Type': 'application/json; charset=utf-8' |
|
|
|
|
|
}, |
|
|
|
|
|
// 若request(axios)未自动序列化,可手动JSON.stringify |
|
|
|
|
|
// 注:axios传递对象时会自动序列化,此处两种方式均可 |
|
|
|
|
|
data: JSON.stringify({ |
|
|
version: '1.0', |
|
|
version: '1.0', |
|
|
seqNo: this.generateUniqueId(), |
|
|
seqNo: this.generateUniqueId(), |
|
|
funcName: funcId, |
|
|
funcName: funcId, |
|
|
inParams: data |
|
|
inParams: data |
|
|
} |
|
|
|
|
|
|
|
|
}) |
|
|
|
|
|
// 若axios自动序列化,也可保留对象格式: |
|
|
|
|
|
// data: { |
|
|
|
|
|
// version: '1.0', |
|
|
|
|
|
// seqNo: this.generateUniqueId(), |
|
|
|
|
|
// funcName: funcId, |
|
|
|
|
|
// inParams: data |
|
|
|
|
|
// } |
|
|
}).then(response => { |
|
|
}).then(response => { |
|
|
const res = response.data |
|
|
|
|
|
|
|
|
const res = response |
|
|
|
|
|
console.log('响应数据:', response) |
|
|
if (res.rtnCode === '0') { |
|
|
if (res.rtnCode === '0') { |
|
|
|
|
|
this.creatSucessLog(funcId, funcName, res) // 先记录日志,再resolve(顺序优化) |
|
|
resolve(res.outParams) |
|
|
resolve(res.outParams) |
|
|
this.creatSucessLog(funcId, funcName, res) |
|
|
|
|
|
} else { |
|
|
} else { |
|
|
|
|
|
this.creatErrLog(funcId, funcName, res) // 先记录日志,再reject |
|
|
reject(res.rsltMsg) |
|
|
reject(res.rsltMsg) |
|
|
this.creatErrLog(funcId, funcName, res) |
|
|
|
|
|
} |
|
|
} |
|
|
}).catch(error => { |
|
|
}).catch(error => { |
|
|
console.error('请求失败:', error) |
|
|
console.error('请求失败:', error) |
|
|
this.$message.error('接口请求失败', { offset: 8 }) |
|
|
this.$message.error('接口请求失败', { offset: 8 }) |
|
|
reject(error.message) |
|
|
|
|
|
|
|
|
reject(error.message || '未知网络错误') |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}, |
|
|
}, |
|
|
@ -123,25 +173,87 @@ export default { |
|
|
.catch(err => reject(err)) |
|
|
.catch(err => reject(err)) |
|
|
}) |
|
|
}) |
|
|
}, |
|
|
}, |
|
|
|
|
|
/** |
|
|
|
|
|
* 写入EAS数据(新增方法) |
|
|
|
|
|
* @param {string} caseId - 盒ID |
|
|
|
|
|
* @param {string} tid - 读取到的TID |
|
|
|
|
|
* @returns {Promise} |
|
|
|
|
|
*/ |
|
|
|
|
|
async writeEAS(caseId, tid) { |
|
|
|
|
|
try { |
|
|
|
|
|
if (!caseId || !tid) { |
|
|
|
|
|
this.$message.warning('盒ID或TID不能为空', { offset: 8 }) |
|
|
|
|
|
return Promise.reject('盒ID或TID不能为空') |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const sDevID = process.env.NODE_ENV === 'production' ? window.g.sDevID : process.env.VUE_APP_SDEVID |
|
|
|
|
|
|
|
|
|
|
|
const writeParams = { |
|
|
|
|
|
op: 'RFID_WriteEPC', |
|
|
|
|
|
sDevID: sDevID, |
|
|
|
|
|
EAS: 1, |
|
|
|
|
|
Type: 2, |
|
|
|
|
|
Code: caseId, |
|
|
|
|
|
Tid: tid |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
console.log('写入EAS参数:', writeParams) |
|
|
|
|
|
const writeRes = await RFID.writeEPC(writeParams) |
|
|
|
|
|
const res = JSON.parse(writeRes) |
|
|
|
|
|
|
|
|
|
|
|
if (res.code === '0') { |
|
|
|
|
|
this.$message.success('标签绑定成功', { offset: 8 }) |
|
|
|
|
|
return Promise.resolve(res) |
|
|
|
|
|
} else if (res.code === '-1') { |
|
|
|
|
|
this.$message.error('绑定失败', { offset: 8 }) |
|
|
|
|
|
return Promise.reject('绑定失败') |
|
|
|
|
|
} else if (res.code === '-1000') { |
|
|
|
|
|
this.$message.error('读写器超时未响应', { offset: 8 }) |
|
|
|
|
|
return Promise.reject('读写器超时未响应') |
|
|
|
|
|
} else { |
|
|
|
|
|
this.$message.error('操作失败', { offset: 8 }) |
|
|
|
|
|
return Promise.reject(`操作失败,错误码:${res.code}`) |
|
|
|
|
|
} |
|
|
|
|
|
} catch (err) { |
|
|
|
|
|
this.$message.error(`标签绑定操作异常:${err.message}`, { offset: 8 }) |
|
|
|
|
|
return Promise.reject(err) |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
/** |
|
|
/** |
|
|
* 打印RFID标签 |
|
|
* 打印RFID标签 |
|
|
* @param {string} printData - 可选,打印的真实数据,不传则使用组件内的actualData |
|
|
|
|
|
|
|
|
* @param {Array|string} printData - 支持两种格式: |
|
|
|
|
|
* 1. 字符串:单条数据的actualData(兼容旧调用方式) |
|
|
|
|
|
* 2. 数组:多条模板数据,格式 [{elemID: 'xxx', actualData: 'xxx', caseId: 'xxx'}, ...] |
|
|
* @returns {Promise} - 返回打印结果Promise |
|
|
* @returns {Promise} - 返回打印结果Promise |
|
|
*/ |
|
|
*/ |
|
|
async handlePrint(printData) { |
|
|
async handlePrint(printData) { |
|
|
const actualData = printData || this.actualData |
|
|
|
|
|
|
|
|
let templateDataList = [] |
|
|
|
|
|
let caseId = '' |
|
|
|
|
|
|
|
|
const test = { |
|
|
|
|
|
'elemID': this.elemID, |
|
|
|
|
|
'actualData': actualData |
|
|
|
|
|
|
|
|
if (typeof printData === 'string') { |
|
|
|
|
|
templateDataList = [ |
|
|
|
|
|
{ elemID: 'epc-id', actualData: printData } |
|
|
|
|
|
] |
|
|
|
|
|
} else if (Array.isArray(printData)) { |
|
|
|
|
|
templateDataList = printData.filter(item => { |
|
|
|
|
|
const isValid = item && item.elemID && item.actualData |
|
|
|
|
|
if (!isValid) { |
|
|
|
|
|
console.warn('过滤无效的模板数据:', item) |
|
|
|
|
|
} |
|
|
|
|
|
if (isValid && item.caseId && !caseId) { |
|
|
|
|
|
caseId = item.caseId |
|
|
|
|
|
} |
|
|
|
|
|
return isValid |
|
|
|
|
|
}) |
|
|
|
|
|
} else { |
|
|
|
|
|
templateDataList = this.defaultTemplateData.filter(item => item.elemID && item.actualData) |
|
|
} |
|
|
} |
|
|
console.log('test', test) |
|
|
|
|
|
|
|
|
|
|
|
// 校验数据 |
|
|
|
|
|
if (!actualData) { |
|
|
|
|
|
this.$message.warning('打印数据不能为空', { offset: 8 }) |
|
|
|
|
|
|
|
|
if (templateDataList.length === 0) { |
|
|
|
|
|
this.$message.warning('打印数据不能为空,请检查传入的模板数据', { offset: 8 }) |
|
|
return Promise.reject('打印数据不能为空') |
|
|
return Promise.reject('打印数据不能为空') |
|
|
} |
|
|
} |
|
|
|
|
|
console.log('最终模板数据列表:', templateDataList) |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
// 1. 获取模板文件Base64 |
|
|
// 1. 获取模板文件Base64 |
|
|
@ -157,7 +269,6 @@ export default { |
|
|
EPC: this.epcChecked, |
|
|
EPC: this.epcChecked, |
|
|
rfidType: this.tidChecked ? '1' : (this.epcChecked ? '2' : '0') |
|
|
rfidType: this.tidChecked ? '1' : (this.epcChecked ? '2' : '0') |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
console.log('组装参数', params) |
|
|
console.log('组装参数', params) |
|
|
|
|
|
|
|
|
// 3. 设置语言 |
|
|
// 3. 设置语言 |
|
|
@ -178,7 +289,7 @@ export default { |
|
|
if (statusRes.isReady !== '1') { |
|
|
if (statusRes.isReady !== '1') { |
|
|
this.$message.error('打印机状态异常', { offset: 8 }) |
|
|
this.$message.error('打印机状态异常', { offset: 8 }) |
|
|
await this.sendPostRequest('web_DSTP2x_DisconnDev', '断开连接', { 'devHdl': connectRes.devHdl }) |
|
|
await this.sendPostRequest('web_DSTP2x_DisconnDev', '断开连接', { 'devHdl': connectRes.devHdl }) |
|
|
return Promise.reject('打印机状态异常', { offset: 8 }) |
|
|
|
|
|
|
|
|
return Promise.reject('打印机状态异常') |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 7. 设置仿真类型 |
|
|
// 7. 设置仿真类型 |
|
|
@ -201,19 +312,35 @@ export default { |
|
|
'tmplFileSrc': params.tmplFileSrc |
|
|
'tmplFileSrc': params.tmplFileSrc |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
// 10. 设置模板数据 |
|
|
|
|
|
await this.sendPostRequest('web_DSTP2x_SetTmplPrnData', '设置模板', { |
|
|
|
|
|
|
|
|
// 10. 批量设置模板数据(支持多条) |
|
|
|
|
|
for (const item of templateDataList) { |
|
|
|
|
|
console.log(`设置模板数据 - elemID: ${item.elemID}, actualData: ${item.actualData}`) |
|
|
|
|
|
await this.sendPostRequest('web_DSTP2x_SetTmplPrnData', `设置模板数据[${item.elemID}]`, { |
|
|
'tmplHdl': tempRes.tmplHdl, |
|
|
'tmplHdl': tempRes.tmplHdl, |
|
|
'elemID': this.elemID, |
|
|
|
|
|
'actualData': actualData |
|
|
|
|
|
|
|
|
'elemID': item.elemID, |
|
|
|
|
|
'actualData': item.actualData |
|
|
}) |
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 11. 读RFID数据 |
|
|
|
|
|
|
|
|
// 11. 读RFID数据(并提取TID) |
|
|
|
|
|
let rfidReadRes = null |
|
|
if (params.rfidType !== '0') { |
|
|
if (params.rfidType !== '0') { |
|
|
await this.sendPostRequest('web_DSTP2x_RFID_ReadData', '读RFID数据', { |
|
|
|
|
|
|
|
|
rfidReadRes = await this.sendPostRequest('web_DSTP2x_RFID_ReadData', '读RFID数据', { |
|
|
'devHdl': connectRes.devHdl, |
|
|
'devHdl': connectRes.devHdl, |
|
|
'rfidType': params.rfidType |
|
|
'rfidType': params.rfidType |
|
|
}) |
|
|
}) |
|
|
|
|
|
console.log('RFID读取结果:', rfidReadRes) |
|
|
|
|
|
|
|
|
|
|
|
if (rfidReadRes && rfidReadRes.tid) { |
|
|
|
|
|
this.readTid = rfidReadRes.tid |
|
|
|
|
|
} else if (rfidReadRes && rfidReadRes.data && rfidReadRes.data[0] && rfidReadRes.data[0].tid) { |
|
|
|
|
|
this.readTid = rfidReadRes.data[0].tid |
|
|
|
|
|
} |
|
|
|
|
|
console.log('提取到的TID:', this.readTid) |
|
|
|
|
|
|
|
|
|
|
|
if (!this.readTid) { |
|
|
|
|
|
this.$message.warning('读取到的标签缺少TID信息', { offset: 8 }) |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 12. 执行打印 |
|
|
// 12. 执行打印 |
|
|
@ -227,8 +354,28 @@ export default { |
|
|
await this.sendPostRequest('web_DSTP2x_DeleteTmpl', '删除模板句柄', { 'tmplHdl': tempRes.tmplHdl }) |
|
|
await this.sendPostRequest('web_DSTP2x_DeleteTmpl', '删除模板句柄', { 'tmplHdl': tempRes.tmplHdl }) |
|
|
await this.sendPostRequest('web_DSTP2x_DisconnDev', '断开连接', { 'devHdl': connectRes.devHdl }) |
|
|
await this.sendPostRequest('web_DSTP2x_DisconnDev', '断开连接', { 'devHdl': connectRes.devHdl }) |
|
|
|
|
|
|
|
|
this.$message.success('打印成功', { offset: 8 }) |
|
|
|
|
|
return Promise.resolve('打印成功') |
|
|
|
|
|
|
|
|
this.$message.success(`成功打印${templateDataList.length}条模板数据`, { offset: 8 }) |
|
|
|
|
|
|
|
|
|
|
|
// 14. 打印成功后,调用writeEAS方法(如果有TID和盒ID) |
|
|
|
|
|
console.log(this.readTid + '/' + caseId) |
|
|
|
|
|
if (this.readTid && caseId) { |
|
|
|
|
|
console.log('开始执行标签绑定操作...') |
|
|
|
|
|
await this.writeEAS(caseId, this.readTid) |
|
|
|
|
|
} else if (!caseId) { |
|
|
|
|
|
console.warn('未传入盒ID,跳过标签绑定') |
|
|
|
|
|
this.$message.warning('未传入盒ID,跳过标签绑定', { offset: 8 }) |
|
|
|
|
|
} else if (!this.readTid) { |
|
|
|
|
|
console.warn('未读取到TID,跳过标签绑定') |
|
|
|
|
|
this.$message.warning('未读取到TID,跳过标签绑定', { offset: 8 }) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return Promise.resolve({ |
|
|
|
|
|
success: true, |
|
|
|
|
|
count: templateDataList.length, |
|
|
|
|
|
data: templateDataList, |
|
|
|
|
|
tid: this.readTid, |
|
|
|
|
|
caseId: caseId |
|
|
|
|
|
}) |
|
|
} catch (error) { |
|
|
} catch (error) { |
|
|
console.error('打印流程异常:', error) |
|
|
console.error('打印流程异常:', error) |
|
|
this.$message.error(`打印流程异常:${error}`, { offset: 8 }) |
|
|
this.$message.error(`打印流程异常:${error}`, { offset: 8 }) |
|
|
|