reshui.vue 33 KB

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