reshui.vue 35 KB

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