reshui.vue 30 KB

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