index.vue 12 KB

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