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