You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
236 lines
6.1 KiB
236 lines
6.1 KiB
2 years ago
|
// #ifdef APP
|
||
|
const sunmi_print = uni.requireNativePlugin('Sunmi-Print-Inner')
|
||
|
const TTSSpeech = uni.requireNativePlugin("MT-TTS-Speech");
|
||
|
// #endif
|
||
|
const utils={
|
||
|
init:()=>{
|
||
|
return new Promise((relove, reject)=>{
|
||
|
sunmi_print.connect(res => {
|
||
|
console.log(res)
|
||
|
if(res.connect == "hello"){
|
||
|
console.log("打印机成功连接")
|
||
|
relove(true)
|
||
|
}else{
|
||
|
relove(false)
|
||
|
}
|
||
|
})
|
||
|
})
|
||
|
},
|
||
|
getstate:()=>{
|
||
|
return new Promise((relove, reject)=>{
|
||
|
sunmi_print.getPrinterStatus(result => {
|
||
|
console.log("打印机当前状态是:" + result.status)
|
||
|
if(result.status=='running'){
|
||
|
relove(true)
|
||
|
}else{
|
||
|
let title;
|
||
|
switch (result.status){
|
||
|
case 'offline':
|
||
|
title='打印机未连接'
|
||
|
break;
|
||
|
case 'fault':
|
||
|
title='打印机故障'
|
||
|
break;
|
||
|
case 'outpaper':
|
||
|
title='打印机缺纸'
|
||
|
break;
|
||
|
case 'hot':
|
||
|
title='打印机过热'
|
||
|
break;
|
||
|
case 'open':
|
||
|
title='打印机开盖'
|
||
|
break;
|
||
|
case 'error':
|
||
|
title='未知错误'
|
||
|
break;
|
||
|
}
|
||
|
uni.showToast({
|
||
|
title,
|
||
|
icon:"error"
|
||
|
})
|
||
|
relove(false)
|
||
|
}
|
||
|
})
|
||
|
})
|
||
|
|
||
|
},
|
||
|
printxie:async (obj)=>{
|
||
|
let res=await sunmi_print.labelLocate();
|
||
|
console.log(res);
|
||
|
setTimeout(()=>{
|
||
|
sunmi_print.printText({
|
||
|
text:'日期:'+obj.rigthnumer+' ',
|
||
|
align:2,
|
||
|
size:22,
|
||
|
bold:true,
|
||
|
underline:false,
|
||
|
compact:false,
|
||
|
skip:true
|
||
|
})
|
||
|
sunmi_print.printText({
|
||
|
text:obj.title,
|
||
|
align:1,
|
||
|
size:90,
|
||
|
bold:true,
|
||
|
underline:false,
|
||
|
compact:true,
|
||
|
skip:true
|
||
|
})
|
||
|
sunmi_print.printDividingline({
|
||
|
style:0,
|
||
|
height:"3"
|
||
|
}),
|
||
|
sunmi_print.printText({
|
||
|
text:`件数:${obj.number1}`,
|
||
|
align:0,
|
||
|
size:40,
|
||
|
bold:true,
|
||
|
underline:false,
|
||
|
compact:true,
|
||
|
skip:true
|
||
|
})
|
||
|
sunmi_print.printText({
|
||
|
text:obj.leftnumber,
|
||
|
align:0,
|
||
|
size:27,
|
||
|
bold:true,
|
||
|
underline:false,
|
||
|
compact:true,
|
||
|
skip:true
|
||
|
})
|
||
|
sunmi_print.labelOutput()
|
||
|
},2000)
|
||
|
|
||
|
},
|
||
|
initbl:()=>{
|
||
|
let deviceList = [];
|
||
|
let main = plus.android.runtimeMainActivity();
|
||
|
let Context = plus.android.importClass("android.content.Context");
|
||
|
let BManager = main.getSystemService(Context.BLUETOOTH_SERVICE);
|
||
|
plus.android.importClass(BManager);
|
||
|
let BAdapter = BManager.getAdapter();
|
||
|
plus.android.importClass(BAdapter);
|
||
|
let lists = BAdapter.getBondedDevices();
|
||
|
plus.android.importClass(lists);
|
||
|
let iterator = lists.iterator();
|
||
|
plus.android.importClass(iterator);
|
||
|
while (iterator.hasNext()) {
|
||
|
let d = iterator.next();
|
||
|
plus.android.importClass(d);
|
||
|
let temp = {
|
||
|
name: d.getName(),
|
||
|
address: d.getAddress(),
|
||
|
status: d.getBondState(),
|
||
|
uuids: d.getUuids(),
|
||
|
op: d
|
||
|
};
|
||
|
deviceList.push(temp);
|
||
|
}
|
||
|
return deviceList
|
||
|
},
|
||
|
getbl:(dev,text)=>{
|
||
|
let main = plus.android.runtimeMainActivity();
|
||
|
let BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
|
||
|
let UUID = plus.android.importClass("java.util.UUID");
|
||
|
let uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
|
||
|
let BAdapter = BluetoothAdapter.getDefaultAdapter();
|
||
|
let device = BAdapter.getRemoteDevice(dev.address);
|
||
|
plus.android.importClass(device);
|
||
|
let bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
|
||
|
plus.android.importClass(bluetoothSocket);
|
||
|
console.log("开始连接打印机:"+dev.name);
|
||
|
if (!bluetoothSocket.isConnected()) {
|
||
|
bluetoothSocket.connect();
|
||
|
if (bluetoothSocket.isConnected()) {
|
||
|
console.log("设备已连接,开始发送打印文件");
|
||
|
let outputStream = bluetoothSocket.getOutputStream();
|
||
|
plus.android.importClass(outputStream);
|
||
|
// sb(outputStream);
|
||
|
|
||
|
// text+='PRINT\r\n'
|
||
|
console.log(text)
|
||
|
let arrayBuffer = plus.android.invoke(text, 'getBytes', 'gbk');
|
||
|
outputStream.write(arrayBuffer);
|
||
|
// console.log(111111111);
|
||
|
outputStream.flush();
|
||
|
bluetoothSocket.close();
|
||
|
if (!bluetoothSocket.isConnected()) {
|
||
|
console.log("设备已关闭");
|
||
|
}
|
||
|
} else {
|
||
|
uni.showToast({
|
||
|
title: '设备连接失败',
|
||
|
icon:'error',
|
||
|
duration: 2000
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
inittts(){
|
||
|
TTSSpeech.init((status) => {
|
||
|
if(status === 0){
|
||
|
console.log('引擎初始化成功')
|
||
|
TTSSpeech.setSpeed(100)
|
||
|
TTSSpeech.getInstallTTS(res => {
|
||
|
console.log(JSON.stringify(res));
|
||
|
let flage=false
|
||
|
res.forEach(v => {
|
||
|
if(v.name=='com.iflytek.speechcloud'){
|
||
|
flage=true
|
||
|
}
|
||
|
console.log(v.label + ': ' +v.name)
|
||
|
})
|
||
|
if(!flage){
|
||
|
// uni.showToast({
|
||
|
// title:'当前未安装语音引擎,前往安装中',
|
||
|
// icon:'none'
|
||
|
// })
|
||
|
uni.showModal({
|
||
|
title: '安装语音引擎',
|
||
|
content: '请选择浏览器下载安装还在直接安装',
|
||
|
cancelText:'浏览器安装',
|
||
|
confirmText:'直接安装',
|
||
|
success: function (res) {
|
||
|
if (res.confirm) {
|
||
|
var fileSaveUrl = plus.io.convertLocalFileSystemURL('static/TTS.apk');
|
||
|
plus.runtime.install( //安装
|
||
|
fileSaveUrl, {
|
||
|
orce: true
|
||
|
},
|
||
|
function(res) {
|
||
|
utils.showToast('更新成功,请重新打开APP');
|
||
|
TTSSpeech.setEngine('com.iflytek.speechcloud');
|
||
|
// plus.runtime.restart()
|
||
|
plus.runtime.quit();
|
||
|
}
|
||
|
);
|
||
|
} else if (res.cancel) {
|
||
|
setTimeout(()=>{
|
||
|
plus.runtime.openURL('http://htys.oss-cn-chengdu.aliyuncs.com/htys/5402c622c319fac17c50fe52581cb627.apk')
|
||
|
setTimeout(()=>{
|
||
|
plus.runtime.quit();
|
||
|
},2500)
|
||
|
},1500)
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
|
||
|
}else{
|
||
|
// uni.showToast({
|
||
|
// title:''
|
||
|
// })
|
||
|
}
|
||
|
})
|
||
|
}else{
|
||
|
console.log('引擎初始化失败')
|
||
|
}
|
||
|
}, 'com.iflytek.speechcloud')
|
||
|
},
|
||
|
ttsspke:(text)=>{ //语音播报
|
||
|
console.log('语音播报');
|
||
|
if(!text){return}
|
||
|
TTSSpeech.speak({ text });
|
||
|
}
|
||
|
}
|
||
|
export default utils
|