reshui.vue 30 KB

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