index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <template>
  2. <view class="bg">
  3. <view class="bgimg">
  4. <u-image width="622rpx" height="953rpx" src="/static/ldbg.png"></u-image>
  5. </view>
  6. <!-- 称重 -->
  7. <view class="content">
  8. <view @click="toTdRecord" class="tdrecord">投递记录</view>
  9. <view class="cz-box" @click="toWork">
  10. <view :class="loading?'turn':''">
  11. <u-image width="100rpx" height="100rpx" src="/static/chengzhong.png"></u-image>
  12. <view class="tits">投放称重</view>
  13. </view>
  14. </view>
  15. <view class="bindDev" @click="toInit">
  16. {{statusStr}}
  17. </view>
  18. <view class="devList">
  19. <view v-for="item in deviceList" :key="item.deviceId">
  20. <view class="names">
  21. <view>设备名称:{{item.name}}</view>
  22. <view class="texts">MAC:{{item.deviceId}}</view>
  23. </view>
  24. <view class="btns" @click="connectBlue(item)" v-if="!connectStatus">点击连接</view>
  25. <view class="btns" @click="closeBlue" v-else>断开连接</view>
  26. </view>
  27. </view>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. import {getLookUpDatas} from '@/api/data.js'
  33. export default {
  34. data() {
  35. return {
  36. deviceList:[], // 设备列表
  37. deviceId: null,
  38. connectedDeviceId: '',
  39. services: [],
  40. readsId:'',
  41. writeId:'',
  42. notifyServicweId: '',
  43. writeDatas: '',
  44. balanceData: '',
  45. hexstr: '',
  46. statusStr: '绑定电子秤',
  47. connectStatus: false ,// 设备连接状态
  48. loading: false ,// 正在连接
  49. }
  50. },
  51. onLoad() {
  52. console.log('onload')
  53. let _this = this
  54. uni.$on('blueReConnect',function(){
  55. uni.showLoading({
  56. mask: true,
  57. title: '正在重连中...'
  58. })
  59. _this.connectBlue()
  60. })
  61. },
  62. onShow() {
  63. // 获取订单状态数据字典
  64. this.getCodeList('RUBBISH_TYPE','vuex_rubbishType')
  65. },
  66. onUnload() {
  67. uni.$off('blueReConnect')
  68. },
  69. methods: {
  70. // 获取数据字典
  71. getCodeList(code,key) {
  72. getLookUpDatas({
  73. type: code
  74. }).then(res => {
  75. if (res.status == 200) {
  76. this.$u.vuex(key, res.data)
  77. }
  78. })
  79. },
  80. // 查看投递记录
  81. toTdRecord(){
  82. uni.navigateTo({
  83. url: '/pages/tdRecord/tdRecord'
  84. })
  85. },
  86. // 去认证用户并开始称重
  87. toWork(){
  88. let _this = this
  89. // 连接成功
  90. if(this.connectStatus){
  91. uni.navigateTo({
  92. url: '/pages/userAuth/userAuth?deviceId=' + _this.deviceId
  93. })
  94. }else{
  95. let hasDev = this.deviceList.length
  96. if(!this.loading){
  97. uni.showModal({
  98. title:'提示',
  99. content:hasDev ? '请先连接设备':'请先绑定设备',
  100. confirmText: hasDev ? '确定':'去绑定',
  101. complete(e) {
  102. console.log(e)
  103. if(e.confirm){
  104. if(hasDev == 1){
  105. _this.connectBlue(_this.deviceList[0])
  106. }
  107. if(hasDev == 0){
  108. _this.toInit()
  109. }
  110. }
  111. }
  112. })
  113. }
  114. }
  115. },
  116. // 初始化蓝牙
  117. toInit(){
  118. let _this = this
  119. console.log(this.connectStatus,this.deviceList.length,this.loading)
  120. if(this.connectStatus || this.deviceList.length > 0 || this.loading){
  121. return
  122. }
  123. uni.openBluetoothAdapter({
  124. success(res) {
  125. console.log(res, 'openBluetoothAdapter success')
  126. _this.onBluetoothAdapterStateChange()
  127. _this.loading = true
  128. setTimeout(()=>{
  129. _this.toFindDev()
  130. },600)
  131. },
  132. fail(err){
  133. console.log(err,'openBluetoothAdapter error')
  134. _this.loading = false
  135. uni.showModal({
  136. title:'提示',
  137. content:'请开启手机蓝牙',
  138. showCancel: false,
  139. complete(e) {
  140. console.log(e)
  141. _this.statusStr = '请开启手机蓝牙'
  142. }
  143. })
  144. }
  145. })
  146. },
  147. toFindDev(){
  148. let _this = this
  149. _this.statusStr = '开始搜寻附近的电子秤,请确认设备已开启'
  150. uni.getBluetoothAdapterState({
  151. success(res) {
  152. console.log(res)
  153. if(res.available){
  154. // 开始搜寻附近的蓝牙外围设备
  155. _this.startBluetoothDevicesDiscovery()
  156. }else{
  157. _this.statusStr = '请开启手机蓝牙'
  158. }
  159. },
  160. fail(err) {
  161. console.log(err)
  162. }
  163. })
  164. },
  165. // 监听蓝牙状态
  166. onBluetoothAdapterStateChange(){
  167. let _this = this
  168. // 监听蓝牙适配器状态变化事件
  169. uni.onBluetoothAdapterStateChange(function (res) {
  170. console.log(res,'适配器变化')
  171. if(!res.available){
  172. // 关闭连接
  173. _this.statusStr = '蓝牙已关闭,请开启手机蓝牙'
  174. _this.connectedDeviceId = null
  175. _this.closeConnect()
  176. }else{
  177. if(!res.discovering && !_this.loading){
  178. _this.statusStr = '蓝牙已启用,请绑定设备'
  179. }
  180. }
  181. })
  182. },
  183. startBluetoothDevicesDiscovery(){
  184. let _this = this
  185. uni.startBluetoothDevicesDiscovery({
  186. services: [],
  187. success(res) {
  188. console.log(res)
  189. // 监听寻找到新设备的事件
  190. uni.onBluetoothDeviceFound(function (res) {
  191. let devices = res.devices.filter(item => item.name == 'FAYA')
  192. console.log(devices,'devices list')
  193. if(devices.length){
  194. _this.statusStr = '已发现设备'
  195. _this.deviceList = _this.deviceList.concat(res.devices)
  196. _this.loading = false
  197. }
  198. })
  199. }
  200. })
  201. },
  202. // 连接蓝牙设备
  203. connectBlue(item){
  204. if(this.connectStatus){
  205. return
  206. }
  207. if(item){
  208. this.deviceId = item.deviceId
  209. }
  210. this.statusStr = '正在连接设备...'
  211. this.loading = true
  212. this.createBLEConnection()
  213. },
  214. // 断开蓝牙设备
  215. closeBlue(){
  216. this.statusStr = '连接已断开,请重新绑定设备'
  217. this.closeConnect()
  218. },
  219. // 开始连接指定设备
  220. createBLEConnection(){
  221. let _this = this
  222. uni.createBLEConnection({
  223. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  224. deviceId:this.deviceId,
  225. success(res) {
  226. console.log(res)
  227. _this.connectedDeviceId = _this.deviceId
  228. // 获取连接设备的service服务
  229. setTimeout(()=>{
  230. _this.getBLEDeviceServices()
  231. },1000)
  232. },
  233. fail(err) {
  234. console.log(err,'createBLEConnection error')
  235. _this.statusStr = '设备连接失败,请检查后重试'
  236. _this.connectStatus = false
  237. _this.loading = false
  238. uni.$emit('blueConnectCallback',0)
  239. uni.hideLoading()
  240. }
  241. })
  242. },
  243. // 获取连接设备的service服务
  244. getBLEDeviceServices(){
  245. let _this = this
  246. uni.getBLEDeviceServices({
  247. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  248. deviceId: _this.deviceId,
  249. success(res) {
  250. console.log('device services:', res.services)
  251. // _this.statusStr = '获取连接设备的service服务成功'
  252. _this.services = res.services
  253. /* 获取连接设备的所有特征值 */
  254. setTimeout(()=>{
  255. _this.getBLEDeviceCharacteristics()
  256. },500)
  257. // 停止搜索
  258. uni.stopBluetoothDevicesDiscovery({
  259. success: function(res) {
  260. console.log(res, '停止搜索')
  261. }
  262. })
  263. }
  264. })
  265. },
  266. // 获取连接设备的所有特征值
  267. getBLEDeviceCharacteristics(){
  268. let _this = this
  269. this.notifyServicweId = this.services[1].uuid
  270. uni.getBLEDeviceCharacteristics({
  271. deviceId: this.connectedDeviceId,
  272. serviceId: this.notifyServicweId,
  273. success: function(res) {
  274. console.log(res)
  275. // _this.statusStr = '获取连接设备的所有特征值成功'
  276. for (let i = 0; i < res.characteristics.length; i++) {
  277. let properties = res.characteristics[i].properties
  278. // 读id
  279. if(properties.notify&&!properties.read&&!properties.write){
  280. _this.readsId = res.characteristics[i].uuid
  281. }
  282. // 写id
  283. if(!properties.notify&&!properties.read&&properties.write){
  284. _this.writeId = res.characteristics[i].uuid
  285. }
  286. console.log(_this.readsId,_this.writeId, '蓝牙特征值 ==========')
  287. // 启用低功耗蓝牙设备特征值变化时的 notify 功能
  288. setTimeout(()=>{
  289. _this.notifyBLECharacteristicValueChange()
  290. },500)
  291. }
  292. },
  293. fail: function(res) {
  294. console.log(res)
  295. }
  296. })
  297. },
  298. // 启用低功耗蓝牙设备特征值变化时的 notify 功能
  299. notifyBLECharacteristicValueChange() {
  300. let that = this;
  301. uni.notifyBLECharacteristicValueChange({
  302. state: true,
  303. deviceId: that.connectedDeviceId,
  304. serviceId: that.notifyServicweId,
  305. characteristicId: that.readsId,
  306. success(res) {
  307. console.log(res,'notifyBLECharacteristicValueChange')
  308. that.statusStr = '连接成功,请投放称重'
  309. if(!that.connectStatus){
  310. uni.$emit('blueConnectCallback',1)
  311. that.connectStatus = true
  312. uni.hideLoading()
  313. }
  314. that.loading = false
  315. /*用来监听手机蓝牙设备的数据变化*/
  316. uni.onBLECharacteristicValueChange(function(res) {
  317. console.log(res,'read data')
  318. that.balanceData = that.buf2string(res.value)
  319. that.$u.vuex('vuex_balanceData',that.balanceData)
  320. })
  321. // 该方法回调中可以用于处理连接意外断开等异常情况
  322. uni.onBLEConnectionStateChange(function (res) {
  323. console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
  324. if(!res.connected){
  325. uni.$emit('blueConnectClose',res)
  326. that.statusStr = '设备连接已断开,请重新连接'
  327. that.connectStatus = false
  328. that.loading = false
  329. }
  330. })
  331. },
  332. fail(res) {
  333. console.log(res, '启用低功耗蓝牙设备监听失败')
  334. that.statusStr = '设备连接失败,请点击重试'
  335. that.closeConnect()
  336. }
  337. })
  338. },
  339. /*转换成需要的格式*/
  340. buf2string(buffer) {
  341. let arr = Array.prototype.map.call(new Uint8Array(buffer), x => x)
  342. return arr.map((char, i) => {
  343. return String.fromCharCode(char);
  344. }).join('');
  345. },
  346. receiveData(buf) {
  347. return this.hexCharCodeToStr(this.ab2hex(buf))
  348. },
  349. /*转成二进制*/
  350. ab2hex (buffer) {
  351. let hexArr = Array.prototype.map.call(
  352. new Uint8Array(buffer), function (bit) {
  353. return ('00' + bit.toString(16)).slice(-2)
  354. }
  355. )
  356. return hexArr.join('')
  357. },
  358. /*转成可展会的文字*/
  359. hexCharCodeToStr(hexCharCodeStr) {
  360. let trimedStr = hexCharCodeStr.trim();
  361. let rawStr = trimedStr.substr(0, 2).toLowerCase() === '0x' ? trimedStr.substr(2) : trimedStr;
  362. let len = rawStr.length;
  363. let curCharCode;
  364. let resultStr = [];
  365. for (let i = 0; i < len; i = i + 2) {
  366. curCharCode = parseInt(rawStr.substr(i, 2), 16);
  367. resultStr.push(String.fromCharCode(curCharCode));
  368. }
  369. return resultStr.join('');
  370. },
  371. // 发送数据
  372. sendData(str) {
  373. let that = this;
  374. let dataBuffer = new ArrayBuffer(3)
  375. let dataView = new DataView(dataBuffer)
  376. dataView.setUint8(0, 0x02)
  377. dataView.setUint8(1, 0x41)
  378. dataView.setUint8(2, 0x03)
  379. let dataHex = that.ab2hex(dataBuffer);
  380. this.writeDatas = that.hexCharCodeToStr(dataHex);
  381. uni.writeBLECharacteristicValue({
  382. deviceId: that.connectedDeviceId,
  383. serviceId: that.notifyServicweId,
  384. characteristicId: that.writeId,
  385. value: dataBuffer,
  386. success: function (res) {
  387. console.log('发送数据成功:' + that.writeDatas)
  388. },
  389. fail: function (res) {
  390. console.log('发送失败:' + res)
  391. },
  392. complete: function (res) {
  393. }
  394. })
  395. },
  396. // 断开设备连接
  397. closeConnect() {
  398. let that = this;
  399. if (that.connectedDeviceId) {
  400. uni.closeBLEConnection({
  401. deviceId: that.connectedDeviceId,
  402. success: function(res) {
  403. console.log(res)
  404. that.deviceId = null
  405. that.closeBluetoothAdapter()
  406. },
  407. fail(res) {
  408. console.log(res)
  409. }
  410. })
  411. } else {
  412. that.closeBluetoothAdapter()
  413. }
  414. },
  415. // 关闭蓝牙模块
  416. closeBluetoothAdapter() {
  417. let that = this;
  418. uni.closeBluetoothAdapter({
  419. success: function(res) {
  420. console.log(res,'closeBluetoothAdapter')
  421. that.connectStatus = false
  422. that.loading = false
  423. that.balanceData = ''
  424. that.deviceList = []
  425. },
  426. fail: function(err) {
  427. }
  428. })
  429. },
  430. },
  431. onUnload() {
  432. this.closeConnect()
  433. }
  434. }
  435. </script>
  436. <style lang="less">
  437. // @import url("bg.less");
  438. .bg{
  439. height: 100vh;
  440. width: 100%;
  441. position: relative;
  442. background-color: #015a50;
  443. }
  444. .bgimg{
  445. position: absolute;
  446. z-index: 100;
  447. width: 100%;
  448. height: 100vh;
  449. top: 0;
  450. left: 0;
  451. display: flex;
  452. justify-content: center;
  453. align-items: center;
  454. }
  455. .content{
  456. display: flex;
  457. align-items: center;
  458. justify-content: center;
  459. flex-direction: column;
  460. height: 100vh;
  461. width: 100%;
  462. position: absolute;
  463. z-index: 1000;
  464. }
  465. .tdrecord{
  466. position: absolute;
  467. right: 0;
  468. top: 5%;
  469. background: rgba(255,255,255,0.8);
  470. border-radius: 150rpx 0 0 150rpx;
  471. box-shadow: 2rpx 2rpx 6rpx rgba(0,0,0,0.4);
  472. padding: 15rpx 20rpx 15rpx 30rpx;
  473. font-size: 26rpx;
  474. }
  475. .cz-box{
  476. text-align: center;
  477. background: #01c9b2;
  478. border-radius: 300rpx;
  479. box-shadow: 3rpx 3rpx 10rpx rgba(0,0,0,0.4);
  480. }
  481. .cz-box > view{
  482. width: 250rpx;
  483. height: 250rpx;
  484. padding: 50rpx 50rpx 30rpx;
  485. display: flex;
  486. align-items: center;
  487. justify-content: center;
  488. flex-direction: column;
  489. }
  490. .tits{
  491. padding: 20rpx 0;
  492. color: #FFFFFF;
  493. }
  494. .devList{
  495. width: 80%;
  496. padding:10rpx 30rpx;
  497. max-height: 50vh;
  498. overflow: auto;
  499. }
  500. .bindDev{
  501. margin: 20rpx 0;
  502. padding: 15rpx 40rpx;
  503. background: rgba(19,22,30,0.8);
  504. box-shadow: 2rpx 2rpx 8rpx rgba(0,0,0,0.2);
  505. border-radius: 100rpx;
  506. color: #FFFFFF;
  507. }
  508. .devList > view{
  509. display: flex;
  510. align-items: center;
  511. color: #666666;
  512. border-radius: 20rpx;
  513. background: rgba(255,255,255,0.8);
  514. box-shadow: 2rpx 2rpx 5rpx rgba(0,0,0,0.2);
  515. margin-bottom: 20rpx;
  516. }
  517. .devList > view .names{
  518. flex-grow: 1;
  519. padding: 15rpx 40rpx;
  520. }
  521. .devList > view .texts{
  522. font-size: 24rpx;
  523. }
  524. .devList > view .btns{
  525. text-align: center;
  526. padding: 15rpx 20rpx;
  527. color: #ff5500;
  528. }
  529. .turn{
  530. animation:turn 1s linear infinite;
  531. }
  532. @keyframes turn{
  533. 0%{-webkit-transform:rotate(0deg);}
  534. 25%{-webkit-transform:rotate(90deg);}
  535. 50%{-webkit-transform:rotate(180deg);}
  536. 75%{-webkit-transform:rotate(270deg);}
  537. 100%{-webkit-transform:rotate(360deg);}
  538. }
  539. </style>