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