reshui.vue 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  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('getCode', 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('getOpenId', 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. if (res.data.mess == '本月无消费记录') {
  332. this.xiaofei_items = []
  333. }
  334. }
  335. }
  336. } else {
  337. uni.showToast({
  338. icon: 'success',
  339. title: '未获得学号'
  340. })
  341. }
  342. },
  343. /**
  344. * 控制进度条显示
  345. * @param {Object} active_value
  346. */
  347. process_control(active_value) {
  348. if (active_value <= this.list1.length - 1) {
  349. this.active = active_value
  350. }
  351. },
  352. /**
  353. * 开始或停止用水
  354. */
  355. switchChange(e) {
  356. this.isChecked = e.target.value
  357. if (this.isChecked) {
  358. if (this.deviceId && this.serviceId && this.devices.length != 0) {
  359. uni.showModal({
  360. title: '提示',
  361. content: '确定要启动设备?',
  362. success: (res) => {
  363. if (res.confirm) {
  364. this.isChecked = true
  365. // 开始连接匹配的蓝牙设备
  366. setTimeout(async () => {
  367. // 请求获取设备启动码
  368. this.request_device_start_code()
  369. }, 300)
  370. } else if (res.cancel) {
  371. setTimeout(() => {
  372. this.isChecked = false
  373. }, 300)
  374. }
  375. }
  376. })
  377. } else {
  378. uni.showToast({
  379. icon: 'none',
  380. title: '先【扫码连接】,后【开启设备】',
  381. duration: 1500
  382. })
  383. setTimeout(() => {
  384. this.isChecked = false
  385. }, 300)
  386. }
  387. } else {
  388. uni.showModal({
  389. title: '提示',
  390. content: '确定要关闭设备?',
  391. success: (res) => {
  392. if (res.confirm) {
  393. this.isChecked = false
  394. if (this.deviceId && this.serviceId) {
  395. this.send('stop')
  396. } else {
  397. uni.showToast({
  398. icon: 'none',
  399. title: '与设备连接中断!请在设备上手动停止即可!',
  400. duration: 5000
  401. })
  402. setTimeout(() => {
  403. this.isChecked = true
  404. }, 300)
  405. }
  406. } else if (res.cancel) {
  407. setTimeout(() => {
  408. this.isChecked = true
  409. }, 300)
  410. }
  411. }
  412. })
  413. }
  414. },
  415. /**
  416. * 跳转到充值页面
  417. */
  418. chongzhi_yemian() {
  419. //在起始页面跳转到recharge.vue页面并传递参数
  420. uni.navigateTo({
  421. url: '../recharge/recharge'
  422. });
  423. },
  424. /**
  425. * 111111111111111111111111111111
  426. * 扫码连接设备
  427. */
  428. scan() {
  429. // 启动最低金额限制
  430. if (this.amount < 10.00) {
  431. uni.showModal({
  432. title: '提示',
  433. content: '当前余额不足¥10元',
  434. cancelText: '我知道了',
  435. confirmText: '我要充值',
  436. success: (res) => {
  437. if (res.confirm) {
  438. // 跳转充值页面
  439. this.chongzhi_yemian()
  440. } else if (res.cancel) {
  441. return
  442. }
  443. }
  444. })
  445. } else {
  446. // 已经启动扫描连接设备中
  447. if (this.isScan) {
  448. uni.showToast({
  449. icon: 'none',
  450. title: '正在扫描,请耐心等待',
  451. })
  452. return
  453. }
  454. // 已经启动设备,不启动扫描
  455. if (this.isChecked) {
  456. uni.showToast({
  457. icon: 'none',
  458. title: '设备已启动,无需重新扫码',
  459. })
  460. return
  461. }
  462. if (this.deviceId) {
  463. uni.showModal({
  464. title: '提示',
  465. content: '设备已经连接,需要重新连接吗?',
  466. success: res => {
  467. if (res.confirm) {
  468. this.closeBLEConnection()
  469. this.scan_qr()
  470. } else if (res.cancel) {
  471. return
  472. }
  473. }
  474. })
  475. } else {
  476. this.scan_qr()
  477. }
  478. }
  479. },
  480. /**
  481. * 启动摄像头,扫描二维码
  482. */
  483. scan_qr() {
  484. uni.scanCode({
  485. onlyFromCamera: true,
  486. success: (res) => {
  487. this.isScan = true
  488. // console.log(res)
  489. // charSet: "utf-8"
  490. // codeVersion: 3
  491. // errMsg: "scanCode:ok"
  492. // rawData: "aHR0cDovL3Rvb2xzLjlrYnMuY29tLz93eG1pbmljb2RlPUtCNTgwMDAyNjUy"
  493. // result: "http://tools.9kbs.com/?wxminicode=KB580002652"
  494. // scanType: "QR_CODE"
  495. this.device_code = res.result.split('=')[1]
  496. if (this.device_code === '') {
  497. uni.showToast({
  498. icon: 'success',
  499. title: '获取设备码失败',
  500. duration: 3000
  501. })
  502. this.isScan = false
  503. } else {
  504. // 初始化蓝牙
  505. this.openBluetoothAdapter()
  506. }
  507. }
  508. })
  509. },
  510. /**
  511. * 22222222222222222222222222222
  512. * 初始化蓝牙
  513. */
  514. openBluetoothAdapter() {
  515. uni.openBluetoothAdapter({
  516. success: (res) => { // 已打开
  517. uni.getBluetoothAdapterState({ // 蓝牙的匹配状态
  518. success: (res1) => {
  519. // console.log(res1, '“本机设备的蓝牙已打开”');
  520. // 本机设备的蓝牙已打开
  521. // 开始搜索蓝牙设备
  522. if (this.deviceId == '' && this.serviceId == '')
  523. this.startBluetoothDeviceDiscovery()
  524. },
  525. fail: (error) => {
  526. // console.log(error)
  527. uni.showToast({
  528. title: "请开启手机蓝牙",
  529. icon: "none",
  530. duration: 3000
  531. })
  532. this.isScan = false
  533. }
  534. });
  535. },
  536. fail: (err) => { // 未打开
  537. // console.log(err)
  538. this.isScan = false
  539. if (err.errCode == 10001) {
  540. uni.showModal({
  541. content: '请开启手机蓝牙!',
  542. showCancel: false,
  543. })
  544. } else {
  545. uni.showToast({
  546. icon: 'none',
  547. title: err.errMsg,
  548. duration: 2000
  549. })
  550. }
  551. }
  552. });
  553. },
  554. /**
  555. * 333333333333333333333333333
  556. * 开始搜索蓝牙设备
  557. */
  558. startBluetoothDeviceDiscovery() {
  559. uni.startBluetoothDevicesDiscovery({ // 开始搜寻附近的蓝牙外围设备
  560. // services: ['FEE7'], // 要搜索但蓝牙设备主 service 的 uuid 列表
  561. allowDuplicatesKey: true,
  562. success: (res) => {
  563. this.process_control(0);
  564. this.onBluetoothDeviceFound() //监听搜索到新设备 同时进行
  565. },
  566. fail: err => {
  567. // console.log(err, '错误信息');
  568. uni.showToast({
  569. icon: 'none',
  570. title: err.errMsg,
  571. duration: 2000
  572. })
  573. this.isScan = false
  574. }
  575. })
  576. },
  577. stopBluetoothDevicesDiscovery: function() { //停止搜索
  578. uni.stopBluetoothDevicesDiscovery({
  579. success: (res) => {
  580. // console.log(res, '停止蓝牙搜索')
  581. // uni.showToast({
  582. // title: '停止蓝牙搜索',
  583. // duration: 2000
  584. // })
  585. this.process_control(0);
  586. // 开始连接匹配的蓝牙设备
  587. this.createBLEConnection()
  588. },
  589. fail: e => {
  590. // console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
  591. this.isScan = false
  592. uni.showToast({
  593. title: err.errCode,
  594. duration: 2000
  595. })
  596. }
  597. })
  598. },
  599. /**
  600. * 444444444444444444444444444444444
  601. * 发现外围设备
  602. */
  603. onBluetoothDeviceFound() {
  604. uni.onBluetoothDeviceFound((res) => {
  605. res.devices.forEach(device => { // 筛选找到的蓝牙中对名称匹配
  606. // 过滤掉没有名字的设备
  607. if (!device.name && !device.localName) {
  608. return
  609. }
  610. if (device.name == this.device_code) {
  611. this.process_control(0);
  612. //data里面建立一个deviceId、device_code,存储起来
  613. // this.device_code = device.name;
  614. this.deviceId = device.deviceId
  615. // 把搜索到的设备存储起来,如有需要可以在页面上展示
  616. if (this.devices.indexOf(res.devices[0].deviceId) == -1) {
  617. this.devices.push(res.devices[0].deviceId)
  618. }
  619. // 放此位置,苹果手机兼容
  620. this.stopBluetoothDevicesDiscovery()
  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()
  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. icon: 'none',
  1023. title: '字节码或学号为空!'
  1024. });
  1025. // 初始化
  1026. this.setInit()
  1027. }
  1028. },
  1029. /**
  1030. * 请求结算
  1031. */
  1032. async request_pay() {
  1033. if (this.byte35 != '') {
  1034. const res = await this.$myRequest({
  1035. host: this.ceshi,
  1036. url: '/HotWaters/wpconsume.action',
  1037. data: {
  1038. stu_number: this.stu_number,
  1039. user_name: this.user_name,
  1040. collect_code: this.byte35.substring(2)
  1041. }
  1042. })
  1043. // console.log(res)
  1044. if (res.data.mess == "支付成功") {
  1045. uni.showToast({
  1046. icon: 'success',
  1047. title: '结算成功!',
  1048. success: (res1) => {
  1049. this.use_amount = res.data.use_amount.toFixed(2)
  1050. this.amount = (this.amount - this.use_amount).toFixed(2)
  1051. // 刷新选定的月份消费记录
  1052. this.request_consumption_records()
  1053. }
  1054. })
  1055. } else {
  1056. uni.showToast({
  1057. icon: 'none',
  1058. title: res.data.mess,
  1059. duration: 3000
  1060. })
  1061. }
  1062. } else {
  1063. uni.showToast({
  1064. icon: 'success',
  1065. title: '字节码为空!'
  1066. });
  1067. // 初始化
  1068. this.setInit()
  1069. }
  1070. },
  1071. /**
  1072. * 返回字符处理
  1073. */
  1074. str2StrArray(start_code) {
  1075. if (typeof(start_code) == 'undefined' || start_code == '') {
  1076. uni.showToast({
  1077. icon: 'none',
  1078. title: '设备码获取失败'
  1079. })
  1080. this.setInit()
  1081. return
  1082. }
  1083. if (start_code.length != 34) {
  1084. // console.log('启动码长度错误')
  1085. throw new Error("启动码长度错误");
  1086. return
  1087. }
  1088. var resultArray = new Array()
  1089. for (var i = 0; i < start_code.length; i += 2) {
  1090. resultArray.push(start_code.substr(i, 2));
  1091. }
  1092. return resultArray;
  1093. },
  1094. /**
  1095. * ArrayBuffer转16进度字符串
  1096. */
  1097. ab2hex(buffer) {
  1098. const hexArr = Array.prototype.map.call(
  1099. new Uint8Array(buffer),
  1100. function(bit) {
  1101. // console.log('====================' + bit + '==========================')
  1102. return ('00' + bit.toString(16)).slice(-2)
  1103. }
  1104. )
  1105. return hexArr.join('');
  1106. },
  1107. /**
  1108. * 16进制转10进制
  1109. */
  1110. sixteen_to_ten(str) {
  1111. return parseInt(str, 16);
  1112. }
  1113. }
  1114. }
  1115. </script>
  1116. <style scoped lang="scss">
  1117. .content {
  1118. width: 100%;
  1119. height: 100%;
  1120. .container {
  1121. width: 100%;
  1122. padding: 20rpx 40rpx;
  1123. box-sizing: border-box;
  1124. .header {
  1125. display: flex;
  1126. justify-content: center;
  1127. position: relative;
  1128. height: 200rpx;
  1129. .scan {
  1130. display: flex;
  1131. flex-direction: column;
  1132. justify-content: center;
  1133. align-items: center;
  1134. height: 200rpx;
  1135. width: 30%;
  1136. .iconfont {
  1137. margin-bottom: 10rpx;
  1138. font-size: 100rpx;
  1139. color: $my-color-primary;
  1140. }
  1141. }
  1142. .scan_hover {
  1143. border-radius: 15rpx;
  1144. background-color: $my-color-btn-background;
  1145. }
  1146. .qinshihao {
  1147. display: flex;
  1148. align-items: center;
  1149. position: fixed;
  1150. top: 20rpx;
  1151. right: 30rpx;
  1152. font-size: 28rpx;
  1153. .icon-zhuye {
  1154. margin-right: 10rpx;
  1155. font-family: Microsoft YaHei-3970(82674968);
  1156. color: $my-color-primary;
  1157. }
  1158. .compustxt {
  1159. font-size: 18rpx;
  1160. }
  1161. }
  1162. }
  1163. .start_text {
  1164. .start_top,
  1165. .start_mid {
  1166. display: flex;
  1167. justify-content: space-between;
  1168. height: 100rpx;
  1169. align-items: center;
  1170. border-bottom: 2rpx #ccc solid;
  1171. .item_left {
  1172. display: flex;
  1173. align-items: center;
  1174. }
  1175. .start_mid_txt {
  1176. color: #333;
  1177. font-size: 30rpx;
  1178. }
  1179. .iconfont {
  1180. font-size: 50rpx;
  1181. color: $my-color-primary;
  1182. margin-right: 20rpx;
  1183. }
  1184. .start_payamount {
  1185. display: flex;
  1186. justify-content: flex-end;
  1187. align-items: center;
  1188. font-size: 38rpx;
  1189. padding: 10rpx;
  1190. font-family: Microsoft YaHei-3970(82674968);
  1191. color: #B2B2B2;
  1192. border-radius: 10rpx;
  1193. .icon-dayuhao {
  1194. margin: 0 0 0 20rpx;
  1195. font-size: 38rpx;
  1196. color: #B2B2B2;
  1197. }
  1198. }
  1199. }
  1200. .start_bot {
  1201. display: flex;
  1202. justify-content: flex-end;
  1203. height: 100rpx;
  1204. align-items: center;
  1205. .start_bot_txt {
  1206. margin-left: 20rpx;
  1207. color: #333;
  1208. font-size: 28rpx;
  1209. }
  1210. }
  1211. }
  1212. .start_up_process {
  1213. padding: 20rpx 0;
  1214. color: #007AFF;
  1215. }
  1216. .title {
  1217. height: 100rpx;
  1218. margin: 0 -40rpx;
  1219. padding: 0 40rpx;
  1220. line-height: 100rpx;
  1221. color: #808080;
  1222. font-size: 32rpx;
  1223. background: #f2f2f2;
  1224. }
  1225. .record-item {
  1226. width: 100%;
  1227. .uni-list-cell-db {
  1228. height: 100rpx;
  1229. line-height: 100rpx;
  1230. font-weight: bold;
  1231. .uni-input {
  1232. display: flex;
  1233. align-items: center;
  1234. text:nth-child(1) {
  1235. margin-right: 10rpx;
  1236. }
  1237. }
  1238. }
  1239. .item-list {
  1240. height: 80rpx;
  1241. display: flex;
  1242. align-items: center;
  1243. justify-content: space-between;
  1244. border-top: 2rpx #ccc solid;
  1245. .icon-shijian {
  1246. margin-right: 8rpx;
  1247. }
  1248. .icon-shuidi {
  1249. margin-right: 3rpx;
  1250. }
  1251. .item-list-txt {
  1252. font-size: 28rpx;
  1253. &:nth-of-type(2) {
  1254. color: #515151;
  1255. }
  1256. &:nth-of-type(3) {
  1257. color: #f76200;
  1258. font-size: 32rpx;
  1259. }
  1260. }
  1261. }
  1262. }
  1263. }
  1264. }
  1265. </style>