index.vue 13 KB

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