sceneItem.vue 8.4 KB

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