feat: 新增场景联动-定时触发
This commit is contained in:
parent
084c43cf76
commit
2659c70804
|
@ -59,7 +59,7 @@ import { detail as deviceDetail } from '@/api/device/instance'
|
||||||
import Product from './Product.vue'
|
import Product from './Product.vue'
|
||||||
import DeviceSelect from './DeviceSelect.vue'
|
import DeviceSelect from './DeviceSelect.vue'
|
||||||
import Type from './Type.vue'
|
import Type from './Type.vue'
|
||||||
import { continuousValue, timeUnitEnum } from '@/views/rule-engine/Scene/Save/components/Timer/util'
|
import {continuousValue, handleTimerOptions, timeUnitEnum} from '@/views/rule-engine/Scene/Save/components/Timer/util'
|
||||||
|
|
||||||
type Emit = {
|
type Emit = {
|
||||||
(e: 'cancel'): void
|
(e: 'cancel'): void
|
||||||
|
@ -161,46 +161,50 @@ const handleOptions = (data: TriggerDeviceOptions) => {
|
||||||
|
|
||||||
if (data.timer) {
|
if (data.timer) {
|
||||||
const _timer = data.timer;
|
const _timer = data.timer;
|
||||||
if (_timer.trigger === 'cron') {
|
const { time, extraTime, when } = handleTimerOptions(_timer)
|
||||||
_options.time = _timer.cron;
|
_options.when = when;
|
||||||
} else {
|
_options.time = time;
|
||||||
// console.log('continuousValue', continuousValue(_timer.when! || [], _timer!.trigger))
|
_options.extraTime = extraTime;
|
||||||
let whenStr = '每天';
|
// if (_timer.trigger === 'cron') {
|
||||||
if (_timer.when!.length) {
|
// _options.time = _timer.cron;
|
||||||
whenStr = _timer!.trigger === 'week' ? '每周' : '每月';
|
// } else {
|
||||||
const whenStrArr = continuousValue(_timer.when! || [], _timer!.trigger);
|
// // console.log('continuousValue', continuousValue(_timer.when! || [], _timer!.trigger))
|
||||||
const whenStrArr3 = whenStrArr.splice(0, 3);
|
// let whenStr = '每天';
|
||||||
whenStr += whenStrArr3.join('、');
|
// if (_timer.when!.length) {
|
||||||
whenStr += `等${_timer.when!.length}天`;
|
// whenStr = _timer!.trigger === 'week' ? '每周' : '每月';
|
||||||
}
|
// const whenStrArr = continuousValue(_timer.when! || [], _timer!.trigger);
|
||||||
_options.when = whenStr;
|
// const whenStrArr3 = whenStrArr.splice(0, 3);
|
||||||
if (_timer.once) {
|
// whenStr += whenStrArr3.join('、');
|
||||||
_options.time = _timer.once.time + ' 执行1次';
|
// whenStr += `等${_timer.when!.length}天`;
|
||||||
} else if (_timer.period) {
|
// }
|
||||||
_options.time = _timer.period.from + '-' + _timer.period.to;
|
// _options.when = whenStr;
|
||||||
_options.extraTime = `每${_timer.period.every}${timeUnitEnum[_timer.period.unit]}执行1次`;
|
// if (_timer.once) {
|
||||||
}
|
// _options.time = _timer.once.time + ' 执行1次';
|
||||||
}
|
// } else if (_timer.period) {
|
||||||
|
// _options.time = _timer.period.from + '-' + _timer.period.to;
|
||||||
if (data.operator === 'online') {
|
// _options.extraTime = `每${_timer.period.every}${timeUnitEnum[_timer.period.unit]}执行1次`;
|
||||||
_options.type = '上线';
|
// }
|
||||||
_options.action = '';
|
// }
|
||||||
_options.typeIcon = 'icon-a-Group4713';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.operator === 'offline') {
|
|
||||||
_options.type = '离线';
|
|
||||||
_options.action = '';
|
|
||||||
_options.typeIcon = 'icon-a-Group4892';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.operator === 'reportProperty') {
|
|
||||||
_options.type = '属性上报';
|
|
||||||
_options.action = '';
|
|
||||||
_options.typeIcon = 'icon-file-upload-outline';
|
|
||||||
}
|
|
||||||
return _options;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (data.operator === 'online') {
|
||||||
|
_options.type = '上线';
|
||||||
|
_options.action = '';
|
||||||
|
_options.typeIcon = 'icon-a-Group4713';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.operator === 'offline') {
|
||||||
|
_options.type = '离线';
|
||||||
|
_options.action = '';
|
||||||
|
_options.typeIcon = 'icon-a-Group4892';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.operator === 'reportProperty') {
|
||||||
|
_options.type = '属性上报';
|
||||||
|
_options.action = '';
|
||||||
|
_options.typeIcon = 'icon-file-upload-outline';
|
||||||
|
}
|
||||||
|
return _options;
|
||||||
}
|
}
|
||||||
|
|
||||||
const prev = () => {
|
const prev = () => {
|
||||||
|
|
|
@ -0,0 +1,65 @@
|
||||||
|
<template>
|
||||||
|
<j-modal
|
||||||
|
title='触发规则'
|
||||||
|
visible
|
||||||
|
:width='820'
|
||||||
|
@click='save'
|
||||||
|
@cancel='cancel'
|
||||||
|
>
|
||||||
|
<Timer
|
||||||
|
ref='timerRef' v-model:value='addModel.timer'
|
||||||
|
/>
|
||||||
|
</j-modal>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts" name="timerAddModel">
|
||||||
|
import Timer from '../components/Timer'
|
||||||
|
import type { OperationTimer } from '@/views/rule-engine/Scene/typings'
|
||||||
|
import {nextTick, PropType} from "vue";
|
||||||
|
import {TriggerDevice} from "@/views/rule-engine/Scene/typings";
|
||||||
|
import {handleTimerOptions} from "@/views/rule-engine/Scene/Save/components/Timer/util";
|
||||||
|
|
||||||
|
type Emit = {
|
||||||
|
(e: 'cancel'): void
|
||||||
|
(e: 'save', data: TriggerDevice, options: Record<string, any>): void
|
||||||
|
}
|
||||||
|
|
||||||
|
const props = defineProps({
|
||||||
|
timer: {
|
||||||
|
type: Object as PropType<OperationTimer>,
|
||||||
|
default: () => ({})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const emit = defineEmits<Emit>()
|
||||||
|
|
||||||
|
const timerRef = ref()
|
||||||
|
|
||||||
|
interface AddModelType {
|
||||||
|
timer: OperationTimer
|
||||||
|
}
|
||||||
|
|
||||||
|
const addModel = reactive<AddModelType>({
|
||||||
|
timer: props.timer
|
||||||
|
})
|
||||||
|
|
||||||
|
const save = async () => {
|
||||||
|
const timerData = await timerRef.value?.validateFields()
|
||||||
|
if (timerData) {
|
||||||
|
const options = handleTimerOptions(timerData)
|
||||||
|
emit("save", timerData, options)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const cancel = () => {
|
||||||
|
emit("cancel")
|
||||||
|
}
|
||||||
|
|
||||||
|
nextTick(() => {
|
||||||
|
Object.assign(addModel, props.timer)
|
||||||
|
})
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
|
||||||
|
</style>
|
|
@ -1,30 +1,67 @@
|
||||||
<template>
|
<template>
|
||||||
<div class='timer'>
|
<div class='timer'>
|
||||||
<Timer ref='timerRef' v-model:value='data.trigger.timer' />
|
<j-form-item
|
||||||
|
:rules="rules"
|
||||||
|
name="timer"
|
||||||
|
>
|
||||||
|
<template #label>
|
||||||
|
<TitleComponent data='触发规则' style='font-size: 14px;' />
|
||||||
|
</template>
|
||||||
|
<AddButton
|
||||||
|
style='width: 100%'
|
||||||
|
@click='visible = true'
|
||||||
|
>
|
||||||
|
<Title :options='data.options.trigger' />
|
||||||
|
</AddButton>
|
||||||
|
</j-form-item>
|
||||||
|
<j-form-item
|
||||||
|
:rules="actionRules"
|
||||||
|
:name="['branches', 0, 'then']"
|
||||||
|
>
|
||||||
<Action
|
<Action
|
||||||
:thenOptions="data.branches ? data?.branches[0].then : []"
|
:thenOptions="data.branches ? data?.branches[0].then : []"
|
||||||
:name="0"
|
:name="0"
|
||||||
@add="onActionAdd"
|
|
||||||
@update="onActionUpdate"
|
|
||||||
/>
|
/>
|
||||||
|
</j-form-item>
|
||||||
|
<AddModel
|
||||||
|
v-if="visible"
|
||||||
|
@cancel='visible = false'
|
||||||
|
@save="save"
|
||||||
|
:value="data.trigger.device"
|
||||||
|
:options="data.options.trigger"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { useSceneStore } from '@/store/scene';
|
import { useSceneStore } from '@/store/scene';
|
||||||
import Action from '../action/index.vue';
|
|
||||||
import { storeToRefs } from 'pinia';
|
import { storeToRefs } from 'pinia';
|
||||||
import Timer from '../components/Timer'
|
import Action from '../action/index.vue';
|
||||||
|
import AddModel from './AddModal.vue'
|
||||||
|
import AddButton from '../components/AddButton.vue'
|
||||||
|
import type { OperationTimer } from '@/views/rule-engine/Scene/typings'
|
||||||
|
|
||||||
const sceneStore = useSceneStore();
|
const sceneStore = useSceneStore();
|
||||||
const { data } = storeToRefs(sceneStore);
|
const { data } = storeToRefs(sceneStore);
|
||||||
|
const visible = ref(false)
|
||||||
|
|
||||||
const onActionAdd = (_data: any) => {
|
const rules = [{
|
||||||
if (data.value?.branches && _data) {
|
validator(_: any, v: any) {
|
||||||
const newThen = [...data.value.branches[0].then, _data];
|
if (!v) {
|
||||||
data.value.branches![0].then = newThen
|
return Promise.reject(new Error('请配置定时触发规则'));
|
||||||
}
|
}
|
||||||
};
|
return Promise.resolve();
|
||||||
|
},
|
||||||
|
}]
|
||||||
|
|
||||||
|
const actionRules = [{
|
||||||
|
validator(_, v) {
|
||||||
|
if (!v || (v && !v.length)) {
|
||||||
|
return Promise.reject('至少配置一个执行动作');
|
||||||
|
}
|
||||||
|
return Promise.resolve();
|
||||||
|
},
|
||||||
|
}]
|
||||||
|
|
||||||
const onActionUpdate = (_data: any, type: boolean) => {
|
const onActionUpdate = (_data: any, type: boolean) => {
|
||||||
const indexOf = data.value.branches![0].then.findIndex(
|
const indexOf = data.value.branches![0].then.findIndex(
|
||||||
|
@ -38,6 +75,11 @@ const onActionUpdate = (_data: any, type: boolean) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const save = (data: OperationTimer, options: Record<string, any>) => {
|
||||||
|
data.value.trigger!.timer = data
|
||||||
|
data.value.options!.trigger = options
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { isArray } from 'lodash-es'
|
import { isArray } from 'lodash-es'
|
||||||
|
import type { OperationTimer } from "@/views/rule-engine/Scene/typings";
|
||||||
export const numberToString = {
|
export const numberToString = {
|
||||||
1: '星期一',
|
1: '星期一',
|
||||||
2: '星期二',
|
2: '星期二',
|
||||||
|
@ -50,3 +51,41 @@ export const continuousValue: continuousValueFn = (data, type) => {
|
||||||
}
|
}
|
||||||
return newArray;
|
return newArray;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type TimerOption = {
|
||||||
|
when?: string
|
||||||
|
time?: string
|
||||||
|
extraTime?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const handleTimerOptions = (timer: OperationTimer):TimerOption => {
|
||||||
|
let when = '每天'
|
||||||
|
let time = undefined
|
||||||
|
let extraTime = undefined
|
||||||
|
|
||||||
|
if (timer.trigger === 'cron') {
|
||||||
|
time = timer.cron
|
||||||
|
return { time }
|
||||||
|
}
|
||||||
|
|
||||||
|
if (timer.when?.length) {
|
||||||
|
when = timer!.trigger === 'week' ? '每周' : '每月';
|
||||||
|
const whenStrArr = continuousValue(timer.when! || [], timer!.trigger);
|
||||||
|
const whenStrArr3 = whenStrArr.splice(0, 3);
|
||||||
|
when += whenStrArr3.join('、');
|
||||||
|
when += `等${timer.when!.length}天`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (timer.once) {
|
||||||
|
time = timer.once.time + ' 执行1次';
|
||||||
|
} else if (timer.period) {
|
||||||
|
time = timer.period.from + '-' + timer.period.to;
|
||||||
|
extraTime = `每${timer.period.every}${timeUnitEnum[timer.period.unit]}执行1次`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
when,
|
||||||
|
time,
|
||||||
|
extraTime
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,7 +15,12 @@
|
||||||
<Manual v-else-if='data.triggerType === "manual"' />
|
<Manual v-else-if='data.triggerType === "manual"' />
|
||||||
<Timer v-else-if='data.triggerType === "timer"' />
|
<Timer v-else-if='data.triggerType === "timer"' />
|
||||||
<j-form-item>
|
<j-form-item>
|
||||||
<j-text-area
|
<j-textarea
|
||||||
|
v-model:value="data.description"
|
||||||
|
placeholder=''
|
||||||
|
:rows="4"
|
||||||
|
show-count
|
||||||
|
:maxLength="200"
|
||||||
/>
|
/>
|
||||||
</j-form-item>
|
</j-form-item>
|
||||||
</j-form>
|
</j-form>
|
||||||
|
|
Loading…
Reference in New Issue