reshui.vue 30 KB

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