reshui.vue 30 KB

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