reshui.vue 30 KB

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