index.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. import React, { useState } from 'react';
  2. import { StatusColorEnum } from '@/components/BadgeStatus';
  3. import { Ellipsis, TableCard } from '@/components';
  4. import '@/style/common.less';
  5. import '../../index.less';
  6. import styles from './index.less';
  7. import type { SceneItem } from '@/pages/rule-engine/Scene/typings';
  8. import { CheckOutlined, DownOutlined, UpOutlined } from '@ant-design/icons';
  9. import classNames from 'classnames';
  10. import { ActionsType, BranchesThen } from '@/pages/rule-engine/Scene/typings';
  11. import MyTooltip from './MyTooltip';
  12. const imageMap = new Map();
  13. imageMap.set('timer', require('/public/images/scene/scene-timer.png'));
  14. imageMap.set('manual', require('/public/images/scene/scene-hand.png'));
  15. imageMap.set('device', require('/public/images/scene/scene-device.png'));
  16. const iconMap = new Map();
  17. iconMap.set('timer', require('/public/images/scene/trigger-type-icon/timing.png'));
  18. iconMap.set('manual', require('/public/images/scene/trigger-type-icon/manual.png'));
  19. iconMap.set('device', require('/public/images/scene/trigger-type-icon/device.png'));
  20. // @ts-ignore
  21. export interface SceneCardProps extends SceneItem {
  22. detail?: React.ReactNode;
  23. tools?: React.ReactNode[];
  24. avatarSize?: number;
  25. className?: string;
  26. onUnBind?: (e: any) => void;
  27. showBindBtn?: boolean;
  28. cardType?: 'bind' | 'unbind';
  29. showTool?: boolean;
  30. onClick?: () => void;
  31. }
  32. enum TriggerWayType {
  33. manual = '手动触发',
  34. timer = '定时触发',
  35. device = '设备触发',
  36. }
  37. enum UnitEnum {
  38. seconds = '秒',
  39. minutes = '分',
  40. hours = '小时',
  41. }
  42. const selectorRender = (obj: any) => {
  43. switch (obj?.selector) {
  44. case 'all':
  45. return (
  46. <span>
  47. 所有的<span className={styles['trigger-device']}>{obj?.productId}</span>
  48. </span>
  49. );
  50. case 'fixed':
  51. return (
  52. <span>
  53. 设备
  54. <span className={styles['trigger-device']}>
  55. {(obj?.selectorValues || '').map((item: any) => item?.name).join(',')}
  56. </span>
  57. </span>
  58. );
  59. case 'org':
  60. return (
  61. <span>
  62. 部门
  63. <span className={styles['trigger-device']}>
  64. {(obj?.selectorValues || '').map((item: any) => item?.name).join(',')}
  65. </span>
  66. </span>
  67. );
  68. default:
  69. return '';
  70. }
  71. };
  72. const selectorContextRender = (obj: any) => {
  73. switch (obj?.selector) {
  74. case 'all':
  75. return `所有的${obj?.productId}`;
  76. case 'fixed':
  77. return `设备${(obj?.selectorValues || '').map((item: any) => item?.name).join(',')}`;
  78. case 'org':
  79. return `部门${(obj?.selectorValues || '').map((item: any) => item?.name).join(',')}`;
  80. default:
  81. return '';
  82. }
  83. };
  84. const timerRender = (timer: any) => {
  85. if (timer?.trigger && timer?.mod) {
  86. const trigger = timer?.trigger;
  87. const mod = timer?.mod;
  88. const str: string = trigger === 'week' ? '星期' : trigger === 'month' ? '月' : timer?.cron;
  89. if (mod === 'once') {
  90. return `,每${str}${timer.when.join('/')},${timer?.once.time}执行一次`;
  91. } else {
  92. return `每${str}${timer.when.join('/')},${timer?.period?.from}-${timer?.period.to},每${
  93. timer?.period.every
  94. }${UnitEnum[timer?.period?.unit]}执行一次`;
  95. }
  96. }
  97. return '';
  98. };
  99. const operatorRender = (operation: any) => {
  100. switch (operation?.operator) {
  101. case 'online':
  102. return '上线';
  103. case 'offline':
  104. return '离线';
  105. case 'reportEvent':
  106. return `上报事件${operation?.options?.eventName}`;
  107. case 'reportProperty':
  108. return `上报属性${(operation?.options?.propertiesName || []).join(',')}`;
  109. case 'readProperty':
  110. return `读取属性${(operation?.options?.propertiesName || []).join(',')}`;
  111. case 'writeProperty':
  112. return `修改属性${(operation?.options?.propertiesName || []).join(',')}`;
  113. case 'invokeFunction':
  114. return `调用功能${operation?.options?.functionName}`;
  115. default:
  116. return '';
  117. }
  118. };
  119. const notifyRender = (data: ActionsType | undefined) => {
  120. switch (data?.notify?.notifyType) {
  121. case 'dingTalk':
  122. return (
  123. <div>
  124. 向<span>{data?.options?.notifierName || data?.notify?.notifierId}</span>
  125. 通过<span className={styles['notify-img-highlight']}>钉钉</span>发送
  126. <span className={styles['notify-text-highlight']}>
  127. {data?.options?.templateName || data?.notify?.templateId}
  128. </span>
  129. </div>
  130. );
  131. case 'weixin':
  132. return (
  133. <div>
  134. 向<span className={styles['notify-text-highlight']}>{data?.options?.sendTo || ''}</span>
  135. <span className={styles['notify-text-highlight']}>{data?.options?.orgName || ''}</span>
  136. <span className={styles['notify-text-highlight']}>{data?.options?.tagName || ''}</span>
  137. 通过<span className={styles['notify-img-highlight']}>微信</span>发送
  138. <span className={styles['notify-text-highlight']}>
  139. {data?.options?.templateName || data?.notify?.templateId}
  140. </span>
  141. </div>
  142. );
  143. case 'email':
  144. return (
  145. <div>
  146. 向<span className={styles['notify-text-highlight']}>{data?.options?.sendTo || ''}</span>
  147. 通过<span className={styles['notify-img-highlight']}>邮件</span>发送
  148. <span className={styles['notify-text-highlight']}>
  149. {data?.options?.templateName || data?.notify?.templateId}
  150. </span>
  151. </div>
  152. );
  153. case 'voice':
  154. return (
  155. <div>
  156. <span className={styles['notify-text-highlight']}>
  157. {data?.options?.calledNumber || ''}
  158. </span>
  159. 通过<span className={styles['notify-img-highlight']}>语音</span>发送
  160. <span className={styles['notify-text-highlight']}>
  161. {data?.options?.templateName || data?.notify?.templateId}
  162. </span>
  163. </div>
  164. );
  165. case 'sms':
  166. return (
  167. <div>
  168. 向<span className={'notify-text-highlight'}>{data?.options?.sendTo || ''}</span>
  169. 通过<span className={'notify-img-highlight'}>短信</span>发送
  170. <span className={'notify-text-highlight'}>
  171. {data?.options?.templateName || data?.notify?.templateId}
  172. </span>
  173. </div>
  174. );
  175. case 'webhook':
  176. return (
  177. <div>
  178. 通过<span className={'notify-img-highlight'}>webhook</span>发送
  179. <span>{data?.options?.templateName || data?.notify?.templateId}</span>
  180. </div>
  181. );
  182. default:
  183. return null;
  184. }
  185. };
  186. const deviceRender = (data: ActionsType | undefined) => {
  187. switch (data?.device?.selector) {
  188. case 'relation':
  189. return (
  190. <div>
  191. {data?.options?.type || ''}与
  192. <span className={styles['notify-text-highlight']}>{data?.options?.deviceName || ''}</span>
  193. 具有相同
  194. <span className={styles['notify-text-highlight']}>
  195. {data?.options?.relationName || ''}
  196. </span>
  197. <span className={styles['notify-text-highlight']}>
  198. {data?.options?.productName || ''}
  199. </span>
  200. 设备的
  201. <span className={styles['notify-text-highlight']}>
  202. {data?.options?.propertyName || data?.options?.functionName || ''}
  203. </span>
  204. </div>
  205. );
  206. case 'tag':
  207. return (
  208. <div>
  209. {data?.options?.type || ''}
  210. <span className={styles['notify-text-highlight']}>{data?.options?.deviceName || ''}</span>
  211. <span className={styles['notify-text-highlight']}>
  212. {data?.options?.productName || ''}
  213. </span>
  214. <span className={styles['notify-text-highlight']}>
  215. {data?.options?.propertyName || data?.options?.functionName || ''}
  216. </span>
  217. </div>
  218. );
  219. default:
  220. return (
  221. <div>
  222. {/*{data?.options?.type || ''}*/}
  223. {/*<span className={styles['notify-text-highlight']}>{data?.options?.deviceName || ''}</span>*/}
  224. {/*<span className={styles['notify-text-highlight']}>*/}
  225. {/* {data?.options?.propertyName || data?.options?.functionName || ''}*/}
  226. {/*</span>*/}
  227. 打开空调动作1执行结果=成功并且 湿度24
  228. </div>
  229. );
  230. }
  231. };
  232. const actionRender = (action: ActionsType, index: number) => {
  233. switch (action?.executor) {
  234. case 'notify':
  235. return (
  236. <div
  237. className={styles['card-item-content-action-item-right-item']}
  238. key={action?.key || index}
  239. >
  240. <div className={classNames(styles['trigger-contents'], 'ellipsis')}>
  241. {notifyRender(action)}
  242. </div>
  243. </div>
  244. );
  245. case 'delay':
  246. return (
  247. <div
  248. className={styles['card-item-content-action-item-right-item']}
  249. key={action?.key || index}
  250. >
  251. <div className={classNames(styles['trigger-contents'], 'ellipsis')}>
  252. <span style={{ fontWeight: 'bold' }}>
  253. {action?.delay?.time}
  254. {UnitEnum[action?.delay?.unit || '']}
  255. </span>
  256. 后执行后续动作
  257. </div>
  258. </div>
  259. );
  260. case 'device':
  261. return (
  262. <div
  263. className={styles['card-item-content-action-item-right-item']}
  264. key={action?.key || index}
  265. >
  266. <div className={classNames(styles['trigger-contents'], 'ellipsis')}>
  267. {deviceRender(action)}
  268. </div>
  269. </div>
  270. );
  271. case 'alarm':
  272. return (
  273. <div
  274. className={styles['card-item-content-action-item-right-item']}
  275. key={action?.key || index}
  276. >
  277. <div className={classNames(styles['trigger-contents'], 'ellipsis')}>
  278. 满足条件后将触发关联此场景的告警
  279. </div>
  280. </div>
  281. );
  282. default:
  283. return null;
  284. }
  285. };
  286. const conditionsRender = (when: any[], index: number) => {
  287. if (when.length) {
  288. return (when[index]?.terms || []).join('');
  289. }
  290. return '';
  291. };
  292. const branchesActionRender = (actions: any[]) => {
  293. if (actions && actions?.length) {
  294. const list: any[] = [];
  295. actions.map((item, index) => {
  296. const dt = actionRender(item, index);
  297. list.push(dt);
  298. });
  299. return list.map((item, index) => (
  300. <div className={styles['right-item-right-item-contents-item']}>
  301. <div style={{ minWidth: 40 }}>动作{index + 1}</div>
  302. {item}
  303. </div>
  304. ));
  305. }
  306. return '';
  307. };
  308. const ContentRender = (data: SceneCardProps) => {
  309. const [visible, setVisible] = useState<boolean>(false);
  310. const type = data.triggerType;
  311. if (
  312. type === 'device' &&
  313. (data.branches || [])?.length &&
  314. Object.keys(data?.trigger?.device || {}).length
  315. ) {
  316. const obj = data.trigger.device;
  317. const operation: any = data.trigger.device?.operation;
  318. return (
  319. <div className={styles['card-item-content-box']}>
  320. <MyTooltip
  321. placement="topLeft"
  322. title={`${
  323. selectorContextRender(obj) + timerRender(operation?.timer) + operatorRender(operation)
  324. }`}
  325. >
  326. <div className={classNames(styles['card-item-content-trigger'], 'ellipsis')}>
  327. {selectorRender(obj)}
  328. {operation ? (
  329. <span>
  330. {timerRender(operation?.timer)}
  331. {operatorRender(operation)}
  332. </span>
  333. ) : (
  334. ''
  335. )}
  336. </div>
  337. </MyTooltip>
  338. <div className={styles['card-item-content-action']}>
  339. {(visible ? data.branches || [] : (data?.branches || []).slice(0, 2)).map(
  340. (item: any, index) => {
  341. return (
  342. <div className={styles['card-item-content-action-item']} key={item?.key || index}>
  343. <div className={styles['card-item-content-action-item-left']}>
  344. {index === 0 ? '当' : '否则'}
  345. </div>
  346. <div className={styles['card-item-content-action-item-right']}>
  347. <div className={styles['card-item-content-action-item-right-item']}>
  348. <div className={styles['right-item-left']}>
  349. <MyTooltip title={conditionsRender(data.options?.terms || [], index)}>
  350. <div className={classNames(styles['trigger-conditions'], 'ellipsis')}>
  351. {conditionsRender(data.options?.terms || [], index)}
  352. </div>
  353. </MyTooltip>
  354. {item.shakeLimit?.enabled && (
  355. <MyTooltip
  356. title={`(${item.shakeLimit?.time}秒内发生${item.shakeLimit?.threshold}
  357. 次以上时执行一次)`}
  358. >
  359. <div className={classNames(styles['trigger-shake'], 'ellipsis')}>
  360. ({item.shakeLimit?.time}秒内发生{item.shakeLimit?.threshold}
  361. 次以上时执行一次)
  362. </div>
  363. </MyTooltip>
  364. )}
  365. </div>
  366. <div className={styles['right-item-right']}>
  367. {(item?.then || []).map((i: BranchesThen, _index: number) => (
  368. <div key={i?.key || _index} className={styles['right-item-right-item']}>
  369. <div className={styles['trigger-ways']}>
  370. {i.parallel ? '并行执行' : '串行执行'}
  371. </div>
  372. <div className={classNames(styles['right-item-right-item-contents'])}>
  373. {branchesActionRender(Array.isArray(i?.actions) ? i?.actions : [])}
  374. </div>
  375. </div>
  376. ))}
  377. </div>
  378. </div>
  379. </div>
  380. </div>
  381. );
  382. },
  383. )}
  384. {(data?.branches || []).length > 1 && (
  385. <div
  386. className={styles['trigger-actions-more']}
  387. onClick={(e) => {
  388. e.stopPropagation();
  389. setVisible(!visible);
  390. }}
  391. >
  392. 展开更多{!visible ? <DownOutlined /> : <UpOutlined />}
  393. </div>
  394. )}
  395. </div>
  396. </div>
  397. );
  398. } else if (type !== 'device' && data.branches?.length) {
  399. return (
  400. <div className={styles['card-item-content-box']}>
  401. <div className={styles['card-item-content-action']}>
  402. {data.branches?.length &&
  403. (data?.branches || []).map((item: any, index) => {
  404. return (
  405. <div className={styles['card-item-content-action-item']} key={item?.key || index}>
  406. <div className={styles['card-item-content-action-item-left']}>执行</div>
  407. <div className={styles['card-item-content-action-item-right']}>
  408. <div className={styles['card-item-content-action-item-right-item']}>
  409. <div className={styles['right-item-right']}>
  410. {(item?.then || []).map((i: BranchesThen, _index: number) => {
  411. return (
  412. <div key={i?.key || _index} className={styles['right-item-right-item']}>
  413. <div className={styles['trigger-ways']}>
  414. {i.parallel ? '并行执行' : '串行执行'}
  415. </div>
  416. <div className={classNames(styles['right-item-right-item-contents'])}>
  417. {branchesActionRender(Array.isArray(i?.actions) ? i?.actions : [])}
  418. </div>
  419. </div>
  420. );
  421. })}
  422. </div>
  423. </div>
  424. </div>
  425. </div>
  426. );
  427. })}
  428. </div>
  429. </div>
  430. );
  431. } else {
  432. return <div className={styles['card-item-content-box-empty']}>未配置规则</div>;
  433. }
  434. };
  435. export const ExtraSceneCard = (props: SceneCardProps) => {
  436. return (
  437. <TableCard
  438. status={props.state.value}
  439. statusText={props.state.text}
  440. statusNames={{
  441. started: StatusColorEnum.success,
  442. disable: StatusColorEnum.error,
  443. notActive: StatusColorEnum.warning,
  444. }}
  445. showTool={props.showTool}
  446. showMask={false}
  447. actions={props.tools}
  448. onClick={props.onClick}
  449. className={props.className}
  450. contentClassName={styles['content-class']}
  451. >
  452. <div className={'pro-table-card-item context-access'}>
  453. <div className={'card-item-avatar'}>
  454. <img width={88} height={88} src={imageMap.get(props.triggerType)} alt={''} />
  455. </div>
  456. <div className={'card-item-body'}>
  457. <div className={'card-item-header'}>
  458. <div className={'card-item-header-item'} style={{ maxWidth: '50%' }}>
  459. <Ellipsis
  460. title={props.name}
  461. style={{ fontSize: 18, opacity: 0.85, color: '#000', fontWeight: 'bold' }}
  462. />
  463. </div>
  464. <div className={'card-item-header-item'} style={{ maxWidth: '50%' }}>
  465. <Ellipsis
  466. title={props.description}
  467. style={{ color: 'rgba(0, 0, 0, 0.65)', margin: '3px 0 0 10px' }}
  468. />
  469. </div>
  470. </div>
  471. <ContentRender {...props} />
  472. </div>
  473. </div>
  474. <div className={styles['card-item-trigger-type']}>
  475. <div className={styles['card-item-trigger-type-text']}>
  476. <img height={16} src={iconMap.get(props.triggerType)} style={{ marginRight: 8 }} />
  477. {TriggerWayType[props.triggerType]}
  478. </div>
  479. </div>
  480. <div className={'checked-icon'}>
  481. <div>
  482. <CheckOutlined />
  483. </div>
  484. </div>
  485. </TableCard>
  486. );
  487. };
  488. export default (props: SceneCardProps) => {
  489. return (
  490. <TableCard
  491. showMask={false}
  492. detail={props.detail}
  493. showTool={props.showTool}
  494. actions={props.tools}
  495. status={props.state.value}
  496. statusText={props.state.text}
  497. statusNames={{
  498. started: StatusColorEnum.success,
  499. disable: StatusColorEnum.error,
  500. notActive: StatusColorEnum.warning,
  501. }}
  502. contentClassName={styles['content-class']}
  503. >
  504. <div className={'pro-table-card-item'} onClick={props.onClick}>
  505. <div className={'card-item-avatar'}>
  506. <img width={88} height={88} src={imageMap.get(props.triggerType)} alt={''} />
  507. </div>
  508. <div className={'card-item-body'}>
  509. <div className={'card-item-header'}>
  510. <div className={'card-item-header-item'} style={{ maxWidth: '50%' }}>
  511. <Ellipsis
  512. title={props.name}
  513. style={{ fontSize: 18, opacity: 0.85, color: '#000', fontWeight: 'bold' }}
  514. />
  515. </div>
  516. <div className={'card-item-header-item'} style={{ maxWidth: '50%' }}>
  517. <Ellipsis
  518. title={props.description}
  519. style={{ color: 'rgba(0, 0, 0, 0.65)', margin: '3px 0 0 10px' }}
  520. />
  521. </div>
  522. </div>
  523. <ContentRender {...props} />
  524. </div>
  525. </div>
  526. <div className={styles['card-item-trigger-type']}>
  527. <div className={styles['card-item-trigger-type-text']}>
  528. <img height={16} src={iconMap.get(props.triggerType)} style={{ marginRight: 8 }} />
  529. {TriggerWayType[props.triggerType]}
  530. </div>
  531. </div>
  532. </TableCard>
  533. );
  534. };