sceneItem.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <template>
  2. <div class="type-item">
  3. <div v-for="(item, index) in sceneList" :key="index" class="item " :class="index > 0 ? 'biankang' : ''">
  4. <div class="conicon" style="width: 100%; text-align: right; position: relative; right: -8px; top: -8px; color: red" v-if="index > 0">
  5. <el-icon @click="delScene(index)">
  6. <CircleClose />
  7. </el-icon>
  8. </div>
  9. <div class="title flex">
  10. <div class="icon"></div>触发规则
  11. </div>
  12. <div class="product flex flex-warp">
  13. <el-form-item label="产品:" prop="productKey">
  14. <el-select v-model="item.productKey" filterable clearable placeholder="请选择产品" @change="seletChange(index, item.productKey!)">
  15. <el-option v-for="it in sourceData" :key="it.key" :label="it.name" :value="it.key">
  16. <span style="float: left">{{ it.name }}</span>
  17. <span style="float: right; font-size: 13px">{{ it.key }}</span>
  18. </el-option>
  19. </el-select>
  20. </el-form-item>
  21. <el-form-item label="设备:" prop="deviceKey">
  22. <el-select v-model="item.deviceKey" filterable clearable placeholder="请选择设备" @change="EditPen(index)">
  23. <el-option v-for="it in deviceListData" :key="it.key" :label="it.name" :value="it.key">
  24. <span style="float: left">{{ it.name }}</span>
  25. <span style="float: right; margin-left: 8px;font-size: 13px">{{ it.key }}</span>
  26. </el-option>
  27. </el-select>
  28. </el-form-item>
  29. <el-form-item label="触发类型:" prop="triggerType">
  30. <el-select v-model="item.triggerType" clearable filterable placeholder="请选择触发类型" @change="getSelectcolumns(index, item.triggerType!)">
  31. <el-option v-for="it in sence_source_type" :key="it.value" :label="it.label" :value="it.value">
  32. <span style="float: left">{{ it.label }}</span>
  33. <span style="float: right; font-size: 13px">{{ it.value }}</span>
  34. </el-option>
  35. </el-select>
  36. </el-form-item>
  37. <el-form-item label="定时请求" v-if="item.triggerType && ['readAttribute', 'functionCall'].includes(item.triggerType)">
  38. <div style="display:flex">
  39. <el-input v-model="item.timer" placeholder="请输入cron表达式" />
  40. <el-dialog v-model="dialogVisible" title="选择Cron规则" width="60%">
  41. <vue3cron @handlelisten="handlelisten" :type="index" @close="cronclose"></vue3cron>
  42. </el-dialog>
  43. <el-button type="success" @click="showCron()" style="margin-left: 5px;">设置</el-button>
  44. </div>
  45. </el-form-item>
  46. </div>
  47. <div class="title flex">
  48. <div class="icon"></div> 触发条件 <div class="ml10"> <el-switch v-model="item.triggerSwitch" @change="EditPen(index)" />
  49. </div>
  50. </div>
  51. <Condition :condition="item.condition" :operate_index="index" :columnList="columnList" v-if="item.triggerSwitch && columnList.length > 0" @EditPen="EditPen">
  52. </Condition>
  53. </div>
  54. <div>
  55. <div class="edit">
  56. <el-button type="primary" :icon="DocumentAdd" @click="addScene()">新增场景定义</el-button>
  57. </div>
  58. </div>
  59. </div>
  60. </template>
  61. <script lang="ts" setup>
  62. import { PropType, ref, unref, reactive, getCurrentInstance } from 'vue'
  63. import { DocumentAdd, CircleClose, Right } from '@element-plus/icons-vue';
  64. import vue3cron from '/@/components/vue3cron/vue3cron.vue';
  65. import api from '/@/api/scene';
  66. import product from '/@/api/device';
  67. import datahub from '/@/api/datahub';
  68. import Condition from './condition.vue';
  69. const { proxy } = getCurrentInstance() as any;
  70. const scene_type = proxy.useDict('scene_type');
  71. const { sence_source_type } = proxy.useDict('sence_source_type');
  72. const emit = defineEmits(['addScenesDetail', 'delScenesDetail', 'editScenesDetail']);
  73. const dialogVisible = ref();
  74. const deviceListData = ref<testIValueType[]>([]);
  75. const functionCallList = ref<testIValueType[]>([]);
  76. const propertyCallList = ref<testIValueType[]>([]);
  77. const columnList = ref([]);
  78. let product_key = "";
  79. interface IConditionItem {
  80. parameter?: string;
  81. operator?: string;
  82. value?: string;
  83. }
  84. interface IValueType {
  85. triggerType?: string;
  86. productKey?: string;
  87. deviceKey?: string;
  88. triggerSwitch?: boolean;
  89. timer?: string;
  90. condition?: IConditionItem[][]; // 更新这里的类型定义
  91. }
  92. interface testIValueType {
  93. id: string;
  94. key: string;
  95. name?: string;
  96. }
  97. const props = defineProps({
  98. sceneList: {
  99. type: Array as PropType<IValueType[]>,
  100. default: () => [],
  101. },
  102. sceneType: {
  103. type: String,
  104. default: () => '',
  105. },
  106. sourceData: {
  107. type: Array as PropType<testIValueType[]>,
  108. default: () => []
  109. },
  110. })
  111. const seletChange = (index: number, val: string) => {
  112. product_key = val;
  113. //根据产品key获取产品ID
  114. let info = props.sourceData?.find((pro: { key: any; }) => pro.key === val);
  115. if (info) {
  116. // 重置 deviceKey 的值
  117. props.sceneList[index].deviceKey = '';
  118. // 重置当前项的 condition 值
  119. props.sceneList[index].condition = [[{
  120. 'parameter': '',
  121. 'operator': '',
  122. 'value': ''
  123. }]];
  124. getDeviceList(info.id)
  125. }
  126. EditPen(index);
  127. }
  128. const getDeviceList = (_id: any) => {
  129. product.device.allList({ productId: _id }).then((res: any) => {
  130. deviceListData.value = res.device
  131. })
  132. }
  133. const getSelectcolumns = (index: number, val: string) => {
  134. EditPen(index);
  135. getcolumns(index, val);
  136. // 重置当前项的 condition 值
  137. props.sceneList[index].condition = [[{
  138. 'parameter': '',
  139. 'operator': '',
  140. 'value': ''
  141. }]];
  142. }
  143. //获取类型数据
  144. const getAction = (val: string) => {
  145. switch (val) {
  146. case 'functionCall':
  147. product.tabDeviceFucntion.getList({ key: product_key }).then((res: any) => {
  148. functionCallList.value = res
  149. })
  150. break;
  151. case 'readAttribute':
  152. datahub.node.getpropertyList({ key: product_key }).then((re: any) => {
  153. propertyCallList.value = re;
  154. });
  155. break;
  156. case 'modifyAttribute':
  157. datahub.node.getpropertyList({ key: product_key }).then((re: any) => {
  158. propertyCallList.value = re;
  159. });
  160. break;
  161. }
  162. }
  163. const getcolumns = (index: number, val: string) => {
  164. let where = {
  165. "sceneType": props.sceneType, //场景类型
  166. "typeName": scene_type[props.sceneType],
  167. "device": {
  168. "operation": {
  169. "operator": val
  170. },
  171. "productKey": product_key,
  172. "selector": "all"
  173. }
  174. }
  175. getcolumnsList(where);
  176. }
  177. const getcolumnsList = (where: any) => {
  178. api.manage.getColumns(where).then((res: any) => {
  179. if (res) {
  180. columnList.value = res;
  181. }
  182. })
  183. }
  184. const addScene = () => {
  185. props.sceneList.push({
  186. 'productKey': '',
  187. 'deviceKey': '',
  188. 'triggerType': '',
  189. 'timer': '',
  190. 'triggerSwitch': false,
  191. 'condition': [[{
  192. 'parameter': '',
  193. 'operator': '',
  194. 'value': ''
  195. }]]
  196. });
  197. emit('addScenesDetail', 'definition');
  198. };
  199. const EditPen = (index: number) => {
  200. emit('editScenesDetail', index);
  201. }
  202. const delScene = (index: number) => {
  203. emit('delScenesDetail', index);
  204. props.sceneList.splice(index, 1);
  205. }
  206. const setNull = (row: any, key: string, val: string) => {
  207. if (!val) row[key] = null
  208. }
  209. const handlelisten = (e: any) => {
  210. props.sceneList[e.type].timer = e.cron;
  211. EditPen(e.type);
  212. };
  213. const showCron = () => {
  214. dialogVisible.value = true;
  215. };
  216. const cronclose = () => {
  217. dialogVisible.value = false;
  218. }
  219. //初始化
  220. const intScenel = () => {
  221. let array_data = props.sceneList;
  222. array_data.map((val: any, index) => {
  223. if (val.productKey) {
  224. product_key = val.productKey;
  225. let info = props.sourceData?.find((pro: { key: any; }) => pro.key === val.productKey);
  226. if (info) {
  227. getDeviceList(info.id)
  228. }
  229. }
  230. if (val.triggerType) {
  231. getcolumns(index, val.triggerType)
  232. }
  233. });
  234. }
  235. intScenel();
  236. </script>
  237. <style scoped lang="scss">
  238. .type-item {
  239. margin-top: 15px;
  240. .edit {
  241. margin-top: 15px;
  242. margin-left: 10px;
  243. color: #2041d4;
  244. }
  245. .conicon {
  246. width: 55px;
  247. height: 25px;
  248. font-size: 28px;
  249. line-height: 28px;
  250. cursor: pointer;
  251. }
  252. .item {
  253. padding: 10px;
  254. }
  255. .biankang {
  256. border: 1px solid #e8e2e2;
  257. border-radius: 10px;
  258. padding: 10px;
  259. margin-top: 10px;
  260. }
  261. .title {
  262. height: 40px;
  263. .icon {
  264. margin-left: 2px;
  265. margin-right: 10px;
  266. width: 5px;
  267. height: 20px;
  268. background-color: #315efb;
  269. }
  270. }
  271. .product {
  272. .el-form-item {
  273. margin-left: 30px;
  274. margin-bottom: 10px;
  275. margin-top: 10px;
  276. }
  277. }
  278. }
  279. </style>