reshui.vue 30 KB

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