reshui.vue 31 KB

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