sceneItem.vue 8.3 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. 'key': 'readAttribute',
  122. 'name': '读取属性',
  123. }, {
  124. 'key': 'modifyAttribute',
  125. 'name': '修改属性',
  126. }, {
  127. 'key': 'reportAttribute',
  128. 'name': '属性上报',
  129. }, {
  130. 'key': 'reportEvent',
  131. 'name': '事件上报',
  132. }, {
  133. 'key': 'functionCall',
  134. 'name': '功能调用',
  135. }]
  136. }
  137. })
  138. const seletChange = (index: number, val: string) => {
  139. product_key = val;
  140. //根据产品key获取产品ID
  141. let info = props.sourceData?.find((pro: { key: any; }) => pro.key === val);
  142. if (info) {
  143. // 重置 deviceKey 的值
  144. props.sceneList[index].deviceKey = '';
  145. // 重置当前项的 condition 值
  146. props.sceneList[index].condition = [[{
  147. 'parameter': '',
  148. 'operator': '',
  149. 'value': ''
  150. }]];
  151. getDeviceList(info.id)
  152. }
  153. EditPen(index);
  154. }
  155. const getDeviceList = (_id: any) => {
  156. product.device.allList({ productId: _id }).then((res: any) => {
  157. deviceListData.value = res.device
  158. })
  159. }
  160. const getSelectcolumns=(index: number, val: string) => {
  161. EditPen(index);
  162. getcolumns(index,val);
  163. // 重置当前项的 condition 值
  164. props.sceneList[index].condition = [[{
  165. 'parameter': '',
  166. 'operator': '',
  167. 'value': ''
  168. }]];
  169. }
  170. const getcolumns = (index: number, val: string) => {
  171. let where = {
  172. "sceneType": props.sceneType, //场景类型
  173. "typeName": scene_type[props.sceneType],
  174. "device": {
  175. "operation": {
  176. "operator": val
  177. },
  178. "productKey": product_key,
  179. "selector": "all"
  180. }
  181. }
  182. getcolumnsList(where);
  183. }
  184. const getcolumnsList = (where: any) => {
  185. api.manage.getColumns(where).then((res: any) => {
  186. if(res){
  187. columnList.value = res;
  188. }
  189. })
  190. }
  191. const addScene = () => {
  192. props.sceneList.push({
  193. 'productKey': '',
  194. 'deviceKey': '',
  195. 'triggerType': '',
  196. 'timer': '',
  197. 'triggerSwitch': false,
  198. 'condition': [[{
  199. 'parameter': '',
  200. 'operator': '',
  201. 'value': ''
  202. }]]
  203. });
  204. emit('addScenesDetail', 'definition');
  205. };
  206. const EditPen=(index: number)=>{
  207. emit('editScenesDetail',index);
  208. }
  209. const delScene = (index: number) => {
  210. emit('delScenesDetail', index);
  211. props.sceneList.splice(index, 1);
  212. }
  213. const setNull = (row: any, key: string, val: string) => {
  214. if (!val) row[key] = null
  215. }
  216. const handlelisten = (e: any) => {
  217. props.sceneList[e.type].timer = e.cron;
  218. EditPen(e.type);
  219. };
  220. const showCron = () => {
  221. dialogVisible.value = true;
  222. };
  223. const cronclose = () => {
  224. dialogVisible.value = false;
  225. }
  226. //初始化
  227. const intScenel=()=>{
  228. let array_data=props.sceneList;
  229. array_data.map((val:any,index) => {
  230. if(val.productKey){
  231. product_key = val.productKey;
  232. let info = props.sourceData?.find((pro: { key: any; }) => pro.key === val.productKey);
  233. if (info) {
  234. getDeviceList(info.id)
  235. }
  236. }
  237. if(val.triggerType){
  238. getcolumns(index,val.triggerType)
  239. }
  240. });
  241. }
  242. intScenel();
  243. </script>
  244. <style scoped lang="scss">
  245. .type-item {
  246. margin-top: 15px;
  247. .edit {
  248. margin-top: 15px;
  249. margin-left: 10px;
  250. color: #2041d4;
  251. }
  252. .conicon {
  253. width: 55px;
  254. height: 25px;
  255. font-size: 28px;
  256. line-height: 28px;
  257. cursor: pointer;
  258. }
  259. .item {
  260. padding: 10px;
  261. }
  262. .biankang {
  263. border: 1px solid #e8e2e2;
  264. border-radius: 10px;
  265. padding: 10px;
  266. margin-top: 10px;
  267. }
  268. .title {
  269. height: 40px;
  270. .icon {
  271. margin-left: 2px;
  272. margin-right: 10px;
  273. width: 5px;
  274. height: 20px;
  275. background-color: #315efb;
  276. }
  277. }
  278. .product {
  279. .el-form-item {
  280. margin-left: 30px;
  281. margin-bottom: 10px;
  282. margin-top: 10px;
  283. }
  284. }
  285. }
  286. </style>