reshui.vue 31 KB

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