reshui.vue 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974
  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. <image :src="require('./images/icon-saoma.png')" style="width: 118rpx;" mode="aspectFit"></image>
  8. <text>扫码连接</text>
  9. </view>
  10. <view class="qinshihao">
  11. <!-- <text class="iconfont icon-zhuye"></text> -->
  12. <image :src="require('./images/icon-zhuye.png')" style="width: 28rpx; margin-right: 8rpx;" mode="aspectFit"></image>
  13. <text class="dormtxt">{{dorm_number || '宿舍未知'}}</text>
  14. </view>
  15. </view>
  16. <view class="start_text">
  17. <view class="start_top">
  18. <view class="item_left">
  19. <text class="iconfont icon-shuibiao"></text>
  20. <text>开启设备</text>
  21. </view>
  22. <view>
  23. <switch :color="colorPrimary" :checked="isChecked" @change="switchChange" />
  24. </view>
  25. </view>
  26. <view class="start_mid">
  27. <view class="item_left">
  28. <text class="iconfont icon-qian"></text>
  29. <!-- <navigator url="/pagesWater/reshuiDetails/reshuiDetails" open-type="navigate"
  30. class="start_mid_txt">¥{{amount}}</navigator> -->
  31. <navigator url="" open-type="navigate" class="start_mid_txt">¥{{amount}}</navigator>
  32. </view>
  33. <view class="start_payamount" @tap="chongzhi_yemian()" hover-class="scan_hover">
  34. <text>点击充值</text>
  35. <image :src="require('./images/icon-dayuhao.png')" style="width: 26rpx; margin-left: 10rpx; padding-top: 3rpx; opacity: 0.6;"
  36. mode="aspectFit">
  37. </image>
  38. <!-- <text class="iconfont icon-dayuhao"></text> -->
  39. </view>
  40. </view>
  41. <view class="start_bot">
  42. <button class="kongzhonglianjie" @click="open">空中连接</button>
  43. <text class="start_bot_txt">当前结算消费:¥{{use_amount}}</text>
  44. </view>
  45. </view>
  46. <view v-if="showRoom" style="width: 100%; height: 100%; ">
  47. <view style="display: flex; flex-wrap: wrap;">
  48. <button v-for="item in rooms" :key="item.id" @tap="clickRoom(item.id)" class="btn_room">{{item.room}}</button>
  49. </view>
  50. </view>
  51. <view class="start_up_process">
  52. <uni-steps :options="list1" :active-color="colorPrimary" :active="active" />
  53. </view>
  54. <!-- <view class="ad">
  55. <image src="../../static/images/ad_reshui.jpg" mode="aspectFill" class="ad_img" @tap="ad_redirect">
  56. </image>
  57. </view> -->
  58. <view class="title">
  59. 消费记录
  60. </view>
  61. <view class="record-item">
  62. <view class="uni-list-cell-db">
  63. <picker mode="date" :value="date" fields="month" :start="startDate" :end="endDate" @change="bindDateChange">
  64. <view class="uni-input">
  65. <text>{{date}}</text>
  66. <!-- <text class="iconfont icon-arrow-down"></text> -->
  67. <image :src="require('./images/icon-arrow-down.png')" style="width: 26rpx;" mode="aspectFit">
  68. </image>
  69. </view>
  70. </picker>
  71. </view>
  72. <view class="item-list" v-for="(item, i) in xiaofei_items" :key="i">
  73. <text class="item-list-txt"><text class="iconfont icon-shijian"></text>{{item.begin_time}}</text>
  74. <text class="item-list-txt"><text class="iconfont icon-shuidi"></text>{{numFilter(item.use_size, 'l')}} t</text>
  75. <text class="item-list-txt">{{numFilter(item.use_amount, 'm')}}</text>
  76. </view>
  77. </view>
  78. </view>
  79. <instructions v-if="flag" @parent_cancel="cancel" @parent_noshow='noshow'></instructions>
  80. <view class="operation-manual">
  81. <button class="default-btn floating-button-manual" @tap="showPopup = true"></button>
  82. </view>
  83. <view class="call">
  84. <button class="default-btn floating-button-call" @tap="makeCall"></button>
  85. </view>
  86. <Popup :showPopup="showPopup" @close="showPopup = false">
  87. <scroll-view class="article" scroll-y>
  88. <view class="desc-title">{{ article.title }}</view>
  89. <view v-for="(section, index) in article.sections" :key="index" class="section">
  90. <view v-if="section.red == true" class="section-title-red">{{ section.title }}</view>
  91. <view v-else class="section-title">{{ section.title }}</view>
  92. <view class="section-content">{{ section.content }}</view>
  93. </view>
  94. <view class="desc-footer"></view>
  95. </scroll-view>
  96. </Popup>
  97. </view>
  98. </template>
  99. <script>
  100. import instructions from '../../components/instructions.vue'
  101. import Popup from '@/components/Popup.vue';
  102. export default {
  103. /**
  104. * 声明组件
  105. */
  106. components: {
  107. instructions,
  108. Popup
  109. },
  110. data() {
  111. return {
  112. colorPrimary: getApp().globalData.primaryColor,
  113. date: this.$getDate({
  114. format: true
  115. }),
  116. startDate: this.$getDate('start_date'),
  117. endDate: this.$getDate('end_date'),
  118. flag: false, // 控制说明弹窗
  119. showRoom: false,
  120. isUserClose: false, // 是否是用户自己关闭
  121. isChecked: false,
  122. isConnected: false, // 是否连接上
  123. isScan: false,
  124. active: -1,
  125. list1: [{
  126. title: '连接设备'
  127. }, {
  128. title: '连设备服务'
  129. }, {
  130. title: '数据通讯'
  131. }, {
  132. title: '启动'
  133. }],
  134. devices: [], // 保存设备
  135. rooms: [], // 保存水表id及对应的宿舍号
  136. device_code: '', // 设备编号
  137. deviceId: '',
  138. serviceId: '',
  139. characteristicIdForWrite: '',
  140. characteristicIdForNotity: '',
  141. deviceIsNotify: false,
  142. deviceIsWrite: false,
  143. byte35: '', // 70 个字符
  144. byte35_tmp: '', // 70 个字符 临时变量
  145. start_code: '',
  146. // start_code: '3459A8C660F1C284E41003E80000120000',
  147. xiaofei_items: [], // 消费记录
  148. use_amount: 0.0.toFixed(2), // 结算金额
  149. amount: 0.0.toFixed(2), // 余额
  150. dorm_number: '', // 宿舍号
  151. stu_number: '', // 学号
  152. user_name: '',
  153. // compus: '', // 校区
  154. ceshi: 'code',
  155. changeDate: false, // 是否是手动选择日期
  156. get_Bluetooth_Bevice: false, // 自动获取蓝牙设备,并列表
  157. timer: -1,
  158. test: this.$store.state.test,
  159. showPopup: false,
  160. article: {
  161. title: "蓝牙卡控操作说明",
  162. sections: [{
  163. title: "蓝牙卡控使用方式为:",
  164. content: "  用微信【扫一扫】功能,扫描热水表上的二维码。",
  165. red: false
  166. },
  167. {
  168. title: "如遇连接热水表问题,请先检查:",
  169. content: "  1.蓝牙本身无故障,且手机蓝牙和GPS定位要开启,并要授权。\n" +
  170. "  2.手机系统要求出厂自带的,系统版本低于安卓4.3或刷过机的,可能搜索不到热水表。\n" +
  171. "  3.之前已经连接过热水表需正常退出,否则再次连接可能搜索不到热水表,需关闭蓝牙5秒钟后再尝试。\n" +
  172. "  4.热水表有人在使用,热水表肯定连接不了。\n" +
  173. "  5.用微信分身,连接热水表会存在意想不到的问题。\n" +
  174. "  6.在连接热水表前,请断开如蓝牙耳机、蓝牙音像等其他蓝牙设备。",
  175. red: false
  176. },
  177. {
  178. title: "如以上检查没问题,部分手机请检查以下权限是否打开(以下路径不能保证绝对正确,不同手机品牌和版本号会有区别,可自行百度或联系客服):",
  179. content: "",
  180. red: true
  181. },
  182. {
  183. title: "华为手机:",
  184. content: "  设置→安全和隐私→定位服务→打开“访问我的位置信息”。\n" +
  185. "  设置→应用和通知→权限管理→微信→打开“您的位置”。",
  186. red: false
  187. },
  188. {
  189. title: "小米手机:",
  190. content: "  设置→更多设置→系统安全→位置信息→开启“位置服务”。\n" +
  191. "  设置→授权管理→应用授权管理→微信→开启“定位”。",
  192. red: false
  193. },
  194. {
  195. title: "苹果手机:",
  196. content: "  设置→隐私→打开“定位服务”。\n" +
  197. "  设置→微信→位置→使用应用期间。",
  198. red: false
  199. },
  200. {
  201. title: "OPPO手机:",
  202. content: "  设置→GPS定位→开启“允许访问我的位置信息。\n" +
  203. "  设置→权限隐私→按应用管理权限→微信→授权。",
  204. red: false
  205. },
  206. {
  207. title: "VIVO手机:",
  208. content: "  设置→定位服务→开启。\n" +
  209. "  设置→更多设置→权限管理→微信→定位手机选择“允许”。",
  210. red: false
  211. },
  212. {
  213. title: "三星手机:",
  214. content: "  设置→连接→打开定位服务。\n" +
  215. "  设置→应用程序→微信→权限→开启位置信息。",
  216. red: false
  217. },
  218. {
  219. title: "魅族手机:",
  220. content: "  设置→定位服务→开启:\n" +
  221. "  设置→应用管理→选择已安装→微信→权限管理→获取定位(允许)。",
  222. red: false
  223. },
  224. {
  225. title: "多卡号(一个人有2个以上微信身份卡号)扣款问题:",
  226. content: "  遇到多卡,扣款会从第1次绑定的卡中扣款,如想从指定的卡中扣款,请先删除微信小程序(微信界面下拉,长按本小程序并拖动删除),再用指定的卡进入即可。",
  227. red: true
  228. }
  229. ]
  230. }
  231. }
  232. },
  233. onLoad(options) {
  234. // console.log(options);
  235. // 系统正在维护...
  236. if (this.test == 'weihuzhong') {
  237. uni.redirectTo({
  238. url: '../../pages/index/index'
  239. })
  240. return;
  241. }
  242. // 向用户发起授权请求
  243. uni.authorize({
  244. scope: 'scope.userLocation',
  245. success(res) {
  246. uni.getLocation({
  247. success: (loc) => {
  248. // console.log(loc);
  249. uni.showToast({
  250. icon: 'success',
  251. title: '已获定位信息'
  252. })
  253. },
  254. fail: (err) => {
  255. uni.showToast({
  256. icon: 'none',
  257. title: '获取定位失败,请手动开启定位!'
  258. })
  259. }
  260. })
  261. },
  262. fail: (err) => {
  263. // console.log(err);
  264. uni.showToast({
  265. icon: 'none',
  266. title: '请手动设置定位授权!'
  267. })
  268. }
  269. })
  270. if (typeof(options) != 'undefined' && typeof(options) != '' && JSON.stringify(options) == '{}') {
  271. // 获取基本信息
  272. this.get_base_info(options, 'onLoad')
  273. } else if (typeof(options.from) != 'undefined' && options.from != 0) {
  274. // 获取基本信息
  275. this.get_base_info(options, 'onLoad')
  276. // 获取设备码
  277. this.device_code = decodeURIComponent(options.q).split('=')[1]
  278. }
  279. const value = uni.getStorageSync('instruction_noshow');
  280. if (!(value == true)) {
  281. this.flag = true
  282. }
  283. },
  284. onShow() {
  285. let _this = this;
  286. // 充值后更新金额
  287. this.amount = this.$store.state.reshui_amount
  288. // 刷新充值后的金额
  289. uni.$once('update_reshui_amount', (data) => {
  290. this.get_base_info('options', 'onShow');
  291. });
  292. let back = uni.getStorageSync('navigateBack');
  293. if (back == 1)
  294. this.get_base_info('options', 'navigateBack');
  295. if (this.timer > -1) {
  296. clearInterval(this.timer)
  297. }
  298. this.timer = setInterval(() => {
  299. uni.openBluetoothAdapter({
  300. success: (res) => { // 已打开
  301. uni.getBluetoothAdapterState({ // 蓝牙的匹配状态
  302. success: (res1) => {
  303. // console.log(res1);
  304. },
  305. fail: (error) => {
  306. // console.log(error)
  307. uni.showToast({
  308. title: '请开启手机蓝牙!',
  309. icon: 'none',
  310. duration: 2000
  311. });
  312. }
  313. });
  314. },
  315. fail: (err) => { // 未打开
  316. // console.log(err)
  317. if (err.errCode == 10001) {
  318. uni.showToast({
  319. title: '请开启手机蓝牙 或 授权蓝牙权限!',
  320. icon: 'none',
  321. duration: 2000
  322. });
  323. setTimeout(() => {
  324. _this.myInit();
  325. }, 100);
  326. } else {
  327. uni.showToast({
  328. icon: 'none',
  329. title: err.errMsg,
  330. duration: 2000
  331. });
  332. }
  333. }
  334. });
  335. }, 3000);
  336. },
  337. onHide() {
  338. clearInterval(this.timer)
  339. },
  340. onUnload() {
  341. clearInterval(this.timer)
  342. },
  343. methods: {
  344. /**
  345. * 拨号
  346. */
  347. makeCall() {
  348. uni.makePhoneCall({
  349. phoneNumber: '18312075670' // 替换为您想要拨打的电话号码
  350. });
  351. },
  352. /**
  353. * 单击连接热水表
  354. */
  355. clickRoom(e) {
  356. try {
  357. if (this.judgeBalance('clickRoom') === false) {
  358. return
  359. } else {
  360. this.device_code = 'K' + e
  361. for (var i = 0, len = this.devices.length; i < len; i++) {
  362. if (this.devices[i].dname == e) {
  363. this.deviceId = this.devices[i].did
  364. break
  365. }
  366. }
  367. // 开始连接匹配的蓝牙设备
  368. this.createBLEConnection();
  369. }
  370. } catch (e) {
  371. console.log('单击连接热水表:' + e.message);
  372. }
  373. },
  374. /**
  375. * 空中连接热水表
  376. */
  377. open() {
  378. if (this.judgeBalance('clickRoom') === false) {
  379. return
  380. } else {
  381. this.setInit()
  382. uni.showLoading({
  383. title: '水表搜索中…',
  384. mask: true,
  385. success: (res) => {
  386. // 获取蓝牙设备,并列表
  387. this.openBluetoothAdapter();
  388. this.showRoom = true
  389. }
  390. })
  391. setTimeout(function() {
  392. uni.hideLoading();
  393. }, 3000);
  394. }
  395. },
  396. /**
  397. * 跳转到广告ad页
  398. */
  399. ad_redirect() {
  400. // uni.navigateTo({
  401. // url: 'ad_reshui'
  402. // })
  403. },
  404. /**
  405. * 保留小数点数值后两位,尾数四舍五入
  406. * @param {Object} value
  407. */
  408. numFilter(value, param) {
  409. var v = parseFloat(value);
  410. if (param == 'l')
  411. return v.toFixed(2);
  412. else
  413. // 截取当前数据到小数点后两位
  414. return v.toFixed(2);
  415. },
  416. /**
  417. * 获取基本信息
  418. */
  419. get_base_info(options, param) {
  420. // console.log(options);
  421. try {
  422. // 获取学号
  423. this.stu_number = this.$store.state.userInfo.card_number
  424. if (this.stu_number == '' || typeof this.stu_number == 'undefined') {
  425. const userinfo = uni.getStorageSync('userinfo_storage_key')
  426. if (userinfo) {
  427. this.stu_number = userinfo.card_number
  428. } else {
  429. uni.navigateTo({
  430. url: '../../pages/index/index?from=' + options.from
  431. })
  432. uni.showToast({
  433. icon: 'none',
  434. title: '学号为空,请进行授权',
  435. duration: 3000
  436. });
  437. return;
  438. }
  439. }
  440. } catch (e) {
  441. console.log('获取基本信息:' + e.message);
  442. }
  443. if (param == 'onShow') {
  444. if (this.$store.state.payInfo.from == 'reshui_pay') {
  445. uni.showToast({
  446. title: this.$store.state.payInfo.resultMsg,
  447. icon: 'success',
  448. success: () => {
  449. this.$store.state.payInfo.resultMsg = ''
  450. this.$store.state.payInfo.from = ''
  451. }
  452. })
  453. }
  454. }
  455. // 获取code
  456. this.getCode(options, param);
  457. },
  458. /**
  459. * 获得code
  460. */
  461. getCode(options, param) {
  462. uni.login({
  463. success: (res) => {
  464. // console.log('getCode', res);
  465. if (res.code) {
  466. // 请求服务器,获得openid
  467. this.getOpenId(options, res.code, param)
  468. } else {
  469. uni.showToast({
  470. title: res.errMsg,
  471. icon: 'none'
  472. });
  473. }
  474. }
  475. })
  476. },
  477. /**
  478. * 请求服务器,获得openid
  479. */
  480. async getOpenId(options, param_code, param) {
  481. var _this = this;
  482. const res = await this.$myRequest({
  483. host: _this.ceshi,
  484. url: '/HotWaters/wpget_stu.action',
  485. method: 'POST',
  486. header: {
  487. 'content-type': 'application/x-www-form-urlencoded'
  488. },
  489. data: {
  490. code: param_code,
  491. card_number: _this.stu_number
  492. }
  493. })
  494. // console.log(res);
  495. if (res.data.mess == '返回成功') {
  496. // stu_number是否存在
  497. let cardNumber = res.data.info[0].card_number
  498. if (typeof cardNumber != 'undefined' && cardNumber != '' && JSON.stringify(cardNumber) != '{}') {
  499. // 学号 和 姓名
  500. _this.stu_number = res.data.info[0].stu_number
  501. _this.user_name = res.data.info[0].user_name
  502. // 剩余金额
  503. _this.amount = res.data.info[0].balance.toFixed(2)
  504. // if (_this.amount > 0)
  505. _this.$store.state.reshui_amount = _this.amount
  506. // 组合楼栋宿舍号
  507. _this.dorm_number = res.data.info[0].build + res.data.info[0].dom
  508. // 校区
  509. // _this.compus = res.data.info[0].campus
  510. if (param == 'onLoad' && typeof options.from != 'undefined' && options.from != 0) {
  511. // 初始化蓝牙
  512. _this.openBluetoothAdapter()
  513. }
  514. // 请求选定的月份消费记录
  515. _this.request_consumption_records()
  516. } else {
  517. uni.showToast({
  518. title: '未获取到学号!',
  519. icon: 'success'
  520. });
  521. uni.navigateTo({
  522. url: '../../pages/index/index?from=' + options.from
  523. })
  524. }
  525. } else if (res.data.mess == '未查询到用户信息') {
  526. // 数据库中 未查询到用户信息,就清除本地存储
  527. // uni.removeStorageSync('userinfo_storage_key');
  528. uni.showToast({
  529. title: '获取用户信息失败!'
  530. })
  531. if (options && typeof options.from != 'undefined' && typeof options.from != '') {
  532. uni.navigateTo({
  533. url: '../../pages/index/index?from=' + options.from
  534. })
  535. } else {
  536. uni.redirectTo({
  537. url: '../../pages/index/index?from=0'
  538. });
  539. }
  540. }
  541. },
  542. /**
  543. * 选择消费年月
  544. */
  545. bindDateChange(e) {
  546. this.date = e.detail.value
  547. this.changeDate = true
  548. // 请求选定的月份消费记录
  549. this.request_consumption_records()
  550. },
  551. /**
  552. * 使用说明组件取消
  553. */
  554. cancel(param) {
  555. this.flag = param
  556. },
  557. /**
  558. * 使用说明组件不再出现
  559. */
  560. noshow(param) {
  561. try {
  562. if (param) {
  563. uni.setStorageSync("instruction_noshow", true)
  564. } else {
  565. uni.removeStorageSync("instruction_noshow")
  566. }
  567. } catch (e) {
  568. console.log('使用说明组件不再出现:' + e.message);
  569. }
  570. },
  571. /**
  572. * 请求选定的月份消费记录
  573. */
  574. async request_consumption_records() {
  575. try {
  576. if (this.stu_number != '' && typeof(this.stu_number) != 'undefined') {
  577. const res = await this.$myRequest({
  578. host: this.ceshi,
  579. url: '/HotWaters/wpqueryConsume.action',
  580. method: 'POST',
  581. header: {
  582. 'content-type': 'application/x-www-form-urlencoded'
  583. },
  584. data: {
  585. stu_number: this.stu_number,
  586. begin_time: this.date
  587. }
  588. });
  589. // console.log(res);
  590. if (res.data.mess == '返回成功') {
  591. this.xiaofei_items = []
  592. console.log(res.data.data);
  593. // 消费记录
  594. let items = res.data.data
  595. for (var i = 0; i < items.length; i++) {
  596. this.xiaofei_items.push(items[i])
  597. }
  598. } else {
  599. if (this.changeDate) {
  600. uni.showToast({
  601. icon: 'success',
  602. title: res.data.mess,
  603. success: () => {
  604. this.changeDate = false
  605. }
  606. })
  607. if (res.data.mess == '本月无消费记录') {
  608. this.xiaofei_items = []
  609. }
  610. }
  611. }
  612. } else {
  613. uni.showToast({
  614. icon: 'success',
  615. title: '未获得学号'
  616. })
  617. }
  618. } catch (e) {
  619. console.log('请求选定的月份消费记录:' + e.message);
  620. }
  621. },
  622. /**
  623. * 控制进度条显示
  624. * @param {Object} active_value
  625. */
  626. process_control(active_value) {
  627. try {
  628. if (active_value <= this.list1.length - 1) {
  629. this.active = active_value
  630. }
  631. } catch (e) {
  632. console.log('控制进度条显示:' + e.message);
  633. }
  634. },
  635. /**
  636. * 开始或停止用水
  637. */
  638. switchChange(e) {
  639. let _this = this;
  640. this.isChecked = e.target.value
  641. if (this.isChecked) {
  642. if (this.deviceId && this.serviceId && this.devices.length != 0) {
  643. uni.showModal({
  644. title: '提示',
  645. content: '确定要启动设备?',
  646. success: (res) => {
  647. if (res.confirm) {
  648. this.isChecked = true;
  649. // 开始连接匹配的蓝牙设备
  650. setTimeout(async () => {
  651. // 请求获取设备启动码
  652. _this.request_device_start_code()
  653. }, 200);
  654. } else if (res.cancel) {
  655. setTimeout(() => {
  656. _this.isChecked = false;
  657. }, 100);
  658. }
  659. }
  660. })
  661. } else {
  662. setTimeout(() => {
  663. this.isChecked = false
  664. }, 200)
  665. uni.showToast({
  666. icon: 'none',
  667. title: '先【扫码连接】,后【开启设备】',
  668. duration: 1500
  669. })
  670. }
  671. } else {
  672. if (!(this.deviceId && this.serviceId)) {
  673. uni.showToast({
  674. icon: 'none',
  675. title: '与设备连接中断!请在设备上手动停止即可!',
  676. duration: 3000
  677. })
  678. setTimeout(() => {
  679. this.setInit()
  680. }, 300)
  681. return
  682. }
  683. uni.showModal({
  684. title: '提示',
  685. content: '确定要关闭设备?',
  686. success: (res) => {
  687. if (res.confirm) {
  688. if (this.deviceId && this.serviceId) {
  689. this.isChecked = false
  690. this.send('stop')
  691. } else {
  692. uni.showToast({
  693. icon: 'none',
  694. title: '与设备连接中断!请在设备上手动停止即可!',
  695. duration: 3000
  696. })
  697. setTimeout(() => {
  698. // this.isChecked = true;
  699. this.setInit()
  700. }, 300)
  701. }
  702. } else if (res.cancel) {
  703. setTimeout(() => {
  704. this.isChecked = true
  705. }, 300)
  706. }
  707. }
  708. })
  709. }
  710. },
  711. /**
  712. * 跳转到充值页面
  713. */
  714. chongzhi_yemian() {
  715. //在起始页面跳转到recharge.vue页面并传递参数
  716. uni.navigateTo({
  717. url: '../recharge/recharge'
  718. });
  719. },
  720. /**
  721. * 判断余额是否充足
  722. */
  723. judgeBalance(param) {
  724. var limit_amount = parseFloat(this.$store.state.amount)
  725. var amount = parseFloat(this.amount);
  726. // 启动最低金额限制
  727. if (amount < limit_amount) {
  728. uni.showModal({
  729. title: '提示',
  730. content: '当前余额不足¥' + limit_amount + '元',
  731. cancelText: '我知道了',
  732. confirmText: '我要充值',
  733. success: (res) => {
  734. if (res.confirm) {
  735. // 跳转充值页面
  736. this.chongzhi_yemian()
  737. // return false
  738. // } else if (res.cancel) {
  739. // return false
  740. }
  741. }
  742. })
  743. return false
  744. }
  745. // 已经启动扫描连接设备中
  746. if (this.isScan) {
  747. uni.showToast({
  748. icon: 'none',
  749. title: '正在扫描,请耐心等待',
  750. })
  751. return false
  752. }
  753. // 已经启动设备,不启动扫描
  754. if (this.isChecked) {
  755. let info_str = ''
  756. if (param == 'scan') {
  757. info_str = '设备已启动,无需重新扫码'
  758. } else {
  759. info_str = '设备已启动,无需重新启动'
  760. }
  761. uni.showToast({
  762. icon: 'none',
  763. title: info_str,
  764. })
  765. return false
  766. }
  767. // console.log(this.deviceId);
  768. // 设备已连接
  769. if (this.deviceId) {
  770. uni.showModal({
  771. title: '提示',
  772. content: '设备已经连接,需要重新连接吗?',
  773. success: res => {
  774. if (res.confirm) {
  775. // 关闭连接
  776. this.closeBLEConnection()
  777. if (param == 'scan') {
  778. // 扫描
  779. this.scan_qr()
  780. } else {
  781. // cancel
  782. }
  783. }
  784. }
  785. })
  786. return false
  787. }
  788. },
  789. /**
  790. * 111111111111111111111111111111
  791. * 扫码连接设备
  792. */
  793. scan() {
  794. if (this.judgeBalance('scan') == false) {
  795. return;
  796. }
  797. this.scan_qr();
  798. },
  799. /**
  800. * 启动摄像头,扫描二维码
  801. */
  802. scan_qr() {
  803. this.get_Bluetooth_Bevice = true
  804. uni.scanCode({
  805. onlyFromCamera: true,
  806. success: (res) => {
  807. this.isScan = true
  808. // console.log(res)
  809. // charSet: "utf-8"
  810. // codeVersion: 3
  811. // errMsg: "scanCode:ok"
  812. // rawData: "aHR0cDovL3Rvb2xzLjlrYnMuY29tLz93eG1pbmljb2RlPUtCNTgwMDAyNjUy"
  813. // result: "http://tools.9kbs.com/?wxminicode=KB580002652"
  814. // scanType: "QR_CODE"
  815. this.device_code = res.result.split('=')[1]
  816. this.isScan = false;
  817. if (this.device_code === '') {
  818. uni.showToast({
  819. icon: 'success',
  820. title: '获取设备码失败',
  821. duration: 3000
  822. });
  823. } else {
  824. // 初始化蓝牙
  825. this.openBluetoothAdapter()
  826. }
  827. }
  828. })
  829. },
  830. /**
  831. * 22222222222222222222222222222
  832. * 初始化蓝牙
  833. */
  834. openBluetoothAdapter() {
  835. let _this = this;
  836. uni.openBluetoothAdapter({
  837. success: (res) => { // 已打开
  838. uni.getBluetoothAdapterState({ // 蓝牙的匹配状态
  839. success: (res1) => {
  840. // console.log(res1, '“本机设备的蓝牙已打开”');
  841. // 本机设备的蓝牙已打开
  842. // 开始搜索蓝牙设备
  843. if (this.deviceId == '' && this.serviceId == '')
  844. this.startBluetoothDeviceDiscovery()
  845. },
  846. fail: (error) => {
  847. // console.log(error)
  848. uni.showModal({
  849. content: '请开启手机蓝牙!',
  850. showCancel: false
  851. });
  852. this.isScan = false;
  853. }
  854. });
  855. },
  856. fail: (err) => { // 未打开
  857. // console.log(err)
  858. this.isScan = false;
  859. if (err.errCode == 10001) {
  860. uni.showModal({
  861. content: '请授权手机蓝牙!',
  862. showCancel: false
  863. });
  864. } else {
  865. uni.showToast({
  866. icon: 'none',
  867. title: err.errMsg,
  868. duration: 2000
  869. })
  870. }
  871. }
  872. });
  873. },
  874. /**
  875. * 333333333333333333333333333
  876. * 开始搜索蓝牙设备
  877. */
  878. startBluetoothDeviceDiscovery() {
  879. uni.startBluetoothDevicesDiscovery({ // 开始搜寻附近的蓝牙外围设备
  880. // services: ['FEE7'], // 要搜索但蓝牙设备主 service 的 uuid 列表
  881. allowDuplicatesKey: true,
  882. success: (res) => {
  883. if (this.get_Bluetooth_Bevice == true)
  884. this.process_control(0)
  885. this.onBluetoothDeviceFound() // 监听搜索到新设备 同时进行
  886. setTimeout(() => {
  887. if (this.isScan) {
  888. uni.showModal({
  889. title: '温馨提示',
  890. content: '请确认已开启【蓝牙授权】或【定位授权】!',
  891. confirmText: '我已开启',
  892. cancelText: '取消',
  893. success: (res) => {
  894. // console.log(res);
  895. if (res.confirm) {
  896. } else if (res.cancel) {
  897. switch (uni.getSystemInfoSync().platform) {
  898. case 'android':
  899. // 打开设置
  900. break;
  901. case 'ios':
  902. // 打开设置
  903. break;
  904. default:
  905. // console.log('运行在开发者工具上')
  906. break;
  907. }
  908. }
  909. }
  910. })
  911. }
  912. }, 6000)
  913. },
  914. fail: err => {
  915. // console.log(err, '错误信息');
  916. uni.showToast({
  917. icon: 'none',
  918. title: err.errMsg,
  919. duration: 2000
  920. })
  921. this.isScan = false
  922. }
  923. })
  924. },
  925. stopBluetoothDevicesDiscovery() { //停止搜索
  926. uni.stopBluetoothDevicesDiscovery({
  927. success: (res) => {
  928. // console.log(res, '停止蓝牙搜索')
  929. // uni.showToast({
  930. // title: '停止蓝牙搜索',
  931. // duration: 2000
  932. // })
  933. if (this.get_Bluetooth_Bevice == true || this.device_code) {
  934. this.createBLEConnection()
  935. } else {
  936. this.get_room()
  937. setTimeout(() => {
  938. this.get_Bluetooth_Bevice = true
  939. }, 2000)
  940. }
  941. },
  942. fail: err => {
  943. // console.log('停止搜索蓝牙设备失败,错误码:' + err.errCode);
  944. this.isScan = false
  945. uni.showToast({
  946. title: err.errCode,
  947. duration: 2000
  948. })
  949. }
  950. })
  951. },
  952. /**
  953. * 444444444444444444444444444444444
  954. * 发现外围设备
  955. */
  956. onBluetoothDeviceFound() {
  957. uni.onBluetoothDeviceFound((res) => {
  958. let dname = ''
  959. res.devices.forEach(device => { // 筛选找到的蓝牙中对名称匹配
  960. try {
  961. // 过滤掉没有名字的设备
  962. if (!device.name && !device.localName) {
  963. return
  964. }
  965. // 把搜索到的设备存储起来,如有需要可以在页面上展示
  966. dname = device.name.substr(1)
  967. let d = {
  968. did: device.deviceId,
  969. dname: dname
  970. }
  971. if (dname.length == 10 && JSON.stringify(this.devices).indexOf(JSON.stringify(
  972. d)) === -1) {
  973. this.devices.push(d)
  974. }
  975. if (device.name == this.device_code) {
  976. this.process_control(0);
  977. //data里面建立一个deviceId、device_code,存储起来
  978. // this.device_code = device.name;
  979. this.deviceId = device.deviceId
  980. // 放此位置,苹果手机兼容
  981. this.stopBluetoothDevicesDiscovery()
  982. return
  983. }
  984. setTimeout(() => {
  985. this.stopBluetoothDevicesDiscovery()
  986. }, 2000)
  987. } catch (e) {
  988. // console.log('发现外围设备:' + e.message);
  989. }
  990. })
  991. });
  992. },
  993. // 获取水表对应的宿舍号
  994. async get_room() {
  995. try {
  996. if (this.devices.length > 0) {
  997. let devices_str = []
  998. for (var i = 0, len = this.devices.length; i < len; i++) {
  999. devices_str.push(this.devices[i].dname)
  1000. }
  1001. const res = await this.$myRequest({
  1002. host: this.ceshi,
  1003. url: '/HotWaters/waterqueryDom.action',
  1004. method: 'POST',
  1005. header: {
  1006. 'content-type': 'application/x-www-form-urlencoded'
  1007. },
  1008. data: {
  1009. user_id: devices_str.join(',')
  1010. }
  1011. });
  1012. if (res.data.msg == '获取成功') {
  1013. // 消费记录
  1014. let items = res.data.data
  1015. if (typeof items === 'undefined') {
  1016. uni.showToast({
  1017. title: '获取宿舍号为空',
  1018. duration: 2000
  1019. })
  1020. return
  1021. }
  1022. for (var i = 0; i < items.length; i++) {
  1023. let item = {
  1024. id: items[i].user_id,
  1025. room: items[i].dom
  1026. }
  1027. if (JSON.stringify(this.rooms).indexOf(JSON.stringify(item)) === -1) {
  1028. this.rooms.push(item); // 进行动态的操作
  1029. }
  1030. }
  1031. } else {
  1032. if (this.changeDate) {
  1033. uni.showToast({
  1034. icon: 'success',
  1035. title: res.data.mess,
  1036. success: () => {
  1037. this.changeDate = false
  1038. }
  1039. })
  1040. if (res.data.mess == '本月无消费记录') {
  1041. this.xiaofei_items = []
  1042. }
  1043. }
  1044. }
  1045. }
  1046. } catch (e) {
  1047. console.log('获取水表对应的宿舍号:' + e.message);
  1048. }
  1049. },
  1050. /**
  1051. * 555555555555555555555555555555555
  1052. * 连接蓝牙设备
  1053. */
  1054. createBLEConnection() {
  1055. //连接蓝牙
  1056. uni.createBLEConnection({
  1057. deviceId: this.deviceId,
  1058. timeout: 6000,
  1059. success: (res) => {
  1060. // console.log(res)
  1061. this.connect_BLE()
  1062. },
  1063. fail: (err) => {
  1064. // console.log(err)
  1065. this.isScan = false
  1066. if (err.errMsg.indexOf('already connect') != -1) {
  1067. this.connect_BLE()
  1068. } else {
  1069. uni.showToast({
  1070. title: '水表蓝牙连接被占用!',
  1071. icon: 'none',
  1072. duration: 3000,
  1073. success: (res1) => {
  1074. this.setInit()
  1075. }
  1076. })
  1077. }
  1078. }
  1079. })
  1080. },
  1081. connect_BLE() {
  1082. if (this.isConnected === false) {
  1083. this.isConnected = true
  1084. // 获取蓝牙设备所有服务(service)
  1085. this.getBLEDeviceServices()
  1086. }
  1087. },
  1088. /**
  1089. * 6666666666666666666666666666666666
  1090. * 获取蓝牙的所有服务
  1091. */
  1092. getBLEDeviceServices() {
  1093. uni.getBLEDeviceServices({
  1094. deviceId: this.deviceId,
  1095. success: (res) => {
  1096. // console.log(res)
  1097. // res1.services.forEach((item) => {
  1098. // this.getBLEDeviceCharacteristics()
  1099. // })
  1100. this.serviceId = res.services[1].uuid
  1101. this.process_control(1);
  1102. this.getBLEDeviceCharacteristics()
  1103. },
  1104. fail: (err) => {
  1105. // console.log(err)
  1106. this.isScan = false
  1107. uni.showToast({
  1108. title: '获取服务失败',
  1109. icon: 'none',
  1110. duration: 3000
  1111. });
  1112. this.setInit();
  1113. return;
  1114. }
  1115. });
  1116. },
  1117. /**
  1118. * 777777777777777777777777777777777
  1119. * 获取蓝牙特征
  1120. */
  1121. getBLEDeviceCharacteristics() {
  1122. // 获取蓝牙设备某个服务中所有特征值(characteristic)
  1123. uni.getBLEDeviceCharacteristics({
  1124. deviceId: this.deviceId,
  1125. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  1126. serviceId: this.serviceId,
  1127. success: (res) => {
  1128. // console.log(res)
  1129. this.characteristicIdForWrite = res.characteristics[0].uuid
  1130. this.characteristicIdForNotity = res.characteristics[1].uuid
  1131. this.deviceIsWrite = res.characteristics[0].properties.write
  1132. this.deviceIsNotify = res.characteristics[1].properties.notify
  1133. // console.log(this.characteristicIdForWrite, this.characteristicIdForNotity)
  1134. // 确保对应服务id下的特征值id具备监听数据变化的特性
  1135. if (res.characteristics[1].properties.notify) {
  1136. this.notifyBLECharacteristicValueChange()
  1137. }
  1138. },
  1139. fail: (err) => {
  1140. this.isScan = false
  1141. // console.error('获取蓝牙设备特征值失败(getBLEDeviceCharacteristics)', err)
  1142. }
  1143. });
  1144. },
  1145. /**
  1146. * 888888888888888888888888888888888888
  1147. * 启用 notify 功能
  1148. */
  1149. notifyBLECharacteristicValueChange() {
  1150. uni.notifyBLECharacteristicValueChange({
  1151. state: true, // 启用 notify 功能
  1152. deviceId: this.deviceId,
  1153. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  1154. serviceId: this.serviceId,
  1155. // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  1156. characteristicId: this.characteristicIdForNotity,
  1157. success: (res) => {
  1158. this.process_control(2)
  1159. uni.showToast({
  1160. icon: 'none',
  1161. title: '要启动水表,请手动点击【开启设备】右侧开关!',
  1162. duration: 5000
  1163. })
  1164. // uni.showModal({
  1165. // title: '提示',
  1166. // content: '要启动水表,请手动点击【开启设备】右侧开关!',
  1167. // showCancel: false,
  1168. // confirmText: '好,知道'
  1169. // })
  1170. this.isScan = false
  1171. // console.log('通知启用(notifyBLECharacteristicValueChange)',res);
  1172. this.onBLECharacteristicValueChange()
  1173. setTimeout(() => {
  1174. // 发送 AAABAC,采集数据(会返回 35字节 的数据)
  1175. this.send('')
  1176. }, 800)
  1177. // // 请求设备信息
  1178. // this.request_device_info()
  1179. // // 显示设备启动授权金额
  1180. // this.display_device_startup_amount()
  1181. },
  1182. fail: (err) => {
  1183. // console.log(err)
  1184. this.isScan = false
  1185. uni.showModal({
  1186. content: err.errMsg,
  1187. showCancel: false
  1188. })
  1189. // 初始化
  1190. this.setInit()
  1191. }
  1192. })
  1193. },
  1194. /**
  1195. * 999999999999999999999999999999999
  1196. * 监听低功耗蓝牙设备的特征值变化
  1197. */
  1198. onBLECharacteristicValueChange() {
  1199. uni.onBLECharacteristicValueChange((characteristic) => {
  1200. let condition = this.ab2hex(characteristic.value)
  1201. // console.log(condition)
  1202. if (condition.length > 2) {
  1203. this.byte35_tmp += condition
  1204. if (this.byte35_tmp.length > 40) {
  1205. this.byte35 = this.byte35_tmp
  1206. this.byte35_tmp = ''
  1207. if (this.byte35.startsWith('ad')) {
  1208. // console.log(this.byte35, '结束用水成功')
  1209. // 关闭蓝牙连接
  1210. this.closeBLEConnection()
  1211. // 请求结算
  1212. this.request_pay()
  1213. } else {
  1214. // 发送 AAABAC,采集数据(会返回 35字节 的数据)
  1215. // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
  1216. // uni.showToast({
  1217. // icon: 'success',
  1218. // title: '采集数据成功!',
  1219. // duration: 2000
  1220. // })
  1221. // console.log(this.byte35, '采集上一条消费数据')
  1222. }
  1223. }
  1224. }
  1225. switch (condition) {
  1226. case 'bb':
  1227. // uni.showToast({
  1228. // title: '设备启动成功',
  1229. // duration: 3000,
  1230. // icon: 'none'
  1231. // })
  1232. this.isChecked = true
  1233. break
  1234. case 'bc':
  1235. uni.showToast({
  1236. title: '启动失败,设备检验启动代码不通过',
  1237. duration: 3000,
  1238. icon: 'none'
  1239. })
  1240. break
  1241. case 'aa':
  1242. uni.showToast({
  1243. title: '设备正在使用',
  1244. duration: 3000,
  1245. icon: 'none'
  1246. })
  1247. break
  1248. case 'ab':
  1249. uni.showToast({
  1250. title: '结束失败,设备对结束代码检验失败',
  1251. duration: 3000,
  1252. icon: 'none'
  1253. })
  1254. break
  1255. default:
  1256. break
  1257. }
  1258. })
  1259. },
  1260. /**
  1261. * 关闭连接
  1262. */
  1263. closeBLEConnection() {
  1264. if (this.deviceId) {
  1265. uni.closeBLEConnection({
  1266. deviceId: this.deviceId,
  1267. success: () => {
  1268. // 关闭蓝牙模块
  1269. this.closeBluetoothAdapter()
  1270. },
  1271. fail: (err) => {
  1272. // this.setInit()
  1273. // uni.showToast({
  1274. // icon: 'none',
  1275. // title: err.errMsg,
  1276. // duration: 3000,
  1277. // success: (res1) => {
  1278. // this.setInit()
  1279. // }
  1280. // })
  1281. }
  1282. });
  1283. } else {
  1284. // 关闭蓝牙模块
  1285. this.closeBluetoothAdapter()
  1286. }
  1287. },
  1288. closeBluetoothAdapter() {
  1289. // 关闭蓝牙模块
  1290. uni.closeBluetoothAdapter({
  1291. success: () => {
  1292. this.myInit();
  1293. if (this.isUserClose == false) { // 设备端结束用水
  1294. if (this.isChecked) {
  1295. // console.log('设备端已结束用水,蓝牙断开成功')
  1296. uni.showToast({
  1297. icon: 'none',
  1298. title: '设备端已结束用水,蓝牙断开成功',
  1299. duration: 3000
  1300. });
  1301. } else {
  1302. // console.log('用户重启扫描,蓝牙断开成功')
  1303. uni.showToast({
  1304. icon: 'none',
  1305. title: '用户重启扫描,蓝牙断开成功',
  1306. duration: 3000
  1307. });
  1308. }
  1309. } else { // 手机移动端结束用水
  1310. // console.log('APP端已结束用水,蓝牙断开成功')
  1311. uni.showToast({
  1312. icon: 'none',
  1313. title: 'APP端已结束用水,蓝牙断开成功',
  1314. duration: 3000
  1315. });
  1316. }
  1317. }
  1318. })
  1319. },
  1320. setInit() {
  1321. this.closeBLEConnection();
  1322. this.myInit();
  1323. },
  1324. myInit() {
  1325. setTimeout(() => {
  1326. this.isUserClose = false;
  1327. this.isChecked = false;
  1328. this.isConnected = false;
  1329. this.isScan = false;
  1330. this.active = -1;
  1331. this.device_code = ''; // 设备编号
  1332. this.devices = []; // 保存设备, 加了【空中连接】,不需要置为空。
  1333. this.deviceId = ''; // 加了【空中连接】,不需要置为空。
  1334. this.serviceId = '';
  1335. this.characteristicIdForWrite = '';
  1336. this.characteristicIdForNotity = '';
  1337. this.deviceIsNotify = false;
  1338. this.deviceIsWrite = false;
  1339. this.byte35 = ''; // 70 个字符
  1340. this.byte35_tmp = ''; // 70 个字符 临时变量
  1341. this.get_Bluetooth_Bevice = false;
  1342. this.rooms = []; // 宿舍号,蓝牙连接具有时效性,断开连接后清空
  1343. this.start_code = '';
  1344. }, 300);
  1345. },
  1346. /**
  1347. * 10===========10==================10
  1348. * 发送数据到设备
  1349. */
  1350. send(val) {
  1351. let buffer
  1352. let dataView
  1353. let newArr
  1354. if (val == 'start') { // 启动设备
  1355. this.isUserClose = false
  1356. // 发送 17字节 启动代码(会返回 35字节 的数据)
  1357. // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
  1358. newArr = this.str2StrArray(this.start_code)
  1359. } else if (val == 'stop') { // 结束当前使用,设备成功返回:AD+35字节 消费结算代码
  1360. this.isUserClose = true
  1361. // 发送 AAABAD+17字节 启动代码(会返回 ad+35字节 的数据)
  1362. // adb58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0104bc
  1363. newArr = ['AA', 'AB', 'AD'].concat(this.str2StrArray(this.start_code))
  1364. } else {
  1365. // 发送 AAABAC 采集数据(会返回 35字节 的数据)
  1366. // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
  1367. newArr = ['AA', 'AB', 'AC']
  1368. }
  1369. buffer = new ArrayBuffer(newArr.length)
  1370. dataView = new DataView(buffer)
  1371. newArr.forEach((item, i) => {
  1372. dataView.setUint8(i, this.sixteen_to_ten(item))
  1373. })
  1374. // 写特征值数据到设备
  1375. if (this.deviceIsWrite) {
  1376. // 写数据到设备
  1377. this.writeBLECharacteristicValue(val, buffer)
  1378. } else {
  1379. uni.showToast({
  1380. title: '设备不可写',
  1381. duration: 3000
  1382. })
  1383. }
  1384. },
  1385. /**
  1386. * 11====================11=====================11
  1387. * 写数据到设备
  1388. */
  1389. writeBLECharacteristicValue(val, buffer) {
  1390. uni.writeBLECharacteristicValue({
  1391. deviceId: this.deviceId,
  1392. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  1393. serviceId: this.serviceId,
  1394. // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  1395. characteristicId: this.characteristicIdForWrite,
  1396. // 这里的value是ArrayBuffer类型
  1397. value: buffer,
  1398. success: (res) => {
  1399. // console.log(res)
  1400. if (val == 'start') {
  1401. setTimeout(() => {
  1402. this.isChecked = true;
  1403. this.isConnected = true;
  1404. this.byte35 = '';
  1405. }, 300)
  1406. this.process_control(3)
  1407. } else if (val == 'stop') {
  1408. setTimeout(() => {
  1409. this.isChecked = false;
  1410. }, 300)
  1411. }
  1412. },
  1413. fail: (err) => {
  1414. // console.log(err)
  1415. if (err.errCode !== 0) {
  1416. setTimeout(() => {
  1417. this.setInit()
  1418. }, 300)
  1419. if (this.isConnected) {
  1420. uni.showToast({
  1421. title: '设备连接已断开!请在水表上手动停止!',
  1422. icon: 'none',
  1423. duration: 5000
  1424. })
  1425. } else {
  1426. uni.showToast({
  1427. title: '设备连接失败!请重试扫描连接!',
  1428. icon: 'none',
  1429. duration: 5000
  1430. })
  1431. }
  1432. }
  1433. }
  1434. })
  1435. },
  1436. /**
  1437. * 请求获取设备启动码
  1438. */
  1439. async request_device_start_code() {
  1440. if (this.byte35 != '' && this.stu_number != '') {
  1441. const res = await this.$myRequest({
  1442. host: this.ceshi,
  1443. url: '/HotWaters/wpstart.action',
  1444. data: {
  1445. collect_code: this.byte35,
  1446. stu_number: this.stu_number
  1447. }
  1448. })
  1449. // console.log(res)
  1450. let sc = res.data.start_code
  1451. if (typeof(sc) != 'undefined' && sc != '') {
  1452. this.start_code = sc
  1453. setTimeout(() => {
  1454. this.send('start')
  1455. }, 100)
  1456. // 刷新选定的月份消费记录
  1457. this.request_consumption_records()
  1458. } else {
  1459. this.isChecked = false
  1460. uni.showToast({
  1461. icon: 'none',
  1462. title: res.data.mess,
  1463. duration: 3000,
  1464. })
  1465. }
  1466. } else {
  1467. if (typeof this.byte35 == 'undefined' || this.byte35 == '') {
  1468. uni.showToast({
  1469. icon: 'none',
  1470. title: '获取字节码空!'
  1471. });
  1472. }
  1473. if (typeof this.stu_number == 'undefined' || this.stu_number == '') {
  1474. uni.showToast({
  1475. icon: 'none',
  1476. title: '学号为空!'
  1477. });
  1478. }
  1479. // 初始化
  1480. this.setInit()
  1481. }
  1482. },
  1483. /**
  1484. * 请求结算
  1485. */
  1486. async request_pay() {
  1487. if (this.byte35 != '') {
  1488. const res = await this.$myRequest({
  1489. host: this.ceshi,
  1490. url: '/HotWaters/wpconsume.action',
  1491. data: {
  1492. stu_number: this.stu_number,
  1493. user_name: this.user_name,
  1494. collect_code: this.byte35.substring(2)
  1495. }
  1496. })
  1497. // console.log(res)
  1498. if (res.data.mess == "支付成功") {
  1499. uni.showToast({
  1500. icon: 'success',
  1501. title: '结算成功!',
  1502. success: (res1) => {
  1503. this.use_amount = res.data.use_amount.toFixed(2)
  1504. this.amount = (this.amount - this.use_amount).toFixed(2)
  1505. this.$store.state.reshui_amount = this.amount
  1506. setTimeout(() => {
  1507. // 刷新选定的月份消费记录
  1508. this.request_consumption_records()
  1509. }, 1000)
  1510. }
  1511. })
  1512. } else {
  1513. uni.showToast({
  1514. icon: 'none',
  1515. title: res.data.mess,
  1516. duration: 3000
  1517. })
  1518. }
  1519. } else {
  1520. uni.showToast({
  1521. icon: 'success',
  1522. title: '结算字节码空!'
  1523. });
  1524. // 初始化
  1525. this.setInit()
  1526. }
  1527. },
  1528. /**
  1529. * 返回字符处理
  1530. */
  1531. str2StrArray(start_code) {
  1532. if (typeof(start_code) == 'undefined' || start_code == '') {
  1533. uni.showToast({
  1534. icon: 'none',
  1535. title: '设备码获取失败'
  1536. })
  1537. this.setInit()
  1538. return
  1539. }
  1540. if (start_code.length != 34) {
  1541. // console.log('启动码长度错误')
  1542. throw new Error("启动码长度错误");
  1543. return
  1544. }
  1545. var resultArray = new Array()
  1546. for (var i = 0; i < start_code.length; i += 2) {
  1547. resultArray.push(start_code.substr(i, 2));
  1548. }
  1549. return resultArray;
  1550. },
  1551. /**
  1552. * ArrayBuffer转16进度字符串
  1553. */
  1554. ab2hex(buffer) {
  1555. const hexArr = Array.prototype.map.call(
  1556. new Uint8Array(buffer),
  1557. function(bit) {
  1558. // console.log('====================' + bit + '==========================')
  1559. return ('00' + bit.toString(16)).slice(-2)
  1560. }
  1561. )
  1562. return hexArr.join('');
  1563. },
  1564. /**
  1565. * 16进制转10进制
  1566. */
  1567. sixteen_to_ten(str) {
  1568. return parseInt(str, 16);
  1569. }
  1570. }
  1571. }
  1572. </script>
  1573. <style scoped lang="scss">
  1574. .content {
  1575. width: 100%;
  1576. height: 100%;
  1577. .container {
  1578. width: 100%;
  1579. padding: 20rpx 40rpx;
  1580. box-sizing: border-box;
  1581. .header {
  1582. display: flex;
  1583. justify-content: center;
  1584. position: relative;
  1585. height: 200rpx;
  1586. .scan {
  1587. display: flex;
  1588. flex-direction: column;
  1589. justify-content: center;
  1590. align-items: center;
  1591. height: 200rpx;
  1592. width: 30%;
  1593. .iconfont {
  1594. margin-bottom: 10rpx;
  1595. font-size: 100rpx;
  1596. color: $my-color-primary;
  1597. }
  1598. }
  1599. .scan_hover {
  1600. border-radius: 15rpx;
  1601. background-color: $my-color-btn-background;
  1602. }
  1603. .qinshihao {
  1604. display: flex;
  1605. align-items: center;
  1606. position: fixed;
  1607. top: 20rpx;
  1608. right: 30rpx;
  1609. font-size: 28rpx;
  1610. height: 38rpx;
  1611. .icon-zhuye {
  1612. margin-right: 10rpx;
  1613. font-family: Microsoft YaHei-3970(82674968);
  1614. color: $my-color-primary;
  1615. }
  1616. .compustxt {
  1617. font-size: 18rpx;
  1618. }
  1619. }
  1620. }
  1621. .start_text {
  1622. .start_top,
  1623. .start_mid {
  1624. display: flex;
  1625. justify-content: space-between;
  1626. height: 100rpx;
  1627. align-items: center;
  1628. border-bottom: 2rpx #ccc solid;
  1629. .item_left {
  1630. display: flex;
  1631. align-items: center;
  1632. }
  1633. .start_mid_txt {
  1634. padding: 16rpx;
  1635. border-radius: 10rpx;
  1636. font-size: 36rpx;
  1637. font-weight: bold;
  1638. color: #333;
  1639. }
  1640. .iconfont {
  1641. font-size: 50rpx;
  1642. color: $my-color-primary;
  1643. margin-right: 20rpx;
  1644. }
  1645. .start_payamount {
  1646. display: flex;
  1647. justify-content: flex-end;
  1648. align-items: center;
  1649. font-size: 38rpx;
  1650. padding: 10rpx;
  1651. font-family: Microsoft YaHei-3970(82674968);
  1652. color: #B2B2B2;
  1653. border-radius: 10rpx;
  1654. .icon-dayuhao {
  1655. margin: 0 0 0 20rpx;
  1656. font-size: 38rpx;
  1657. color: #B2B2B2;
  1658. }
  1659. }
  1660. }
  1661. .start_bot {
  1662. display: flex;
  1663. justify-content: space-between;
  1664. height: 100rpx;
  1665. align-items: center;
  1666. .kongzhonglianjie {
  1667. margin: 0;
  1668. height: 60rpx;
  1669. line-height: 60rpx;
  1670. font-size: 28rpx;
  1671. background-color: #1296DB;
  1672. color: #FFFFFF;
  1673. }
  1674. .start_bot_txt {
  1675. margin-left: 20rpx;
  1676. color: #333;
  1677. font-size: 28rpx;
  1678. }
  1679. }
  1680. }
  1681. .btn_room {
  1682. font-size: 28rpx;
  1683. background-color: #41c560;
  1684. margin: 10rpx auto;
  1685. }
  1686. .start_up_process {
  1687. padding: 20rpx 0;
  1688. color: #007AFF;
  1689. }
  1690. .ad {
  1691. margin: 0 -40rpx;
  1692. padding: 10rpx;
  1693. width: 730rpx;
  1694. height: 424rpx;
  1695. .ad_img {
  1696. height: 100%;
  1697. width: 100%;
  1698. }
  1699. }
  1700. .title {
  1701. height: 100rpx;
  1702. margin: 0 -40rpx;
  1703. padding: 0 40rpx;
  1704. line-height: 100rpx;
  1705. color: #808080;
  1706. font-size: 32rpx;
  1707. background: #f2f2f2;
  1708. }
  1709. .record-item {
  1710. width: 100%;
  1711. .uni-list-cell-db {
  1712. height: 100rpx;
  1713. line-height: 100rpx;
  1714. font-weight: bold;
  1715. .uni-input {
  1716. display: flex;
  1717. align-items: center;
  1718. height: 58rpx;
  1719. text:nth-child(1) {
  1720. margin-right: 10rpx;
  1721. }
  1722. }
  1723. }
  1724. .item-list {
  1725. height: 80rpx;
  1726. display: flex;
  1727. align-items: center;
  1728. justify-content: space-between;
  1729. border-top: 2rpx #ccc solid;
  1730. .icon-shijian {
  1731. margin-right: 8rpx;
  1732. }
  1733. .icon-shuidi {
  1734. margin-right: 3rpx;
  1735. }
  1736. .item-list-txt {
  1737. font-size: 28rpx;
  1738. &:nth-of-type(2) {
  1739. color: #515151;
  1740. }
  1741. &:nth-of-type(3) {
  1742. color: #f76200;
  1743. font-size: 32rpx;
  1744. }
  1745. }
  1746. }
  1747. }
  1748. }
  1749. .call {
  1750. position: fixed;
  1751. right: 40rpx;
  1752. bottom: 180rpx;
  1753. }
  1754. .default-btn {
  1755. background-color: transparent;
  1756. border: none;
  1757. outline: none;
  1758. }
  1759. .floating-button-call {
  1760. position: relative;
  1761. font-size: 28rpx;
  1762. width: 90rpx;
  1763. height: 90rpx;
  1764. border: 1px solid #ccc;
  1765. border-radius: 50%;
  1766. opacity: 0.5;
  1767. background: url('../../static/images/kefu.png') center center no-repeat;
  1768. background-size: 68rpx auto;
  1769. }
  1770. .operation-manual {
  1771. position: fixed;
  1772. right: 40rpx;
  1773. bottom: 300rpx;
  1774. }
  1775. .floating-button-manual {
  1776. position: relative;
  1777. font-size: 28rpx;
  1778. width: 90rpx;
  1779. height: 90rpx;
  1780. border: 1px solid #ccc;
  1781. border-radius: 50%;
  1782. opacity: 0.5;
  1783. background: url('../../static/images/yhsc.png') center center no-repeat;
  1784. background-size: 68rpx auto;
  1785. }
  1786. .article {
  1787. padding: 20rpx;
  1788. height: 100%;
  1789. }
  1790. .desc-title {
  1791. height: 50rpx;
  1792. line-height: 50rpx;
  1793. font-size: 36rpx;
  1794. font-weight: bold;
  1795. margin-bottom: 50rpx;
  1796. text-align: center;
  1797. }
  1798. .section {
  1799. margin-bottom: 40rpx;
  1800. }
  1801. .section-title-red,
  1802. .section-title {
  1803. font-size: 30rpx;
  1804. font-weight: bold;
  1805. margin-bottom: 10rpx;
  1806. }
  1807. .section-title-red {
  1808. color: #ca0000;
  1809. }
  1810. .section-content {
  1811. font-size: 28rpx;
  1812. line-height: 1.5;
  1813. white-space: pre-wrap;
  1814. }
  1815. .desc-footer {
  1816. height: 50rpx;
  1817. line-height: 50rpx;
  1818. }
  1819. }
  1820. </style>