reshui.vue 33 KB

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