Files
smart_storage_app/pages/home-page/policy-Config/index.vue

279 lines
7.4 KiB
Vue
Raw Normal View History

2026-03-09 15:02:49 +08:00
<template>
2026-03-10 16:36:41 +08:00
<view style="height: 100vh">
<view class="">
2026-03-13 17:33:00 +08:00
<u-navbar :title="$t('homePage.policy.policyTitle')" :is-back="true" title-color="#FFF"
:background="background" :border-bottom="false" :custom-back="toback">
2026-03-10 16:36:41 +08:00
</u-navbar>
</view>
<view class="policeForm">
2026-03-13 17:33:00 +08:00
<u-form ref="form" labelPosition="top">
<view class="" v-for="(item, index) in smallArr" :key="index">
<view class="" v-if="item.type == 'Input'">
<u-form-item :label="item[`label_${lang}`]" :prop="item.prop">
<u-input :key="'input_' + item.prop + '_' + item.inputKey" :min="item.min" :max="item.max"
:value="item.value" type="number" :placeholder="item[`place_${lang}`]"
@input="handleInput(item, $event)" />
</u-form-item>
</view>
<view class="" v-if="item.type == 'Switch'">
<u-form-item :label="item[`label_${lang}`]" :prop="item.prop">
<u-switch :size="40" v-model="item.value"></u-switch>
</u-form-item>
</view>
<view class="" v-if="item.type == 'Select'" @click="selectShow(item, index)">
<u-form-item :label="item[`label_${lang}`]" :prop="item.prop">
<view class="" v-show="false">
<u-input v-model="item.value" disabled @click="selectShow(item, index)" />
</view>
<view class="">
{{ item.selectLabel || $t("homePage.alarm.placeSelect") }}
</view>
<u-select :key="item.prop" @confirm="confirm($event, item)" v-model="item.isShow"
:list="getSelectList(item)"></u-select>
</u-form-item>
</view>
2026-03-10 16:36:41 +08:00
</view>
2026-03-13 17:33:00 +08:00
</u-form>
2026-03-09 15:02:49 +08:00
2026-03-10 16:36:41 +08:00
<button @click="submitDevice" type="success" size="mini" style="
2026-03-09 15:02:49 +08:00
background-color: #009458;
padding: 10rpx 0;
color: #fff;
margin-top: 40rpx;
width: 100%;
2026-03-10 16:36:41 +08:00
">
{{ $t("homePage.mine.submit") }}
</button>
</view>
2026-03-13 17:33:00 +08:00
<u-toast ref="uToast" />
2026-03-10 16:36:41 +08:00
</view>
2026-03-09 15:02:49 +08:00
</template>
<script>
2026-03-10 16:36:41 +08:00
import {
formList
} from "@/common/form.js";
import {
Langlist
} from "@/common/lang";
2026-03-11 10:55:28 +08:00
import mqtt from "@/uni_modules/leliven-mqtt/index.js";
2026-03-13 17:33:00 +08:00
import pako from "pako";
2026-03-10 16:36:41 +08:00
export default {
2026-03-11 08:47:54 +08:00
2026-03-10 16:36:41 +08:00
data() {
return {
formList: formList,
background: {
backgroundColor: "#0EA17E",
},
2026-03-13 17:33:00 +08:00
2026-03-10 16:36:41 +08:00
mqttClient: null,
backData: {},
smallArr: [],
2026-03-11 10:55:28 +08:00
user: {
username: 'admin', // 设置用户名
password: 'zzkj@688737' // 设置密码
}
2026-03-11 08:47:54 +08:00
2026-03-10 16:36:41 +08:00
};
},
computed: {
2026-03-13 17:33:00 +08:00
lang() {
return Langlist.find(v => v.value == this.$store.state.vuex_language).prop || 'en'
2026-03-11 08:47:54 +08:00
},
currentStation() {
2026-03-13 17:33:00 +08:00
return this.$store.state.vuex_currentStation;
2026-03-10 16:36:41 +08:00
},
},
2026-03-11 08:47:54 +08:00
2026-03-10 16:36:41 +08:00
onShow() {
2026-03-11 10:55:28 +08:00
this.initmqttClient()
2026-03-10 16:36:41 +08:00
},
2026-03-11 08:47:54 +08:00
2026-03-10 16:36:41 +08:00
methods: {
2026-03-11 10:55:28 +08:00
initmqttClient() {
2026-03-13 17:33:00 +08:00
2026-03-11 10:55:28 +08:00
// 初始化客户端连接地址
// #ifdef H5
2026-03-13 17:33:00 +08:00
let mqttClient = mqtt.connect('ws://1.95.170.86:8083/mqtt', {
2026-03-11 10:55:28 +08:00
...this.user
2026-03-11 08:47:54 +08:00
})
2026-03-11 10:55:28 +08:00
// #endif
// #ifdef MP-WEIXIN||APP-PLUS
2026-03-13 17:33:00 +08:00
let mqttClient = mqtt.connect('wx://1.95.170.86:8083/mqtt', {
2026-03-11 10:55:28 +08:00
...this.user
2026-03-11 08:47:54 +08:00
})
2026-03-11 10:55:28 +08:00
// #endif
this.mqttClient = mqttClient
// 连接MQ服务
mqttClient.on('connect', function(res) {
console.log('连接成功')
// 订阅主题
2026-03-13 17:33:00 +08:00
mqttClient.subscribe(`1/ems/zzkj0002/control/response`, function(err) {
2026-03-11 10:55:28 +08:00
if (!err) {
// 订阅成功
2026-03-13 17:33:00 +08:00
mqttClient.publish('1/app/zzkj0002/control/request', JSON.stringify({
fun: 'GET',
type: 'WJ_Get_NewControlSystem',
content: 0
}))
2026-03-11 10:55:28 +08:00
}
2026-03-11 08:47:54 +08:00
})
2026-03-11 10:55:28 +08:00
}).on('reconnect', function() {
console.log('重新连接')
}).on('error', function() {
console.log('连接失败')
}).on('end', function() {
console.log('连接End')
2026-03-13 17:33:00 +08:00
}).on('message', (topic, message) => {
console.log(this.decodeGzipBase64(message.toString()))
let data = this.decodeGzipBase64(message.toString())
if (data.content && data.content.params) {
this.control(data.content)
} else
if (data.content == 1) {
this.$refs.uToast.show({
title: this.$t('homePage.mine.operateSuccess'),
type: 'success',
})
} else {
uni.showToast({
title: this.$t('homePage.mine.operateFail'),
icon: 'none'
})
}
2026-03-11 08:47:54 +08:00
})
2026-03-10 16:36:41 +08:00
},
2026-03-11 08:47:54 +08:00
2026-03-10 16:36:41 +08:00
decodeGzipBase64(base64Str) {
// 1. base64 → Uint8Array
const binaryStr = atob(base64Str);
const len = binaryStr.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryStr.charCodeAt(i);
}
// 2. gunzip 解压
const decompressed = pako.ungzip(bytes, {
to: "string"
});
// 3. 转 JSON如果是 JSON
return JSON.parse(decompressed);
},
getSelectList(item) {
if (item.selectArr && item.selectArr.length > 0) {
return item.selectArr.map(option => ({
value: option.value.toString(),
label: option[`label_${this.lang}`] || option.label
}))
}
return []
},
control(NewControlSystemShow_data) {
2026-03-13 17:33:00 +08:00
console.log(NewControlSystemShow_data)
2026-03-10 16:36:41 +08:00
this.formList.forEach(item => {
if (item.type == "Switch") {
item.value = NewControlSystemShow_data?.params[item.prop] ? true : false
2026-03-13 17:33:00 +08:00
}
if (item.type == "Input") {
2026-03-10 16:36:41 +08:00
item.value = NewControlSystemShow_data?.params[item.prop] || item.default
}
2026-03-13 17:33:00 +08:00
2026-03-10 16:36:41 +08:00
if (item.type == 'Select') {
item.selectLabel = item.selectArr.find(v => v.value == item.value)?.[`label_${this.lang}`]
}
item.isShow = false
})
this.backData = NewControlSystemShow_data
this.smallArr = this.formList.filter(v => NewControlSystemShow_data?.smallScreen.includes(v.prop))
},
confirm(val, item) {
if (val && val.length > 0) {
item.value = val[0].value
item.selectLabel = val[0].label
}
item.isShow = false
},
2026-03-13 17:33:00 +08:00
handleInput(item, value) {
2026-03-10 16:36:41 +08:00
try {
2026-03-13 17:33:00 +08:00
// u-input 组件 @input 事件直接传递 value 值,不是 event 对象
let iptval = String(value || '').replace(/[^0-9.]/g, '');
2026-03-10 16:36:41 +08:00
const parts = iptval.split('.');
if (parts.length > 2) {
iptval = parts[0] + '.' + parts.slice(1).join('');
}
if (!iptval || iptval === '.') {
item.value = 0;
return;
}
let numVal = Number(iptval);
2026-03-13 17:33:00 +08:00
numVal = isNaN(numVal) ? 0 : numVal;
// 检查是否需要强制更新(值被限制)
let needForceUpdate = false;
if (item.min !== undefined && item.min !== null && numVal < item.min) {
numVal = item.min;
needForceUpdate = true;
2026-03-10 16:36:41 +08:00
}
2026-03-13 17:33:00 +08:00
if (item.max !== undefined && item.max !== null && numVal > item.max) {
numVal = item.max;
needForceUpdate = true;
2026-03-10 16:36:41 +08:00
}
2026-03-13 17:33:00 +08:00
2026-03-10 16:36:41 +08:00
item.value = numVal;
2026-03-13 17:33:00 +08:00
// 如果值被限制,强制重新渲染输入框
if (needForceUpdate) {
this.$set(item, 'inputKey', (item.inputKey || 0) + 1);
}
2026-03-10 16:36:41 +08:00
} catch (error) {
console.log('handleInput方法错误:', error);
}
},
2026-03-11 08:47:54 +08:00
2026-03-10 16:36:41 +08:00
toback() {
uni.navigateBack()
},
selectShow(val, index) {
val.isShow = true
},
submitDevice() {
let smallParam = this.smallArr.reduce((pre, cur) => {
pre[cur.prop] = cur.type == "Switch" ? cur.value ? 1 : 0 : cur.value
pre[cur.prop] = Number(pre[cur.prop])
return pre
}, {})
2026-03-13 17:33:00 +08:00
this.mqttClient.publish(`1/app/zzkj0002/control/request`, JSON.stringify({
2026-03-11 10:55:28 +08:00
fun: 'SET',
type: 'WJ_Set_NewControlSystem',
content: {
...this.backData,
params: {
...this.backData.params,
...smallParam
2026-03-10 16:36:41 +08:00
}
2026-03-11 10:55:28 +08:00
}
}))
2026-03-11 08:47:54 +08:00
2026-03-10 16:36:41 +08:00
},
},
};
2026-03-09 15:02:49 +08:00
</script>
<style lang="scss" scoped>
2026-03-10 16:36:41 +08:00
.policeForm {
margin: 22rpx;
padding: 22rpx;
background-color: #fff;
box-shadow: 0px 4rpx 16rpx rgba(0, 0, 0, 0.1);
border-radius: 8rpx;
}
</style>