reshui.vue 32 KB

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