index.vue 15 KB

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