reshui.vue 33 KB

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