272 lines
5.7 KiB
Vue
Executable File
272 lines
5.7 KiB
Vue
Executable File
<template>
|
||
<view class="container">
|
||
<view class="container-top-control">
|
||
<button @tap="searchBlueTooth">搜索蓝牙</button>
|
||
</view>
|
||
<view class="content-col" v-for="item in itemArray">
|
||
<view class="content-col-row">
|
||
<view>名    称:{{item.name}}</view>
|
||
<view>物理地址:{{item.mac}}</view>
|
||
<text v-if="item.matched">已匹配</text>
|
||
<text v-if="item.connected">已连接</text>
|
||
</view>
|
||
<button @tap="connectBlueTooth(item)" v-if="!isConected">连 接</button>
|
||
<button @tap="disConnectedBlueTooth(item)" v-if="item.connected">取消连接</button>
|
||
</view>
|
||
<view class="font-30" v-show="searchFlag==1">正在搜索附近蓝牙中...</view>
|
||
<view class="font-30" v-show="searchFlag==2">已完成蓝牙搜索</view>
|
||
<view class="btn-print" v-if="showPrint" @click="boothPrint">打印</view>
|
||
</view>
|
||
</template>
|
||
|
||
<script>
|
||
import {
|
||
mapState,
|
||
mapMutations
|
||
} from 'vuex';
|
||
|
||
const blueToothModule = uni.requireNativePlugin("BlueUtilModule");
|
||
var _self;
|
||
export default {
|
||
data() {
|
||
return {
|
||
itemArray: new Array(),
|
||
scanBlueDisabled: true,
|
||
scanStopShow: false,
|
||
isConected: false,
|
||
showPrint:false,
|
||
//0不显示 1搜索中 2搜索完成
|
||
searchFlag:0,
|
||
printInfo:[],
|
||
currItem:""
|
||
}
|
||
},
|
||
|
||
onLoad(options) {
|
||
_self = this;
|
||
if(options.item && options.item.length>0){
|
||
const item = JSON.parse(decodeURIComponent(options.item));
|
||
this.printInfo = item;
|
||
}
|
||
this.updateCacheByStorage();
|
||
},
|
||
// onBackPress() {
|
||
// this.disConnectedBlueTooth(this.currItem);
|
||
// return true;
|
||
// },
|
||
|
||
methods: {
|
||
|
||
searchBlueTooth(){
|
||
// //debugger
|
||
_self.searchFlag = 1;
|
||
_self.itemArray = new Array();
|
||
blueToothModule.searchBlueTooth(
|
||
(data)=>{
|
||
let device = data.data;
|
||
|
||
if(device == true) {
|
||
_self.searchFlag = 2;
|
||
return true;
|
||
}
|
||
|
||
if(_self.searchFlag == 2) {
|
||
_self.searchFlag = 1;
|
||
}
|
||
|
||
if(this.itemArray.length > 0) {
|
||
let addFlag = true;
|
||
this.itemArray.forEach(
|
||
function(el){
|
||
if(el.mac == device.mac){
|
||
addFlag = false;
|
||
}
|
||
}
|
||
);
|
||
|
||
if(addFlag){
|
||
_self.itemArray.push(device);
|
||
if(device.connected) {
|
||
this.isConected = true;
|
||
this.scanBlueDisabled = false;
|
||
}
|
||
}
|
||
}
|
||
else {
|
||
_self.itemArray.push(device);
|
||
if(device.connected) {
|
||
this.isConected = true;
|
||
this.scanBlueDisabled = false;
|
||
}
|
||
}
|
||
}
|
||
);
|
||
},
|
||
|
||
connectBlueTooth(item){
|
||
let mac = item.mac;
|
||
uni.showLoading({
|
||
title:"连接中",
|
||
mask:true
|
||
});
|
||
|
||
blueToothModule.connectBlueTooth(
|
||
{"mac": mac, "className": "BTBlueTooth"},
|
||
(data)=>{
|
||
uni.hideLoading();
|
||
_self.scanBlueDisabled = !data.data;
|
||
_self.isConected = data.data;
|
||
_self.showPrint = data.data;
|
||
item.connected = data.data;
|
||
|
||
uni.navigateBack({
|
||
delta: 1
|
||
});
|
||
this.$store.commit('blueConnectedFlag', true);
|
||
}
|
||
);
|
||
},
|
||
|
||
boothPrint(){
|
||
let printInfo = this.printInfo;
|
||
if(printInfo && printInfo.length > 0){
|
||
uni.showLoading({
|
||
title:"打印中",
|
||
mask:true
|
||
});
|
||
blueToothModule.printBarCode(
|
||
{"printInfo":printInfo},
|
||
(data)=>{
|
||
|
||
}
|
||
);
|
||
uni.hideLoading();
|
||
} else {
|
||
uni.showToast({
|
||
icon:"none",
|
||
|
||
title:"无打印信息",
|
||
duration:3500
|
||
});
|
||
return false
|
||
}
|
||
},
|
||
|
||
disConnectedBlueTooth(item){
|
||
this.currItem = item;
|
||
uni.showLoading({
|
||
title:"正在停止连接",
|
||
mask:true
|
||
});
|
||
item.connected = false;
|
||
this.isConected = false;
|
||
this.scanBlueDisabled = true;
|
||
this.scanStopShow = false;
|
||
this.showPrint = false;
|
||
this.$store.commit('blueConnectedFlag',false)
|
||
uni.setStorage({
|
||
key: 'isScanning',
|
||
data: "0"
|
||
});
|
||
|
||
blueToothModule.destroyScan(
|
||
(data)=>{
|
||
let jsonString = data.data;
|
||
uni.hideLoading();
|
||
}
|
||
);
|
||
},
|
||
|
||
...mapMutations(['updateCacheByStorage'])
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style>
|
||
.container {
|
||
padding: 40rpx;
|
||
padding-top: 30rpx;
|
||
}
|
||
|
||
.container-top-control {
|
||
display: flex;
|
||
flex-direction: row;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
}
|
||
|
||
button {
|
||
width: 180rpx;
|
||
height: 60rpx;
|
||
line-height: 60rpx;
|
||
font-size: 30rpx;
|
||
text-align: center;
|
||
background-color: #007AFF;
|
||
color: #FFFFFF;
|
||
flex-shrink: 0;
|
||
flex-grow: 0;
|
||
}
|
||
|
||
.content-col {
|
||
display:flex;
|
||
flex-direction: row;
|
||
align-items: center;
|
||
justify-content: center;
|
||
background-color: #DDDDDD;
|
||
border-radius: 10rpx;
|
||
margin-top: 20rpx;
|
||
padding-left: 10rpx;
|
||
padding-right: 10rpx;
|
||
}
|
||
|
||
.content-col-row {
|
||
display:flex;
|
||
flex-direction: column;
|
||
flex-shrink: 1;
|
||
flex-grow: 1;
|
||
padding-top: 10rpx;
|
||
padding-bottom: 10rpx;
|
||
font-size: 30rpx;
|
||
}
|
||
|
||
.content-col-row text{
|
||
flex-shrink: 1;
|
||
flex-grow: 1;
|
||
line-height:54rpx;
|
||
height: 54rpx;
|
||
}
|
||
|
||
|
||
|
||
.content-col button{
|
||
flex-shrink: 0;
|
||
flex-grow: 0;
|
||
width: 180rpx;
|
||
height: 54rpx;
|
||
line-height: 54rpx;
|
||
font-size: 30rpx;
|
||
text-align: center;
|
||
background-color: #007AFF;
|
||
color: #FFFFFF;
|
||
}
|
||
|
||
.font-30{
|
||
font-size: 30rpx;
|
||
line-height: 40rpx;
|
||
}
|
||
|
||
.btn-print{
|
||
width: 100%;
|
||
height: 80rpx;
|
||
line-height: 80rpx;
|
||
font-size: 32rpx;
|
||
font-family: MicrosoftYaHei;
|
||
color: #FFFFFF;
|
||
text-align: center;
|
||
background-color: #007AFF;
|
||
border-radius: unset;
|
||
position: fixed;
|
||
left: 0;
|
||
bottom: 0;
|
||
}
|
||
</style> |