reshui.vue 49 KB

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