reshui.vue 32 KB

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