edit.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. <template>
  2. <div class="system-edit-dic-container">
  3. <el-dialog :title="(ruleForm.id !== 0 ? '修改' : '添加') + '告警'" v-model="isShowDialog" width="50%">
  4. <el-form :model="ruleForm" ref="formRef" :rules="rules" size="default" label-width="110px">
  5. <el-form-item label="名称" prop="name">
  6. <el-input v-model="ruleForm.name" placeholder="请输入名称" />
  7. </el-form-item>
  8. <el-form-item label="告警级别" prop="level">
  9. <el-radio-group v-model="ruleForm.level">
  10. <el-radio :label="item.level" v-for="item in levelData" :key="item.level">{{ item.name }}</el-radio>
  11. </el-radio-group>
  12. </el-form-item>
  13. <el-form-item label="选择产品" prop="productKey">
  14. <el-select v-model="ruleForm.productKey" filterable placeholder="请选择产品" @change="setType()">
  15. <el-option v-for="item in productData" :key="item.key" :label="item.name" :value="item.key">
  16. <span style="float: left">{{ item.name }}</span>
  17. <span style="float: right; font-size: 13px">{{ item.key }}</span>
  18. </el-option>
  19. </el-select>
  20. </el-form-item>
  21. <el-form-item label="选择设备" prop="deviceKey">
  22. <el-select v-model="ruleForm.deviceKey" filterable placeholder="请选择设备">
  23. <el-option label="全部" value="all">全部</el-option>
  24. <el-option v-for="item in sourceData" :key="item.key" :label="item.name" :value="item.key">
  25. <span style="float: left">{{ item.name }}</span>
  26. <span style="float: right; font-size: 13px">{{ item.key }}</span>
  27. </el-option>
  28. </el-select>
  29. </el-form-item>
  30. <el-form-item label="触发方式" prop="triggerType" v-if="ruleForm.productKey">
  31. <el-radio-group v-model="ruleForm.triggerType" @change="getRadio()">
  32. <el-radio :label="item.type" v-for="item in typeData" :key="item.type">{{ item.title }}</el-radio>
  33. </el-radio-group>
  34. </el-form-item>
  35. <el-form-item label="选择事件" prop="eventKey" v-if="ruleForm.triggerType === 4">
  36. <el-select v-model="ruleForm.eventKey" filterable placeholder="请选择事件" @change="eventTypeChange">
  37. <el-option v-for="item in eventList" :key="item.key" :label="item.name" :value="item.key"></el-option>
  38. </el-select>
  39. </el-form-item>
  40. <div v-if="ruleForm.triggerType>2" >
  41. <el-divider content-position="left">触发条件</el-divider>
  42. <div class="box-content">
  43. <div v-for="(item, index) in requestParams" :key="index">
  44. <div style="text-align: center" v-if="index > 0">
  45. <el-icon>
  46. <Top />
  47. </el-icon>
  48. <div>
  49. <el-select v-model="item.andOr" placeholder="选择条件关系">
  50. <el-option label="无" :value="0" />
  51. <el-option label="并且" :value="1" />
  52. <el-option label="或" :value="2" />
  53. </el-select>
  54. </div>
  55. <el-icon>
  56. <Bottom />
  57. </el-icon>
  58. </div>
  59. <div style="
  60. padding: 10px;
  61. border: 1px solid var(--next-border-color-light);
  62. background-color: var(--next-border-color-light);
  63. margin-bottom: 10px;
  64. position: relative;
  65. ">
  66. <div class="conicon" style="width: 100%; text-align: right; position: absolute; right: -8px; top: -8px; color: red">
  67. <el-icon @click="delParams(index)">
  68. <CircleClose />
  69. </el-icon>
  70. </div>
  71. <div style="display: flex">
  72. <el-divider content-position="left">参数设置</el-divider>
  73. </div>
  74. <div v-for="(aaa, bbb) in item.filters" :key="bbb">
  75. <div style="text-align: center" v-if="bbb > 0">
  76. <el-icon>
  77. <Top />
  78. </el-icon>
  79. <div>
  80. <el-select v-model="aaa.andOr" placeholder="选择条件关系" style="width: 150px">
  81. <el-option label="无" :value="0" />
  82. <el-option label="并且" :value="1" />
  83. <el-option label="或" :value="2" />
  84. </el-select>
  85. </div>
  86. <el-icon>
  87. <Bottom />
  88. </el-icon>
  89. </div>
  90. <div class="content-f">
  91. <el-select v-model="aaa.key" :placeholder="ruleForm.triggerType === 4 && !ruleForm.eventKey ? '请先选择事件' : '选择参数'" style="width: 320px">
  92. <el-option v-for="a in triData" :key="a.paramKey" :label="a.title" :value="a.paramKey" />
  93. </el-select>
  94. <el-select v-model="aaa.operator" placeholder="选择操作符" style="width: 320px">
  95. <el-option v-for="b in operData" :key="b.type" :label="b.title" :value="b.type" />
  96. </el-select>
  97. <el-input v-model="aaa.value" placeholder="请输入条件值" style="width: 320px" />
  98. <div class="conicon">
  99. <el-icon @click="delParamss(index, bbb)">
  100. <Delete />
  101. </el-icon>
  102. </div>
  103. </div>
  104. </div>
  105. <el-button type="primary" class="addbutton" @click="addParams(index)">增加条件</el-button>
  106. </div>
  107. </div>
  108. </div>
  109. <el-button type="success" class="addbutton" @click="addParamss">增加分组</el-button>
  110. </div>
  111. <el-divider content-position="left">执行动作</el-divider>
  112. <div class="box-content">
  113. <div v-for="(item, index) in action" :key="index">
  114. <div style="
  115. padding: 10px;
  116. border: 1px solid var(--next-border-color-light);
  117. background-color: var(--next-border-color-light);
  118. margin-bottom: 10px;
  119. position: relative;
  120. ">
  121. <div class="conicon" style="width: 100%; text-align: right; position: absolute; right: -8px; top: -8px; color: red">
  122. <el-icon @click="delAction(index)">
  123. <CircleClose />
  124. </el-icon>
  125. </div>
  126. <div style="display: flex">
  127. <el-divider content-position="left">消息通知</el-divider>
  128. </div>
  129. <div class="content-f">
  130. <el-select v-model="item.sendGateway" placeholder="请选择通知方式" style="width: 320px" @change="getNode(item.sendGateway, index)">
  131. <el-option v-for="a in notice_send_gateway" :key="a.value" :label="a.label" :value="a.value" />
  132. </el-select>
  133. <el-select v-model="item.noticeConfig" placeholder="请选择通知配置" style="width: 320px" @change="getTem(item.noticeConfig, index)">
  134. <el-option v-for="b in sendGatewayData[index]" :key="b.id" :label="b.title" :value="b.id" />
  135. </el-select>
  136. <el-select v-model="item.noticeTemplate" placeholder="请选择通知模板" style="width: 320px">
  137. <el-option v-for="c in noticeConfigData[index]" :key="c.id" :label="c.title" :value="c.id" />
  138. </el-select>
  139. </div>
  140. <div>
  141. <div style="display: flex; margin-bottom: 10px" v-for="(ph, phindex) in item.addressee" :key="phindex">
  142. <el-input v-model="ph.phone" placeholder="请输入接收人信息" style="width: 320px" />
  143. <el-icon style="width: 32px; height: 32px; font-size: 24px" v-if="phindex == 0" @click="AddPhone(index)">
  144. <CirclePlus />
  145. </el-icon>
  146. <el-icon style="width: 32px; height: 32px; font-size: 24px" v-if="phindex > 0" @click="DelPhone(index, phindex)">
  147. <Remove />
  148. </el-icon>
  149. </div>
  150. </div>
  151. </div>
  152. </div>
  153. </div>
  154. <el-button type="success" class="addbutton" @click="addAction">增加执行</el-button>
  155. </el-form>
  156. <template #footer>
  157. <span class="dialog-footer">
  158. <el-button @click="onCancel" size="default">取 消</el-button>
  159. <el-button type="primary" @click="onSubmit" size="default">{{ ruleForm.id !== 0 ? '修 改' : '添 加' }}</el-button>
  160. </span>
  161. </template>
  162. </el-dialog>
  163. </div>
  164. </template>
  165. <script lang="ts">
  166. import { reactive, toRefs, defineComponent, ref, unref, getCurrentInstance, watch } from 'vue';
  167. import api from '/@/api/datahub';
  168. import iotapi from '/@/api/device';
  169. import alarm from '/@/api/alarm';
  170. import notice from '/@/api/notice';
  171. import { ElMessage } from 'element-plus';
  172. import { Delete, CircleClose, Top, Bottom, CirclePlus, Remove } from '@element-plus/icons-vue';
  173. interface RuleFormState {
  174. id: number;
  175. name: string;
  176. triggerType: number;
  177. level: string;
  178. eventKey: string;
  179. productKey: string;
  180. deviceKey: string;
  181. triggerCondition: any;
  182. action: any;
  183. }
  184. interface DicState {
  185. isShowDialog: boolean;
  186. ruleForm: RuleFormState;
  187. rules: any;
  188. sourceData: any;
  189. productData: any;
  190. typeData: any;
  191. triData: any;
  192. operData: any;
  193. levelData: any;
  194. requestParams: any;
  195. triggerCondition: any;
  196. action: any;
  197. tempData: any;
  198. sendGatewayData: any;
  199. noticeConfigData: any;
  200. eventList: any;
  201. }
  202. export default defineComponent({
  203. name: 'Edit',
  204. components: { Delete, CircleClose, Top, Bottom, CirclePlus, Remove },
  205. setup(prop, { emit }) {
  206. const myRef = ref<HTMLElement | null>(null);
  207. const formRef = ref<HTMLElement | null>(null);
  208. const { proxy } = getCurrentInstance() as any;
  209. const { notice_send_gateway } = proxy.useDict('notice_send_gateway');
  210. const state = reactive<DicState>({
  211. id: 0,
  212. isShowDialog: false,
  213. sourceData: [],
  214. tempData: [],
  215. productData: [],
  216. typeData: [],
  217. triData: [],
  218. operData: [],
  219. levelData: [],
  220. sendGatewayData: [],
  221. noticeConfigData: [],
  222. eventList: [],
  223. action: [
  224. {
  225. sendGateway: '',
  226. noticeConfig: '',
  227. noticeTemplate: '',
  228. addressee: [
  229. {
  230. phone: '',
  231. },
  232. ],
  233. },
  234. ],
  235. requestParams: [
  236. {
  237. andOr: '',
  238. filters: [
  239. {
  240. key: '',
  241. operator: '',
  242. value: '',
  243. andOr: 0,
  244. },
  245. ],
  246. },
  247. ],
  248. ruleForm: {
  249. id: 0,
  250. name: '',
  251. triggerType: 1,
  252. eventKey: '',
  253. level: '',
  254. productKey: '',
  255. deviceKey: '',
  256. action: [
  257. {
  258. sendGateway: '',
  259. noticeConfig: '',
  260. noticeTemplate: '',
  261. addressee: {},
  262. },
  263. ],
  264. triggerCondition: [
  265. {
  266. andOr: '',
  267. filters: [
  268. {
  269. key: '',
  270. operator: '',
  271. value: '',
  272. andOr: 0,
  273. },
  274. ],
  275. },
  276. ],
  277. },
  278. rules: {
  279. name: [{ required: true, message: '告警名称不能为空', trigger: 'blur' }],
  280. level: [{ required: true, message: '告警级别不能为空', trigger: 'blur' }],
  281. productKey: [{ required: true, message: '请选择产品', trigger: 'blur' }],
  282. deviceKey: [{ required: true, message: '请选择设备', trigger: 'blur' }],
  283. },
  284. });
  285. // 打开弹窗
  286. const openDialog = (row: RuleFormState | null) => {
  287. resetForm();
  288. getDevData();
  289. if (row) {
  290. setType(true);
  291. alarm.common.detail(row.id).then((res: any) => {
  292. state.requestParams = res.data.condition.triggerCondition;
  293. let product_key=res.data.productKey;
  294. res.data.performAction.action.forEach(function (value: { sendGateway: any; noticeConfig: number; }, index: string | number) {
  295. notice.config.getList({ sendGateway: value.sendGateway }).then((res: any) => {
  296. state.sendGatewayData[index] = res.Data;
  297. });
  298. if (value.noticeConfig) {
  299. notice.template.configIddetail(value.noticeConfig).then((res: any) => {
  300. state.noticeConfigData[index] = [res];
  301. });
  302. }
  303. });
  304. state.action = res.data.performAction.action;
  305. state.action.forEach(function (value: { addressee: any[]; }, index: string | number) {
  306. state.action[index].addressee = value.addressee.map((p: any) => {
  307. return { phone: p };
  308. });
  309. });
  310. iotapi.product.event({key:res.data.productKey}).then((ress: any) => {
  311. state.eventList = ress || []
  312. state.ruleForm.eventKey=row.eventKey
  313. })
  314. state.ruleForm = res.data;
  315. if(product_key){
  316. alarm.common.trigger_type(product_key).then((res: any) => {
  317. state.typeData = res.list || [];
  318. });
  319. }
  320. });
  321. }
  322. state.isShowDialog = true;
  323. };
  324. //获取设备列表
  325. const getDevData = () => {
  326. iotapi.product.getLists({ status: 1 }).then((res: any) => {
  327. state.productData = res.product || [];
  328. });
  329. alarm.common.levelall('').then((res: any) => {
  330. state.levelData = res.list || [];
  331. });
  332. alarm.common.operator('').then((res: any) => {
  333. state.operData = res.list || [];
  334. });
  335. };
  336. const resetForm = () => {
  337. state.requestParams = [
  338. {
  339. andOr: '',
  340. filters: [
  341. {
  342. key: '',
  343. operator: '',
  344. value: '',
  345. andOr: 0,
  346. },
  347. ],
  348. },
  349. ];
  350. state.action = [
  351. {
  352. sendGateway: '',
  353. noticeConfig: '',
  354. noticeTemplate: '',
  355. addressee: [
  356. {
  357. phone: '',
  358. },
  359. ],
  360. },
  361. ];
  362. state.ruleForm = {
  363. id: 0,
  364. name: '',
  365. triggerType: 1,
  366. level: '',
  367. productKey: '',
  368. deviceKey: '',
  369. action: [
  370. {
  371. sendGateway: '',
  372. noticeConfig: '',
  373. noticeTemplate: '',
  374. addressee: {},
  375. },
  376. ],
  377. triggerCondition: [
  378. {
  379. andOr: '',
  380. filters: [
  381. {
  382. key: '',
  383. operator: '',
  384. value: '',
  385. andOr: 0,
  386. },
  387. ],
  388. },
  389. ],
  390. };
  391. };
  392. // 关闭弹窗
  393. const closeDialog = () => {
  394. state.isShowDialog = false;
  395. };
  396. // 取消
  397. const onCancel = () => {
  398. closeDialog();
  399. };
  400. const eventTypeChange = () => {
  401. gettriData()
  402. };
  403. watch(() => state.ruleForm.productKey, (key) => {
  404. if (!key) return
  405. // 切换产品时候重新获取事件列表,清空之前选中的事件
  406. state.ruleForm.eventKey = ''
  407. iotapi.product.event({ key }).then((res: any) => {
  408. state.eventList = res || []
  409. })
  410. })
  411. // 新增
  412. const onSubmit = () => {
  413. const formWrap = unref(formRef) as any;
  414. if (!formWrap) return;
  415. formWrap.validate((valid: boolean) => {
  416. if (valid) {
  417. state.ruleForm.triggerCondition = state.requestParams;
  418. state.action.forEach(function (value: { addressee: any[]; }, index: string | number) {
  419. state.action[index].addressee = value.addressee.map((p: { phone: any; }) => {
  420. return p.phone;
  421. });
  422. });
  423. state.ruleForm.action = state.action;
  424. if (state.ruleForm.id !== 0) {
  425. //修改
  426. alarm.common.edit(state.ruleForm).then(() => {
  427. ElMessage.success('告警修改成功');
  428. closeDialog(); // 关闭弹窗
  429. emit('typeList');
  430. });
  431. } else {
  432. //添加
  433. alarm.common.add(state.ruleForm).then(() => {
  434. ElMessage.success('告警添加成功');
  435. closeDialog(); // 关闭弹窗
  436. emit('dataList');
  437. });
  438. }
  439. }
  440. });
  441. };
  442. const AddPhone = (index: string | number) => {
  443. state.action[index].addressee.push({
  444. phone: '',
  445. });
  446. };
  447. const DelPhone = (index: string | number, bbb: any) => {
  448. state.action[index].addressee.splice(bbb, 1);
  449. };
  450. const addAction = () => {
  451. state.action.push({
  452. sendGateway: '',
  453. noticeConfig: '',
  454. noticeTemplate: '',
  455. addressee: [
  456. {
  457. phone: '',
  458. },
  459. ],
  460. });
  461. };
  462. const delAction = (index: any) => {
  463. state.action.splice(index, 1);
  464. };
  465. const delParams = (index: any) => {
  466. state.requestParams.splice(index, 1);
  467. };
  468. const delParamss = (index: string | number, bbb: any) => {
  469. state.requestParams[index].filters.splice(bbb, 1);
  470. };
  471. const addParamss = () => {
  472. state.requestParams.push({
  473. andOr: '',
  474. filters: [
  475. {
  476. key: '',
  477. operator: '',
  478. value: '',
  479. andOr: '',
  480. },
  481. ],
  482. });
  483. };
  484. const addParams = (index: string | number) => {
  485. state.requestParams[index].filters.push({
  486. key: '',
  487. operator: '',
  488. value: '',
  489. andOr: '',
  490. });
  491. };
  492. const setType = (notResetDeviceKey: boolean) => {
  493. !notResetDeviceKey && (state.ruleForm.deviceKey = '')
  494. let product_id = 0;
  495. state.productData.forEach((item: { key: string; id: number; }) => {
  496. if (item.key == state.ruleForm.productKey) {
  497. product_id = item.id;
  498. }
  499. });
  500. api.common.getdevList({ productId: product_id }).then((res: any) => {
  501. state.sourceData = res.device;
  502. });
  503. alarm.common.trigger_type(state.ruleForm.productKey).then((res: any) => {
  504. state.typeData = res.list || [];
  505. });
  506. gettriData();
  507. };
  508. const getRadio = () => {
  509. gettriData();
  510. }
  511. const gettriData = () => {
  512. // 清空之前设置的参数设置
  513. state.requestParams = [{
  514. andOr: '',
  515. filters: [
  516. {
  517. key: '',
  518. operator: '',
  519. value: '',
  520. andOr: 0,
  521. },
  522. ],
  523. }]
  524. setTriData()
  525. }
  526. function setTriData() {
  527. // 重置参数列表
  528. state.triData = [];
  529. const triggerType = state.ruleForm.triggerType
  530. const form = {
  531. productKey: state.ruleForm.productKey,
  532. triggerType
  533. }
  534. // 如果是事件上报,需要传eventKey参数
  535. if (triggerType === 4) {
  536. form.eventKey = state.ruleForm.eventKey
  537. if (!form.eventKey) return
  538. }
  539. alarm.common.trigger_params(form).then((res: any) => {
  540. state.triData = res.list || [];
  541. });
  542. }
  543. const getNode = (event: any, index: string | number) => {
  544. state.action[index].noticeConfig = '';
  545. notice.config.getList({ sendGateway: event }).then((res: any) => {
  546. state.sendGatewayData[index] = res.Data;
  547. });
  548. };
  549. const getTem = (event: number, index: string | number) => {
  550. state.action[index].noticeTemplate = '';
  551. notice.template.configIddetail(event).then((res: any) => {
  552. state.noticeConfigData[index] = [res];
  553. });
  554. };
  555. return {
  556. getRadio,
  557. gettriData,
  558. getTem,
  559. getNode,
  560. delAction,
  561. addAction,
  562. eventTypeChange,
  563. AddPhone,
  564. DelPhone,
  565. setType,
  566. addParams,
  567. addParamss,
  568. delParamss,
  569. delParams,
  570. openDialog,
  571. closeDialog,
  572. getDevData,
  573. onCancel,
  574. onSubmit,
  575. formRef,
  576. notice_send_gateway,
  577. myRef,
  578. ...toRefs(state),
  579. };
  580. },
  581. });
  582. </script>
  583. <style>
  584. .inline {
  585. display: inline-flex;
  586. }
  587. .el-input__wrapper {
  588. width: 98%;
  589. }
  590. .box-content {
  591. border: 1px solid #e8e8e8;
  592. margin: 10px;
  593. padding: 10px;
  594. }
  595. .content-f {
  596. display: flex;
  597. margin-bottom: 10px;
  598. }
  599. .content-f .el-input__wrapper {
  600. margin-right: 5px;
  601. }
  602. .addbutton {
  603. width: 100%;
  604. margin-top: 10px;
  605. background: #fff;
  606. border: 1px solid #d1d1d1;
  607. color: #8d8b8b;
  608. }
  609. .conicon {
  610. width: 55px;
  611. height: 25px;
  612. font-size: 28px;
  613. line-height: 28px;
  614. cursor: pointer;
  615. }
  616. .jv-node {
  617. margin-left: 25px;
  618. }</style>