reshui.vue 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  1. <template>
  2. <view class="content">
  3. <view class="container">
  4. <view class="header">
  5. <view class="scan" @tap="scan()">
  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="txt">{{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">
  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"></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: getDate({
  78. format: true
  79. }),
  80. startDate: getDate('start_date'),
  81. endDate: 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. // order_no: '', // 收集结算的订单号
  110. xiaofei_items: [],
  111. userinfo: '',
  112. card_number: '',
  113. user_name: '',
  114. use_amount: 0.0.toFixed(2), // 结算金额
  115. amount: 0.0.toFixed(2), // 余额
  116. dorm_number: '' // 宿舍号
  117. }
  118. },
  119. onLoad(options) {
  120. try {
  121. // 获取存储的用户数据
  122. const value = uni.getStorageSync('userinfo_storage_key')
  123. let item = decodeURIComponent(options.item)
  124. if (item === '{}' && value == '') { // 如果没有用户信息,返回首页
  125. uni.redirectTo({
  126. url: '../index/index'
  127. });
  128. return;
  129. }
  130. if (item != '') {
  131. // 处理JSON字符串
  132. // this.userinfo = item.replace(/"/g, "'");
  133. this.userinfo = JSON.parse(item);
  134. } else if (value != '') {
  135. // 处理JSON字符串
  136. // this.userinfo = value.replace(/"/g, "'");
  137. this.userinfo = value;
  138. }
  139. } catch (e) {
  140. console.log(e)
  141. }
  142. // 查询用户信息
  143. this.select_user_info()
  144. },
  145. methods: {
  146. /**
  147. * 选择消费年月
  148. */
  149. bindDateChange(e) {
  150. this.date = e.detail.value
  151. // 请求选定的月份消费记录
  152. this.request_consumption_records()
  153. },
  154. /**
  155. * 导航页面取消
  156. */
  157. cancel(param) {
  158. this.flag = param
  159. },
  160. /**
  161. * 请求选定的月份消费记录
  162. */
  163. async request_consumption_records() {
  164. this.xiaofei_items = []
  165. const res = await this.$myRequest({
  166. host: 'code',
  167. url: '/HotWater/wxpayqueryConsume.action',
  168. method: 'POST',
  169. header: {
  170. 'content-type': 'application/x-www-form-urlencoded'
  171. },
  172. data: {
  173. card_number: this.card_number,
  174. begin_time: this.date
  175. }
  176. });
  177. // console.log(res.data.mess, 'request_consumption_records');
  178. if (typeof(res.data.mess) != 'undefined') {
  179. let items = res.data.mess
  180. // 按时间倒序排列
  181. // items.sort((a, b) => {
  182. // return a.begin_time < b.begin_time ? 1 : -1;
  183. // })
  184. for (var i = 0; i < items.length; i++) {
  185. // 过滤掉为0的记录,只显示消费大于0的记录
  186. // if (items[i].use_amount > 0.00) {
  187. this.xiaofei_items.push(items[i])
  188. // }
  189. }
  190. } else {
  191. uni.showToast({
  192. icon: 'success',
  193. title: '该月无消费记录'
  194. })
  195. }
  196. },
  197. /**
  198. * 查询用户信息
  199. */
  200. async select_user_info() {
  201. const res = await this.$myRequest({
  202. host: 'code',
  203. url: '/HotWater/userget_stu.action',
  204. method: 'POST',
  205. header: {
  206. 'content-type': 'application/x-www-form-urlencoded'
  207. },
  208. data: {
  209. card_number: this.userinfo.card_number
  210. }
  211. });
  212. // console.log(res, 'select_user_info');
  213. // 将JSON字符串转化为JSON对象,取余额
  214. let userinfo = res.data.info[0];
  215. let amount = userinfo.balance; // 余额
  216. let cnumber = userinfo.card_number; // 卡号
  217. this.user_name = userinfo.user_name;
  218. this.card_number = cnumber; // 卡号
  219. this.dorm_number = userinfo.dom; // 宿舍号
  220. if (amount != 0) {
  221. this.amount = amount.toFixed(2)
  222. }
  223. if (cnumber == null || cnumber == undefined || cnumber == '') {
  224. // 之前未绑定用户信息,现在去绑定用户信息
  225. this.send_save_user_info()
  226. }
  227. // 请求选定的月份消费记录
  228. this.request_consumption_records()
  229. },
  230. /**
  231. * 绑定用户信息
  232. */
  233. async send_save_user_info() {
  234. const res = await this.$myRequest({
  235. host: 'code',
  236. url: '/HotWater/userstu_info.action',
  237. method: 'POST',
  238. header: {
  239. 'content-type': 'application/x-www-form-urlencoded'
  240. },
  241. data: {
  242. user: JSON.stringify(this.userinfo)
  243. }
  244. });
  245. console.log('send_save_user_info:', res)
  246. let error = res.data.erro;
  247. if (error == '未获取到数据!') {
  248. uni.showToast({
  249. title: '绑定用户失败',
  250. duration: 2000
  251. })
  252. } else {
  253. uni.showToast({
  254. icon: 'success',
  255. title: '绑定用户成功',
  256. duration: 2000
  257. })
  258. }
  259. },
  260. /**
  261. * 控制进度条显示
  262. * @param {Object} active_value
  263. */
  264. process_control(active_value) {
  265. if (active_value <= this.list1.length - 1) {
  266. this.active = active_value
  267. }
  268. },
  269. /**
  270. * 开始或停止用水
  271. */
  272. switchChange(e) {
  273. this.isChecked = e.target.value
  274. if (this.isChecked) {
  275. if (this.deviceId && this.serviceId && this.devices.length != 0) {
  276. uni.showModal({
  277. title: '提示',
  278. content: '确定要启动设备?',
  279. success: (res) => {
  280. if (res.confirm) {
  281. this.isChecked = true
  282. } else if (res.cancel) {
  283. setTimeout(() => {
  284. this.isChecked = false
  285. }, 300)
  286. }
  287. }
  288. })
  289. // 开始连接匹配的蓝牙设备
  290. setTimeout(async () => {
  291. // 请求获取设备启动码
  292. await this.request_device_start_code()
  293. }, 3000)
  294. } else {
  295. uni.showToast({
  296. icon: 'none',
  297. title: '先【扫码连接】,后【开启设备】',
  298. duration: 1500
  299. })
  300. setTimeout(() => {
  301. this.isChecked = false
  302. }, 300)
  303. }
  304. } else {
  305. uni.showModal({
  306. title: '提示',
  307. content: '确定要关闭设备?',
  308. success: (res) => {
  309. if (res.confirm) {
  310. this.isChecked = false
  311. if (this.deviceId && this.serviceId) {
  312. this.send('stop')
  313. } else {
  314. uni.showToast({
  315. icon: 'none',
  316. title: '与设备连接中断!请在设备上手动停止即可!',
  317. duration: 5000
  318. })
  319. setTimeout(() => {
  320. this.isChecked = true
  321. }, 300)
  322. }
  323. } else if (res.cancel) {
  324. setTimeout(() => {
  325. this.isChecked = true
  326. }, 300)
  327. }
  328. }
  329. })
  330. }
  331. },
  332. /**
  333. * 跳转到充值页面并传参数
  334. */
  335. chongzhi_yemian() {
  336. //在起始页面跳转到recharge.vue页面并传递参数
  337. uni.navigateTo({
  338. url: '../recharge/recharge?item=' + encodeURIComponent(JSON.stringify(this.userinfo))
  339. });
  340. },
  341. /**
  342. * 111111111111111111111111111111
  343. * 扫码连接设备
  344. */
  345. scan() {
  346. // 启动最低金额限制
  347. if (this.amount < 10) {
  348. uni.showModal({
  349. title: '提示',
  350. content: '当前余额不足¥10元',
  351. cancelText: '我知道了',
  352. confirmText: '我要充值',
  353. success: (res) => {
  354. if (res.confirm) {
  355. // 跳转充值页面并传参数
  356. this.chongzhi_yemian()
  357. } else if (res.cancel) {
  358. return
  359. }
  360. }
  361. })
  362. } else {
  363. // 已经启动扫描连接设备中
  364. if (this.isScan) {
  365. uni.showToast({
  366. icon: 'none',
  367. title: '正在扫描,请耐心等待',
  368. })
  369. return
  370. }
  371. // 已经启动设备,不启动扫描
  372. if (this.isChecked) {
  373. uni.showToast({
  374. icon: 'none',
  375. title: '设备已启动,无需重新扫码',
  376. })
  377. return
  378. }
  379. if (this.deviceId) {
  380. uni.showModal({
  381. title: '提示',
  382. content: '设备已经连接,需要重新连接吗?',
  383. success: res => {
  384. if (res.confirm) {
  385. this.closeBLEConnection()
  386. this.scan_qr()
  387. } else if (res.cancel) {
  388. return
  389. }
  390. }
  391. })
  392. } else {
  393. this.scan_qr()
  394. }
  395. }
  396. },
  397. /**
  398. * 启动摄像头,扫描二维码
  399. */
  400. scan_qr() {
  401. uni.scanCode({
  402. onlyFromCamera: true,
  403. success: (res) => {
  404. this.isScan = true
  405. // console.log(res)
  406. // charSet: "utf-8"
  407. // codeVersion: 3
  408. // errMsg: "scanCode:ok"
  409. // rawData: "aHR0cDovL3Rvb2xzLjlrYnMuY29tLz93eG1pbmljb2RlPUtCNTgwMDAyNjUy"
  410. // result: "http://tools.9kbs.com/?wxminicode=KB580002652"
  411. // scanType: "QR_CODE"
  412. this.device_code = res.result.split('?')[1].split('=')[1]
  413. if (this.device_code === '') {
  414. uni.showToast({
  415. icon: 'success',
  416. title: '获取设备码失败',
  417. duration: 3000
  418. })
  419. this.isScan = false
  420. } else {
  421. // 初始化蓝牙
  422. this.openBluetoothAdapter()
  423. }
  424. }
  425. })
  426. },
  427. /**
  428. * 22222222222222222222222222222
  429. * 初始化蓝牙
  430. */
  431. openBluetoothAdapter() {
  432. uni.openBluetoothAdapter({
  433. success: (res) => { // 已打开
  434. uni.getBluetoothAdapterState({ // 蓝牙的匹配状态
  435. success: (res1) => {
  436. // console.log(res1, '“本机设备的蓝牙已打开”');
  437. // 本机设备的蓝牙已打开
  438. // 开始搜索蓝牙设备
  439. if (this.deviceId == '' && this.serviceId == '')
  440. this.startBluetoothDeviceDiscovery()
  441. },
  442. fail: (error) => {
  443. // console.log(error)
  444. uni.showToast({
  445. title: "请开启手机蓝牙",
  446. icon: "none",
  447. duration: 3000
  448. })
  449. this.isScan = false
  450. }
  451. });
  452. },
  453. fail: (err) => { // 未打开
  454. // console.log(err)
  455. this.isScan = false
  456. if (err.errCode == 10001) {
  457. uni.showModal({
  458. content: '请开启手机蓝牙!',
  459. showCancel: false,
  460. })
  461. } else {
  462. uni.showToast({
  463. title: err.errMsg,
  464. duration: 2000
  465. })
  466. }
  467. }
  468. });
  469. },
  470. /**
  471. * 333333333333333333333333333
  472. * 开始搜索蓝牙设备
  473. */
  474. startBluetoothDeviceDiscovery() {
  475. uni.startBluetoothDevicesDiscovery({ // 开始搜寻附近的蓝牙外围设备
  476. // services: ['FEE7'], // 要搜索但蓝牙设备主 service 的 uuid 列表
  477. allowDuplicatesKey: true,
  478. success: (res) => {
  479. this.process_control(0);
  480. this.onBluetoothDeviceFound() //监听搜索到新设备 同时进行
  481. },
  482. fail: err => {
  483. // console.log(err, '错误信息');
  484. uni.showToast({
  485. title: err.errMsg,
  486. duration: 2000
  487. })
  488. this.isScan = false
  489. }
  490. })
  491. },
  492. stopBluetoothDevicesDiscovery: function() { //停止搜索
  493. uni.stopBluetoothDevicesDiscovery({
  494. success: (res) => {
  495. // console.log(res, '停止蓝牙搜索')
  496. // uni.showToast({
  497. // title: '停止蓝牙搜索',
  498. // duration: 2000
  499. // })
  500. this.process_control(0);
  501. // 开始连接匹配的蓝牙设备
  502. this.createBLEConnection()
  503. },
  504. fail: e => {
  505. // console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
  506. this.isScan = false
  507. uni.showToast({
  508. title: err.errCode,
  509. duration: 2000
  510. })
  511. }
  512. })
  513. },
  514. /**
  515. * 444444444444444444444444444444444
  516. * 发现外围设备
  517. */
  518. onBluetoothDeviceFound() {
  519. uni.onBluetoothDeviceFound((res) => {
  520. res.devices.forEach(device => { // 筛选找到的蓝牙中对名称匹配
  521. // 过滤掉没有名字的设备
  522. if (!device.name && !device.localName) {
  523. return
  524. }
  525. if (device.name == this.device_code) {
  526. //data里面建立一个deviceId、device_code,存储起来
  527. // this.device_code = device.name;
  528. this.deviceId = device.deviceId
  529. // 把搜索到的设备存储起来,如有需要可以在页面上展示
  530. if (this.devices.indexOf(res.devices[0].deviceId) == -1) {
  531. this.devices.push(res.devices[0].deviceId)
  532. }
  533. this.process_control(0);
  534. this.stopBluetoothDevicesDiscovery()
  535. }
  536. })
  537. });
  538. },
  539. /**
  540. * 555555555555555555555555555555555
  541. * 连接蓝牙设备
  542. */
  543. createBLEConnection() {
  544. //连接蓝牙
  545. uni.createBLEConnection({
  546. deviceId: this.deviceId,
  547. timeout: 3000,
  548. success: (res) => {
  549. // console.log(res)
  550. this.connect_BLE()
  551. },
  552. fail: (err) => {
  553. // console.log(err)
  554. this.isScan = false
  555. if (err.errMsg.indexOf('already connect') != -1) {
  556. this.connect_BLE()
  557. } else {
  558. uni.showToast({
  559. title: '建立连接失败',
  560. icon: 'none',
  561. duration: 3000,
  562. success: (res1) => {
  563. this.setInit()
  564. }
  565. })
  566. }
  567. }
  568. })
  569. },
  570. connect_BLE() {
  571. if (this.isConnected === false) {
  572. this.isConnected = true
  573. this.process_control(1);
  574. // 获取蓝牙设备所有服务(service)
  575. this.getBLEDeviceServices()
  576. }
  577. },
  578. /**
  579. * 6666666666666666666666666666666666
  580. * 获取蓝牙的所有服务
  581. */
  582. getBLEDeviceServices() {
  583. uni.getBLEDeviceServices({
  584. deviceId: this.deviceId,
  585. success: (res) => {
  586. // console.log(res)
  587. // res1.services.forEach((item) => {
  588. // this.getBLEDeviceCharacteristics()
  589. // })
  590. this.serviceId = res.services[1].uuid
  591. this.process_control(1);
  592. this.getBLEDeviceCharacteristics(this.deviceId, res.services[1].uuid)
  593. },
  594. fail: (err) => {
  595. // console.log(err)
  596. this.isScan = false
  597. uni.showToast({
  598. title: '获取服务失败',
  599. icon: 'none',
  600. duration: 3000
  601. })
  602. return
  603. }
  604. });
  605. },
  606. /**
  607. * 777777777777777777777777777777777
  608. * 获取蓝牙特征
  609. */
  610. getBLEDeviceCharacteristics() {
  611. // 获取蓝牙设备某个服务中所有特征值(characteristic)
  612. uni.getBLEDeviceCharacteristics({
  613. deviceId: this.deviceId,
  614. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  615. serviceId: this.serviceId,
  616. success: (res) => {
  617. this.process_control(2);
  618. // console.log(res)
  619. this.characteristicIdForWrite = res.characteristics[0].uuid
  620. this.characteristicIdForNotity = res.characteristics[1].uuid
  621. this.deviceIsWrite = res.characteristics[0].properties.write
  622. this.deviceIsNotify = res.characteristics[1].properties.notify
  623. // console.log(this.characteristicIdForWrite, this.characteristicIdForNotity)
  624. // 确保对应服务id下的特征值id具备监听数据变化的特性
  625. if (res.characteristics[1].properties.notify) {
  626. this.notifyBLECharacteristicValueChange()
  627. }
  628. },
  629. fail: (err) => {
  630. this.isScan = false
  631. console.error('获取蓝牙设备特征值失败(getBLEDeviceCharacteristics)', err)
  632. }
  633. });
  634. },
  635. /**
  636. * 888888888888888888888888888888888888
  637. * 启用 notify 功能
  638. */
  639. notifyBLECharacteristicValueChange() {
  640. // console.log(characteristicId, 'characteristicId')
  641. uni.notifyBLECharacteristicValueChange({
  642. state: true, // 启用 notify 功能
  643. deviceId: this.deviceId,
  644. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  645. serviceId: this.serviceId,
  646. // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  647. characteristicId: this.characteristicIdForNotity,
  648. success: (res) => {
  649. this.process_control(2)
  650. this.isScan = false
  651. // console.log('通知启用(notifyBLECharacteristicValueChange)',res);
  652. this.onBLECharacteristicValueChange()
  653. setTimeout(() => {
  654. // 发送 AAABAC,采集数据(会返回 35字节 的数据)
  655. this.send('')
  656. }, 300)
  657. // // 请求设备信息
  658. // this.request_device_info()
  659. // // 显示设备启动授权金额
  660. // this.display_device_startup_amount()
  661. },
  662. fail: (err) => {
  663. // console.log(err)
  664. this.isScan = false
  665. uni.showModal({
  666. content: err.errMsg,
  667. showCancel: false
  668. })
  669. }
  670. })
  671. },
  672. // /**
  673. // * 请求设备信息
  674. // */
  675. // async request_device_info() {
  676. // const res = await this.$myRequest({
  677. // url: '',
  678. // data: {
  679. // }
  680. // })
  681. // console.log('', res)
  682. // },
  683. // /**
  684. // * 显示设备启动授权金额
  685. // */
  686. // display_device_startup_amount() {
  687. // // 待开发
  688. // },
  689. /**
  690. * 999999999999999999999999999999999
  691. * 监听低功耗蓝牙设备的特征值变化
  692. */
  693. onBLECharacteristicValueChange() {
  694. uni.onBLECharacteristicValueChange((characteristic) => {
  695. let condition = ab2hex(characteristic.value)
  696. // console.log(condition)
  697. if (condition.length > 2) {
  698. this.byte35_tmp += condition
  699. if (this.byte35_tmp.length > 40) {
  700. this.byte35 = this.byte35_tmp
  701. this.byte35_tmp = ''
  702. if (this.byte35.startsWith('ad')) {
  703. // console.log(this.byte35, '结束用水成功')
  704. // 关闭蓝牙连接
  705. this.closeBLEConnection()
  706. // 处理 35字节
  707. // APP 接收后,根据用户操作情况主要做如下几个方面的处理。
  708. // 1、向后台提交【结算数据】
  709. // 2、提交【结算数据】并生成【启动代码】
  710. // 3、提示:用户进行下面的流程
  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. const res = await this.$myRequest({
  929. host: 'code',
  930. url: '/HotWater/wxpaystart.action',
  931. data: {
  932. collect_code: this.byte35,
  933. card_number: this.card_number,
  934. user_name: this.user_name
  935. }
  936. })
  937. // console.log(res.data.start_code)
  938. if (res.data.start_code != '') {
  939. this.start_code = res.data.start_code
  940. this.send('start')
  941. } else {
  942. uni.showToast({
  943. icon: 'none',
  944. title: '未获得启动码',
  945. duration: 3000
  946. })
  947. }
  948. },
  949. /**
  950. * 请求结算
  951. */
  952. async request_pay() {
  953. const res = await this.$myRequest({
  954. host: 'code',
  955. url: '/HotWater/wxpayconsume.action',
  956. data: {
  957. collect_code: this.byte35.substring(2)
  958. }
  959. })
  960. // console.log(res)
  961. if (res.data.mess === "支付成功!") {
  962. uni.showToast({
  963. icon: 'success',
  964. title: '结算成功!',
  965. success: (res1) => {
  966. this.use_amount = res.data.use_amount.toFixed(2)
  967. this.amount = (this.amount - this.use_amount).toFixed(2)
  968. // 刷新选定的月份消费记录
  969. this.request_consumption_records()
  970. }
  971. })
  972. } else {
  973. uni.showToast({
  974. icon: 'success',
  975. title: '结算失败!',
  976. duration: 3000
  977. })
  978. }
  979. }
  980. }
  981. }
  982. /**
  983. * 返回字符处理
  984. */
  985. function str2StrArray(start_code) {
  986. if (start_code.length != 34) {
  987. console.log('启动码长度错误')
  988. throw new Error("启动码长度错误");
  989. return
  990. }
  991. var resultArray = new Array()
  992. for (var i = 0; i < start_code.length; i += 2) {
  993. resultArray.push(start_code.substr(i, 2));
  994. }
  995. return resultArray;
  996. }
  997. /**
  998. * ArrayBuffer转16进度字符串
  999. */
  1000. function ab2hex(buffer) {
  1001. const hexArr = Array.prototype.map.call(
  1002. new Uint8Array(buffer),
  1003. function(bit) {
  1004. // console.log('====================' + bit + '==========================')
  1005. return ('00' + bit.toString(16)).slice(-2)
  1006. }
  1007. )
  1008. return hexArr.join('');
  1009. }
  1010. /**
  1011. * 16进制转10进制
  1012. */
  1013. function sixteen_to_ten(str) {
  1014. return parseInt(str, 16);
  1015. }
  1016. /**
  1017. * 时间计算
  1018. */
  1019. function getDate(type) {
  1020. const date = new Date();
  1021. let year = date.getFullYear();
  1022. let month = date.getMonth() + 1;
  1023. // let day = date.getDate();
  1024. if (type === 'start_date') {
  1025. year = year - 4;
  1026. } else if (type === 'end_date') {
  1027. year = year;
  1028. }
  1029. month = month > 9 ? month : '0' + month;
  1030. // day = day > 9 ? day : '0' + day;
  1031. return `${year}-${month}`;
  1032. }
  1033. </script>
  1034. <style scoped lang="scss">
  1035. .content {
  1036. width: 100%;
  1037. height: 100%;
  1038. .container {
  1039. width: 100%;
  1040. padding: 20rpx 40rpx;
  1041. box-sizing: border-box;
  1042. .header {
  1043. position: relative;
  1044. height: 200rpx;
  1045. .scan {
  1046. display: flex;
  1047. height: 200rpx;
  1048. flex-direction: column;
  1049. align-items: center;
  1050. justify-content: center;
  1051. .iconfont {
  1052. margin-bottom: 10rpx;
  1053. font-size: 100rpx;
  1054. color: $my-color-primary;
  1055. }
  1056. }
  1057. .qinshihao {
  1058. position: fixed;
  1059. top: 20rpx;
  1060. right: 40rpx;
  1061. font-size: 28rpx;
  1062. .icon-zhuye {
  1063. margin-right: 10rpx;
  1064. font-family: Microsoft YaHei-3970(82674968);
  1065. color: $my-color-primary;
  1066. }
  1067. }
  1068. }
  1069. .start_text {
  1070. .start_top,
  1071. .start_mid {
  1072. display: flex;
  1073. justify-content: space-between;
  1074. height: 100rpx;
  1075. align-items: center;
  1076. border-bottom: 2rpx #ccc solid;
  1077. .item_left {
  1078. display: flex;
  1079. align-items: center;
  1080. }
  1081. .start_mid_txt {
  1082. color: #333;
  1083. font-size: 28rpx;
  1084. }
  1085. .iconfont {
  1086. font-size: 50rpx;
  1087. color: $my-color-primary;
  1088. margin-right: 20rpx;
  1089. }
  1090. .start_payamount {
  1091. display: flex;
  1092. justify-content: flex-end;
  1093. align-items: center;
  1094. font-size: 38rpx;
  1095. font-family: Microsoft YaHei-3970(82674968);
  1096. color: #B3B3B3;
  1097. .icon-dayuhao {
  1098. margin: 0 0 0 20rpx;
  1099. font-size: 38rpx;
  1100. color: #B3B3B3;
  1101. }
  1102. }
  1103. }
  1104. .start_bot {
  1105. display: flex;
  1106. justify-content: flex-end;
  1107. height: 100rpx;
  1108. align-items: center;
  1109. .start_bot_txt {
  1110. margin-left: 20rpx;
  1111. color: #333;
  1112. font-size: 28rpx;
  1113. }
  1114. }
  1115. }
  1116. .start_up_process {
  1117. padding: 20rpx 0;
  1118. color: #007AFF;
  1119. }
  1120. .title {
  1121. height: 100rpx;
  1122. margin: 0 -40rpx;
  1123. padding: 0 40rpx;
  1124. line-height: 100rpx;
  1125. color: #808080;
  1126. font-size: 32rpx;
  1127. background: #f2f2f2;
  1128. }
  1129. .record-item {
  1130. width: 100%;
  1131. .uni-list-cell-db {
  1132. height: 100rpx;
  1133. line-height: 100rpx;
  1134. font-weight: bold;
  1135. .uni-input {
  1136. display: flex;
  1137. align-items: center;
  1138. text:nth-child(1) {
  1139. margin-right: 10rpx;
  1140. }
  1141. }
  1142. }
  1143. .item-list {
  1144. height: 80rpx;
  1145. display: flex;
  1146. align-items: center;
  1147. justify-content: space-between;
  1148. border-top: 2rpx #ccc solid;
  1149. .icon-shijian {
  1150. margin-right: 8rpx;
  1151. }
  1152. .icon-shuidi {
  1153. margin-right: 3rpx;
  1154. }
  1155. .item-list-txt {
  1156. font-size: 28rpx;
  1157. &:nth-of-type(2) {
  1158. color: #515151;
  1159. }
  1160. &:nth-of-type(3) {
  1161. color: #f76200;
  1162. font-size: 32rpx;
  1163. }
  1164. }
  1165. }
  1166. }
  1167. }
  1168. }
  1169. </style>