| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320 |
- <template>
- <view class="content">
- <view class="container">
- <view class="header">
- <view class="scan" @tap="scan()" hover-class="scan_hover">
- <text class="iconfont icon-saoma"></text>
- <text>扫码连接</text>
- </view>
- <view class="qinshihao">
- <text class="iconfont icon-zhuye"></text>
- <text class="txt">{{dorm_number || '宿舍未知'}}</text>
- </view>
- </view>
- <view class="start_text">
- <view class="start_top">
- <view class="item_left">
- <text class="iconfont icon-shuibiao"></text>
- <text>开启设备</text>
- </view>
- <view>
- <switch :color="colorPrimary" :checked="isChecked" @change="switchChange" />
- </view>
- </view>
- <view class="start_mid">
- <view class="item_left">
- <text class="iconfont icon-qian"></text>
- <text class="start_mid_txt">¥{{amount}}</text>
- </view>
- <view class="start_payamount" @tap="chongzhi_yemian()" hover-class="scan_hover">
- <text>点击充值</text>
- <text class="iconfont icon-dayuhao"></text>
- </view>
- </view>
- <view class="start_bot">
- <text class="start_bot_txt" v-if="false">时长:00:00:00</text>
- <text class="start_bot_txt">当前结算消费:¥{{use_amount}}</text>
- </view>
- </view>
- <view class="start_up_process">
- <uni-steps :options="list1" :active-color="colorPrimary" :active="active" />
- </view>
- <view class="title">
- 消费记录
- </view>
- <view class="record-item">
- <view class="uni-list-cell-db">
- <picker mode="date" :value="date" fields="month" :start="startDate" :end="endDate"
- @change="bindDateChange">
- <view class="uni-input">
- <text>{{date}}</text>
- <text class="iconfont icon-arrow-down"></text>
- </view>
- </picker>
- </view>
- <view class="item-list" v-for="(item, i) in xiaofei_items" :key="i">
- <text class="item-list-txt"><text class="iconfont icon-shijian"></text>{{item.begin_time}}</text>
- <text class="item-list-txt"><text class="iconfont icon-shuidi"></text>{{item.use_size}}升</text>
- <text class="item-list-txt">{{item.use_amount}}</text>
- </view>
- </view>
- </view>
- <instructions v-if="flag" @parent_cancel="cancel"></instructions>
- </view>
- </template>
- <script>
- import instructions from '../../components/instructions.vue'
- export default {
- /**
- * 声明组件
- */
- components: {
- instructions
- },
- data() {
- return {
- colorPrimary: getApp().globalData.primaryColor,
- date: this.$getDate({
- format: true
- }),
- startDate: this.$getDate('start_date'),
- endDate: this.$getDate('end_date'),
- flag: true,
- isUserClose: false, // 是否是用户自己关闭
- isChecked: false,
- isConnected: false, // 是否连接上
- isScan: false,
- active: -1,
- list1: [{
- title: '连接设备'
- }, {
- title: '连设备服务'
- }, {
- title: '数据通讯'
- }, {
- title: '启动'
- }],
- devices: [], // 保存设备
- device_code: '', // 设备编号
- deviceId: '',
- serviceId: '',
- characteristicIdForWrite: '',
- characteristicIdForNotity: '',
- deviceIsNotify: false,
- deviceIsWrite: false,
- byte35: '', // 70 个字符
- byte35_tmp: '', // 70 个字符 临时变量
- start_code: '',
- // start_code: '3459A8C660F1C284E41003E80000120000',
- xiaofei_items: [],
- userinfo: '',
- card_number: '',
- user_name: '',
- use_amount: 0.0.toFixed(2), // 结算金额
- amount: 0.0.toFixed(2), // 余额
- dorm_number: '' // 宿舍号
- }
- },
- onLoad(options) {
- try {
- // 获取存储的用户数据
- const value = uni.getStorageSync('userinfo_storage_key')
- if (typeof(options.item) != 'undefined') {
- let item = decodeURIComponent(options.item)
- // console.log(value)
- // console.log(item)
- if (item === '{}') { // 如果没有用户信息,返回首页
- uni.redirectTo({
- url: '../index/index'
- });
- return;
- }
- if (item != '') {
- // 处理JSON字符串
- // this.userinfo = item.replace(/"/g, "'");
- this.userinfo = JSON.parse(item);
- }
- } else if (value != '') {
- // 处理JSON字符串
- // this.userinfo = value.replace(/"/g, "'");
- this.userinfo = value;
- } else {
- uni.showToast({
- title: '未获得授权信息',
- icon: 'success'
- })
- uni.redirectTo({
- url: '../index/index'
- });
- }
- } catch (e) {
- console.log(e)
- }
- this.card_number = this.userinfo.card_number; // 卡号
- // 查询用户信息
- this.select_user_info()
- },
- onShow() {
- try {
- const pay_info = uni.getStorageSync('pay_info_storage_key')
- if (pay_info && pay_info.resultCode == 0) {
- uni.showToast({
- title: pay_info.resultMsg,
- icon: 'success'
- })
-
- uni.removeStorageSync('pay_info_storage_key')
- }
- } catch (e) {
- console.log(e)
- }
- },
- methods: {
- /**
- * 选择消费年月
- */
- bindDateChange(e) {
- this.date = e.detail.value
- // 请求选定的月份消费记录
- this.request_consumption_records()
- },
- /**
- * 导航页面取消
- */
- cancel(param) {
- this.flag = param
- },
- /**
- * 请求选定的月份消费记录
- */
- async request_consumption_records() {
- if (this.card_number != '') {
- this.xiaofei_items = []
- const res = await this.$myRequest({
- host: 'code',
- url: '/HotWater/wxpayqueryConsume.action',
- method: 'POST',
- header: {
- 'content-type': 'application/x-www-form-urlencoded'
- },
- data: {
- card_number: this.card_number,
- begin_time: this.date
- }
- });
- // console.log(res.data.mess, 'request_consumption_records');
- if (typeof(res.data.mess) != 'undefined') {
- let items = res.data.mess
- // 按时间倒序排列
- // items.sort((a, b) => {
- // return a.begin_time < b.begin_time ? 1 : -1;
- // })
- for (var i = 0; i < items.length; i++) {
- // 过滤掉为0的记录,只显示消费大于0的记录
- // if (items[i].use_amount > 0.00) {
- this.xiaofei_items.push(items[i])
- // }
- }
- } else {
- uni.showToast({
- icon: 'success',
- title: '该月无消费记录'
- })
- }
- } else {
- uni.showToast({
- icon: 'success',
- title: '用户卡号为空'
- })
- }
- },
- /**
- * 查询用户信息
- */
- async select_user_info() {
- const res = await this.$myRequest({
- host: 'code',
- url: '/HotWater/userget_stu.action',
- method: 'POST',
- header: {
- 'content-type': 'application/x-www-form-urlencoded'
- },
- data: {
- card_number: this.userinfo.card_number
- }
- });
- // console.log(res, 'select_user_info');
- if (res.data.info) {
- // 将JSON字符串转化为JSON对象,取余额
- let userinfo = res.data.info[0]
- let amount = userinfo.balance // 余额
- let cnumber = this.userinfo.card_number
- this.user_name = userinfo.user_name;
- this.dorm_number = userinfo.dom // 宿舍号
- if (amount != 0) {
- this.amount = amount
- }
- if (cnumber == null || cnumber == undefined || cnumber == '') {
- // 之前未绑定用户信息,现在去绑定用户信息
- this.send_save_user_info()
- }
- // 请求选定的月份消费记录
- this.request_consumption_records()
- } else {
- uni.showToast({
- icon: 'success',
- title: '获取用户信息失败'
- })
- }
- },
- /**
- * 绑定用户信息
- */
- async send_save_user_info() {
- const res = await this.$myRequest({
- host: 'code',
- url: '/HotWater/userstu_info.action',
- method: 'POST',
- header: {
- 'content-type': 'application/x-www-form-urlencoded'
- },
- data: {
- user: JSON.stringify(this.userinfo)
- }
- });
- // console.log('send_save_user_info:', res)
- let error = res.data.erro;
- if (error == '未获取到数据!') {
- uni.showToast({
- title: '绑定用户失败',
- duration: 2000
- })
- } else {
- uni.showToast({
- icon: 'success',
- title: '绑定用户成功',
- duration: 2000
- })
- }
- },
- /**
- * 控制进度条显示
- * @param {Object} active_value
- */
- process_control(active_value) {
- if (active_value <= this.list1.length - 1) {
- this.active = active_value
- }
- },
- /**
- * 开始或停止用水
- */
- switchChange(e) {
- this.isChecked = e.target.value
- if (this.isChecked) {
- if (this.deviceId && this.serviceId && this.devices.length != 0) {
- uni.showModal({
- title: '提示',
- content: '确定要启动设备?',
- success: (res) => {
- if (res.confirm) {
- this.isChecked = true
- } else if (res.cancel) {
- setTimeout(() => {
- this.isChecked = false
- }, 300)
- }
- }
- })
- // 开始连接匹配的蓝牙设备
- setTimeout(async () => {
- // 请求获取设备启动码
- await this.request_device_start_code()
- }, 3000)
- } else {
- uni.showToast({
- icon: 'none',
- title: '先【扫码连接】,后【开启设备】',
- duration: 1500
- })
- setTimeout(() => {
- this.isChecked = false
- }, 300)
- }
- } else {
- uni.showModal({
- title: '提示',
- content: '确定要关闭设备?',
- success: (res) => {
- if (res.confirm) {
- this.isChecked = false
- if (this.deviceId && this.serviceId) {
- this.send('stop')
- } else {
- uni.showToast({
- icon: 'none',
- title: '与设备连接中断!请在设备上手动停止即可!',
- duration: 5000
- })
- setTimeout(() => {
- this.isChecked = true
- }, 300)
- }
- } else if (res.cancel) {
- setTimeout(() => {
- this.isChecked = true
- }, 300)
- }
- }
- })
- }
- },
- /**
- * 跳转到充值页面并传参数
- */
- chongzhi_yemian() {
- //在起始页面跳转到recharge.vue页面并传递参数
- uni.navigateTo({
- url: '../recharge/recharge?item=' + encodeURIComponent(JSON.stringify(this.userinfo))
- });
- },
- /**
- * 111111111111111111111111111111
- * 扫码连接设备
- */
- scan() {
- // 启动最低金额限制
- if (this.amount < 10) {
- uni.showModal({
- title: '提示',
- content: '当前余额不足¥10元',
- cancelText: '我知道了',
- confirmText: '我要充值',
- success: (res) => {
- if (res.confirm) {
- // 跳转充值页面并传参数
- this.chongzhi_yemian()
- } else if (res.cancel) {
- return
- }
- }
- })
- } else {
- // 已经启动扫描连接设备中
- if (this.isScan) {
- uni.showToast({
- icon: 'none',
- title: '正在扫描,请耐心等待',
- })
- return
- }
- // 已经启动设备,不启动扫描
- if (this.isChecked) {
- uni.showToast({
- icon: 'none',
- title: '设备已启动,无需重新扫码',
- })
- return
- }
- if (this.deviceId) {
- uni.showModal({
- title: '提示',
- content: '设备已经连接,需要重新连接吗?',
- success: res => {
- if (res.confirm) {
- this.closeBLEConnection()
- this.scan_qr()
- } else if (res.cancel) {
- return
- }
- }
- })
- } else {
- this.scan_qr()
- }
- }
- },
- /**
- * 启动摄像头,扫描二维码
- */
- scan_qr() {
- uni.scanCode({
- onlyFromCamera: true,
- success: (res) => {
- this.isScan = true
- // console.log(res)
- // charSet: "utf-8"
- // codeVersion: 3
- // errMsg: "scanCode:ok"
- // rawData: "aHR0cDovL3Rvb2xzLjlrYnMuY29tLz93eG1pbmljb2RlPUtCNTgwMDAyNjUy"
- // result: "http://tools.9kbs.com/?wxminicode=KB580002652"
- // scanType: "QR_CODE"
- this.device_code = res.result.split('?')[1].split('=')[1]
- if (this.device_code === '') {
- uni.showToast({
- icon: 'success',
- title: '获取设备码失败',
- duration: 3000
- })
- this.isScan = false
- } else {
- // 初始化蓝牙
- this.openBluetoothAdapter()
- }
- }
- })
- },
- /**
- * 22222222222222222222222222222
- * 初始化蓝牙
- */
- openBluetoothAdapter() {
- uni.openBluetoothAdapter({
- success: (res) => { // 已打开
- uni.getBluetoothAdapterState({ // 蓝牙的匹配状态
- success: (res1) => {
- // console.log(res1, '“本机设备的蓝牙已打开”');
- // 本机设备的蓝牙已打开
- // 开始搜索蓝牙设备
- if (this.deviceId == '' && this.serviceId == '')
- this.startBluetoothDeviceDiscovery()
- },
- fail: (error) => {
- // console.log(error)
- uni.showToast({
- title: "请开启手机蓝牙",
- icon: "none",
- duration: 3000
- })
- this.isScan = false
- }
- });
- },
- fail: (err) => { // 未打开
- // console.log(err)
- this.isScan = false
- if (err.errCode == 10001) {
- uni.showModal({
- content: '请开启手机蓝牙!',
- showCancel: false,
- })
- } else {
- uni.showToast({
- title: err.errMsg,
- duration: 2000
- })
- }
- }
- });
- },
- /**
- * 333333333333333333333333333
- * 开始搜索蓝牙设备
- */
- startBluetoothDeviceDiscovery() {
- uni.startBluetoothDevicesDiscovery({ // 开始搜寻附近的蓝牙外围设备
- // services: ['FEE7'], // 要搜索但蓝牙设备主 service 的 uuid 列表
- allowDuplicatesKey: true,
- success: (res) => {
- this.process_control(0);
- this.onBluetoothDeviceFound() //监听搜索到新设备 同时进行
- },
- fail: err => {
- // console.log(err, '错误信息');
- uni.showToast({
- title: err.errMsg,
- duration: 2000
- })
- this.isScan = false
- }
- })
- },
- stopBluetoothDevicesDiscovery: function() { //停止搜索
- uni.stopBluetoothDevicesDiscovery({
- success: (res) => {
- // console.log(res, '停止蓝牙搜索')
- // uni.showToast({
- // title: '停止蓝牙搜索',
- // duration: 2000
- // })
- this.process_control(0);
- // 开始连接匹配的蓝牙设备
- this.createBLEConnection()
- },
- fail: e => {
- // console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
- this.isScan = false
- uni.showToast({
- title: err.errCode,
- duration: 2000
- })
- }
- })
- },
- /**
- * 444444444444444444444444444444444
- * 发现外围设备
- */
- onBluetoothDeviceFound() {
- uni.onBluetoothDeviceFound((res) => {
- res.devices.forEach(device => { // 筛选找到的蓝牙中对名称匹配
- // 过滤掉没有名字的设备
- if (!device.name && !device.localName) {
- return
- }
- if (device.name == this.device_code) {
- //data里面建立一个deviceId、device_code,存储起来
- // this.device_code = device.name;
- this.deviceId = device.deviceId
- // 把搜索到的设备存储起来,如有需要可以在页面上展示
- if (this.devices.indexOf(res.devices[0].deviceId) == -1) {
- this.devices.push(res.devices[0].deviceId)
- }
- this.process_control(0);
- this.stopBluetoothDevicesDiscovery()
- }
- })
- });
- },
- /**
- * 555555555555555555555555555555555
- * 连接蓝牙设备
- */
- createBLEConnection() {
- //连接蓝牙
- uni.createBLEConnection({
- deviceId: this.deviceId,
- timeout: 3000,
- success: (res) => {
- // console.log(res)
- this.connect_BLE()
- },
- fail: (err) => {
- // console.log(err)
- this.isScan = false
- if (err.errMsg.indexOf('already connect') != -1) {
- this.connect_BLE()
- } else {
- uni.showToast({
- title: '建立连接失败',
- icon: 'none',
- duration: 3000,
- success: (res1) => {
- this.setInit()
- }
- })
- }
- }
- })
- },
- connect_BLE() {
- if (this.isConnected === false) {
- this.isConnected = true
- this.process_control(1);
- // 获取蓝牙设备所有服务(service)
- this.getBLEDeviceServices()
- }
- },
- /**
- * 6666666666666666666666666666666666
- * 获取蓝牙的所有服务
- */
- getBLEDeviceServices() {
- uni.getBLEDeviceServices({
- deviceId: this.deviceId,
- success: (res) => {
- // console.log(res)
- // res1.services.forEach((item) => {
- // this.getBLEDeviceCharacteristics()
- // })
- this.serviceId = res.services[1].uuid
- this.process_control(1);
- this.getBLEDeviceCharacteristics(this.deviceId, res.services[1].uuid)
- },
- fail: (err) => {
- // console.log(err)
- this.isScan = false
- uni.showToast({
- title: '获取服务失败',
- icon: 'none',
- duration: 3000
- })
- return
- }
- });
- },
- /**
- * 777777777777777777777777777777777
- * 获取蓝牙特征
- */
- getBLEDeviceCharacteristics() {
- // 获取蓝牙设备某个服务中所有特征值(characteristic)
- uni.getBLEDeviceCharacteristics({
- deviceId: this.deviceId,
- // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
- serviceId: this.serviceId,
- success: (res) => {
- this.process_control(2);
- // console.log(res)
- this.characteristicIdForWrite = res.characteristics[0].uuid
- this.characteristicIdForNotity = res.characteristics[1].uuid
- this.deviceIsWrite = res.characteristics[0].properties.write
- this.deviceIsNotify = res.characteristics[1].properties.notify
- // console.log(this.characteristicIdForWrite, this.characteristicIdForNotity)
- // 确保对应服务id下的特征值id具备监听数据变化的特性
- if (res.characteristics[1].properties.notify) {
- this.notifyBLECharacteristicValueChange()
- }
- },
- fail: (err) => {
- this.isScan = false
- console.error('获取蓝牙设备特征值失败(getBLEDeviceCharacteristics)', err)
- }
- });
- },
- /**
- * 888888888888888888888888888888888888
- * 启用 notify 功能
- */
- notifyBLECharacteristicValueChange() {
- // console.log(characteristicId, 'characteristicId')
- uni.notifyBLECharacteristicValueChange({
- state: true, // 启用 notify 功能
- deviceId: this.deviceId,
- // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
- serviceId: this.serviceId,
- // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
- characteristicId: this.characteristicIdForNotity,
- success: (res) => {
- this.process_control(2)
- this.isScan = false
- // console.log('通知启用(notifyBLECharacteristicValueChange)',res);
- this.onBLECharacteristicValueChange()
- setTimeout(() => {
- // 发送 AAABAC,采集数据(会返回 35字节 的数据)
- this.send('')
- }, 300)
- // // 请求设备信息
- // this.request_device_info()
- // // 显示设备启动授权金额
- // this.display_device_startup_amount()
- },
- fail: (err) => {
- // console.log(err)
- this.isScan = false
- uni.showModal({
- content: err.errMsg,
- showCancel: false
- })
- }
- })
- },
- // /**
- // * 请求设备信息
- // */
- // async request_device_info() {
- // const res = await this.$myRequest({
- // url: '',
- // data: {
- // }
- // })
- // console.log('', res)
- // },
- // /**
- // * 显示设备启动授权金额
- // */
- // display_device_startup_amount() {
- // // 待开发
- // },
- /**
- * 999999999999999999999999999999999
- * 监听低功耗蓝牙设备的特征值变化
- */
- onBLECharacteristicValueChange() {
- uni.onBLECharacteristicValueChange((characteristic) => {
- let condition = ab2hex(characteristic.value)
- // console.log(condition)
- if (condition.length > 2) {
- this.byte35_tmp += condition
- if (this.byte35_tmp.length > 40) {
- this.byte35 = this.byte35_tmp
- this.byte35_tmp = ''
- if (this.byte35.startsWith('ad')) {
- // console.log(this.byte35, '结束用水成功')
- // 关闭蓝牙连接
- this.closeBLEConnection()
- // 处理 35字节
- // APP 接收后,根据用户操作情况主要做如下几个方面的处理。
- // 1、向后台提交【结算数据】
- // 2、提交【结算数据】并生成【启动代码】
- // 3、提示:用户进行下面的流程
- // 请求结算
- this.request_pay()
- } else {
- // 发送 AAABAC,采集数据(会返回 35字节 的数据)
- // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
- // uni.showToast({
- // icon: 'success',
- // title: '采集数据成功!',
- // duration: 2000
- // })
- // console.log(this.byte35, '采集上一条消费数据')
- }
- }
- }
- switch (condition) {
- case 'bb':
- // uni.showToast({
- // title: '设备启动成功',
- // duration: 3000,
- // icon: 'none'
- // })
- this.process_control(3)
- this.isChecked = true
- break
- case 'bc':
- uni.showToast({
- title: '启动失败,设备检验启动代码不通过',
- duration: 3000,
- icon: 'none'
- })
- break
- case 'aa':
- uni.showToast({
- title: '设备正在使用',
- duration: 3000,
- icon: 'none'
- })
- break
- case 'ab':
- uni.showToast({
- title: '结束失败,设备对结束代码检验失败',
- duration: 3000,
- icon: 'none'
- })
- break
- default:
- break
- }
- })
- },
- /**
- * 关闭连接
- */
- closeBLEConnection() {
- uni.closeBLEConnection({
- deviceId: this.deviceId,
- success: () => {
- // 关闭蓝牙模块
- this.closeBluetoothAdapter()
- },
- fail: () => {
- uni.showToast({
- icon: 'none',
- title: '蓝牙连接断开失败',
- duration: 3000,
- success: (res1) => {
- this.setInit()
- }
- })
- }
- })
- },
- closeBluetoothAdapter() {
- // 关闭蓝牙模块
- uni.closeBluetoothAdapter({
- success: () => {
- this.setInit()
- if (this.isUserClose == false) { // 设备端结束用水
- if (this.isChecked) {
- // console.log('设备端已结束用水,蓝牙断开成功')
- uni.showToast({
- icon: 'none',
- title: '设备端已结束用水,蓝牙断开成功',
- duration: 3000
- })
- } else {
- // console.log('用户重启扫描,蓝牙断开成功')
- uni.showToast({
- icon: 'none',
- title: '用户重启扫描,蓝牙断开成功',
- duration: 3000
- })
- }
- } else { // 手机移动端结束用水
- // console.log('APP端已结束用水,蓝牙断开成功')
- uni.showToast({
- icon: 'none',
- title: 'APP端已结束用水,蓝牙断开成功',
- duration: 3000
- })
- }
- }
- })
- },
- setInit() {
- setTimeout(() => {
- this.isUserClose = false
- this.isChecked = false
- this.isConnected = false
- this.isScan = false
- this.active = -1
- this.devices = [] // 保存设备
- this.device_code = '' // 设备编号
- this.deviceId = ''
- this.serviceId = ''
- this.characteristicIdForWrite = ''
- this.characteristicIdForNotity = ''
- this.deviceIsNotify = false
- this.deviceIsWrite = false
- this.byte35 = '' // 70 个字符
- this.byte35_tmp = '' // 70 个字符 临时变量
- // this.start_code = ''
- })
- },
- /**
- * 10===========10==================10
- * 发送数据到设备
- */
- send(val) {
- let buffer
- let dataView
- let newArr
- if (val == 'start') { // 启动设备
- this.isUserClose = false
- this.process_control(3)
- // this.createBLEConnection()
- // 发送 17字节 启动代码(会返回 35字节 的数据)
- // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
- newArr = str2StrArray(this.start_code)
- } else if (val == 'stop') { // 结束当前使用,设备成功返回:AD+35字节 消费结算代码
- this.isUserClose = true
- // 发送 AAABAD+17字节 启动代码(会返回 ad+35字节 的数据)
- // adb58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0104bc
- newArr = ['AA', 'AB', 'AD'].concat(str2StrArray(this.start_code))
- } else {
- // 发送 AAABAC 采集数据(会返回 35字节 的数据)
- // b58000265243eb3dd4bb3c5a2ab9042efcf0d2a30a000000000000000014392a0103bc
- newArr = ['AA', 'AB', 'AC']
- }
- buffer = new ArrayBuffer(newArr.length)
- dataView = new DataView(buffer)
- newArr.forEach((item, i) => {
- dataView.setUint8(i, sixteen_to_ten(item))
- })
- // 写特征值数据到设备
- if (this.deviceIsWrite) {
- // 写数据到设备
- this.writeBLECharacteristicValue(val, buffer)
- } else {
- uni.showToast({
- title: '设备不可写',
- duration: 3000
- })
- }
- },
- /**
- * 11====================11=====================11
- * 写数据到设备
- */
- writeBLECharacteristicValue(val, buffer) {
- uni.writeBLECharacteristicValue({
- deviceId: this.deviceId,
- // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
- serviceId: this.serviceId,
- // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
- characteristicId: this.characteristicIdForWrite,
- // 这里的value是ArrayBuffer类型
- value: buffer,
- success: (res) => {
- // console.log(res)
- if (val == 'start') {
- setTimeout(() => {
- this.isChecked = true
- this.isConnected = true
- }, 300)
- this.process_control(3)
- } else if (val == 'stop') {
- setTimeout(() => {
- this.isChecked = false
- }, 300)
- }
- },
- fail: (err) => {
- // console.log(err)
- if (err.errCode == 10006) {
- setTimeout(() => {
- this.setInit()
- }, 300)
- if (this.isConnected) {
- uni.showToast({
- title: '设备连接已断开!请在水表上手动停止!',
- icon: 'none'
- })
- } else {
- uni.showToast({
- title: '设备连接失败!请重试扫描连接!',
- icon: 'none'
- })
- }
- }
- }
- })
- },
- /**
- * 请求获取设备启动码
- */
- async request_device_start_code() {
- const res = await this.$myRequest({
- host: 'code',
- url: '/HotWater/wxpaystart.action',
- data: {
- collect_code: this.byte35,
- card_number: this.card_number,
- user_name: this.user_name
- }
- })
- // console.log(res.data.start_code)
- if (res.data.start_code != '') {
- this.start_code = res.data.start_code
- this.send('start')
- } else {
- uni.showToast({
- icon: 'none',
- title: '未获得启动码',
- duration: 3000
- })
- }
- },
- /**
- * 请求结算
- */
- async request_pay() {
- const res = await this.$myRequest({
- host: 'code',
- url: '/HotWater/wxpayconsume.action',
- data: {
- collect_code: this.byte35.substring(2)
- }
- })
- // console.log(res)
- if (res.data.mess === "支付成功!") {
- uni.showToast({
- icon: 'success',
- title: '结算成功!',
- success: (res1) => {
- this.use_amount = res.data.use_amount.toFixed(2)
- this.amount = (this.amount - this.use_amount).toFixed(2)
- // 刷新选定的月份消费记录
- this.request_consumption_records()
- }
- })
- } else {
- uni.showToast({
- icon: 'success',
- title: '结算失败!',
- duration: 3000
- })
- }
- }
- }
- }
- /**
- * 返回字符处理
- */
- function str2StrArray(start_code) {
- if (start_code.length != 34) {
- console.log('启动码长度错误')
- throw new Error("启动码长度错误");
- return
- }
- var resultArray = new Array()
- for (var i = 0; i < start_code.length; i += 2) {
- resultArray.push(start_code.substr(i, 2));
- }
- return resultArray;
- }
- /**
- * ArrayBuffer转16进度字符串
- */
- function ab2hex(buffer) {
- const hexArr = Array.prototype.map.call(
- new Uint8Array(buffer),
- function(bit) {
- // console.log('====================' + bit + '==========================')
- return ('00' + bit.toString(16)).slice(-2)
- }
- )
- return hexArr.join('');
- }
- /**
- * 16进制转10进制
- */
- function sixteen_to_ten(str) {
- return parseInt(str, 16);
- }
- </script>
- <style scoped lang="scss">
- .content {
- width: 100%;
- height: 100%;
- .container {
- width: 100%;
- padding: 20rpx 40rpx;
- box-sizing: border-box;
- .header {
- display: flex;
- justify-content: center;
- position: relative;
- height: 200rpx;
- .scan {
- display: flex;
- flex-direction: column;
- justify-content: center;
- align-items: center;
- height: 200rpx;
- width: 30%;
- .iconfont {
- margin-bottom: 10rpx;
- font-size: 100rpx;
- color: $my-color-primary;
- }
- }
- .scan_hover {
- border-radius: 15rpx;
- background-color: $my-color-btn-background;
- }
- .qinshihao {
- position: fixed;
- top: 20rpx;
- right: 40rpx;
- font-size: 28rpx;
- .icon-zhuye {
- margin-right: 10rpx;
- font-family: Microsoft YaHei-3970(82674968);
- color: $my-color-primary;
- }
- }
- }
- .start_text {
- .start_top,
- .start_mid {
- display: flex;
- justify-content: space-between;
- height: 100rpx;
- align-items: center;
- border-bottom: 2rpx #ccc solid;
- .item_left {
- display: flex;
- align-items: center;
- }
- .start_mid_txt {
- color: #333;
- font-size: 28rpx;
- }
- .iconfont {
- font-size: 50rpx;
- color: $my-color-primary;
- margin-right: 20rpx;
- }
- .start_payamount {
- display: flex;
- justify-content: flex-end;
- align-items: center;
- font-size: 38rpx;
- padding: 10rpx;
- font-family: Microsoft YaHei-3970(82674968);
- color: #B3B3B3;
- border-radius: 10rpx;
- .icon-dayuhao {
- margin: 0 0 0 20rpx;
- font-size: 38rpx;
- color: #B3B3B3;
- }
- }
- }
- .start_bot {
- display: flex;
- justify-content: flex-end;
- height: 100rpx;
- align-items: center;
- .start_bot_txt {
- margin-left: 20rpx;
- color: #333;
- font-size: 28rpx;
- }
- }
- }
- .start_up_process {
- padding: 20rpx 0;
- color: #007AFF;
- }
- .title {
- height: 100rpx;
- margin: 0 -40rpx;
- padding: 0 40rpx;
- line-height: 100rpx;
- color: #808080;
- font-size: 32rpx;
- background: #f2f2f2;
- }
- .record-item {
- width: 100%;
- .uni-list-cell-db {
- height: 100rpx;
- line-height: 100rpx;
- font-weight: bold;
- .uni-input {
- display: flex;
- align-items: center;
- text:nth-child(1) {
- margin-right: 10rpx;
- }
- }
- }
- .item-list {
- height: 80rpx;
- display: flex;
- align-items: center;
- justify-content: space-between;
- border-top: 2rpx #ccc solid;
- .icon-shijian {
- margin-right: 8rpx;
- }
- .icon-shuidi {
- margin-right: 3rpx;
- }
- .item-list-txt {
- font-size: 28rpx;
- &:nth-of-type(2) {
- color: #515151;
- }
- &:nth-of-type(3) {
- color: #f76200;
- font-size: 32rpx;
- }
- }
- }
- }
- }
- }
- </style>
|