reshui.vue 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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. // }, 1000)
  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. // 处理 35字节
  682. // APP 接收后,根据用户操作情况主要做如下几个方面的处理。
  683. // 1、向后台提交【结算数据】
  684. // 2、提交【结算数据】并生成【启动代码】
  685. // 3、提示:用户进行下面的流程
  686. // 请求结算
  687. this.request_pay()
  688. } else {
  689. // 发送 AAABAC,采集数据(会返回 35字节 的数据)
  690. // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
  691. // uni.showToast({
  692. // icon: 'success',
  693. // title: '采集数据成功!',
  694. // duration: 2000
  695. // })
  696. // console.log(this.byte35, '采集上一条消费数据')
  697. }
  698. }
  699. }
  700. switch (condition) {
  701. case 'bb':
  702. // uni.showToast({
  703. // title: '设备启动成功',
  704. // duration: 3000,
  705. // icon: 'none'
  706. // })
  707. this.process_control(3)
  708. this.isChecked = true
  709. break
  710. case 'bc':
  711. uni.showToast({
  712. title: '启动失败,设备检验启动代码不通过',
  713. duration: 3000,
  714. icon: 'none'
  715. })
  716. break
  717. case 'aa':
  718. uni.showToast({
  719. title: '设备正在使用',
  720. duration: 3000,
  721. icon: 'none'
  722. })
  723. break
  724. case 'ab':
  725. uni.showToast({
  726. title: '结束失败,设备对结束代码检验失败',
  727. duration: 3000,
  728. icon: 'none'
  729. })
  730. break
  731. default:
  732. break
  733. }
  734. })
  735. },
  736. /**
  737. * 关闭连接
  738. */
  739. closeBLEConnection() {
  740. uni.closeBLEConnection({
  741. deviceId: this.deviceId,
  742. success: () => {
  743. // 关闭蓝牙模块
  744. this.closeBluetoothAdapter()
  745. },
  746. fail: () => {
  747. uni.showToast({
  748. icon: 'none',
  749. title: '蓝牙连接断开失败',
  750. duration: 3000,
  751. success: (res1) => {
  752. this.setInit()
  753. }
  754. })
  755. }
  756. })
  757. },
  758. closeBluetoothAdapter() {
  759. // 关闭蓝牙模块
  760. uni.closeBluetoothAdapter({
  761. success: () => {
  762. this.setInit()
  763. if (this.isUserClose == false) { // 设备端结束用水
  764. if (this.isChecked) {
  765. // console.log('设备端已结束用水,蓝牙断开成功')
  766. uni.showToast({
  767. icon: 'none',
  768. title: '设备端已结束用水,蓝牙断开成功',
  769. duration: 3000
  770. })
  771. } else {
  772. // console.log('用户重启扫描,蓝牙断开成功')
  773. uni.showToast({
  774. icon: 'none',
  775. title: '用户重启扫描,蓝牙断开成功',
  776. duration: 3000
  777. })
  778. }
  779. } else { // 手机移动端结束用水
  780. // console.log('APP端已结束用水,蓝牙断开成功')
  781. uni.showToast({
  782. icon: 'none',
  783. title: 'APP端已结束用水,蓝牙断开成功',
  784. duration: 3000
  785. })
  786. }
  787. }
  788. })
  789. },
  790. setInit() {
  791. setTimeout(() => {
  792. this.isUserClose = false
  793. this.isChecked = false
  794. this.isConnected = false
  795. this.isScan = false
  796. this.active = -1
  797. this.devices = [] // 保存设备
  798. this.device_code = '' // 设备编号
  799. this.deviceId = ''
  800. this.serviceId = ''
  801. this.characteristicIdForWrite = ''
  802. this.characteristicIdForNotity = ''
  803. this.deviceIsNotify = false
  804. this.deviceIsWrite = false
  805. this.byte35 = '' // 70 个字符
  806. this.byte35_tmp = '' // 70 个字符 临时变量
  807. // this.start_code = ''
  808. })
  809. },
  810. /**
  811. * 10===========10==================10
  812. * 发送数据到设备
  813. */
  814. send(val) {
  815. let buffer
  816. let dataView
  817. let newArr
  818. if (val == 'start') { // 启动设备
  819. this.isUserClose = false
  820. this.process_control(3)
  821. // this.createBLEConnection()
  822. // 发送 17字节 启动代码(会返回 35字节 的数据)
  823. // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
  824. newArr = str2StrArray(this.start_code)
  825. } else if (val == 'stop') { // 结束当前使用,设备成功返回:AD+35字节 消费结算代码
  826. this.isUserClose = true
  827. // 发送 AAABAD+17字节 启动代码(会返回 ad+35字节 的数据)
  828. // adb58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0104bc
  829. newArr = ['AA', 'AB', 'AD'].concat(str2StrArray(this.start_code))
  830. } else {
  831. // 发送 AAABAC 采集数据(会返回 35字节 的数据)
  832. // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
  833. newArr = ['AA', 'AB', 'AC']
  834. }
  835. buffer = new ArrayBuffer(newArr.length)
  836. dataView = new DataView(buffer)
  837. newArr.forEach((item, i) => {
  838. dataView.setUint8(i, sixteen_to_ten(item))
  839. })
  840. // 写特征值数据到设备
  841. if (this.deviceIsWrite) {
  842. // 写数据到设备
  843. this.writeBLECharacteristicValue(val, buffer)
  844. } else {
  845. uni.showToast({
  846. title: '设备不可写',
  847. duration: 3000
  848. })
  849. }
  850. },
  851. /**
  852. * 11====================11=====================11
  853. * 写数据到设备
  854. */
  855. writeBLECharacteristicValue(val, buffer) {
  856. uni.writeBLECharacteristicValue({
  857. deviceId: this.deviceId,
  858. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  859. serviceId: this.serviceId,
  860. // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  861. characteristicId: this.characteristicIdForWrite,
  862. // 这里的value是ArrayBuffer类型
  863. value: buffer,
  864. success: (res) => {
  865. // console.log(res)
  866. if (val == 'start') {
  867. setTimeout(() => {
  868. this.isChecked = true
  869. this.isConnected = true
  870. }, 300)
  871. this.process_control(3)
  872. } else if (val == 'stop') {
  873. setTimeout(() => {
  874. this.isChecked = false
  875. }, 300)
  876. }
  877. },
  878. fail: (err) => {
  879. // console.log(err)
  880. if (err.errCode == 10006) {
  881. setTimeout(() => {
  882. this.setInit()
  883. }, 300)
  884. if (this.isConnected) {
  885. uni.showToast({
  886. title: '设备连接已断开!请在水表上手动停止!',
  887. icon: 'none'
  888. })
  889. } else {
  890. uni.showToast({
  891. title: '设备连接失败!请重试扫描连接!',
  892. icon: 'none'
  893. })
  894. }
  895. }
  896. }
  897. })
  898. },
  899. /**
  900. * 请求获取设备启动码
  901. */
  902. async request_device_start_code() {
  903. if (this.byte35 == '' || this.$store.state.stu_number == '') {
  904. uni.showToast({
  905. title: '字节码或学号为空!'
  906. });
  907. return
  908. }
  909. const res = await this.$myRequest({
  910. host: this.ceshi,
  911. url: '/HotWaters/wpstart.action',
  912. data: {
  913. collect_code: this.byte35,
  914. stu_number: this.$store.state.stu_number
  915. }
  916. })
  917. // console.log(res)
  918. if (res.data.start_code != 'undefined' && res.data.start_code != '') {
  919. this.start_code = res.data.start_code
  920. this.send('start')
  921. } else {
  922. uni.showToast({
  923. icon: 'none',
  924. title: '未获得启动码',
  925. duration: 3000
  926. })
  927. }
  928. },
  929. /**
  930. * 请求结算
  931. */
  932. async request_pay() {
  933. if (this.byte35.substring(2) == '') {
  934. uni.showToast({
  935. title: '字节码为空!',
  936. icon: 'success'
  937. });
  938. return
  939. }
  940. const res = await this.$myRequest({
  941. host: this.ceshi,
  942. url: '/HotWaters/wpconsume.action',
  943. data: {
  944. collect_code: this.byte35.substring(2)
  945. }
  946. })
  947. // console.log(res)
  948. if (res.data.mess === "支付成功!") {
  949. uni.showToast({
  950. icon: 'success',
  951. title: '结算成功!',
  952. success: (res1) => {
  953. this.use_amount = res.data.use_amount.toFixed(2)
  954. this.amount = (this.amount - this.use_amount).toFixed(2)
  955. // 刷新选定的月份消费记录
  956. this.request_consumption_records()
  957. }
  958. })
  959. } else {
  960. uni.showToast({
  961. icon: 'success',
  962. title: '结算失败!',
  963. duration: 3000
  964. })
  965. }
  966. }
  967. }
  968. }
  969. /**
  970. * 返回字符处理
  971. */
  972. function str2StrArray(start_code) {
  973. if (start_code.length != 34) {
  974. console.log('启动码长度错误')
  975. throw new Error("启动码长度错误");
  976. return
  977. }
  978. var resultArray = new Array()
  979. for (var i = 0; i < start_code.length; i += 2) {
  980. resultArray.push(start_code.substr(i, 2));
  981. }
  982. return resultArray;
  983. }
  984. /**
  985. * ArrayBuffer转16进度字符串
  986. */
  987. function ab2hex(buffer) {
  988. const hexArr = Array.prototype.map.call(
  989. new Uint8Array(buffer),
  990. function(bit) {
  991. // console.log('====================' + bit + '==========================')
  992. return ('00' + bit.toString(16)).slice(-2)
  993. }
  994. )
  995. return hexArr.join('');
  996. }
  997. /**
  998. * 16进制转10进制
  999. */
  1000. function sixteen_to_ten(str) {
  1001. return parseInt(str, 16);
  1002. }
  1003. </script>
  1004. <style scoped lang="scss">
  1005. .content {
  1006. width: 100%;
  1007. height: 100%;
  1008. .container {
  1009. width: 100%;
  1010. padding: 20rpx 40rpx;
  1011. box-sizing: border-box;
  1012. .header {
  1013. display: flex;
  1014. justify-content: center;
  1015. position: relative;
  1016. height: 200rpx;
  1017. .scan {
  1018. display: flex;
  1019. flex-direction: column;
  1020. justify-content: center;
  1021. align-items: center;
  1022. height: 200rpx;
  1023. width: 30%;
  1024. .iconfont {
  1025. margin-bottom: 10rpx;
  1026. font-size: 100rpx;
  1027. color: $my-color-primary;
  1028. }
  1029. }
  1030. .scan_hover {
  1031. border-radius: 15rpx;
  1032. background-color: $my-color-btn-background;
  1033. }
  1034. .qinshihao {
  1035. display: flex;
  1036. align-items: center;
  1037. position: fixed;
  1038. top: 20rpx;
  1039. right: 10rpx;
  1040. font-size: 28rpx;
  1041. .icon-zhuye {
  1042. margin-right: 10rpx;
  1043. font-family: Microsoft YaHei-3970(82674968);
  1044. color: $my-color-primary;
  1045. }
  1046. .compustxt {
  1047. font-size: 18rpx;
  1048. }
  1049. }
  1050. }
  1051. .start_text {
  1052. .start_top,
  1053. .start_mid {
  1054. display: flex;
  1055. justify-content: space-between;
  1056. height: 100rpx;
  1057. align-items: center;
  1058. border-bottom: 2rpx #ccc solid;
  1059. .item_left {
  1060. display: flex;
  1061. align-items: center;
  1062. }
  1063. .start_mid_txt {
  1064. color: #333;
  1065. font-size: 28rpx;
  1066. }
  1067. .iconfont {
  1068. font-size: 50rpx;
  1069. color: $my-color-primary;
  1070. margin-right: 20rpx;
  1071. }
  1072. .start_payamount {
  1073. display: flex;
  1074. justify-content: flex-end;
  1075. align-items: center;
  1076. font-size: 38rpx;
  1077. padding: 10rpx;
  1078. font-family: Microsoft YaHei-3970(82674968);
  1079. color: #B3B3B3;
  1080. border-radius: 10rpx;
  1081. .icon-dayuhao {
  1082. margin: 0 0 0 20rpx;
  1083. font-size: 38rpx;
  1084. color: #B3B3B3;
  1085. }
  1086. }
  1087. }
  1088. .start_bot {
  1089. display: flex;
  1090. justify-content: flex-end;
  1091. height: 100rpx;
  1092. align-items: center;
  1093. .start_bot_txt {
  1094. margin-left: 20rpx;
  1095. color: #333;
  1096. font-size: 28rpx;
  1097. }
  1098. }
  1099. }
  1100. .start_up_process {
  1101. padding: 20rpx 0;
  1102. color: #007AFF;
  1103. }
  1104. .title {
  1105. height: 100rpx;
  1106. margin: 0 -40rpx;
  1107. padding: 0 40rpx;
  1108. line-height: 100rpx;
  1109. color: #808080;
  1110. font-size: 32rpx;
  1111. background: #f2f2f2;
  1112. }
  1113. .record-item {
  1114. width: 100%;
  1115. .uni-list-cell-db {
  1116. height: 100rpx;
  1117. line-height: 100rpx;
  1118. font-weight: bold;
  1119. .uni-input {
  1120. display: flex;
  1121. align-items: center;
  1122. text:nth-child(1) {
  1123. margin-right: 10rpx;
  1124. }
  1125. }
  1126. }
  1127. .item-list {
  1128. height: 80rpx;
  1129. display: flex;
  1130. align-items: center;
  1131. justify-content: space-between;
  1132. border-top: 2rpx #ccc solid;
  1133. .icon-shijian {
  1134. margin-right: 8rpx;
  1135. }
  1136. .icon-shuidi {
  1137. margin-right: 3rpx;
  1138. }
  1139. .item-list-txt {
  1140. font-size: 28rpx;
  1141. &:nth-of-type(2) {
  1142. color: #515151;
  1143. }
  1144. &:nth-of-type(3) {
  1145. color: #f76200;
  1146. font-size: 32rpx;
  1147. }
  1148. }
  1149. }
  1150. }
  1151. }
  1152. }
  1153. </style>