index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <template>
  2. <view class="kk-printer">
  3. <view class="kk-printer-btn">
  4. <u-button v-if="printBtnStyle=='blue-large'" @tap="handlePrintTap(1)" :loading="isPrinting||isConnecting" shape="circle" :custom-style="{background:$config('primaryColor')}" type="primary">{{isPrinting?printingText:defaultText}}</u-button>
  5. <u-button v-if="printBtnStyle=='default-mid'" shape="circle" size="medium" hover-class="none" @tap="handlePrintTap(1)" :loading="isPrinting||isConnecting">{{isPrinting?printingText:defaultText}}</u-button>
  6. </view>
  7. <view class="kk-shadow" :class="isShowSearch?'show':''" @tap="handleSearchClose">
  8. <view class="kk-modal" @tap.stop="doNothing">
  9. <view class="kk-search-device">
  10. <!-- <view class="kk-filter-wrap">
  11. <view class="filter-title">根据SRRI过滤设备</view>
  12. <slider @change="handleSRRIChange" max='-20' min='-100' value="-95" show-value/>
  13. </view> -->
  14. <!-- <view class="kk-filter-wrap">
  15. <view class="filter-title">根据蓝牙名过滤</view>
  16. <input type="text" placeholder-class="kk-placeholder-class" placeholder="请输入蓝牙名字或设备ID搜索" v-model="filterName" />
  17. </view> -->
  18. <view style="text-align: left;">
  19. <view>注意事项:</view>
  20. <view>1、连接打印机之前,先打开手机蓝牙开关</view>
  21. <view>2、请检查打印机是否已被其它手机连接,如果是请关闭其它手机蓝牙,然后再用您的手机连接</view>
  22. </view>
  23. <view class="kk-btn-wrap">
  24. <view class="kk-btn-item confirm-btn" @tap="searchBtnTap" v-if="!isSearching">
  25. 搜索设备
  26. </view>
  27. <view class="kk-btn-item confirm-btn" v-else>
  28. 搜索中...
  29. </view>
  30. <view class="kk-btn-item" @tap="stopSearchBtnTap">
  31. 停止搜索
  32. </view>
  33. </view>
  34. <view class="kk-devices-wrap">
  35. <view class="empty-wrap" v-if="filterDeviceList.length <= 0">
  36. <view class="empty-icon"></view>
  37. <view class="empty-text">~ 无可搜索到的设备 ~</view>
  38. </view>
  39. <view class="" v-else>
  40. <view class="kk-devices-item flex align_center justify_between" v-for="(item,index) in filterDeviceList" :key="index" @tap="handleConnectDevice(item)">
  41. <view style="text-align: left;flex-grow: 1;">
  42. <view class="name">
  43. <text>设备名称:</text>
  44. <text>{{item.name?item.name:'未命名'}}</text>
  45. </view>
  46. <view class="rssi">
  47. <text>信号强度:</text>
  48. <text>({{Math.max(0, item.RSSI + 100)}}%)</text>
  49. </view>
  50. <view class="deviceid">
  51. <text>设备ID:</text>
  52. <text>{{item.deviceId}}</text>
  53. </view>
  54. </view>
  55. <view style="width: 120rpx;">
  56. <!-- <u-icon :size="60" color="#55aaff" name="plus-circle"></u-icon> -->
  57. <u-button @tap="handleConnectDevice(item)" :loading="isConnecting" shape="circle" size="mini" :custom-style="{background:$config('primaryColor')}" type="primary">{{isConnecting?'连接中':'连接'}}</u-button>
  58. </view>
  59. </view>
  60. </view>
  61. </view>
  62. <view style="margin-top: 20upx;">
  63. <view v-if="!isConnecting" style="text-align: center;padding: 20upx;border:2upx solid #eee;border-radius: 15upx;" @tap="handleSearchClose">取消打印</view>
  64. </view>
  65. </view>
  66. </view>
  67. </view>
  68. </view>
  69. </template>
  70. <script>
  71. import tsc from '@/components/kk-printer/printer/tsc.js'
  72. // import esc from '@/components/kk-printer/printer/esc.js'
  73. import * as blesdk from './utils/bluetoolth';
  74. import util from './utils/util.js';
  75. export default {
  76. data(){
  77. return{
  78. //是否正在打印
  79. isPrinting:false,
  80. //是否正在搜索设备
  81. isSearching:false,
  82. // 是否正在连接
  83. isConnecting: false,
  84. //是否显示蓝牙列表
  85. isShowSearch:false,
  86. //按蓝牙名过滤
  87. filterName:'DL-',
  88. //按信号过滤
  89. filterRSSI:-95,
  90. //设备列表
  91. devicesList:[],
  92. //连接的设备ID
  93. deviceId:'',
  94. //根据设备ID获取的服务
  95. services:'',
  96. //获取特征值时返回的三要素
  97. serviceId: '',
  98. writeId: '',
  99. readId: ''
  100. }
  101. },
  102. props:{
  103. //按钮默认文字
  104. defaultText:{
  105. type:String,
  106. default:'快速打印'
  107. },
  108. //按钮打印中的文字
  109. printingText:{
  110. type:String,
  111. default:'打印中...'
  112. },
  113. // 按钮样式
  114. printBtnStyle:{
  115. type: String,
  116. default: 'blue-large'
  117. },
  118. roomcode:{
  119. type:String,
  120. require:true
  121. },
  122. CustName:{
  123. type:String,
  124. require:true
  125. },
  126. roomid:{
  127. type:String,
  128. require:true
  129. },
  130. autoConnect: {
  131. type: Boolean,
  132. default: true
  133. }
  134. },
  135. computed:{
  136. mapFilterRSSI(){
  137. return (0 - this.filterRSSI)
  138. },
  139. filterDeviceList(){
  140. let devices = this.devicesList;
  141. let name = this.filterName;
  142. let rssi = this.filterRSSI;
  143. //按RSSI过滤
  144. let filterDevices1 = devices.filter((item)=>{
  145. return item.RSSI > rssi
  146. })
  147. // 按名字过滤
  148. let filterDevices2
  149. if(name!=''){
  150. filterDevices2 = filterDevices1.filter((item)=>{
  151. return (item.name.indexOf(name) >= 0 || item.deviceId.indexOf(name) >= 0)
  152. })
  153. }else{
  154. filterDevices2 = filterDevices1
  155. }
  156. // 根据广播数据提取MAC地址
  157. for (let i = 0; i < filterDevices2.length;i++) {
  158. if (filterDevices2[i].hasOwnProperty('advertisData')){
  159. if (filterDevices2[i].advertisData.byteLength == 8) {
  160. filterDevices2[i].advMac = util.buf2hex(filterDevices2[i].advertisData.slice(2, 7));
  161. }
  162. }
  163. }
  164. return filterDevices2
  165. }
  166. },
  167. mounted() {
  168. this.handlePrintTap(0)
  169. },
  170. beforeDestroy(){
  171. this.stopSearchBtnTap();
  172. },
  173. methods:{
  174. doNothing(){
  175. return;
  176. },
  177. //点击打印按钮,flag : true 如果已连接直接执行打印命令,false,不执行
  178. handlePrintTap(flag){
  179. //打开蓝牙适配器
  180. blesdk.openBlue().then((res)=>{
  181. //获取已连接设备
  182. blesdk.getConnectedBluetoothDevices().then((res)=>{
  183. const lastDevice = uni.getStorageSync('vuex_lastBuleDevice')
  184. console.log(lastDevice)
  185. console.log(res,this.deviceId,this.serviceId,this.writeId,this.onPrintSuccess)
  186. // 如果之前连接过设备
  187. if(lastDevice){
  188. // 有正在连接的设备,且和上次连接设备一样
  189. if(res.devices.length && res.devices[0].deviceId == lastDevice.deviceId){
  190. this.deviceId = lastDevice.deviceId
  191. this.serviceId = lastDevice.serviceId
  192. this.writeId = lastDevice.writeId
  193. this.readId = lastDevice.readId
  194. if(flag){
  195. this.isPrinting = true;
  196. this.$nextTick(()=>{
  197. this.$emit("startPrint",{
  198. deviceId: this.deviceId,
  199. serviceId: this.serviceId,
  200. writeId: this.writeId
  201. },tsc,blesdk)
  202. })
  203. }
  204. }else{
  205. // 重新直接连接
  206. this.handleConnectDevice(lastDevice)
  207. }
  208. }else{
  209. if(this.autoConnect || flag){
  210. //若没有已连接设备,弹框搜索设备
  211. this.isShowSearch = true
  212. this.devicesList = []
  213. if(this.deviceId){
  214. this.closeConnect()
  215. }
  216. }
  217. }
  218. }).catch((err)=>{
  219. blesdk.catchToast(err);
  220. })
  221. }).catch((err)=>{
  222. console.log(err)
  223. blesdk.catchToast(err);
  224. })
  225. },
  226. onGetDevice(res){
  227. this.devicesList = res;
  228. },
  229. handleSearchClose(){
  230. if(!this.isConnecting){
  231. this.isShowSearch = false
  232. this.stopSearchBtnTap()
  233. }
  234. },
  235. handleSRRIChange(e){
  236. this.filterRSSI = e.detail.value
  237. },
  238. //开始搜索设备
  239. searchBtnTap(){
  240. blesdk.startBluetoothDevicesDiscovery();
  241. this.isSearching = true;
  242. blesdk.onfindBlueDevices(this.onGetDevice)
  243. },
  244. //停止搜索设备
  245. stopSearchBtnTap(){
  246. blesdk.stopBlueDevicesDiscovery();
  247. this.isSearching = false;
  248. },
  249. //点击连接设备
  250. handleConnectDevice(device){
  251. const _this = this
  252. let deviceId = device.deviceId;
  253. let name = device.name;
  254. this.deviceId = deviceId;
  255. console.log('deviceId',this.deviceId)
  256. this.isConnecting = true
  257. this.stopSearchBtnTap()
  258. uni.onBLEConnectionStateChange(function(res){
  259. console.log('连接',res)
  260. if(res.connected){
  261. plus.nativeUI.toast('设备'+ res.deviceId + '已连接',{
  262. verticalAlign:'center'
  263. })
  264. }else{
  265. _this.closeConnect()
  266. plus.nativeUI.toast('设备'+ res.deviceId + '已断开连接',{
  267. verticalAlign:'center'
  268. })
  269. const lastDevice = uni.getStorageSync('vuex_lastBuleDevice')
  270. if(lastDevice){
  271. this.handleConnectDevice(lastDevice)
  272. }
  273. }
  274. _this.isConnecting = false
  275. })
  276. blesdk.createBLEConnection(deviceId, this.onConnectSuccess, this.onConnectFail);
  277. },
  278. onConnectSuccess(res){
  279. this.stopSearchBtnTap()
  280. this.isConnecting = false
  281. blesdk.getBLEDeviceServices(this.deviceId, this.onGetServicesSuccess, this.onGetServicesFail);
  282. },
  283. onConnectFail(err){
  284. console.log('链接失败',err)
  285. this.isConnecting = false
  286. blesdk.catchToast(err.res)
  287. uni.setStorageSync('vuex_lastBuleDevice','')
  288. },
  289. onGetServicesSuccess(res){
  290. console.log('获取服务',res)
  291. this.services = res.serviceId;
  292. blesdk.getDeviceCharacteristics(this.deviceId, this.services, this.onGetCharacterSuccess, this.onGetCharacterFail);
  293. },
  294. onGetServicesFail(err){
  295. console.log('获取服务失败')
  296. },
  297. onGetCharacterSuccess(res){
  298. console.log('获取特征值成功',res)
  299. this.serviceId = res.serviceId;
  300. this.writeId = res.writeId;
  301. this.readId = res.readId;
  302. this.isShowSearch = false;
  303. try {
  304. uni.setStorageSync('vuex_lastBuleDevice', {deviceId:this.deviceId,serviceId:this.serviceId,writeId:this.writeId,readId:this.readId});
  305. } catch (e) {
  306. // error
  307. }
  308. },
  309. onGetCharacterFail(err){
  310. console.log('特征值获取失败')
  311. },
  312. onPrintSuccess(){
  313. this.isPrinting = false;
  314. console.log('打印成功')
  315. // this.$emit('onPrintSuccess')
  316. },
  317. onPrintFail(){
  318. console.log('打印失败')
  319. this.isPrinting = false;
  320. },
  321. closeConnect(){
  322. blesdk.closeBLEConnection(this.deviceId)
  323. },
  324. }
  325. }
  326. </script>
  327. <style lang="scss" scoped>
  328. .kk-printer{
  329. &-btn{
  330. width:100%;
  331. height:100%;
  332. }
  333. .kk-shadow{
  334. display: none;
  335. &.show{
  336. display: block;
  337. width:100vw;
  338. height:100vh;
  339. background: rgba(0,0,0,0.4);
  340. position: fixed;
  341. top: 0;
  342. left: 0;
  343. display: flex;
  344. justify-content: center;
  345. align-items: center;
  346. z-index: 10000;
  347. .kk-modal{
  348. width:680upx;
  349. height: 60%;
  350. padding:24upx;
  351. box-sizing: border-box;
  352. overflow-y: auto;
  353. border-radius: 20upx;
  354. background: #ffffff;
  355. display: flex;
  356. justify-content: center;
  357. align-items: center;
  358. .kk-search-device{
  359. width:100%;
  360. height: 100%;
  361. .kk-filter-wrap{
  362. width:100%;
  363. height: 200upx;
  364. display: flex;
  365. flex-direction: column;
  366. justify-content: flex-start;
  367. align-items: flex-start;
  368. .filter-title{
  369. line-height: 70upx;
  370. font-size: 30upx;
  371. color: #999999;
  372. }
  373. &>slider{
  374. width:90%;
  375. height: 90upx;
  376. }
  377. &>input{
  378. padding:0 20upx;
  379. box-sizing: border-box;
  380. border-radius: 10upx;
  381. height: 90upx;
  382. width:100%;
  383. border: 1upx solid #ebebeb;
  384. }
  385. }
  386. .kk-btn-wrap{
  387. width:100%;
  388. height: 140upx;
  389. display: flex;
  390. justify-content: space-between;
  391. align-items: center;
  392. &>view{
  393. flex:1 1 auto;
  394. height: 80upx;
  395. line-height: 80upx;
  396. border-radius: 100upx;
  397. text-align: center;
  398. color:#ffffff;
  399. &.confirm-btn{
  400. background: #007AFF;
  401. margin-right:30upx;
  402. }
  403. &:nth-last-child(1){
  404. background: #DD524D;
  405. }
  406. }
  407. }
  408. .kk-devices-wrap{
  409. height: calc(100% - 460upx);
  410. overflow-y:auto;
  411. padding:10upx 20upx;
  412. box-sizing: border-box;
  413. border: 1upx solid #ebebeb;
  414. box-sizing: border-box;
  415. border-radius: 20upx;
  416. .empty-wrap{
  417. width:100%;
  418. height: 100%;
  419. display: flex;
  420. flex-direction: column;
  421. justify-content: center;
  422. align-items: center;
  423. .empty-icon{
  424. width:268upx;
  425. height: 240upx;
  426. background: url('./empty-icon.png') no-repeat;
  427. background-size:100% 100%;
  428. margin-bottom: 26upx;
  429. }
  430. .empty-text{
  431. width: 100%;
  432. line-height: 50upx;
  433. font-size: 30upx;
  434. text-align: center;
  435. color: #999999;
  436. }
  437. }
  438. .kk-devices-item{
  439. width:100%;
  440. border-bottom: 1upx solid #ebebeb;
  441. padding:10upx 0;
  442. box-sizing: border-box;
  443. &:nth-last-child(1){
  444. border-bottom: none;
  445. }
  446. > view{
  447. &:first-child{
  448. flex-grow: 1;
  449. width: 80%;
  450. }
  451. }
  452. button{
  453. width: auto;
  454. }
  455. }
  456. }
  457. }
  458. }
  459. }
  460. }
  461. }
  462. .kk-placeholder-class{
  463. font-size: 30upx;
  464. color:#999999;
  465. }
  466. </style>