index.vue 14 KB

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