index.vue 12 KB

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