reshui.vue 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. <template>
  2. <view class="content">
  3. <view class="container">
  4. <view class="header">
  5. <view class="scan" @tap="scan()" hover-class="scan_hover">
  6. <text class="iconfont icon-saoma"></text>
  7. <text>扫码连接</text>
  8. </view>
  9. <view class="qinshihao">
  10. <text class="iconfont icon-zhuye"></text>
  11. <text class="dormtxt">{{dorm_number || '宿舍未知'}}</text>
  12. <text class="compustxt">{{compus}}</text>
  13. </view>
  14. </view>
  15. <view class="start_text">
  16. <view class="start_top">
  17. <view class="item_left">
  18. <text class="iconfont icon-shuibiao"></text>
  19. <text>开启设备</text>
  20. </view>
  21. <view>
  22. <switch :color="colorPrimary" :checked="isChecked" @change="switchChange" />
  23. </view>
  24. </view>
  25. <view class="start_mid">
  26. <view class="item_left">
  27. <text class="iconfont icon-qian"></text>
  28. <text class="start_mid_txt">¥{{amount}}</text>
  29. </view>
  30. <view class="start_payamount" @tap="chongzhi_yemian()" hover-class="scan_hover">
  31. <text>点击充值</text>
  32. <text class="iconfont icon-dayuhao"></text>
  33. </view>
  34. </view>
  35. <view class="start_bot">
  36. <text class="start_bot_txt" v-if="false">时长:00:00:00</text>
  37. <text class="start_bot_txt">当前结算消费:¥{{use_amount}}</text>
  38. </view>
  39. </view>
  40. <view class="start_up_process">
  41. <uni-steps :options="list1" :active-color="colorPrimary" :active="active" />
  42. </view>
  43. <view class="title">
  44. 消费记录
  45. </view>
  46. <view class="record-item">
  47. <view class="uni-list-cell-db">
  48. <picker mode="date" :value="date" fields="month" :start="startDate" :end="endDate"
  49. @change="bindDateChange">
  50. <view class="uni-input">
  51. <text>{{date}}</text>
  52. <text class="iconfont icon-arrow-down"></text>
  53. </view>
  54. </picker>
  55. </view>
  56. <view class="item-list" v-for="(item, i) in xiaofei_items" :key="i">
  57. <text class="item-list-txt"><text class="iconfont icon-shijian"></text>{{item.begin_time}}</text>
  58. <text class="item-list-txt"><text class="iconfont icon-shuidi"></text>{{item.use_size}}升</text>
  59. <text class="item-list-txt">{{item.use_amount}}</text>
  60. </view>
  61. </view>
  62. </view>
  63. <instructions v-if="flag" @parent_cancel="cancel"></instructions>
  64. </view>
  65. </template>
  66. <script>
  67. import instructions from '../../components/instructions.vue'
  68. export default {
  69. /**
  70. * 声明组件
  71. */
  72. components: {
  73. instructions
  74. },
  75. data() {
  76. return {
  77. colorPrimary: getApp().globalData.primaryColor,
  78. date: this.$getDate({
  79. format: true
  80. }),
  81. startDate: this.$getDate('start_date'),
  82. endDate: this.$getDate('end_date'),
  83. flag: true,
  84. isUserClose: false, // 是否是用户自己关闭
  85. isChecked: false,
  86. isConnected: false, // 是否连接上
  87. isScan: false,
  88. active: -1,
  89. list1: [{
  90. title: '连接设备'
  91. }, {
  92. title: '连设备服务'
  93. }, {
  94. title: '数据通讯'
  95. }, {
  96. title: '启动'
  97. }],
  98. devices: [], // 保存设备
  99. device_code: '', // 设备编号
  100. deviceId: '',
  101. serviceId: '',
  102. characteristicIdForWrite: '',
  103. characteristicIdForNotity: '',
  104. deviceIsNotify: false,
  105. deviceIsWrite: false,
  106. byte35: '', // 70 个字符
  107. byte35_tmp: '', // 70 个字符 临时变量
  108. start_code: '',
  109. // start_code: '3459A8C660F1C284E41003E80000120000',
  110. xiaofei_items: [], // 消费记录
  111. use_amount: 0.0.toFixed(2), // 结算金额
  112. amount: 0.0.toFixed(2), // 余额
  113. dorm_number: '', // 宿舍号
  114. compus: '', // 校区
  115. code: '',
  116. ceshi: 'code'
  117. }
  118. },
  119. onLoad() {
  120. // 获得code
  121. this.getCode()
  122. },
  123. onShow() {
  124. if (this.$store.state.payInfo.from == 'reshui_pay') {
  125. uni.showToast({
  126. title: this.$store.state.payInfo.resultMsg,
  127. icon: 'success',
  128. success: () => {
  129. this.$store.state.payInfo.resultMsg = ''
  130. this.$store.state.payInfo.from = ''
  131. }
  132. })
  133. // this.amount = this.$store.state.reshui_amount + this.amount
  134. // 重新调用一次,刷新金额
  135. this.getCode()
  136. }
  137. },
  138. methods: {
  139. /**
  140. * 获得code
  141. */
  142. getCode() {
  143. uni.login({
  144. success: (res) => {
  145. // console.log('reshui', res);
  146. if (res.code) {
  147. this.code = res.code
  148. // 请求服务器,获得openid
  149. this.getOpenId()
  150. } else {
  151. uni.showToast({
  152. title: res.errMsg,
  153. icon: 'none'
  154. });
  155. }
  156. }
  157. })
  158. },
  159. /**
  160. * 请求服务器,获得openid
  161. */
  162. async getOpenId() {
  163. if (this.code == '') {
  164. uni.showToast({
  165. title: 'code未获取到!',
  166. icon: 'success'
  167. });
  168. return
  169. }
  170. const res = await this.$myRequest({
  171. host: this.ceshi,
  172. url: '/HotWaters/wpget_stu.action',
  173. method: 'POST',
  174. header: {
  175. 'content-type': 'application/x-www-form-urlencoded'
  176. },
  177. data: {
  178. code: this.code
  179. }
  180. })
  181. // console.log(res);
  182. if (res.data.mess == '返回成功') {
  183. // 学号
  184. this.$store.state.stu_number = res.data.info[0].stu_number
  185. // 剩余金额
  186. if (res.data.info[0].balance > 0)
  187. this.$store.state.reshui_amount = res.data.info[0].balance
  188. // 剩余金额
  189. this.amount = this.$store.state.reshui_amount
  190. // 组合校区楼栋宿舍号
  191. this.dorm_number = res.data.info[0].build + res.data.info[0].dom
  192. // 校区
  193. this.compus = res.data.info[0].campus
  194. // 请求选定的月份消费记录
  195. this.request_consumption_records()
  196. } else if (res.data.mess == '未查询到用户信息') {
  197. uni.redirectTo({
  198. url: '../index/index'
  199. });
  200. }
  201. },
  202. /**
  203. * 选择消费年月
  204. */
  205. bindDateChange(e) {
  206. this.date = e.detail.value
  207. // 请求选定的月份消费记录
  208. this.request_consumption_records()
  209. },
  210. /**
  211. * 导航页面取消
  212. */
  213. cancel(param) {
  214. this.flag = param
  215. },
  216. /**
  217. * 请求选定的月份消费记录
  218. */
  219. async request_consumption_records() {
  220. if (this.$store.state.stu_number != '') {
  221. this.xiaofei_items = []
  222. const res = await this.$myRequest({
  223. host: this.ceshi,
  224. url: '/HotWaters/wpqueryConsume.action',
  225. method: 'POST',
  226. header: {
  227. 'content-type': 'application/x-www-form-urlencoded'
  228. },
  229. data: {
  230. stu_number: this.$store.state.stu_number,
  231. begin_time: this.date
  232. }
  233. });
  234. // console.log(res, 'request_consumption_records');
  235. if (res.data.mess == '返回成功') {
  236. // 消费记录
  237. let items = res.data.data
  238. for (var i = 0; i < items.length; i++) {
  239. this.xiaofei_items.push(items[i])
  240. }
  241. } else {
  242. uni.showToast({
  243. icon: 'success',
  244. title: res.data.mess
  245. })
  246. }
  247. } else {
  248. uni.showToast({
  249. icon: 'success',
  250. title: '学号为空'
  251. })
  252. }
  253. },
  254. /**
  255. * 控制进度条显示
  256. * @param {Object} active_value
  257. */
  258. process_control(active_value) {
  259. if (active_value <= this.list1.length - 1) {
  260. this.active = active_value
  261. }
  262. },
  263. /**
  264. * 开始或停止用水
  265. */
  266. switchChange(e) {
  267. this.isChecked = e.target.value
  268. if (this.isChecked) {
  269. if (this.deviceId && this.serviceId && this.devices.length != 0) {
  270. uni.showModal({
  271. title: '提示',
  272. content: '确定要启动设备?',
  273. success: (res) => {
  274. if (res.confirm) {
  275. this.isChecked = true
  276. // 开始连接匹配的蓝牙设备
  277. setTimeout(async () => {
  278. // 请求获取设备启动码
  279. this.request_device_start_code()
  280. }, 300)
  281. } else if (res.cancel) {
  282. setTimeout(() => {
  283. this.isChecked = false
  284. }, 300)
  285. }
  286. }
  287. })
  288. } else {
  289. uni.showToast({
  290. icon: 'none',
  291. title: '先【扫码连接】,后【开启设备】',
  292. duration: 1500
  293. })
  294. setTimeout(() => {
  295. this.isChecked = false
  296. }, 300)
  297. }
  298. } else {
  299. uni.showModal({
  300. title: '提示',
  301. content: '确定要关闭设备?',
  302. success: (res) => {
  303. if (res.confirm) {
  304. this.isChecked = false
  305. if (this.deviceId && this.serviceId) {
  306. this.send('stop')
  307. } else {
  308. uni.showToast({
  309. icon: 'none',
  310. title: '与设备连接中断!请在设备上手动停止即可!',
  311. duration: 5000
  312. })
  313. setTimeout(() => {
  314. this.isChecked = true
  315. }, 300)
  316. }
  317. } else if (res.cancel) {
  318. setTimeout(() => {
  319. this.isChecked = true
  320. }, 300)
  321. }
  322. }
  323. })
  324. }
  325. },
  326. /**
  327. * 跳转到充值页面并传参数
  328. */
  329. chongzhi_yemian() {
  330. //在起始页面跳转到recharge.vue页面并传递参数
  331. uni.navigateTo({
  332. url: '../recharge/recharge'
  333. });
  334. },
  335. /**
  336. * 111111111111111111111111111111
  337. * 扫码连接设备
  338. */
  339. scan() {
  340. // 启动最低金额限制
  341. if (this.amount < 10) {
  342. uni.showModal({
  343. title: '提示',
  344. content: '当前余额不足¥10元',
  345. cancelText: '我知道了',
  346. confirmText: '我要充值',
  347. success: (res) => {
  348. if (res.confirm) {
  349. // 跳转充值页面并传参数
  350. this.chongzhi_yemian()
  351. } else if (res.cancel) {
  352. return
  353. }
  354. }
  355. })
  356. } else {
  357. // 已经启动扫描连接设备中
  358. if (this.isScan) {
  359. uni.showToast({
  360. icon: 'none',
  361. title: '正在扫描,请耐心等待',
  362. })
  363. return
  364. }
  365. // 已经启动设备,不启动扫描
  366. if (this.isChecked) {
  367. uni.showToast({
  368. icon: 'none',
  369. title: '设备已启动,无需重新扫码',
  370. })
  371. return
  372. }
  373. if (this.deviceId) {
  374. uni.showModal({
  375. title: '提示',
  376. content: '设备已经连接,需要重新连接吗?',
  377. success: res => {
  378. if (res.confirm) {
  379. this.closeBLEConnection()
  380. this.scan_qr()
  381. } else if (res.cancel) {
  382. return
  383. }
  384. }
  385. })
  386. } else {
  387. this.scan_qr()
  388. }
  389. }
  390. },
  391. /**
  392. * 启动摄像头,扫描二维码
  393. */
  394. scan_qr() {
  395. uni.scanCode({
  396. onlyFromCamera: true,
  397. success: (res) => {
  398. this.isScan = true
  399. // console.log(res)
  400. // charSet: "utf-8"
  401. // codeVersion: 3
  402. // errMsg: "scanCode:ok"
  403. // rawData: "aHR0cDovL3Rvb2xzLjlrYnMuY29tLz93eG1pbmljb2RlPUtCNTgwMDAyNjUy"
  404. // result: "http://tools.9kbs.com/?wxminicode=KB580002652"
  405. // scanType: "QR_CODE"
  406. this.device_code = res.result.split('?')[1].split('=')[1]
  407. if (this.device_code === '') {
  408. uni.showToast({
  409. icon: 'success',
  410. title: '获取设备码失败',
  411. duration: 3000
  412. })
  413. this.isScan = false
  414. } else {
  415. // 初始化蓝牙
  416. this.openBluetoothAdapter()
  417. }
  418. }
  419. })
  420. },
  421. /**
  422. * 22222222222222222222222222222
  423. * 初始化蓝牙
  424. */
  425. openBluetoothAdapter() {
  426. uni.openBluetoothAdapter({
  427. success: (res) => { // 已打开
  428. uni.getBluetoothAdapterState({ // 蓝牙的匹配状态
  429. success: (res1) => {
  430. // console.log(res1, '“本机设备的蓝牙已打开”');
  431. // 本机设备的蓝牙已打开
  432. // 开始搜索蓝牙设备
  433. if (this.deviceId == '' && this.serviceId == '')
  434. this.startBluetoothDeviceDiscovery()
  435. },
  436. fail: (error) => {
  437. // console.log(error)
  438. uni.showToast({
  439. title: "请开启手机蓝牙",
  440. icon: "none",
  441. duration: 3000
  442. })
  443. this.isScan = false
  444. }
  445. });
  446. },
  447. fail: (err) => { // 未打开
  448. // console.log(err)
  449. this.isScan = false
  450. if (err.errCode == 10001) {
  451. uni.showModal({
  452. content: '请开启手机蓝牙!',
  453. showCancel: false,
  454. })
  455. } else {
  456. uni.showToast({
  457. title: err.errMsg,
  458. duration: 2000
  459. })
  460. }
  461. }
  462. });
  463. },
  464. /**
  465. * 333333333333333333333333333
  466. * 开始搜索蓝牙设备
  467. */
  468. startBluetoothDeviceDiscovery() {
  469. uni.startBluetoothDevicesDiscovery({ // 开始搜寻附近的蓝牙外围设备
  470. // services: ['FEE7'], // 要搜索但蓝牙设备主 service 的 uuid 列表
  471. allowDuplicatesKey: true,
  472. success: (res) => {
  473. this.process_control(0);
  474. this.onBluetoothDeviceFound() //监听搜索到新设备 同时进行
  475. },
  476. fail: err => {
  477. // console.log(err, '错误信息');
  478. uni.showToast({
  479. title: err.errMsg,
  480. duration: 2000
  481. })
  482. this.isScan = false
  483. }
  484. })
  485. },
  486. stopBluetoothDevicesDiscovery: function() { //停止搜索
  487. uni.stopBluetoothDevicesDiscovery({
  488. success: (res) => {
  489. // console.log(res, '停止蓝牙搜索')
  490. // uni.showToast({
  491. // title: '停止蓝牙搜索',
  492. // duration: 2000
  493. // })
  494. this.process_control(0);
  495. // 开始连接匹配的蓝牙设备
  496. this.createBLEConnection()
  497. },
  498. fail: e => {
  499. // console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
  500. this.isScan = false
  501. uni.showToast({
  502. title: err.errCode,
  503. duration: 2000
  504. })
  505. }
  506. })
  507. },
  508. /**
  509. * 444444444444444444444444444444444
  510. * 发现外围设备
  511. */
  512. onBluetoothDeviceFound() {
  513. uni.onBluetoothDeviceFound((res) => {
  514. res.devices.forEach(device => { // 筛选找到的蓝牙中对名称匹配
  515. // 过滤掉没有名字的设备
  516. if (!device.name && !device.localName) {
  517. return
  518. }
  519. if (device.name == this.device_code) {
  520. //data里面建立一个deviceId、device_code,存储起来
  521. // this.device_code = device.name;
  522. this.deviceId = device.deviceId
  523. // 把搜索到的设备存储起来,如有需要可以在页面上展示
  524. if (this.devices.indexOf(res.devices[0].deviceId) == -1) {
  525. this.devices.push(res.devices[0].deviceId)
  526. }
  527. this.process_control(0);
  528. this.stopBluetoothDevicesDiscovery()
  529. }
  530. })
  531. });
  532. },
  533. /**
  534. * 555555555555555555555555555555555
  535. * 连接蓝牙设备
  536. */
  537. createBLEConnection() {
  538. //连接蓝牙
  539. uni.createBLEConnection({
  540. deviceId: this.deviceId,
  541. timeout: 3000,
  542. success: (res) => {
  543. // console.log(res)
  544. this.connect_BLE()
  545. },
  546. fail: (err) => {
  547. // console.log(err)
  548. this.isScan = false
  549. if (err.errMsg.indexOf('already connect') != -1) {
  550. this.connect_BLE()
  551. } else {
  552. uni.showToast({
  553. title: '建立连接失败',
  554. icon: 'none',
  555. duration: 3000,
  556. success: (res1) => {
  557. this.setInit()
  558. }
  559. })
  560. }
  561. }
  562. })
  563. },
  564. connect_BLE() {
  565. if (this.isConnected === false) {
  566. this.isConnected = true
  567. this.process_control(1);
  568. // 获取蓝牙设备所有服务(service)
  569. this.getBLEDeviceServices()
  570. }
  571. },
  572. /**
  573. * 6666666666666666666666666666666666
  574. * 获取蓝牙的所有服务
  575. */
  576. getBLEDeviceServices() {
  577. uni.getBLEDeviceServices({
  578. deviceId: this.deviceId,
  579. success: (res) => {
  580. // console.log(res)
  581. // res1.services.forEach((item) => {
  582. // this.getBLEDeviceCharacteristics()
  583. // })
  584. this.serviceId = res.services[1].uuid
  585. this.process_control(1);
  586. this.getBLEDeviceCharacteristics(this.deviceId, res.services[1].uuid)
  587. },
  588. fail: (err) => {
  589. // console.log(err)
  590. this.isScan = false
  591. uni.showToast({
  592. title: '获取服务失败',
  593. icon: 'none',
  594. duration: 3000
  595. })
  596. return
  597. }
  598. });
  599. },
  600. /**
  601. * 777777777777777777777777777777777
  602. * 获取蓝牙特征
  603. */
  604. getBLEDeviceCharacteristics() {
  605. // 获取蓝牙设备某个服务中所有特征值(characteristic)
  606. uni.getBLEDeviceCharacteristics({
  607. deviceId: this.deviceId,
  608. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  609. serviceId: this.serviceId,
  610. success: (res) => {
  611. this.process_control(2);
  612. // console.log(res)
  613. this.characteristicIdForWrite = res.characteristics[0].uuid
  614. this.characteristicIdForNotity = res.characteristics[1].uuid
  615. this.deviceIsWrite = res.characteristics[0].properties.write
  616. this.deviceIsNotify = res.characteristics[1].properties.notify
  617. // console.log(this.characteristicIdForWrite, this.characteristicIdForNotity)
  618. // 确保对应服务id下的特征值id具备监听数据变化的特性
  619. if (res.characteristics[1].properties.notify) {
  620. this.notifyBLECharacteristicValueChange()
  621. }
  622. },
  623. fail: (err) => {
  624. this.isScan = false
  625. // console.error('获取蓝牙设备特征值失败(getBLEDeviceCharacteristics)', err)
  626. }
  627. });
  628. },
  629. /**
  630. * 888888888888888888888888888888888888
  631. * 启用 notify 功能
  632. */
  633. notifyBLECharacteristicValueChange() {
  634. // console.log(characteristicId, 'characteristicId')
  635. uni.notifyBLECharacteristicValueChange({
  636. state: true, // 启用 notify 功能
  637. deviceId: this.deviceId,
  638. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  639. serviceId: this.serviceId,
  640. // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  641. characteristicId: this.characteristicIdForNotity,
  642. success: (res) => {
  643. this.process_control(2)
  644. this.isScan = false
  645. // console.log('通知启用(notifyBLECharacteristicValueChange)',res);
  646. this.onBLECharacteristicValueChange()
  647. setTimeout(() => {
  648. // 发送 AAABAC,采集数据(会返回 35字节 的数据)
  649. this.send('')
  650. }, 300)
  651. // // 请求设备信息
  652. // this.request_device_info()
  653. // // 显示设备启动授权金额
  654. // this.display_device_startup_amount()
  655. },
  656. fail: (err) => {
  657. // console.log(err)
  658. this.isScan = false
  659. uni.showModal({
  660. content: err.errMsg,
  661. showCancel: false
  662. })
  663. }
  664. })
  665. },
  666. /**
  667. * 999999999999999999999999999999999
  668. * 监听低功耗蓝牙设备的特征值变化
  669. */
  670. onBLECharacteristicValueChange() {
  671. uni.onBLECharacteristicValueChange((characteristic) => {
  672. let condition = ab2hex(characteristic.value)
  673. // console.log(condition)
  674. if (condition.length > 2) {
  675. this.byte35_tmp += condition
  676. if (this.byte35_tmp.length > 40) {
  677. this.byte35 = this.byte35_tmp
  678. this.byte35_tmp = ''
  679. if (this.byte35.startsWith('ad')) {
  680. // console.log(this.byte35, '结束用水成功')
  681. // 关闭蓝牙连接
  682. this.closeBLEConnection()
  683. // 请求结算
  684. this.request_pay()
  685. } else {
  686. // 发送 AAABAC,采集数据(会返回 35字节 的数据)
  687. // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
  688. // uni.showToast({
  689. // icon: 'success',
  690. // title: '采集数据成功!',
  691. // duration: 2000
  692. // })
  693. // console.log(this.byte35, '采集上一条消费数据')
  694. }
  695. }
  696. }
  697. switch (condition) {
  698. case 'bb':
  699. // uni.showToast({
  700. // title: '设备启动成功',
  701. // duration: 3000,
  702. // icon: 'none'
  703. // })
  704. this.process_control(3)
  705. this.isChecked = true
  706. break
  707. case 'bc':
  708. uni.showToast({
  709. title: '启动失败,设备检验启动代码不通过',
  710. duration: 3000,
  711. icon: 'none'
  712. })
  713. break
  714. case 'aa':
  715. uni.showToast({
  716. title: '设备正在使用',
  717. duration: 3000,
  718. icon: 'none'
  719. })
  720. break
  721. case 'ab':
  722. uni.showToast({
  723. title: '结束失败,设备对结束代码检验失败',
  724. duration: 3000,
  725. icon: 'none'
  726. })
  727. break
  728. default:
  729. break
  730. }
  731. })
  732. },
  733. /**
  734. * 关闭连接
  735. */
  736. closeBLEConnection() {
  737. uni.closeBLEConnection({
  738. deviceId: this.deviceId,
  739. success: () => {
  740. // 关闭蓝牙模块
  741. this.closeBluetoothAdapter()
  742. },
  743. fail: () => {
  744. uni.showToast({
  745. icon: 'none',
  746. title: '蓝牙连接断开失败',
  747. duration: 3000,
  748. success: (res1) => {
  749. this.setInit()
  750. }
  751. })
  752. }
  753. })
  754. },
  755. closeBluetoothAdapter() {
  756. // 关闭蓝牙模块
  757. uni.closeBluetoothAdapter({
  758. success: () => {
  759. this.setInit()
  760. if (this.isUserClose == false) { // 设备端结束用水
  761. if (this.isChecked) {
  762. // console.log('设备端已结束用水,蓝牙断开成功')
  763. uni.showToast({
  764. icon: 'none',
  765. title: '设备端已结束用水,蓝牙断开成功',
  766. duration: 3000
  767. })
  768. } else {
  769. // console.log('用户重启扫描,蓝牙断开成功')
  770. uni.showToast({
  771. icon: 'none',
  772. title: '用户重启扫描,蓝牙断开成功',
  773. duration: 3000
  774. })
  775. }
  776. } else { // 手机移动端结束用水
  777. // console.log('APP端已结束用水,蓝牙断开成功')
  778. uni.showToast({
  779. icon: 'none',
  780. title: 'APP端已结束用水,蓝牙断开成功',
  781. duration: 3000
  782. })
  783. }
  784. }
  785. })
  786. },
  787. setInit() {
  788. setTimeout(() => {
  789. this.isUserClose = false
  790. this.isChecked = false
  791. this.isConnected = false
  792. this.isScan = false
  793. this.active = -1
  794. this.devices = [] // 保存设备
  795. this.device_code = '' // 设备编号
  796. this.deviceId = ''
  797. this.serviceId = ''
  798. this.characteristicIdForWrite = ''
  799. this.characteristicIdForNotity = ''
  800. this.deviceIsNotify = false
  801. this.deviceIsWrite = false
  802. this.byte35 = '' // 70 个字符
  803. this.byte35_tmp = '' // 70 个字符 临时变量
  804. // this.start_code = ''
  805. })
  806. },
  807. /**
  808. * 10===========10==================10
  809. * 发送数据到设备
  810. */
  811. send(val) {
  812. let buffer
  813. let dataView
  814. let newArr
  815. if (val == 'start') { // 启动设备
  816. this.isUserClose = false
  817. this.process_control(3)
  818. // this.createBLEConnection()
  819. // 发送 17字节 启动代码(会返回 35字节 的数据)
  820. // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
  821. newArr = str2StrArray(this.start_code)
  822. } else if (val == 'stop') { // 结束当前使用,设备成功返回:AD+35字节 消费结算代码
  823. this.isUserClose = true
  824. // 发送 AAABAD+17字节 启动代码(会返回 ad+35字节 的数据)
  825. // adb58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0104bc
  826. newArr = ['AA', 'AB', 'AD'].concat(str2StrArray(this.start_code))
  827. } else {
  828. // 发送 AAABAC 采集数据(会返回 35字节 的数据)
  829. // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
  830. newArr = ['AA', 'AB', 'AC']
  831. }
  832. buffer = new ArrayBuffer(newArr.length)
  833. dataView = new DataView(buffer)
  834. newArr.forEach((item, i) => {
  835. dataView.setUint8(i, sixteen_to_ten(item))
  836. })
  837. // 写特征值数据到设备
  838. if (this.deviceIsWrite) {
  839. // 写数据到设备
  840. this.writeBLECharacteristicValue(val, buffer)
  841. } else {
  842. uni.showToast({
  843. title: '设备不可写',
  844. duration: 3000
  845. })
  846. }
  847. },
  848. /**
  849. * 11====================11=====================11
  850. * 写数据到设备
  851. */
  852. writeBLECharacteristicValue(val, buffer) {
  853. uni.writeBLECharacteristicValue({
  854. deviceId: this.deviceId,
  855. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  856. serviceId: this.serviceId,
  857. // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  858. characteristicId: this.characteristicIdForWrite,
  859. // 这里的value是ArrayBuffer类型
  860. value: buffer,
  861. success: (res) => {
  862. // console.log(res)
  863. if (val == 'start') {
  864. setTimeout(() => {
  865. this.isChecked = true
  866. this.isConnected = true
  867. }, 300)
  868. this.process_control(3)
  869. } else if (val == 'stop') {
  870. setTimeout(() => {
  871. this.isChecked = false
  872. }, 300)
  873. }
  874. },
  875. fail: (err) => {
  876. // console.log(err)
  877. if (err.errCode == 10006) {
  878. setTimeout(() => {
  879. this.setInit()
  880. }, 300)
  881. if (this.isConnected) {
  882. uni.showToast({
  883. title: '设备连接已断开!请在水表上手动停止!',
  884. icon: 'none'
  885. })
  886. } else {
  887. uni.showToast({
  888. title: '设备连接失败!请重试扫描连接!',
  889. icon: 'none'
  890. })
  891. }
  892. }
  893. }
  894. })
  895. },
  896. /**
  897. * 请求获取设备启动码
  898. */
  899. async request_device_start_code() {
  900. if (this.byte35 != '' && this.$store.state.stu_number != '') {
  901. const res = await this.$myRequest({
  902. host: this.ceshi,
  903. url: '/HotWaters/wpstart.action',
  904. data: {
  905. collect_code: this.byte35,
  906. stu_number: this.$store.state.stu_number
  907. }
  908. })
  909. // console.log(res)
  910. if (res.data.start_code != 'undefined' && res.data.start_code != '') {
  911. this.start_code = res.data.start_code
  912. this.send('start')
  913. } else {
  914. uni.showToast({
  915. title: res.data.mess,
  916. duration: 3000
  917. })
  918. }
  919. } else {
  920. uni.showToast({
  921. title: '字节码或学号为空!'
  922. });
  923. // 初始化
  924. this.setInit()
  925. }
  926. },
  927. /**
  928. * 请求结算
  929. */
  930. async request_pay() {
  931. if (this.byte35 != '') {
  932. const res = await this.$myRequest({
  933. host: this.ceshi,
  934. url: '/HotWaters/wpconsume.action',
  935. data: {
  936. collect_code: this.byte35.substring(2)
  937. }
  938. })
  939. // console.log(res)
  940. if (res.data.mess == "支付成功") {
  941. uni.showToast({
  942. icon: 'success',
  943. title: '结算成功!',
  944. success: (res1) => {
  945. this.use_amount = res.data.use_amount.toFixed(2)
  946. this.amount = (this.amount - this.use_amount).toFixed(2)
  947. // 刷新选定的月份消费记录
  948. this.request_consumption_records()
  949. }
  950. })
  951. } else {
  952. uni.showToast({
  953. icon: 'success',
  954. title: '结算失败!',
  955. duration: 3000
  956. })
  957. }
  958. } else {
  959. uni.showToast({
  960. title: '字节码为空!',
  961. icon: 'success'
  962. });
  963. // 初始化
  964. this.setInit()
  965. }
  966. }
  967. }
  968. }
  969. /**
  970. * 返回字符处理
  971. */
  972. function str2StrArray(start_code) {
  973. if (start_code.length != 34) {
  974. // console.log('启动码长度错误')
  975. throw new Error("启动码长度错误");
  976. return
  977. }
  978. var resultArray = new Array()
  979. for (var i = 0; i < start_code.length; i += 2) {
  980. resultArray.push(start_code.substr(i, 2));
  981. }
  982. return resultArray;
  983. }
  984. /**
  985. * ArrayBuffer转16进度字符串
  986. */
  987. function ab2hex(buffer) {
  988. const hexArr = Array.prototype.map.call(
  989. new Uint8Array(buffer),
  990. function(bit) {
  991. // console.log('====================' + bit + '==========================')
  992. return ('00' + bit.toString(16)).slice(-2)
  993. }
  994. )
  995. return hexArr.join('');
  996. }
  997. /**
  998. * 16进制转10进制
  999. */
  1000. function sixteen_to_ten(str) {
  1001. return parseInt(str, 16);
  1002. }
  1003. </script>
  1004. <style scoped lang="scss">
  1005. .content {
  1006. width: 100%;
  1007. height: 100%;
  1008. .container {
  1009. width: 100%;
  1010. padding: 20rpx 40rpx;
  1011. box-sizing: border-box;
  1012. .header {
  1013. display: flex;
  1014. justify-content: center;
  1015. position: relative;
  1016. height: 200rpx;
  1017. .scan {
  1018. display: flex;
  1019. flex-direction: column;
  1020. justify-content: center;
  1021. align-items: center;
  1022. height: 200rpx;
  1023. width: 30%;
  1024. .iconfont {
  1025. margin-bottom: 10rpx;
  1026. font-size: 100rpx;
  1027. color: $my-color-primary;
  1028. }
  1029. }
  1030. .scan_hover {
  1031. border-radius: 15rpx;
  1032. background-color: $my-color-btn-background;
  1033. }
  1034. .qinshihao {
  1035. display: flex;
  1036. align-items: center;
  1037. position: fixed;
  1038. top: 20rpx;
  1039. right: 10rpx;
  1040. font-size: 28rpx;
  1041. .icon-zhuye {
  1042. margin-right: 10rpx;
  1043. font-family: Microsoft YaHei-3970(82674968);
  1044. color: $my-color-primary;
  1045. }
  1046. .compustxt {
  1047. font-size: 18rpx;
  1048. }
  1049. }
  1050. }
  1051. .start_text {
  1052. .start_top,
  1053. .start_mid {
  1054. display: flex;
  1055. justify-content: space-between;
  1056. height: 100rpx;
  1057. align-items: center;
  1058. border-bottom: 2rpx #ccc solid;
  1059. .item_left {
  1060. display: flex;
  1061. align-items: center;
  1062. }
  1063. .start_mid_txt {
  1064. color: #333;
  1065. font-size: 30rpx;
  1066. }
  1067. .iconfont {
  1068. font-size: 50rpx;
  1069. color: $my-color-primary;
  1070. margin-right: 20rpx;
  1071. }
  1072. .start_payamount {
  1073. display: flex;
  1074. justify-content: flex-end;
  1075. align-items: center;
  1076. font-size: 38rpx;
  1077. padding: 10rpx;
  1078. font-family: Microsoft YaHei-3970(82674968);
  1079. color: #B2B2B2;
  1080. border-radius: 10rpx;
  1081. .icon-dayuhao {
  1082. margin: 0 0 0 20rpx;
  1083. font-size: 38rpx;
  1084. color: #B2B2B2;
  1085. }
  1086. }
  1087. }
  1088. .start_bot {
  1089. display: flex;
  1090. justify-content: flex-end;
  1091. height: 100rpx;
  1092. align-items: center;
  1093. .start_bot_txt {
  1094. margin-left: 20rpx;
  1095. color: #333;
  1096. font-size: 28rpx;
  1097. }
  1098. }
  1099. }
  1100. .start_up_process {
  1101. padding: 20rpx 0;
  1102. color: #007AFF;
  1103. }
  1104. .title {
  1105. height: 100rpx;
  1106. margin: 0 -40rpx;
  1107. padding: 0 40rpx;
  1108. line-height: 100rpx;
  1109. color: #808080;
  1110. font-size: 32rpx;
  1111. background: #f2f2f2;
  1112. }
  1113. .record-item {
  1114. width: 100%;
  1115. .uni-list-cell-db {
  1116. height: 100rpx;
  1117. line-height: 100rpx;
  1118. font-weight: bold;
  1119. .uni-input {
  1120. display: flex;
  1121. align-items: center;
  1122. text:nth-child(1) {
  1123. margin-right: 10rpx;
  1124. }
  1125. }
  1126. }
  1127. .item-list {
  1128. height: 80rpx;
  1129. display: flex;
  1130. align-items: center;
  1131. justify-content: space-between;
  1132. border-top: 2rpx #ccc solid;
  1133. .icon-shijian {
  1134. margin-right: 8rpx;
  1135. }
  1136. .icon-shuidi {
  1137. margin-right: 3rpx;
  1138. }
  1139. .item-list-txt {
  1140. font-size: 28rpx;
  1141. &:nth-of-type(2) {
  1142. color: #515151;
  1143. }
  1144. &:nth-of-type(3) {
  1145. color: #f76200;
  1146. font-size: 32rpx;
  1147. }
  1148. }
  1149. }
  1150. }
  1151. }
  1152. }
  1153. </style>