index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. <template>
  2. <view class="kk-printer">
  3. <view class="kk-printer-btn">
  4. <u-button v-if="printBtnStyle=='blue-large'" @tap="handlePrintTap" :loading="isPrinting" 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" :loading="isPrinting">{{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. },
  131. computed:{
  132. mapFilterRSSI(){
  133. return (0 - this.filterRSSI)
  134. },
  135. filterDeviceList(){
  136. let devices = this.devicesList;
  137. let name = this.filterName;
  138. let rssi = this.filterRSSI;
  139. //按RSSI过滤
  140. let filterDevices1 = devices.filter((item)=>{
  141. return item.RSSI > rssi
  142. })
  143. // 按名字过滤
  144. let filterDevices2
  145. if(name!=''){
  146. filterDevices2 = filterDevices1.filter((item)=>{
  147. return (item.name.indexOf(name) >= 0 || item.deviceId.indexOf(name) >= 0)
  148. })
  149. }else{
  150. filterDevices2 = filterDevices1
  151. }
  152. // 根据广播数据提取MAC地址
  153. for (let i = 0; i < filterDevices2.length;i++) {
  154. if (filterDevices2[i].hasOwnProperty('advertisData')){
  155. if (filterDevices2[i].advertisData.byteLength == 8) {
  156. filterDevices2[i].advMac = util.buf2hex(filterDevices2[i].advertisData.slice(2, 7));
  157. }
  158. }
  159. }
  160. return filterDevices2
  161. }
  162. },
  163. mounted() {
  164. },
  165. beforeDestroy(){
  166. this.stopSearchBtnTap();
  167. },
  168. methods:{
  169. doNothing(){
  170. return;
  171. },
  172. //点击打印按钮
  173. handlePrintTap(){
  174. //打开蓝牙适配器
  175. blesdk.openBlue().then((res)=>{
  176. //获取已连接设备
  177. blesdk.getConnectedBluetoothDevices().then((res)=>{
  178. //若没有已连接设备,弹框搜索设备
  179. console.log(res,this.deviceId,this.serviceId,this.writeId,this.onPrintSuccess)
  180. if(res.devices.length == 0){
  181. this.isShowSearch = true
  182. this.devicesList = []
  183. if(this.deviceId){
  184. this.closeConnect(this.deviceId)
  185. }
  186. }else{
  187. const lastDevice = this.$store.state.vuex_lastBuleDevice
  188. if(lastDevice && res.devices[0].deviceId == lastDevice.deviceId){
  189. this.deviceId = lastDevice.deviceId
  190. this.serviceId = lastDevice.serviceId
  191. this.writeId = lastDevice.writeId
  192. this.readId = lastDevice.readId
  193. }
  194. this.isPrinting = true;
  195. this.$nextTick(()=>{
  196. this.$emit("startPrint",{
  197. deviceId: this.deviceId,
  198. serviceId: this.serviceId,
  199. writeId: this.writeId
  200. },tsc,blesdk)
  201. })
  202. }
  203. }).catch((err)=>{
  204. blesdk.catchToast(err);
  205. })
  206. }).catch((err)=>{
  207. console.log(err)
  208. blesdk.catchToast(err);
  209. })
  210. },
  211. onGetDevice(res){
  212. this.devicesList = res;
  213. },
  214. handleSearchClose(){
  215. if(!this.isConnecting){
  216. this.isShowSearch = false
  217. }
  218. },
  219. handleSRRIChange(e){
  220. this.filterRSSI = e.detail.value
  221. },
  222. //开始搜索设备
  223. searchBtnTap(){
  224. blesdk.startBluetoothDevicesDiscovery();
  225. this.isSearching = true;
  226. blesdk.onfindBlueDevices(this.onGetDevice)
  227. },
  228. //停止搜索设备
  229. stopSearchBtnTap(){
  230. blesdk.stopBlueDevicesDiscovery();
  231. this.isSearching = false;
  232. },
  233. //点击连接设备
  234. handleConnectDevice(device){
  235. const _this = this
  236. let deviceId = device.deviceId;
  237. let name = device.name;
  238. this.deviceId = deviceId;
  239. console.log('deviceId',this.deviceId)
  240. this.isConnecting = true
  241. this.stopSearchBtnTap()
  242. uni.onBLEConnectionStateChange(function(res){
  243. console.log('连接',res)
  244. if(res.connected){
  245. plus.nativeUI.toast('设备'+ res.deviceId + '已连接',{
  246. verticalAlign:'center'
  247. })
  248. }else{
  249. _this.closeConnect(deviceId)
  250. plus.nativeUI.toast('设备'+ res.deviceId + '已断开连接',{
  251. verticalAlign:'center'
  252. })
  253. }
  254. _this.isConnecting = false
  255. })
  256. blesdk.createBLEConnection(deviceId, this.onConnectSuccess, this.onConnectFail);
  257. },
  258. onConnectSuccess(res){
  259. this.stopSearchBtnTap()
  260. this.isConnecting = false
  261. blesdk.getBLEDeviceServices(this.deviceId, this.onGetServicesSuccess, this.onGetServicesFail);
  262. },
  263. onConnectFail(err){
  264. console.log('链接失败',err)
  265. this.isConnecting = false
  266. blesdk.catchToast(err.res)
  267. },
  268. onGetServicesSuccess(res){
  269. console.log('获取服务',res)
  270. this.services = res.serviceId;
  271. blesdk.getDeviceCharacteristics(this.deviceId, this.services, this.onGetCharacterSuccess, this.onGetCharacterFail);
  272. },
  273. onGetServicesFail(err){
  274. console.log('获取服务失败')
  275. },
  276. onGetCharacterSuccess(res){
  277. console.log('获取特征值成功',res)
  278. this.serviceId = res.serviceId;
  279. this.writeId = res.writeId;
  280. this.readId = res.readId;
  281. this.isShowSearch = false;
  282. this.$store.state.vuex_lastBuleDevice = {deviceId:this.deviceId,serviceId:this.serviceId,writeId:this.writeId,readId:this.readId}
  283. },
  284. onGetCharacterFail(err){
  285. console.log('特征值获取失败')
  286. },
  287. onPrintSuccess(){
  288. this.isPrinting = false;
  289. console.log('打印成功')
  290. // this.$emit('onPrintSuccess')
  291. },
  292. onPrintFail(){
  293. console.log('打印失败')
  294. this.isPrinting = false;
  295. },
  296. closeConnect(){
  297. blesdk.closeBLEConnection(this.deviceId)
  298. },
  299. }
  300. }
  301. </script>
  302. <style lang="scss" scoped>
  303. .kk-printer{
  304. &-btn{
  305. width:100%;
  306. height:100%;
  307. }
  308. .kk-shadow{
  309. display: none;
  310. &.show{
  311. display: block;
  312. width:100vw;
  313. height:100vh;
  314. background: rgba(0,0,0,0.4);
  315. position: fixed;
  316. top: 0;
  317. left: 0;
  318. display: flex;
  319. justify-content: center;
  320. align-items: center;
  321. z-index: 10000;
  322. .kk-modal{
  323. width:680upx;
  324. height: 60%;
  325. padding:24upx;
  326. box-sizing: border-box;
  327. overflow-y: auto;
  328. border-radius: 20upx;
  329. background: #ffffff;
  330. display: flex;
  331. justify-content: center;
  332. align-items: center;
  333. .kk-search-device{
  334. width:100%;
  335. height: 100%;
  336. .kk-filter-wrap{
  337. width:100%;
  338. height: 200upx;
  339. display: flex;
  340. flex-direction: column;
  341. justify-content: flex-start;
  342. align-items: flex-start;
  343. .filter-title{
  344. line-height: 70upx;
  345. font-size: 30upx;
  346. color: #999999;
  347. }
  348. &>slider{
  349. width:90%;
  350. height: 90upx;
  351. }
  352. &>input{
  353. padding:0 20upx;
  354. box-sizing: border-box;
  355. border-radius: 10upx;
  356. height: 90upx;
  357. width:100%;
  358. border: 1upx solid #ebebeb;
  359. }
  360. }
  361. .kk-btn-wrap{
  362. width:100%;
  363. height: 140upx;
  364. display: flex;
  365. justify-content: space-between;
  366. align-items: center;
  367. &>view{
  368. flex:1 1 auto;
  369. height: 80upx;
  370. line-height: 80upx;
  371. border-radius: 100upx;
  372. text-align: center;
  373. color:#ffffff;
  374. &.confirm-btn{
  375. background: #007AFF;
  376. margin-right:30upx;
  377. }
  378. &:nth-last-child(1){
  379. background: #DD524D;
  380. }
  381. }
  382. }
  383. .kk-devices-wrap{
  384. height: calc(100% - 460upx);
  385. overflow-y:auto;
  386. padding:10upx 20upx;
  387. box-sizing: border-box;
  388. border: 1upx solid #ebebeb;
  389. box-sizing: border-box;
  390. border-radius: 20upx;
  391. .empty-wrap{
  392. width:100%;
  393. height: 100%;
  394. display: flex;
  395. flex-direction: column;
  396. justify-content: center;
  397. align-items: center;
  398. .empty-icon{
  399. width:268upx;
  400. height: 240upx;
  401. background: url('./empty-icon.png') no-repeat;
  402. background-size:100% 100%;
  403. margin-bottom: 26upx;
  404. }
  405. .empty-text{
  406. width: 100%;
  407. line-height: 50upx;
  408. font-size: 30upx;
  409. text-align: center;
  410. color: #999999;
  411. }
  412. }
  413. .kk-devices-item{
  414. width:100%;
  415. border-bottom: 1upx solid #ebebeb;
  416. padding:10upx 0;
  417. box-sizing: border-box;
  418. &:nth-last-child(1){
  419. border-bottom: none;
  420. }
  421. > view{
  422. &:first-child{
  423. flex-grow: 1;
  424. width: 80%;
  425. }
  426. }
  427. button{
  428. width: auto;
  429. }
  430. }
  431. }
  432. }
  433. }
  434. }
  435. }
  436. }
  437. .kk-placeholder-class{
  438. font-size: 30upx;
  439. color:#999999;
  440. }
  441. </style>