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. // 获得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. console.log(res)
  938. if (res.data.mess == "支付成功") {
  939. uni.showToast({
  940. icon: 'success',
  941. title: '结算成功!',
  942. success: (res1) => {
  943. this.use_amount = res.data.use_amount.toFixed(2)
  944. this.amount = (this.amount - this.use_amount).toFixed(2)
  945. // 刷新选定的月份消费记录
  946. this.request_consumption_records()
  947. }
  948. })
  949. } else {
  950. uni.showToast({
  951. icon: 'success',
  952. title: '结算失败!',
  953. duration: 3000
  954. })
  955. }
  956. } else {
  957. uni.showToast({
  958. title: '字节码为空!',
  959. icon: 'success'
  960. });
  961. // 初始化
  962. this.setInit()
  963. }
  964. }
  965. }
  966. }
  967. /**
  968. * 返回字符处理
  969. */
  970. function str2StrArray(start_code) {
  971. if (start_code.length != 34) {
  972. console.log('启动码长度错误')
  973. throw new Error("启动码长度错误");
  974. return
  975. }
  976. var resultArray = new Array()
  977. for (var i = 0; i < start_code.length; i += 2) {
  978. resultArray.push(start_code.substr(i, 2));
  979. }
  980. return resultArray;
  981. }
  982. /**
  983. * ArrayBuffer转16进度字符串
  984. */
  985. function ab2hex(buffer) {
  986. const hexArr = Array.prototype.map.call(
  987. new Uint8Array(buffer),
  988. function(bit) {
  989. // console.log('====================' + bit + '==========================')
  990. return ('00' + bit.toString(16)).slice(-2)
  991. }
  992. )
  993. return hexArr.join('');
  994. }
  995. /**
  996. * 16进制转10进制
  997. */
  998. function sixteen_to_ten(str) {
  999. return parseInt(str, 16);
  1000. }
  1001. </script>
  1002. <style scoped lang="scss">
  1003. .content {
  1004. width: 100%;
  1005. height: 100%;
  1006. .container {
  1007. width: 100%;
  1008. padding: 20rpx 40rpx;
  1009. box-sizing: border-box;
  1010. .header {
  1011. display: flex;
  1012. justify-content: center;
  1013. position: relative;
  1014. height: 200rpx;
  1015. .scan {
  1016. display: flex;
  1017. flex-direction: column;
  1018. justify-content: center;
  1019. align-items: center;
  1020. height: 200rpx;
  1021. width: 30%;
  1022. .iconfont {
  1023. margin-bottom: 10rpx;
  1024. font-size: 100rpx;
  1025. color: $my-color-primary;
  1026. }
  1027. }
  1028. .scan_hover {
  1029. border-radius: 15rpx;
  1030. background-color: $my-color-btn-background;
  1031. }
  1032. .qinshihao {
  1033. display: flex;
  1034. align-items: center;
  1035. position: fixed;
  1036. top: 20rpx;
  1037. right: 10rpx;
  1038. font-size: 28rpx;
  1039. .icon-zhuye {
  1040. margin-right: 10rpx;
  1041. font-family: Microsoft YaHei-3970(82674968);
  1042. color: $my-color-primary;
  1043. }
  1044. .compustxt {
  1045. font-size: 18rpx;
  1046. }
  1047. }
  1048. }
  1049. .start_text {
  1050. .start_top,
  1051. .start_mid {
  1052. display: flex;
  1053. justify-content: space-between;
  1054. height: 100rpx;
  1055. align-items: center;
  1056. border-bottom: 2rpx #ccc solid;
  1057. .item_left {
  1058. display: flex;
  1059. align-items: center;
  1060. }
  1061. .start_mid_txt {
  1062. color: #333;
  1063. font-size: 28rpx;
  1064. }
  1065. .iconfont {
  1066. font-size: 50rpx;
  1067. color: $my-color-primary;
  1068. margin-right: 20rpx;
  1069. }
  1070. .start_payamount {
  1071. display: flex;
  1072. justify-content: flex-end;
  1073. align-items: center;
  1074. font-size: 38rpx;
  1075. padding: 10rpx;
  1076. font-family: Microsoft YaHei-3970(82674968);
  1077. color: #B3B3B3;
  1078. border-radius: 10rpx;
  1079. .icon-dayuhao {
  1080. margin: 0 0 0 20rpx;
  1081. font-size: 38rpx;
  1082. color: #B3B3B3;
  1083. }
  1084. }
  1085. }
  1086. .start_bot {
  1087. display: flex;
  1088. justify-content: flex-end;
  1089. height: 100rpx;
  1090. align-items: center;
  1091. .start_bot_txt {
  1092. margin-left: 20rpx;
  1093. color: #333;
  1094. font-size: 28rpx;
  1095. }
  1096. }
  1097. }
  1098. .start_up_process {
  1099. padding: 20rpx 0;
  1100. color: #007AFF;
  1101. }
  1102. .title {
  1103. height: 100rpx;
  1104. margin: 0 -40rpx;
  1105. padding: 0 40rpx;
  1106. line-height: 100rpx;
  1107. color: #808080;
  1108. font-size: 32rpx;
  1109. background: #f2f2f2;
  1110. }
  1111. .record-item {
  1112. width: 100%;
  1113. .uni-list-cell-db {
  1114. height: 100rpx;
  1115. line-height: 100rpx;
  1116. font-weight: bold;
  1117. .uni-input {
  1118. display: flex;
  1119. align-items: center;
  1120. text:nth-child(1) {
  1121. margin-right: 10rpx;
  1122. }
  1123. }
  1124. }
  1125. .item-list {
  1126. height: 80rpx;
  1127. display: flex;
  1128. align-items: center;
  1129. justify-content: space-between;
  1130. border-top: 2rpx #ccc solid;
  1131. .icon-shijian {
  1132. margin-right: 8rpx;
  1133. }
  1134. .icon-shuidi {
  1135. margin-right: 3rpx;
  1136. }
  1137. .item-list-txt {
  1138. font-size: 28rpx;
  1139. &:nth-of-type(2) {
  1140. color: #515151;
  1141. }
  1142. &:nth-of-type(3) {
  1143. color: #f76200;
  1144. font-size: 32rpx;
  1145. }
  1146. }
  1147. }
  1148. }
  1149. }
  1150. }
  1151. </style>