reshui.vue 31 KB

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