reshui.vue 35 KB

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