reshui.vue 30 KB

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