index.vue 13 KB

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