reshui.vue 31 KB

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