reshui.vue 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  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. title: err.errMsg,
  547. duration: 2000
  548. })
  549. }
  550. }
  551. });
  552. },
  553. /**
  554. * 333333333333333333333333333
  555. * 开始搜索蓝牙设备
  556. */
  557. startBluetoothDeviceDiscovery() {
  558. uni.startBluetoothDevicesDiscovery({ // 开始搜寻附近的蓝牙外围设备
  559. // services: ['FEE7'], // 要搜索但蓝牙设备主 service 的 uuid 列表
  560. allowDuplicatesKey: true,
  561. success: (res) => {
  562. this.process_control(0);
  563. this.onBluetoothDeviceFound() //监听搜索到新设备 同时进行
  564. },
  565. fail: err => {
  566. // console.log(err, '错误信息');
  567. uni.showToast({
  568. title: err.errMsg,
  569. duration: 2000
  570. })
  571. this.isScan = false
  572. }
  573. })
  574. },
  575. stopBluetoothDevicesDiscovery: function() { //停止搜索
  576. uni.stopBluetoothDevicesDiscovery({
  577. success: (res) => {
  578. // console.log(res, '停止蓝牙搜索')
  579. // uni.showToast({
  580. // title: '停止蓝牙搜索',
  581. // duration: 2000
  582. // })
  583. this.process_control(0);
  584. // 开始连接匹配的蓝牙设备
  585. this.createBLEConnection()
  586. },
  587. fail: e => {
  588. // console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
  589. this.isScan = false
  590. uni.showToast({
  591. title: err.errCode,
  592. duration: 2000
  593. })
  594. }
  595. })
  596. },
  597. /**
  598. * 444444444444444444444444444444444
  599. * 发现外围设备
  600. */
  601. onBluetoothDeviceFound() {
  602. uni.onBluetoothDeviceFound((res) => {
  603. res.devices.forEach(device => { // 筛选找到的蓝牙中对名称匹配
  604. // 过滤掉没有名字的设备
  605. if (!device.name && !device.localName) {
  606. return
  607. }
  608. if (device.name == this.device_code) {
  609. this.process_control(0);
  610. this.stopBluetoothDevicesDiscovery()
  611. //data里面建立一个deviceId、device_code,存储起来
  612. // this.device_code = device.name;
  613. this.deviceId = device.deviceId
  614. // 把搜索到的设备存储起来,如有需要可以在页面上展示
  615. if (this.devices.indexOf(res.devices[0].deviceId) == -1) {
  616. this.devices.push(res.devices[0].deviceId)
  617. }
  618. return
  619. }
  620. })
  621. });
  622. },
  623. /**
  624. * 555555555555555555555555555555555
  625. * 连接蓝牙设备
  626. */
  627. createBLEConnection() {
  628. //连接蓝牙
  629. uni.createBLEConnection({
  630. deviceId: this.deviceId,
  631. timeout: 5000,
  632. success: (res) => {
  633. // console.log(res)
  634. this.connect_BLE()
  635. },
  636. fail: (err) => {
  637. // console.log(err)
  638. this.isScan = false
  639. if (err.errMsg.indexOf('already connect') != -1) {
  640. // this.connect_BLE()
  641. } else {
  642. uni.showToast({
  643. title: '建立连接失败',
  644. icon: 'none',
  645. duration: 3000,
  646. success: (res1) => {
  647. this.setInit()
  648. }
  649. })
  650. }
  651. }
  652. })
  653. },
  654. connect_BLE() {
  655. if (this.isConnected === false) {
  656. this.isConnected = true
  657. this.process_control(1);
  658. // 获取蓝牙设备所有服务(service)
  659. this.getBLEDeviceServices()
  660. }
  661. },
  662. /**
  663. * 6666666666666666666666666666666666
  664. * 获取蓝牙的所有服务
  665. */
  666. getBLEDeviceServices() {
  667. uni.getBLEDeviceServices({
  668. deviceId: this.deviceId,
  669. success: (res) => {
  670. // console.log(res)
  671. // res1.services.forEach((item) => {
  672. // this.getBLEDeviceCharacteristics()
  673. // })
  674. this.serviceId = res.services[1].uuid
  675. this.process_control(1);
  676. this.getBLEDeviceCharacteristics(this.deviceId, res.services[1].uuid)
  677. },
  678. fail: (err) => {
  679. // console.log(err)
  680. this.isScan = false
  681. uni.showToast({
  682. title: '获取服务失败',
  683. icon: 'none',
  684. duration: 3000
  685. })
  686. return
  687. }
  688. });
  689. },
  690. /**
  691. * 777777777777777777777777777777777
  692. * 获取蓝牙特征
  693. */
  694. getBLEDeviceCharacteristics() {
  695. // 获取蓝牙设备某个服务中所有特征值(characteristic)
  696. uni.getBLEDeviceCharacteristics({
  697. deviceId: this.deviceId,
  698. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  699. serviceId: this.serviceId,
  700. success: (res) => {
  701. this.process_control(2);
  702. // console.log(res)
  703. this.characteristicIdForWrite = res.characteristics[0].uuid
  704. this.characteristicIdForNotity = res.characteristics[1].uuid
  705. this.deviceIsWrite = res.characteristics[0].properties.write
  706. this.deviceIsNotify = res.characteristics[1].properties.notify
  707. // console.log(this.characteristicIdForWrite, this.characteristicIdForNotity)
  708. // 确保对应服务id下的特征值id具备监听数据变化的特性
  709. if (res.characteristics[1].properties.notify) {
  710. this.notifyBLECharacteristicValueChange()
  711. }
  712. },
  713. fail: (err) => {
  714. this.isScan = false
  715. // console.error('获取蓝牙设备特征值失败(getBLEDeviceCharacteristics)', err)
  716. }
  717. });
  718. },
  719. /**
  720. * 888888888888888888888888888888888888
  721. * 启用 notify 功能
  722. */
  723. notifyBLECharacteristicValueChange() {
  724. // console.log(characteristicId, 'characteristicId')
  725. uni.notifyBLECharacteristicValueChange({
  726. state: true, // 启用 notify 功能
  727. deviceId: this.deviceId,
  728. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  729. serviceId: this.serviceId,
  730. // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  731. characteristicId: this.characteristicIdForNotity,
  732. success: (res) => {
  733. this.process_control(2)
  734. this.isScan = false
  735. // console.log('通知启用(notifyBLECharacteristicValueChange)',res);
  736. this.onBLECharacteristicValueChange()
  737. setTimeout(() => {
  738. // 发送 AAABAC,采集数据(会返回 35字节 的数据)
  739. this.send('')
  740. }, 800)
  741. // // 请求设备信息
  742. // this.request_device_info()
  743. // // 显示设备启动授权金额
  744. // this.display_device_startup_amount()
  745. },
  746. fail: (err) => {
  747. // console.log(err)
  748. this.isScan = false
  749. uni.showModal({
  750. content: err.errMsg,
  751. showCancel: false
  752. })
  753. // 初始化
  754. this.setInit()
  755. }
  756. })
  757. },
  758. /**
  759. * 999999999999999999999999999999999
  760. * 监听低功耗蓝牙设备的特征值变化
  761. */
  762. onBLECharacteristicValueChange() {
  763. uni.onBLECharacteristicValueChange((characteristic) => {
  764. let condition = this.ab2hex(characteristic.value)
  765. // console.log(condition)
  766. if (condition.length > 2) {
  767. this.byte35_tmp += condition
  768. if (this.byte35_tmp.length > 40) {
  769. this.byte35 = this.byte35_tmp
  770. this.byte35_tmp = ''
  771. if (this.byte35.startsWith('ad')) {
  772. // console.log(this.byte35, '结束用水成功')
  773. // 关闭蓝牙连接
  774. this.closeBLEConnection()
  775. // 请求结算
  776. this.request_pay()
  777. } else {
  778. // 发送 AAABAC,采集数据(会返回 35字节 的数据)
  779. // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
  780. // uni.showToast({
  781. // icon: 'success',
  782. // title: '采集数据成功!',
  783. // duration: 2000
  784. // })
  785. // console.log(this.byte35, '采集上一条消费数据')
  786. }
  787. }
  788. }
  789. switch (condition) {
  790. case 'bb':
  791. // uni.showToast({
  792. // title: '设备启动成功',
  793. // duration: 3000,
  794. // icon: 'none'
  795. // })
  796. this.process_control(3)
  797. this.isChecked = true
  798. break
  799. case 'bc':
  800. uni.showToast({
  801. title: '启动失败,设备检验启动代码不通过',
  802. duration: 3000,
  803. icon: 'none'
  804. })
  805. break
  806. case 'aa':
  807. uni.showToast({
  808. title: '设备正在使用',
  809. duration: 3000,
  810. icon: 'none'
  811. })
  812. break
  813. case 'ab':
  814. uni.showToast({
  815. title: '结束失败,设备对结束代码检验失败',
  816. duration: 3000,
  817. icon: 'none'
  818. })
  819. break
  820. default:
  821. break
  822. }
  823. })
  824. },
  825. /**
  826. * 关闭连接
  827. */
  828. closeBLEConnection() {
  829. uni.closeBLEConnection({
  830. deviceId: this.deviceId,
  831. success: () => {
  832. // 关闭蓝牙模块
  833. this.closeBluetoothAdapter()
  834. },
  835. fail: () => {
  836. uni.showToast({
  837. icon: 'none',
  838. title: '蓝牙连接断开失败',
  839. duration: 3000,
  840. success: (res1) => {
  841. this.setInit()
  842. }
  843. })
  844. }
  845. })
  846. },
  847. closeBluetoothAdapter() {
  848. // 关闭蓝牙模块
  849. uni.closeBluetoothAdapter({
  850. success: () => {
  851. this.setInit()
  852. if (this.isUserClose == false) { // 设备端结束用水
  853. if (this.isChecked) {
  854. // console.log('设备端已结束用水,蓝牙断开成功')
  855. uni.showToast({
  856. icon: 'none',
  857. title: '设备端已结束用水,蓝牙断开成功',
  858. duration: 3000
  859. })
  860. } else {
  861. // console.log('用户重启扫描,蓝牙断开成功')
  862. uni.showToast({
  863. icon: 'none',
  864. title: '用户重启扫描,蓝牙断开成功',
  865. duration: 3000
  866. })
  867. }
  868. } else { // 手机移动端结束用水
  869. // console.log('APP端已结束用水,蓝牙断开成功')
  870. uni.showToast({
  871. icon: 'none',
  872. title: 'APP端已结束用水,蓝牙断开成功',
  873. duration: 3000
  874. })
  875. }
  876. }
  877. })
  878. },
  879. setInit() {
  880. setTimeout(() => {
  881. this.isUserClose = false
  882. this.isChecked = false
  883. this.isConnected = false
  884. this.isScan = false
  885. this.active = -1
  886. this.devices = [] // 保存设备
  887. this.device_code = '' // 设备编号
  888. this.deviceId = ''
  889. this.serviceId = ''
  890. this.characteristicIdForWrite = ''
  891. this.characteristicIdForNotity = ''
  892. this.deviceIsNotify = false
  893. this.deviceIsWrite = false
  894. this.byte35 = '' // 70 个字符
  895. this.byte35_tmp = '' // 70 个字符 临时变量
  896. // this.start_code = ''
  897. })
  898. },
  899. /**
  900. * 10===========10==================10
  901. * 发送数据到设备
  902. */
  903. send(val) {
  904. let buffer
  905. let dataView
  906. let newArr
  907. if (val == 'start') { // 启动设备
  908. this.isUserClose = false
  909. this.process_control(3)
  910. // this.createBLEConnection()
  911. // 发送 17字节 启动代码(会返回 35字节 的数据)
  912. // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
  913. newArr = this.str2StrArray(this.start_code)
  914. } else if (val == 'stop') { // 结束当前使用,设备成功返回:AD+35字节 消费结算代码
  915. this.isUserClose = true
  916. // 发送 AAABAD+17字节 启动代码(会返回 ad+35字节 的数据)
  917. // adb58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0104bc
  918. newArr = ['AA', 'AB', 'AD'].concat(this.str2StrArray(this.start_code))
  919. } else {
  920. // 发送 AAABAC 采集数据(会返回 35字节 的数据)
  921. // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
  922. newArr = ['AA', 'AB', 'AC']
  923. }
  924. buffer = new ArrayBuffer(newArr.length)
  925. dataView = new DataView(buffer)
  926. newArr.forEach((item, i) => {
  927. dataView.setUint8(i, this.sixteen_to_ten(item))
  928. })
  929. // 写特征值数据到设备
  930. if (this.deviceIsWrite) {
  931. // 写数据到设备
  932. this.writeBLECharacteristicValue(val, buffer)
  933. } else {
  934. uni.showToast({
  935. title: '设备不可写',
  936. duration: 3000
  937. })
  938. }
  939. },
  940. /**
  941. * 11====================11=====================11
  942. * 写数据到设备
  943. */
  944. writeBLECharacteristicValue(val, buffer) {
  945. uni.writeBLECharacteristicValue({
  946. deviceId: this.deviceId,
  947. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  948. serviceId: this.serviceId,
  949. // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  950. characteristicId: this.characteristicIdForWrite,
  951. // 这里的value是ArrayBuffer类型
  952. value: buffer,
  953. success: (res) => {
  954. // console.log(res)
  955. if (val == 'start') {
  956. setTimeout(() => {
  957. this.isChecked = true
  958. this.isConnected = true
  959. }, 300)
  960. this.process_control(3)
  961. } else if (val == 'stop') {
  962. setTimeout(() => {
  963. this.isChecked = false
  964. }, 300)
  965. }
  966. },
  967. fail: (err) => {
  968. // console.log(err)
  969. if (err.errCode == 10006) {
  970. setTimeout(() => {
  971. this.setInit()
  972. }, 300)
  973. if (this.isConnected) {
  974. uni.showToast({
  975. title: '设备连接已断开!请在水表上手动停止!',
  976. icon: 'none'
  977. })
  978. } else {
  979. uni.showToast({
  980. title: '设备连接失败!请重试扫描连接!',
  981. icon: 'none'
  982. })
  983. }
  984. }
  985. }
  986. })
  987. },
  988. /**
  989. * 请求获取设备启动码
  990. */
  991. async request_device_start_code() {
  992. if (this.byte35 != '' && this.stu_number != '') {
  993. const res = await this.$myRequest({
  994. host: this.ceshi,
  995. url: '/HotWaters/wpstart.action',
  996. data: {
  997. collect_code: this.byte35,
  998. stu_number: this.stu_number
  999. }
  1000. })
  1001. // console.log(res)
  1002. let sc = res.data.start_code
  1003. if (typeof(sc) != 'undefined' && sc != '') {
  1004. this.start_code = sc
  1005. setTimeout(() => {
  1006. this.send('start')
  1007. }, 100)
  1008. // 刷新选定的月份消费记录
  1009. this.request_consumption_records()
  1010. } else {
  1011. this.isChecked = false
  1012. uni.showToast({
  1013. title: res.data.mess,
  1014. duration: 3000,
  1015. })
  1016. }
  1017. } else {
  1018. uni.showToast({
  1019. title: '字节码或学号为空!'
  1020. });
  1021. // 初始化
  1022. this.setInit()
  1023. }
  1024. },
  1025. /**
  1026. * 请求结算
  1027. */
  1028. async request_pay() {
  1029. if (this.byte35 != '') {
  1030. const res = await this.$myRequest({
  1031. host: this.ceshi,
  1032. url: '/HotWaters/wpconsume.action',
  1033. data: {
  1034. stu_number: this.stu_number,
  1035. user_name: this.user_name,
  1036. collect_code: this.byte35.substring(2)
  1037. }
  1038. })
  1039. // console.log(res)
  1040. if (res.data.mess == "支付成功") {
  1041. uni.showToast({
  1042. icon: 'success',
  1043. title: '结算成功!',
  1044. success: (res1) => {
  1045. this.use_amount = res.data.use_amount.toFixed(2)
  1046. this.amount = (this.amount - this.use_amount).toFixed(2)
  1047. // 刷新选定的月份消费记录
  1048. this.request_consumption_records()
  1049. }
  1050. })
  1051. } else {
  1052. uni.showToast({
  1053. icon: 'success',
  1054. title: '结算失败!',
  1055. duration: 3000
  1056. })
  1057. }
  1058. } else {
  1059. uni.showToast({
  1060. title: '字节码为空!',
  1061. icon: 'success'
  1062. });
  1063. // 初始化
  1064. this.setInit()
  1065. }
  1066. },
  1067. /**
  1068. * 返回字符处理
  1069. */
  1070. str2StrArray(start_code) {
  1071. if (typeof(start_code) == 'undefined' || start_code == '') {
  1072. uni.showToast({
  1073. icon: 'none',
  1074. title: '设备码获取失败'
  1075. })
  1076. this.setInit()
  1077. return
  1078. }
  1079. if (start_code.length != 34) {
  1080. // console.log('启动码长度错误')
  1081. throw new Error("启动码长度错误");
  1082. return
  1083. }
  1084. var resultArray = new Array()
  1085. for (var i = 0; i < start_code.length; i += 2) {
  1086. resultArray.push(start_code.substr(i, 2));
  1087. }
  1088. return resultArray;
  1089. },
  1090. /**
  1091. * ArrayBuffer转16进度字符串
  1092. */
  1093. ab2hex(buffer) {
  1094. const hexArr = Array.prototype.map.call(
  1095. new Uint8Array(buffer),
  1096. function(bit) {
  1097. // console.log('====================' + bit + '==========================')
  1098. return ('00' + bit.toString(16)).slice(-2)
  1099. }
  1100. )
  1101. return hexArr.join('');
  1102. },
  1103. /**
  1104. * 16进制转10进制
  1105. */
  1106. sixteen_to_ten(str) {
  1107. return parseInt(str, 16);
  1108. }
  1109. }
  1110. }
  1111. </script>
  1112. <style scoped lang="scss">
  1113. .content {
  1114. width: 100%;
  1115. height: 100%;
  1116. .container {
  1117. width: 100%;
  1118. padding: 20rpx 40rpx;
  1119. box-sizing: border-box;
  1120. .header {
  1121. display: flex;
  1122. justify-content: center;
  1123. position: relative;
  1124. height: 200rpx;
  1125. .scan {
  1126. display: flex;
  1127. flex-direction: column;
  1128. justify-content: center;
  1129. align-items: center;
  1130. height: 200rpx;
  1131. width: 30%;
  1132. .iconfont {
  1133. margin-bottom: 10rpx;
  1134. font-size: 100rpx;
  1135. color: $my-color-primary;
  1136. }
  1137. }
  1138. .scan_hover {
  1139. border-radius: 15rpx;
  1140. background-color: $my-color-btn-background;
  1141. }
  1142. .qinshihao {
  1143. display: flex;
  1144. align-items: center;
  1145. position: fixed;
  1146. top: 20rpx;
  1147. right: 30rpx;
  1148. font-size: 28rpx;
  1149. .icon-zhuye {
  1150. margin-right: 10rpx;
  1151. font-family: Microsoft YaHei-3970(82674968);
  1152. color: $my-color-primary;
  1153. }
  1154. .compustxt {
  1155. font-size: 18rpx;
  1156. }
  1157. }
  1158. }
  1159. .start_text {
  1160. .start_top,
  1161. .start_mid {
  1162. display: flex;
  1163. justify-content: space-between;
  1164. height: 100rpx;
  1165. align-items: center;
  1166. border-bottom: 2rpx #ccc solid;
  1167. .item_left {
  1168. display: flex;
  1169. align-items: center;
  1170. }
  1171. .start_mid_txt {
  1172. color: #333;
  1173. font-size: 30rpx;
  1174. }
  1175. .iconfont {
  1176. font-size: 50rpx;
  1177. color: $my-color-primary;
  1178. margin-right: 20rpx;
  1179. }
  1180. .start_payamount {
  1181. display: flex;
  1182. justify-content: flex-end;
  1183. align-items: center;
  1184. font-size: 38rpx;
  1185. padding: 10rpx;
  1186. font-family: Microsoft YaHei-3970(82674968);
  1187. color: #B2B2B2;
  1188. border-radius: 10rpx;
  1189. .icon-dayuhao {
  1190. margin: 0 0 0 20rpx;
  1191. font-size: 38rpx;
  1192. color: #B2B2B2;
  1193. }
  1194. }
  1195. }
  1196. .start_bot {
  1197. display: flex;
  1198. justify-content: flex-end;
  1199. height: 100rpx;
  1200. align-items: center;
  1201. .start_bot_txt {
  1202. margin-left: 20rpx;
  1203. color: #333;
  1204. font-size: 28rpx;
  1205. }
  1206. }
  1207. }
  1208. .start_up_process {
  1209. padding: 20rpx 0;
  1210. color: #007AFF;
  1211. }
  1212. .title {
  1213. height: 100rpx;
  1214. margin: 0 -40rpx;
  1215. padding: 0 40rpx;
  1216. line-height: 100rpx;
  1217. color: #808080;
  1218. font-size: 32rpx;
  1219. background: #f2f2f2;
  1220. }
  1221. .record-item {
  1222. width: 100%;
  1223. .uni-list-cell-db {
  1224. height: 100rpx;
  1225. line-height: 100rpx;
  1226. font-weight: bold;
  1227. .uni-input {
  1228. display: flex;
  1229. align-items: center;
  1230. text:nth-child(1) {
  1231. margin-right: 10rpx;
  1232. }
  1233. }
  1234. }
  1235. .item-list {
  1236. height: 80rpx;
  1237. display: flex;
  1238. align-items: center;
  1239. justify-content: space-between;
  1240. border-top: 2rpx #ccc solid;
  1241. .icon-shijian {
  1242. margin-right: 8rpx;
  1243. }
  1244. .icon-shuidi {
  1245. margin-right: 3rpx;
  1246. }
  1247. .item-list-txt {
  1248. font-size: 28rpx;
  1249. &:nth-of-type(2) {
  1250. color: #515151;
  1251. }
  1252. &:nth-of-type(3) {
  1253. color: #f76200;
  1254. font-size: 32rpx;
  1255. }
  1256. }
  1257. }
  1258. }
  1259. }
  1260. }
  1261. </style>