reshui.vue 32 KB

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