reshui.vue 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. <template>
  2. <view class="content">
  3. <view class="container">
  4. <view class="header">
  5. <view class="scan" @tap="scan()" hover-class="scan_hover">
  6. <text class="iconfont icon-saoma"></text>
  7. <text>扫码连接</text>
  8. </view>
  9. <view class="qinshihao">
  10. <text class="iconfont icon-zhuye"></text>
  11. <text class="dormtxt">{{dorm_number || '宿舍未知'}}</text>
  12. </view>
  13. </view>
  14. <view class="start_text">
  15. <view class="start_top">
  16. <view class="item_left">
  17. <text class="iconfont icon-shuibiao"></text>
  18. <text>开启设备</text>
  19. </view>
  20. <view>
  21. <switch :color="colorPrimary" :checked="isChecked" @change="switchChange" />
  22. </view>
  23. </view>
  24. <view class="start_mid">
  25. <view class="item_left">
  26. <text class="iconfont icon-qian"></text>
  27. <text class="start_mid_txt">¥{{amount}}</text>
  28. </view>
  29. <view class="start_payamount" @tap="chongzhi_yemian()" hover-class="scan_hover">
  30. <text>点击充值</text>
  31. <text class="iconfont icon-dayuhao"></text>
  32. </view>
  33. </view>
  34. <view class="start_bot">
  35. <text class="start_bot_txt" v-if="false">时长:00:00:00</text>
  36. <text class="start_bot_txt">当前结算消费:¥{{use_amount}}</text>
  37. </view>
  38. </view>
  39. <view class="start_up_process">
  40. <uni-steps :options="list1" :active-color="colorPrimary" :active="active" />
  41. </view>
  42. <view class="title">
  43. 消费记录
  44. </view>
  45. <view class="record-item">
  46. <view class="uni-list-cell-db">
  47. <picker mode="date" :value="date" fields="month" :start="startDate" :end="endDate"
  48. @change="bindDateChange">
  49. <view class="uni-input">
  50. <text>{{date}}</text>
  51. <text class="iconfont icon-arrow-down"></text>
  52. </view>
  53. </picker>
  54. </view>
  55. <view class="item-list" v-for="(item, i) in xiaofei_items" :key="i">
  56. <text class="item-list-txt"><text class="iconfont icon-shijian"></text>{{item.begin_time}}</text>
  57. <text class="item-list-txt"><text class="iconfont icon-shuidi"></text>{{item.use_size}}升</text>
  58. <text class="item-list-txt">{{item.use_amount}}</text>
  59. </view>
  60. </view>
  61. </view>
  62. <instructions v-if="flag" @parent_cancel="cancel" @parent_noshow='noshow'></instructions>
  63. </view>
  64. </template>
  65. <script>
  66. import instructions from '../../components/instructions.vue'
  67. export default {
  68. /**
  69. * 声明组件
  70. */
  71. components: {
  72. instructions
  73. },
  74. data() {
  75. return {
  76. colorPrimary: getApp().globalData.primaryColor,
  77. date: this.$getDate({
  78. format: true
  79. }),
  80. startDate: this.$getDate('start_date'),
  81. endDate: this.$getDate('end_date'),
  82. flag: false,
  83. isUserClose: false, // 是否是用户自己关闭
  84. isChecked: false,
  85. isConnected: false, // 是否连接上
  86. isScan: false,
  87. active: -1,
  88. list1: [{
  89. title: '连接设备'
  90. }, {
  91. title: '连设备服务'
  92. }, {
  93. title: '数据通讯'
  94. }, {
  95. title: '启动'
  96. }],
  97. devices: [], // 保存设备
  98. device_code: '', // 设备编号
  99. deviceId: '',
  100. serviceId: '',
  101. characteristicIdForWrite: '',
  102. characteristicIdForNotity: '',
  103. deviceIsNotify: false,
  104. deviceIsWrite: false,
  105. byte35: '', // 70 个字符
  106. byte35_tmp: '', // 70 个字符 临时变量
  107. start_code: '',
  108. // start_code: '3459A8C660F1C284E41003E80000120000',
  109. xiaofei_items: [], // 消费记录
  110. use_amount: 0.0.toFixed(2), // 结算金额
  111. amount: 0.0.toFixed(2), // 余额
  112. dorm_number: '', // 宿舍号
  113. stu_number: '', // 学号
  114. // compus: '', // 校区
  115. ceshi: 'code',
  116. changeDate: false // 是否是手动选择日期
  117. }
  118. },
  119. onLoad(options) {
  120. // 获取基本信息
  121. this.get_base_info(options, 'onLoad')
  122. },
  123. onShow() {
  124. // 获取基本信息
  125. this.get_base_info('', 'onShow')
  126. },
  127. methods: {
  128. /**
  129. * 获取基本信息
  130. */
  131. get_base_info(options, param) {
  132. // console.log(options);
  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. } else {
  141. uni.navigateTo({
  142. url: '../index/index?from=' + options.from
  143. })
  144. uni.showToast({
  145. icon: 'none',
  146. title: '学号为空,请进行授权',
  147. duration: 3000
  148. });
  149. }
  150. } else {
  151. const value = uni.getStorageSync('instruction_noshow');
  152. if (!value) {
  153. this.flag = true
  154. }
  155. // 获取code
  156. this.getCode()
  157. }
  158. } catch (e) {
  159. console.log(e);
  160. }
  161. if (param == 'onShow') {
  162. if (this.$store.state.payInfo.from == 'reshui_pay') {
  163. uni.showToast({
  164. title: this.$store.state.payInfo.resultMsg,
  165. icon: 'success',
  166. success: () => {
  167. this.$store.state.payInfo.resultMsg = ''
  168. this.$store.state.payInfo.from = ''
  169. }
  170. })
  171. // this.amount = this.$store.state.reshui_amount + this.amount
  172. // 重新调用一次,刷新金额
  173. this.getCode()
  174. }
  175. }
  176. },
  177. /**
  178. * 获得code
  179. */
  180. getCode() {
  181. uni.login({
  182. success: (res) => {
  183. // console.log('reshui', res);
  184. if (res.code) {
  185. // 请求服务器,获得openid
  186. this.getOpenId(res.code)
  187. } else {
  188. uni.showToast({
  189. title: res.errMsg,
  190. icon: 'none'
  191. });
  192. }
  193. }
  194. })
  195. },
  196. /**
  197. * 请求服务器,获得openid
  198. */
  199. async getOpenId(param_code) {
  200. const res = await this.$myRequest({
  201. host: this.ceshi,
  202. url: '/HotWaters/wpget_stu.action',
  203. method: 'POST',
  204. header: {
  205. 'content-type': 'application/x-www-form-urlencoded'
  206. },
  207. data: {
  208. code: param_code
  209. }
  210. })
  211. // console.log(res);
  212. if (res.data.mess == '返回成功') {
  213. // 判断存储的stu_number与返回的stu_number是否一致
  214. // if (res.data.info[0].stu_number == this.$store.state.userInfo.card_number) {
  215. if (res.data.info[0].card_number == this.$store.state.userInfo.card_number) {
  216. // 学号
  217. this.stu_number = res.data.info[0].stu_number
  218. // 剩余金额
  219. this.amount = res.data.info[0].balance.toFixed(2)
  220. if (this.amount > 0)
  221. this.$store.state.reshui_amount = this.amount
  222. // 组合楼栋宿舍号
  223. this.dorm_number = res.data.info[0].build + res.data.info[0].dom
  224. // 校区
  225. // this.compus = res.data.info[0].campus
  226. // 请求选定的月份消费记录
  227. this.request_consumption_records()
  228. } else {
  229. uni.showToast({
  230. title: '学工号不一致!',
  231. icon: 'success'
  232. });
  233. }
  234. } else if (res.data.mess == '未查询到用户信息') {
  235. uni.redirectTo({
  236. url: '../index/index?from=0'
  237. });
  238. }
  239. },
  240. /**
  241. * 选择消费年月
  242. */
  243. bindDateChange(e) {
  244. this.date = e.detail.value
  245. this.changeDate = true
  246. // 请求选定的月份消费记录
  247. this.request_consumption_records()
  248. },
  249. /**
  250. * 使用说明组件取消
  251. */
  252. cancel(param) {
  253. this.flag = param
  254. },
  255. /**
  256. * 使用说明组件不再出现
  257. */
  258. noshow(param) {
  259. try {
  260. if (param) {
  261. uni.setStorageSync("instruction_noshow", true)
  262. } else {
  263. uni.removeStorageSync("instruction_noshow")
  264. }
  265. } catch (e) {
  266. console.log(e);
  267. }
  268. },
  269. /**
  270. * 请求选定的月份消费记录
  271. */
  272. async request_consumption_records() {
  273. if (this.stu_number != '' && typeof(this.stu_number) != 'undefined') {
  274. this.xiaofei_items = []
  275. const res = await this.$myRequest({
  276. host: this.ceshi,
  277. url: '/HotWaters/wpqueryConsume.action',
  278. method: 'POST',
  279. header: {
  280. 'content-type': 'application/x-www-form-urlencoded'
  281. },
  282. data: {
  283. stu_number: this.stu_number,
  284. begin_time: this.date
  285. }
  286. });
  287. // console.log(res);
  288. if (res.data.mess == '返回成功') {
  289. // 消费记录
  290. let items = res.data.data
  291. for (var i = 0; i < items.length; i++) {
  292. this.xiaofei_items.push(items[i])
  293. }
  294. } else {
  295. if (this.changeDate) {
  296. uni.showToast({
  297. icon: 'success',
  298. title: res.data.mess,
  299. success: () => {
  300. this.changeDate = false
  301. }
  302. })
  303. }
  304. }
  305. } else {
  306. uni.showToast({
  307. icon: 'success',
  308. title: '未获得学号'
  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. // 开始连接匹配的蓝牙设备
  335. setTimeout(async () => {
  336. // 请求获取设备启动码
  337. this.request_device_start_code()
  338. }, 300)
  339. } else if (res.cancel) {
  340. setTimeout(() => {
  341. this.isChecked = false
  342. }, 300)
  343. }
  344. }
  345. })
  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'
  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. * 999999999999999999999999999999999
  726. * 监听低功耗蓝牙设备的特征值变化
  727. */
  728. onBLECharacteristicValueChange() {
  729. uni.onBLECharacteristicValueChange((characteristic) => {
  730. let condition = ab2hex(characteristic.value)
  731. // console.log(condition)
  732. if (condition.length > 2) {
  733. this.byte35_tmp += condition
  734. if (this.byte35_tmp.length > 40) {
  735. this.byte35 = this.byte35_tmp
  736. this.byte35_tmp = ''
  737. if (this.byte35.startsWith('ad')) {
  738. // console.log(this.byte35, '结束用水成功')
  739. // 关闭蓝牙连接
  740. this.closeBLEConnection()
  741. // 请求结算
  742. this.request_pay()
  743. } else {
  744. // 发送 AAABAC,采集数据(会返回 35字节 的数据)
  745. // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
  746. // uni.showToast({
  747. // icon: 'success',
  748. // title: '采集数据成功!',
  749. // duration: 2000
  750. // })
  751. // console.log(this.byte35, '采集上一条消费数据')
  752. }
  753. }
  754. }
  755. switch (condition) {
  756. case 'bb':
  757. // uni.showToast({
  758. // title: '设备启动成功',
  759. // duration: 3000,
  760. // icon: 'none'
  761. // })
  762. this.process_control(3)
  763. this.isChecked = true
  764. break
  765. case 'bc':
  766. uni.showToast({
  767. title: '启动失败,设备检验启动代码不通过',
  768. duration: 3000,
  769. icon: 'none'
  770. })
  771. break
  772. case 'aa':
  773. uni.showToast({
  774. title: '设备正在使用',
  775. duration: 3000,
  776. icon: 'none'
  777. })
  778. break
  779. case 'ab':
  780. uni.showToast({
  781. title: '结束失败,设备对结束代码检验失败',
  782. duration: 3000,
  783. icon: 'none'
  784. })
  785. break
  786. default:
  787. break
  788. }
  789. })
  790. },
  791. /**
  792. * 关闭连接
  793. */
  794. closeBLEConnection() {
  795. uni.closeBLEConnection({
  796. deviceId: this.deviceId,
  797. success: () => {
  798. // 关闭蓝牙模块
  799. this.closeBluetoothAdapter()
  800. },
  801. fail: () => {
  802. uni.showToast({
  803. icon: 'none',
  804. title: '蓝牙连接断开失败',
  805. duration: 3000,
  806. success: (res1) => {
  807. this.setInit()
  808. }
  809. })
  810. }
  811. })
  812. },
  813. closeBluetoothAdapter() {
  814. // 关闭蓝牙模块
  815. uni.closeBluetoothAdapter({
  816. success: () => {
  817. this.setInit()
  818. if (this.isUserClose == false) { // 设备端结束用水
  819. if (this.isChecked) {
  820. // console.log('设备端已结束用水,蓝牙断开成功')
  821. uni.showToast({
  822. icon: 'none',
  823. title: '设备端已结束用水,蓝牙断开成功',
  824. duration: 3000
  825. })
  826. } else {
  827. // console.log('用户重启扫描,蓝牙断开成功')
  828. uni.showToast({
  829. icon: 'none',
  830. title: '用户重启扫描,蓝牙断开成功',
  831. duration: 3000
  832. })
  833. }
  834. } else { // 手机移动端结束用水
  835. // console.log('APP端已结束用水,蓝牙断开成功')
  836. uni.showToast({
  837. icon: 'none',
  838. title: 'APP端已结束用水,蓝牙断开成功',
  839. duration: 3000
  840. })
  841. }
  842. }
  843. })
  844. },
  845. setInit() {
  846. setTimeout(() => {
  847. this.isUserClose = false
  848. this.isChecked = false
  849. this.isConnected = false
  850. this.isScan = false
  851. this.active = -1
  852. this.devices = [] // 保存设备
  853. this.device_code = '' // 设备编号
  854. this.deviceId = ''
  855. this.serviceId = ''
  856. this.characteristicIdForWrite = ''
  857. this.characteristicIdForNotity = ''
  858. this.deviceIsNotify = false
  859. this.deviceIsWrite = false
  860. this.byte35 = '' // 70 个字符
  861. this.byte35_tmp = '' // 70 个字符 临时变量
  862. // this.start_code = ''
  863. })
  864. },
  865. /**
  866. * 10===========10==================10
  867. * 发送数据到设备
  868. */
  869. send(val) {
  870. let buffer
  871. let dataView
  872. let newArr
  873. if (val == 'start') { // 启动设备
  874. this.isUserClose = false
  875. this.process_control(3)
  876. // this.createBLEConnection()
  877. // 发送 17字节 启动代码(会返回 35字节 的数据)
  878. // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
  879. newArr = str2StrArray(this.start_code)
  880. } else if (val == 'stop') { // 结束当前使用,设备成功返回:AD+35字节 消费结算代码
  881. this.isUserClose = true
  882. // 发送 AAABAD+17字节 启动代码(会返回 ad+35字节 的数据)
  883. // adb58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0104bc
  884. newArr = ['AA', 'AB', 'AD'].concat(str2StrArray(this.start_code))
  885. } else {
  886. // 发送 AAABAC 采集数据(会返回 35字节 的数据)
  887. // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
  888. newArr = ['AA', 'AB', 'AC']
  889. }
  890. buffer = new ArrayBuffer(newArr.length)
  891. dataView = new DataView(buffer)
  892. newArr.forEach((item, i) => {
  893. dataView.setUint8(i, sixteen_to_ten(item))
  894. })
  895. // 写特征值数据到设备
  896. if (this.deviceIsWrite) {
  897. // 写数据到设备
  898. this.writeBLECharacteristicValue(val, buffer)
  899. } else {
  900. uni.showToast({
  901. title: '设备不可写',
  902. duration: 3000
  903. })
  904. }
  905. },
  906. /**
  907. * 11====================11=====================11
  908. * 写数据到设备
  909. */
  910. writeBLECharacteristicValue(val, buffer) {
  911. uni.writeBLECharacteristicValue({
  912. deviceId: this.deviceId,
  913. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  914. serviceId: this.serviceId,
  915. // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  916. characteristicId: this.characteristicIdForWrite,
  917. // 这里的value是ArrayBuffer类型
  918. value: buffer,
  919. success: (res) => {
  920. // console.log(res)
  921. if (val == 'start') {
  922. setTimeout(() => {
  923. this.isChecked = true
  924. this.isConnected = true
  925. }, 300)
  926. this.process_control(3)
  927. } else if (val == 'stop') {
  928. setTimeout(() => {
  929. this.isChecked = false
  930. }, 300)
  931. }
  932. },
  933. fail: (err) => {
  934. // console.log(err)
  935. if (err.errCode == 10006) {
  936. setTimeout(() => {
  937. this.setInit()
  938. }, 300)
  939. if (this.isConnected) {
  940. uni.showToast({
  941. title: '设备连接已断开!请在水表上手动停止!',
  942. icon: 'none'
  943. })
  944. } else {
  945. uni.showToast({
  946. title: '设备连接失败!请重试扫描连接!',
  947. icon: 'none'
  948. })
  949. }
  950. }
  951. }
  952. })
  953. },
  954. /**
  955. * 请求获取设备启动码
  956. */
  957. async request_device_start_code() {
  958. if (this.byte35 != '' && this.stu_number != '') {
  959. const res = await this.$myRequest({
  960. host: this.ceshi,
  961. url: '/HotWaters/wpstart.action',
  962. data: {
  963. collect_code: this.byte35,
  964. stu_number: this.stu_number
  965. }
  966. })
  967. // console.log(res)
  968. if (res.data.start_code != 'undefined' && res.data.start_code != '') {
  969. this.start_code = res.data.start_code
  970. this.send('start')
  971. } else {
  972. uni.showToast({
  973. title: res.data.mess,
  974. duration: 3000
  975. })
  976. }
  977. } else {
  978. uni.showToast({
  979. title: '字节码或学号为空!'
  980. });
  981. // 初始化
  982. this.setInit()
  983. }
  984. },
  985. /**
  986. * 请求结算
  987. */
  988. async request_pay() {
  989. if (this.byte35 != '') {
  990. const res = await this.$myRequest({
  991. host: this.ceshi,
  992. url: '/HotWaters/wpconsume.action',
  993. data: {
  994. collect_code: this.byte35.substring(2)
  995. }
  996. })
  997. // console.log(res)
  998. if (res.data.mess == "支付成功") {
  999. uni.showToast({
  1000. icon: 'success',
  1001. title: '结算成功!',
  1002. success: (res1) => {
  1003. this.use_amount = res.data.use_amount.toFixed(2)
  1004. this.amount = (this.amount - this.use_amount).toFixed(2)
  1005. // 刷新选定的月份消费记录
  1006. this.request_consumption_records()
  1007. }
  1008. })
  1009. } else {
  1010. uni.showToast({
  1011. icon: 'success',
  1012. title: '结算失败!',
  1013. duration: 3000
  1014. })
  1015. }
  1016. } else {
  1017. uni.showToast({
  1018. title: '字节码为空!',
  1019. icon: 'success'
  1020. });
  1021. // 初始化
  1022. this.setInit()
  1023. }
  1024. }
  1025. }
  1026. }
  1027. /**
  1028. * 返回字符处理
  1029. */
  1030. function str2StrArray(start_code) {
  1031. if (start_code.length != 34) {
  1032. // console.log('启动码长度错误')
  1033. throw new Error("启动码长度错误");
  1034. return
  1035. }
  1036. var resultArray = new Array()
  1037. for (var i = 0; i < start_code.length; i += 2) {
  1038. resultArray.push(start_code.substr(i, 2));
  1039. }
  1040. return resultArray;
  1041. }
  1042. /**
  1043. * ArrayBuffer转16进度字符串
  1044. */
  1045. function ab2hex(buffer) {
  1046. const hexArr = Array.prototype.map.call(
  1047. new Uint8Array(buffer),
  1048. function(bit) {
  1049. // console.log('====================' + bit + '==========================')
  1050. return ('00' + bit.toString(16)).slice(-2)
  1051. }
  1052. )
  1053. return hexArr.join('');
  1054. }
  1055. /**
  1056. * 16进制转10进制
  1057. */
  1058. function sixteen_to_ten(str) {
  1059. return parseInt(str, 16);
  1060. }
  1061. </script>
  1062. <style scoped lang="scss">
  1063. .content {
  1064. width: 100%;
  1065. height: 100%;
  1066. .container {
  1067. width: 100%;
  1068. padding: 20rpx 40rpx;
  1069. box-sizing: border-box;
  1070. .header {
  1071. display: flex;
  1072. justify-content: center;
  1073. position: relative;
  1074. height: 200rpx;
  1075. .scan {
  1076. display: flex;
  1077. flex-direction: column;
  1078. justify-content: center;
  1079. align-items: center;
  1080. height: 200rpx;
  1081. width: 30%;
  1082. .iconfont {
  1083. margin-bottom: 10rpx;
  1084. font-size: 100rpx;
  1085. color: $my-color-primary;
  1086. }
  1087. }
  1088. .scan_hover {
  1089. border-radius: 15rpx;
  1090. background-color: $my-color-btn-background;
  1091. }
  1092. .qinshihao {
  1093. display: flex;
  1094. align-items: center;
  1095. position: fixed;
  1096. top: 20rpx;
  1097. right: 30rpx;
  1098. font-size: 28rpx;
  1099. .icon-zhuye {
  1100. margin-right: 10rpx;
  1101. font-family: Microsoft YaHei-3970(82674968);
  1102. color: $my-color-primary;
  1103. }
  1104. .compustxt {
  1105. font-size: 18rpx;
  1106. }
  1107. }
  1108. }
  1109. .start_text {
  1110. .start_top,
  1111. .start_mid {
  1112. display: flex;
  1113. justify-content: space-between;
  1114. height: 100rpx;
  1115. align-items: center;
  1116. border-bottom: 2rpx #ccc solid;
  1117. .item_left {
  1118. display: flex;
  1119. align-items: center;
  1120. }
  1121. .start_mid_txt {
  1122. color: #333;
  1123. font-size: 30rpx;
  1124. }
  1125. .iconfont {
  1126. font-size: 50rpx;
  1127. color: $my-color-primary;
  1128. margin-right: 20rpx;
  1129. }
  1130. .start_payamount {
  1131. display: flex;
  1132. justify-content: flex-end;
  1133. align-items: center;
  1134. font-size: 38rpx;
  1135. padding: 10rpx;
  1136. font-family: Microsoft YaHei-3970(82674968);
  1137. color: #B2B2B2;
  1138. border-radius: 10rpx;
  1139. .icon-dayuhao {
  1140. margin: 0 0 0 20rpx;
  1141. font-size: 38rpx;
  1142. color: #B2B2B2;
  1143. }
  1144. }
  1145. }
  1146. .start_bot {
  1147. display: flex;
  1148. justify-content: flex-end;
  1149. height: 100rpx;
  1150. align-items: center;
  1151. .start_bot_txt {
  1152. margin-left: 20rpx;
  1153. color: #333;
  1154. font-size: 28rpx;
  1155. }
  1156. }
  1157. }
  1158. .start_up_process {
  1159. padding: 20rpx 0;
  1160. color: #007AFF;
  1161. }
  1162. .title {
  1163. height: 100rpx;
  1164. margin: 0 -40rpx;
  1165. padding: 0 40rpx;
  1166. line-height: 100rpx;
  1167. color: #808080;
  1168. font-size: 32rpx;
  1169. background: #f2f2f2;
  1170. }
  1171. .record-item {
  1172. width: 100%;
  1173. .uni-list-cell-db {
  1174. height: 100rpx;
  1175. line-height: 100rpx;
  1176. font-weight: bold;
  1177. .uni-input {
  1178. display: flex;
  1179. align-items: center;
  1180. text:nth-child(1) {
  1181. margin-right: 10rpx;
  1182. }
  1183. }
  1184. }
  1185. .item-list {
  1186. height: 80rpx;
  1187. display: flex;
  1188. align-items: center;
  1189. justify-content: space-between;
  1190. border-top: 2rpx #ccc solid;
  1191. .icon-shijian {
  1192. margin-right: 8rpx;
  1193. }
  1194. .icon-shuidi {
  1195. margin-right: 3rpx;
  1196. }
  1197. .item-list-txt {
  1198. font-size: 28rpx;
  1199. &:nth-of-type(2) {
  1200. color: #515151;
  1201. }
  1202. &:nth-of-type(3) {
  1203. color: #f76200;
  1204. font-size: 32rpx;
  1205. }
  1206. }
  1207. }
  1208. }
  1209. }
  1210. }
  1211. </style>