index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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;text-align: right;">
  56. <!-- <u-icon :size="60" color="#55aaff" name="plus-circle"></u-icon> -->
  57. <u-button :loading="isConnecting" @tap="handleConnectDevice(item)" 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. this.searchBtnTap()
  214. }
  215. }
  216. }).catch((err)=>{
  217. blesdk.catchToast(err);
  218. })
  219. }).catch((err)=>{
  220. console.log(err)
  221. blesdk.catchToast(err);
  222. })
  223. },
  224. onGetDevice(res){
  225. this.devicesList = res;
  226. },
  227. handleSearchClose(){
  228. if(!this.isConnecting){
  229. this.isShowSearch = false
  230. this.stopSearchBtnTap()
  231. }
  232. },
  233. handleSRRIChange(e){
  234. this.filterRSSI = e.detail.value
  235. },
  236. //开始搜索设备
  237. searchBtnTap(){
  238. blesdk.startBluetoothDevicesDiscovery();
  239. this.isSearching = true;
  240. blesdk.onfindBlueDevices(this.onGetDevice)
  241. },
  242. //停止搜索设备
  243. stopSearchBtnTap(){
  244. blesdk.stopBlueDevicesDiscovery();
  245. this.isSearching = false;
  246. },
  247. //点击连接设备
  248. handleConnectDevice(device){
  249. const _this = this
  250. let deviceId = device.deviceId;
  251. let name = device.name;
  252. this.deviceId = deviceId;
  253. console.log('deviceId',this.deviceId)
  254. this.isConnecting = true
  255. this.stopSearchBtnTap()
  256. uni.onBLEConnectionStateChange(function(res){
  257. console.log('连接',res)
  258. if(res.connected){
  259. plus.nativeUI.toast('设备'+ res.deviceId + '已连接',{
  260. verticalAlign:'center'
  261. })
  262. }else{
  263. _this.closeConnect()
  264. plus.nativeUI.toast('设备'+ res.deviceId + '已断开连接',{
  265. verticalAlign:'center'
  266. })
  267. this.isShowSearch = true
  268. this.devicesList = []
  269. this.searchBtnTap()
  270. }
  271. _this.isConnecting = false
  272. })
  273. blesdk.createBLEConnection(deviceId, this.onConnectSuccess, this.onConnectFail);
  274. },
  275. onConnectSuccess(res){
  276. this.stopSearchBtnTap()
  277. this.isConnecting = false
  278. blesdk.getBLEDeviceServices(this.deviceId, this.onGetServicesSuccess, this.onGetServicesFail);
  279. },
  280. onConnectFail(err){
  281. console.log('链接失败',err)
  282. this.isConnecting = false
  283. blesdk.catchToast(err.res)
  284. uni.setStorageSync('vuex_lastBuleDevice','')
  285. if(err.res.code == 10012){
  286. this.isShowSearch = true
  287. this.devicesList = []
  288. this.searchBtnTap()
  289. }
  290. },
  291. onGetServicesSuccess(res){
  292. console.log('获取服务',res)
  293. this.services = res.serviceId;
  294. blesdk.getDeviceCharacteristics(this.deviceId, this.services, this.onGetCharacterSuccess, this.onGetCharacterFail);
  295. },
  296. onGetServicesFail(err){
  297. console.log('获取服务失败')
  298. },
  299. onGetCharacterSuccess(res){
  300. console.log('获取特征值成功',res)
  301. this.serviceId = res.serviceId;
  302. this.writeId = res.writeId;
  303. this.readId = res.readId;
  304. this.isShowSearch = false;
  305. try {
  306. uni.setStorageSync('vuex_lastBuleDevice', {deviceId:this.deviceId,serviceId:this.serviceId,writeId:this.writeId,readId:this.readId});
  307. } catch (e) {
  308. // error
  309. }
  310. },
  311. onGetCharacterFail(err){
  312. console.log('特征值获取失败')
  313. },
  314. onPrintSuccess(){
  315. this.isPrinting = false;
  316. console.log('打印成功')
  317. // this.$emit('onPrintSuccess')
  318. },
  319. onPrintFail(){
  320. console.log('打印失败')
  321. this.isPrinting = false;
  322. },
  323. closeConnect(){
  324. blesdk.closeBLEConnection(this.deviceId)
  325. },
  326. }
  327. }
  328. </script>
  329. <style lang="scss" scoped>
  330. .kk-printer{
  331. &-btn{
  332. width:100%;
  333. height:100%;
  334. }
  335. .kk-shadow{
  336. display: none;
  337. &.show{
  338. display: block;
  339. width:100vw;
  340. height:100vh;
  341. background: rgba(0,0,0,0.4);
  342. position: fixed;
  343. top: 0;
  344. left: 0;
  345. display: flex;
  346. justify-content: center;
  347. align-items: center;
  348. z-index: 10000;
  349. .kk-modal{
  350. width:680upx;
  351. height: 60%;
  352. padding:24upx;
  353. box-sizing: border-box;
  354. overflow-y: auto;
  355. border-radius: 20upx;
  356. background: #ffffff;
  357. display: flex;
  358. justify-content: center;
  359. align-items: center;
  360. .kk-search-device{
  361. width:100%;
  362. height: 100%;
  363. .kk-filter-wrap{
  364. width:100%;
  365. height: 200upx;
  366. display: flex;
  367. flex-direction: column;
  368. justify-content: flex-start;
  369. align-items: flex-start;
  370. .filter-title{
  371. line-height: 70upx;
  372. font-size: 30upx;
  373. color: #999999;
  374. }
  375. &>slider{
  376. width:90%;
  377. height: 90upx;
  378. }
  379. &>input{
  380. padding:0 20upx;
  381. box-sizing: border-box;
  382. border-radius: 10upx;
  383. height: 90upx;
  384. width:100%;
  385. border: 1upx solid #ebebeb;
  386. }
  387. }
  388. .kk-btn-wrap{
  389. width:100%;
  390. height: 140upx;
  391. display: flex;
  392. justify-content: space-between;
  393. align-items: center;
  394. &>view{
  395. flex:1 1 auto;
  396. height: 80upx;
  397. line-height: 80upx;
  398. border-radius: 100upx;
  399. text-align: center;
  400. color:#ffffff;
  401. &.confirm-btn{
  402. background: #007AFF;
  403. margin-right:30upx;
  404. }
  405. &:nth-last-child(1){
  406. background: #DD524D;
  407. }
  408. }
  409. }
  410. .kk-devices-wrap{
  411. height: calc(100% - 460upx);
  412. overflow-y:auto;
  413. padding:10upx 20upx;
  414. box-sizing: border-box;
  415. border: 1upx solid #ebebeb;
  416. box-sizing: border-box;
  417. border-radius: 20upx;
  418. .empty-wrap{
  419. width:100%;
  420. height: 100%;
  421. display: flex;
  422. flex-direction: column;
  423. justify-content: center;
  424. align-items: center;
  425. .empty-icon{
  426. width:268upx;
  427. height: 240upx;
  428. background: url('./empty-icon.png') no-repeat;
  429. background-size:100% 100%;
  430. margin-bottom: 26upx;
  431. }
  432. .empty-text{
  433. width: 100%;
  434. line-height: 50upx;
  435. font-size: 30upx;
  436. text-align: center;
  437. color: #999999;
  438. }
  439. }
  440. .kk-devices-item{
  441. width:100%;
  442. border-bottom: 1upx solid #ebebeb;
  443. padding:10upx 0;
  444. box-sizing: border-box;
  445. &:nth-last-child(1){
  446. border-bottom: none;
  447. }
  448. > view{
  449. &:first-child{
  450. flex-grow: 1;
  451. width: 80%;
  452. }
  453. }
  454. button{
  455. width: auto;
  456. }
  457. }
  458. }
  459. }
  460. }
  461. }
  462. }
  463. }
  464. .kk-placeholder-class{
  465. font-size: 30upx;
  466. color:#999999;
  467. }
  468. </style>