reshui.vue 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970
  1. <template>
  2. <view class="content">
  3. <view class="container">
  4. <view class="header">
  5. <view class="scan" @tap="scan()" hover-class="scan_hover">
  6. <!-- <text class="iconfont icon-saoma"></text> -->
  7. <image :src="require('./images/icon-saoma.png')" style="width: 118rpx;" mode="aspectFit"></image>
  8. <text>扫码连接</text>
  9. </view>
  10. <view class="qinshihao">
  11. <!-- <text class="iconfont icon-zhuye"></text> -->
  12. <image :src="require('./images/icon-zhuye.png')" style="width: 28rpx; margin-right: 8rpx;" mode="aspectFit"></image>
  13. <text class="dormtxt">{{dorm_number || '宿舍未知'}}</text>
  14. </view>
  15. </view>
  16. <view class="start_text">
  17. <view class="start_top">
  18. <view class="item_left">
  19. <text class="iconfont icon-shuibiao"></text>
  20. <text>开启设备</text>
  21. </view>
  22. <view>
  23. <switch :color="colorPrimary" :checked="isChecked" @change="switchChange" />
  24. </view>
  25. </view>
  26. <view class="start_mid">
  27. <view class="item_left">
  28. <text class="iconfont icon-qian"></text>
  29. <!-- <navigator url="/pagesWater/reshuiDetails/reshuiDetails" open-type="navigate"
  30. class="start_mid_txt">¥{{amount}}</navigator> -->
  31. <navigator url="" open-type="navigate" class="start_mid_txt">¥{{amount}}</navigator>
  32. </view>
  33. <view class="start_payamount" @tap="chongzhi_yemian()" hover-class="scan_hover">
  34. <text>点击充值</text>
  35. <image :src="require('./images/icon-dayuhao.png')" style="width: 26rpx; margin-left: 10rpx; padding-top: 3rpx; opacity: 0.6;"
  36. mode="aspectFit">
  37. </image>
  38. <!-- <text class="iconfont icon-dayuhao"></text> -->
  39. </view>
  40. </view>
  41. <view class="start_bot">
  42. <button class="kongzhonglianjie" @click="open">空中连接</button>
  43. <text class="start_bot_txt">当前结算消费:¥{{use_amount}}</text>
  44. </view>
  45. </view>
  46. <view v-if="showRoom" style="width: 100%; height: 100%; ">
  47. <view style="display: flex; flex-wrap: wrap;">
  48. <button v-for="item in rooms" :key="item.id" @tap="clickRoom(item.id)" class="btn_room">{{item.room}}</button>
  49. </view>
  50. </view>
  51. <view class="start_up_process">
  52. <uni-steps :options="list1" :active-color="colorPrimary" :active="active" />
  53. </view>
  54. <!-- <view class="ad">
  55. <image src="../../static/images/ad_reshui.jpg" mode="aspectFill" class="ad_img" @tap="ad_redirect">
  56. </image>
  57. </view> -->
  58. <view class="title">
  59. 消费记录
  60. </view>
  61. <view class="record-item">
  62. <view class="uni-list-cell-db">
  63. <picker mode="date" :value="date" fields="month" :start="startDate" :end="endDate" @change="bindDateChange">
  64. <view class="uni-input">
  65. <text>{{date}}</text>
  66. <!-- <text class="iconfont icon-arrow-down"></text> -->
  67. <image :src="require('./images/icon-arrow-down.png')" style="width: 26rpx;" mode="aspectFit">
  68. </image>
  69. </view>
  70. </picker>
  71. </view>
  72. <view class="item-list" v-for="(item, i) in xiaofei_items" :key="i">
  73. <text class="item-list-txt"><text class="iconfont icon-shijian"></text>{{item.begin_time}}</text>
  74. <text class="item-list-txt"><text class="iconfont icon-shuidi"></text>{{numFilter(item.use_size, 'l')}} t</text>
  75. <text class="item-list-txt">{{numFilter(item.use_amount, 'm')}}</text>
  76. </view>
  77. </view>
  78. </view>
  79. <instructions v-if="flag" @parent_cancel="cancel" @parent_noshow='noshow'></instructions>
  80. <view class="operation-manual">
  81. <button class="default-btn floating-button-manual" @tap="showPopup = true"></button>
  82. </view>
  83. <view class="call">
  84. <button class="default-btn floating-button-call" @tap="makeCall"></button>
  85. </view>
  86. <Popup :showPopup="showPopup" @close="showPopup = false">
  87. <scroll-view class="article" scroll-y>
  88. <view class="desc-title">{{ article.title }}</view>
  89. <view v-for="(section, index) in article.sections" :key="index" class="section">
  90. <view v-if="section.red == true" class="section-title-red">{{ section.title }}</view>
  91. <view v-else class="section-title">{{ section.title }}</view>
  92. <view class="section-content">{{ section.content }}</view>
  93. </view>
  94. <view class="desc-footer"></view>
  95. </scroll-view>
  96. </Popup>
  97. </view>
  98. </template>
  99. <script>
  100. import instructions from '../../components/instructions.vue'
  101. import Popup from '@/components/Popup.vue';
  102. export default {
  103. /**
  104. * 声明组件
  105. */
  106. components: {
  107. instructions,
  108. Popup
  109. },
  110. data() {
  111. return {
  112. colorPrimary: getApp().globalData.primaryColor,
  113. date: this.$getDate({
  114. format: true
  115. }),
  116. startDate: this.$getDate('start_date'),
  117. endDate: this.$getDate('end_date'),
  118. flag: false, // 控制说明弹窗
  119. showRoom: false,
  120. isUserClose: false, // 是否是用户自己关闭
  121. isChecked: false,
  122. isConnected: false, // 是否连接上
  123. isScan: false,
  124. active: -1,
  125. list1: [{
  126. title: '连接设备'
  127. }, {
  128. title: '连设备服务'
  129. }, {
  130. title: '数据通讯'
  131. }, {
  132. title: '启动'
  133. }],
  134. devices: [], // 保存设备
  135. rooms: [], // 保存水表id及对应的宿舍号
  136. device_code: '', // 设备编号
  137. deviceId: '',
  138. serviceId: '',
  139. characteristicIdForWrite: '',
  140. characteristicIdForNotity: '',
  141. deviceIsNotify: false,
  142. deviceIsWrite: false,
  143. byte35: '', // 70 个字符
  144. byte35_tmp: '', // 70 个字符 临时变量
  145. start_code: '',
  146. // start_code: '3459A8C660F1C284E41003E80000120000',
  147. xiaofei_items: [], // 消费记录
  148. use_amount: 0.0.toFixed(2), // 结算金额
  149. amount: 0.0.toFixed(2), // 余额
  150. dorm_number: '', // 宿舍号
  151. stu_number: '', // 学号
  152. user_name: '',
  153. // compus: '', // 校区
  154. ceshi: 'code',
  155. changeDate: false, // 是否是手动选择日期
  156. get_Bluetooth_Bevice: false, // 自动获取蓝牙设备,并列表
  157. timer: -1,
  158. test: this.$store.state.test,
  159. showPopup: false,
  160. article: {
  161. title: "蓝牙卡控操作说明",
  162. sections: [{
  163. title: "蓝牙卡控使用方式为:",
  164. content: "  用微信【扫一扫】功能,扫描热水表上的二维码。",
  165. red: false
  166. },
  167. {
  168. title: "如遇连接热水表问题,请先检查:",
  169. content: "  1.蓝牙本身无故障,且手机蓝牙和GPS定位要开启,并要授权。\n" +
  170. "  2.手机系统要求出厂自带的,系统版本低于安卓4.3或刷过机的,可能搜索不到热水表。\n" +
  171. "  3.之前已经连接过热水表需正常退出,否则再次连接可能搜索不到热水表,需关闭蓝牙5秒钟后再尝试。\n" +
  172. "  4.热水表有人在使用,热水表肯定连接不了。\n" +
  173. "  5.用微信分身,连接热水表会存在意想不到的问题。\n" +
  174. "  6.在连接热水表前,请断开如蓝牙耳机、蓝牙音像等其他蓝牙设备。",
  175. red: false
  176. },
  177. {
  178. title: "如以上检查没问题,部分手机请检查以下权限是否打开(以下路径不能保证绝对正确,不同手机品牌和版本号会有区别,可自行百度或联系客服):",
  179. content: "",
  180. red: true
  181. },
  182. {
  183. title: "华为手机:",
  184. content: "  设置→安全和隐私→定位服务→打开“访问我的位置信息”。\n" +
  185. "  设置→应用和通知→权限管理→微信→打开“您的位置”。",
  186. red: false
  187. },
  188. {
  189. title: "小米手机:",
  190. content: "  设置→更多设置→系统安全→位置信息→开启“位置服务”。\n" +
  191. "  设置→授权管理→应用授权管理→微信→开启“定位”。",
  192. red: false
  193. },
  194. {
  195. title: "苹果手机:",
  196. content: "  设置→隐私→打开“定位服务”。\n" +
  197. "  设置→微信→位置→使用应用期间。",
  198. red: false
  199. },
  200. {
  201. title: "OPPO手机:",
  202. content: "  设置→GPS定位→开启“允许访问我的位置信息。\n" +
  203. "  设置→权限隐私→按应用管理权限→微信→授权。",
  204. red: false
  205. },
  206. {
  207. title: "VIVO手机:",
  208. content: "  设置→定位服务→开启。\n" +
  209. "  设置→更多设置→权限管理→微信→定位手机选择“允许”。",
  210. red: false
  211. },
  212. {
  213. title: "三星手机:",
  214. content: "  设置→连接→打开定位服务。\n" +
  215. "  设置→应用程序→微信→权限→开启位置信息。",
  216. red: false
  217. },
  218. {
  219. title: "魅族手机:",
  220. content: "  设置→定位服务→开启:\n" +
  221. "  设置→应用管理→选择已安装→微信→权限管理→获取定位(允许)。",
  222. red: false
  223. },
  224. {
  225. title: "多卡号(一个人有2个以上微信身份卡号)扣款问题:",
  226. content: "  遇到多卡,扣款会从第1次绑定的卡中扣款,如想从指定的卡中扣款,请先删除微信小程序(微信界面下拉,长按本小程序并拖动删除),再用指定的卡进入即可。",
  227. red: true
  228. }
  229. ]
  230. }
  231. }
  232. },
  233. onLoad(options) {
  234. // console.log(options);
  235. // 系统正在维护...
  236. if (this.test == 'weihuzhong') {
  237. uni.redirectTo({
  238. url: '../../pages/index/index'
  239. })
  240. return;
  241. }
  242. // 向用户发起授权请求
  243. uni.authorize({
  244. scope: 'scope.userLocation',
  245. success(res) {
  246. uni.getLocation({
  247. success: (loc) => {
  248. // console.log(loc);
  249. uni.showToast({
  250. icon: 'success',
  251. title: '已获定位信息'
  252. })
  253. },
  254. fail: (err) => {
  255. uni.showToast({
  256. icon: 'none',
  257. title: '获取定位失败,请手动开启定位!'
  258. })
  259. }
  260. })
  261. },
  262. fail: (err) => {
  263. // console.log(err);
  264. uni.showToast({
  265. icon: 'none',
  266. title: '请手动设置定位授权!'
  267. })
  268. }
  269. })
  270. if (typeof(options) != 'undefined' && typeof(options) != '' && JSON.stringify(options) == '{}') {
  271. // 获取基本信息
  272. this.get_base_info(options, 'onLoad')
  273. } else if (typeof(options.from) != 'undefined' && options.from != 0) {
  274. // 获取基本信息
  275. this.get_base_info(options, 'onLoad')
  276. // 获取设备码
  277. this.device_code = decodeURIComponent(options.q).split('=')[1]
  278. }
  279. const value = uni.getStorageSync('instruction_noshow');
  280. if (!(value == true)) {
  281. this.flag = true
  282. }
  283. },
  284. onShow() {
  285. let _this = this;
  286. // 充值后更新金额
  287. this.amount = this.$store.state.reshui_amount
  288. // 刷新充值后的金额
  289. uni.$once('update_reshui_amount', (data) => {
  290. this.get_base_info('options', 'onShow');
  291. });
  292. 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: 180rpx;
  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: 90rpx;
  1760. height: 90rpx;
  1761. border: 1px solid #ccc;
  1762. border-radius: 50%;
  1763. opacity: 0.5;
  1764. background: url('../../static/images/kefu.png') center center no-repeat;
  1765. background-size: 68rpx auto;
  1766. }
  1767. .operation-manual {
  1768. position: fixed;
  1769. right: 40rpx;
  1770. bottom: 300rpx;
  1771. }
  1772. .floating-button-manual {
  1773. position: relative;
  1774. font-size: 28rpx;
  1775. width: 90rpx;
  1776. height: 90rpx;
  1777. border: 1px solid #ccc;
  1778. border-radius: 50%;
  1779. opacity: 0.5;
  1780. background: url('../../static/images/yhsc.png') center center no-repeat;
  1781. background-size: 68rpx auto;
  1782. }
  1783. .article {
  1784. padding: 20rpx;
  1785. height: 100%;
  1786. }
  1787. .desc-title {
  1788. height: 50rpx;
  1789. line-height: 50rpx;
  1790. font-size: 36rpx;
  1791. font-weight: bold;
  1792. margin-bottom: 50rpx;
  1793. text-align: center;
  1794. }
  1795. .section {
  1796. margin-bottom: 40rpx;
  1797. }
  1798. .section-title-red,
  1799. .section-title {
  1800. font-size: 30rpx;
  1801. font-weight: bold;
  1802. margin-bottom: 10rpx;
  1803. }
  1804. .section-title-red {
  1805. color: #ca0000;
  1806. }
  1807. .section-content {
  1808. font-size: 28rpx;
  1809. line-height: 1.5;
  1810. white-space: pre-wrap;
  1811. }
  1812. .desc-footer {
  1813. height: 50rpx;
  1814. line-height: 50rpx;
  1815. }
  1816. }
  1817. </style>