Commit a091d076 authored by 张海景's avatar 张海景

update:修改打印的方法

parent 51b1b54a
...@@ -6,6 +6,10 @@ export default { ...@@ -6,6 +6,10 @@ export default {
plus.screen.lockOrientation('landscape-primary'); plus.screen.lockOrientation('landscape-primary');
// #endif // #endif
}, },
//把想要全局的数据放在globalData对象里,globalData是uniapp的不可以自定义命名。
globalData:{
printSocket:null
},
onShow: function () {}, onShow: function () {},
onHide: function () {} onHide: function () {}
}; };
......
...@@ -135,6 +135,7 @@ export default { ...@@ -135,6 +135,7 @@ export default {
}; };
}, },
created() { created() {
this.createSocket()
uni.$on('switchTab', (indexCode) => { uni.$on('switchTab', (indexCode) => {
this.tabIndex = indexCode; this.tabIndex = indexCode;
}); });
...@@ -238,8 +239,11 @@ export default { ...@@ -238,8 +239,11 @@ export default {
this.$u.api.getUserPrintInfo().then((res) => { this.$u.api.getUserPrintInfo().then((res) => {
// console.log(res, 'res888') // console.log(res, 'res888')
if (res.code === 200) { if (res.code === 200) {
this.$u.vuex('vuex_printerHostUrl', 'http://' + res.data.printerIp + ':' + res.data.printerPort); this.$u.vuex('vuex_printerInfo', {
this.$u.vuex('vuex_printerName', res.data.printerName); hostUrl:'http://'+ res.data.printerIp +':'+ res.data.printerPort,
printerName: res.data.printerName
});
this.createSocket()
} }
}); });
this.getPrint(); this.getPrint();
......
...@@ -1534,8 +1534,12 @@ export default { ...@@ -1534,8 +1534,12 @@ export default {
this.printTemplateData.printTemplate = this.$refs.printTemplateRef.tableSelectData[0].name this.printTemplateData.printTemplate = this.$refs.printTemplateRef.tableSelectData[0].name
this.printTemplateVisible = false this.printTemplateVisible = false
}, },
doFeedback() { async doFeedback() {
// this.reset(); // this.reset();
if (!this.vuex_printConnect) {
this.$u.toast('打印机连接失败');
return
}
this.feedbackForms = []; this.feedbackForms = [];
this.tableSelectData.forEach((item) => { this.tableSelectData.forEach((item) => {
let defQualify = item.quantity-item.quantityQualify-item.quantityUnqualify; let defQualify = item.quantity-item.quantityQualify-item.quantityUnqualify;
......
...@@ -86,8 +86,11 @@ ...@@ -86,8 +86,11 @@
this.$u.api.getUserPrintInfo().then(res=> { this.$u.api.getUserPrintInfo().then(res=> {
// console.log(res, 'res888') // console.log(res, 'res888')
if (res.code === 200) { if (res.code === 200) {
this.$u.vuex('vuex_printerHostUrl', 'http://'+ res.data.printerIp +':'+ res.data.printerPort); this.$u.vuex('vuex_printerInfo', {
this.$u.vuex('vuex_printerName', res.data.printerName); hostUrl:'http://'+ res.data.printerIp +':'+ res.data.printerPort,
printerName: res.data.printerName
});
this.createSocket()
} }
}) })
setTimeout(() => { setTimeout(() => {
......
...@@ -26,8 +26,6 @@ module.exports = { ...@@ -26,8 +26,6 @@ module.exports = {
}, },
data() { data() {
return { return {
pdfSocket: null,
socketConnect: false,
} }
}, },
computed: { computed: {
...@@ -35,66 +33,61 @@ module.exports = { ...@@ -35,66 +33,61 @@ module.exports = {
...mapState($uStoreKey) ...mapState($uStoreKey)
}, },
methods: { methods: {
createSocket(params) { createSocket() {
this.pdfSocket = io(this.vuex_printerHostUrl, { this.$u.vuex('vuex_printConnect', false)
let socket = io(this.vuex_printerInfo.hostUrl, {
transports: ['websocket'], transports: ['websocket'],
auth: { auth: {
token: 'Bearer ' + this.vuex_token, // 在此处填入你 client 设置的 token,缺省可留空 token: 'Bearer ' + this.vuex_token, // 在此处填入你 client 设置的 token,缺省可留空
}, },
}); });
this.pdfSocket.on('connect', () => {
this.socketConnect = true; socket.on('connect', () => {
// console.log('连接成功444'); console.log('连接成功444');
this.printPdfSocket(params)
// TODO: Do something for your project // TODO: Do something for your project
this.$u.vuex('vuex_printConnect', true);
// console.log(this.vuex_printConnect, 'vuex_printConnect');
}); });
this.pdfSocket.on('clients', (clients) => { socket.on('clients', (clients) => {
this.socketConnect = clients;
console.log(clients, 'clients'); console.log(clients, 'clients');
}); });
this.pdfSocket.on('printerList', (printerList) => { socket.on('printerList', (printerList) => {
// globalThis.printerList = printerList; // globalThis.printerList = printerList;
// console.log(printerList, 'printerList'); // console.log(printerList, 'printerList');
}); });
this.pdfSocket.on("success", (res) => { socket.on('success', (res) => {
console.log(res, '打印成功') console.log(res, '打印成功');
}) });
this.pdfSocket.on("error", (res) => {
console.log(res, '打印失败')
})
this.pdfSocket.on("disconnect", () => {
this.socketConnect = false;
console.log('打印机断开')
})
setTimeout(() => {
if (!this.socketConnect) {
console.log('连接失败')
this.$u.toast('打印机连接失败')
}
}, 1000)
socket.on('error', (res) => {
console.log(res, '打印失败');
});
socket.on('disconnect', () => {
console.log('打印机断开');
this.$u.vuex('vuex_printConnect', false);
});
getApp().globalData.printSocket = socket
}, },
printPdfSocket(params) { async printPdfSocket(params) {
// console.log(this.pdfSocket, 'pdfSocket') if (!this.vuex_printerInfo.hostUrl) {
if (!this.socketConnect) { this.$u.toast('请绑定打印机');
this.createSocket(params) return;
return
} }
this.$u.api if (this.vuex_printConnect) {
.getPrintDown(params) this.$u.api.getPrintDown(params).then((res) => {
.then((res) => {
if (res.code === 200) { if (res.code === 200) {
this.pdfSocket.emit('news', { getApp().globalData.printSocket.emit('news', {
client: null, client: null,
printer: this.vuex_printerName, printer: this.vuex_printerInfo.printerName,
type: 'url_pdf', type: 'url_pdf',
// pdf_path: 'http://192.168.3.91:8082/static/123.pdf', // pdf_path: 'http://192.168.3.91:8082/static/123.pdf',
pdf_path: res.fileUrl, pdf_path: res.fileUrl,
}); });
} }
}); });
} else {
this.$u.toast('打印机连接失败');
}
} }
} }
} }
\ No newline at end of file
...@@ -16,8 +16,16 @@ try { ...@@ -16,8 +16,16 @@ try {
} }
// 需要永久存储,且下次APP启动需要取出的,在state中的变量名 // 需要永久存储,且下次APP启动需要取出的,在state中的变量名
let saveStateKeys = ['vuex_user', 'vuex_token', 'vuex_remember', 'vuex_locale', 'vuex_isAgent', 'vuex_workstation', let saveStateKeys = [
'vuex_task', 'vuex_printerHostUrl', 'vuex_printerName' 'vuex_user',
'vuex_token',
'vuex_remember',
'vuex_locale',
'vuex_isAgent',
'vuex_workstation',
'vuex_task',
'vuex_printerInfo',
'vuex_printConnect',
]; ];
// 保存变量到本地存储中 // 保存变量到本地存储中
...@@ -38,9 +46,11 @@ const store = new Vuex.Store({ ...@@ -38,9 +46,11 @@ const store = new Vuex.Store({
state: { state: {
// 如果上面从本地获取的lifeData对象下有对应的属性,就赋值给state中对应的变量 // 如果上面从本地获取的lifeData对象下有对应的属性,就赋值给state中对应的变量
// 加上vuex_前缀,是防止变量名冲突,也让人一目了然 // 加上vuex_前缀,是防止变量名冲突,也让人一目了然
vuex_user: lifeData.vuex_user ? lifeData.vuex_user : { vuex_user: lifeData.vuex_user
userName: 'Aidex' ? lifeData.vuex_user
}, : {
userName: 'Aidex',
},
vuex_token: lifeData.vuex_token ? lifeData.vuex_token : '', vuex_token: lifeData.vuex_token ? lifeData.vuex_token : '',
vuex_remember: lifeData.vuex_remember ? lifeData.vuex_remember : '', vuex_remember: lifeData.vuex_remember ? lifeData.vuex_remember : '',
vuex_locale: lifeData.vuex_locale ? lifeData.vuex_locale : '', vuex_locale: lifeData.vuex_locale ? lifeData.vuex_locale : '',
...@@ -53,10 +63,14 @@ const store = new Vuex.Store({ ...@@ -53,10 +63,14 @@ const store = new Vuex.Store({
vuex_workunit: null, vuex_workunit: null,
//当前工作站正在进行的生产任务 //当前工作站正在进行的生产任务
vuex_task: null, vuex_task: null,
// 打印机服务器地址 // 打印机服务器信息
vuex_printerHostUrl: lifeData.vuex_printerHostUrl ? lifeData.vuex_printerHostUrl : '', vuex_printerInfo: lifeData.vuex_printerInfo
// 打印机名称s ? lifeData.vuex_printerInfo
vuex_printerName: lifeData.vuex_printerName ? lifeData.vuex_printerName : '', : '',
// 存打印状态
vuex_printConnect: lifeData.vuex_printConnect
? lifeData.vuex_printConnect
: '',
// 自定义tabbar数据 // 自定义tabbar数据
// vuex_tabbar: [{ // vuex_tabbar: [{
...@@ -99,9 +113,9 @@ const store = new Vuex.Store({ ...@@ -99,9 +113,9 @@ const store = new Vuex.Store({
saveKey = payload.name; saveKey = payload.name;
} }
// 保存变量到本地,见顶部函数定义 // 保存变量到本地,见顶部函数定义
saveLifeData(saveKey, state[saveKey]) saveLifeData(saveKey, state[saveKey]);
} },
} },
}) });
export default store export default store
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment