DeviceTriggerConfig.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <!-- 设备触发配置组件 -->
  2. <template>
  3. <div class="flex flex-col gap-16px">
  4. <!-- 主条件配置 - 默认直接展示 -->
  5. <div class="space-y-16px">
  6. <!-- 主条件配置 -->
  7. <div class="flex flex-col gap-16px">
  8. <!-- 主条件配置 -->
  9. <div class="space-y-16px">
  10. <!-- 主条件头部 - 与附加条件组保持一致的绿色风格 -->
  11. <div
  12. class="flex items-center justify-between p-16px bg-gradient-to-r from-green-50 to-emerald-50 border border-green-200 rounded-8px"
  13. >
  14. <div class="flex items-center gap-12px">
  15. <div class="flex items-center gap-8px text-16px font-600 text-green-700">
  16. <div
  17. class="w-24px h-24px bg-green-500 text-white rounded-full flex items-center justify-center text-12px font-bold"
  18. >
  19. </div>
  20. <span>主条件</span>
  21. </div>
  22. <el-tag size="small" type="success">必须满足</el-tag>
  23. </div>
  24. </div>
  25. <!-- 主条件内容配置 -->
  26. <MainConditionInnerConfig
  27. :model-value="trigger"
  28. @update:model-value="updateCondition"
  29. :trigger-type="trigger.type"
  30. @trigger-type-change="handleTriggerTypeChange"
  31. />
  32. </div>
  33. </div>
  34. </div>
  35. <!-- 条件组配置 -->
  36. <div class="space-y-16px">
  37. <!-- 条件组配置 -->
  38. <div class="flex flex-col gap-16px">
  39. <!-- 条件组容器头部 -->
  40. <div
  41. class="flex items-center justify-between p-16px bg-gradient-to-r from-green-50 to-emerald-50 border border-green-200 rounded-8px"
  42. >
  43. <div class="flex items-center gap-12px">
  44. <div class="flex items-center gap-8px text-16px font-600 text-green-700">
  45. <div
  46. class="w-24px h-24px bg-green-500 text-white rounded-full flex items-center justify-center text-12px font-bold"
  47. >
  48. </div>
  49. <span>附加条件组</span>
  50. </div>
  51. <el-tag size="small" type="success">与"主条件"为且关系</el-tag>
  52. <el-tag size="small" type="info">
  53. {{ trigger.conditionGroups?.length || 0 }} 个子条件组
  54. </el-tag>
  55. </div>
  56. <div class="flex items-center gap-8px">
  57. <el-button
  58. type="primary"
  59. size="small"
  60. @click="addSubGroup"
  61. :disabled="(trigger.conditionGroups?.length || 0) >= maxSubGroups"
  62. >
  63. <Icon icon="ep:plus" />
  64. 添加子条件组
  65. </el-button>
  66. <el-button type="danger" size="small" text @click="removeConditionGroup">
  67. <Icon icon="ep:delete" />
  68. 删除条件组
  69. </el-button>
  70. </div>
  71. </div>
  72. <!-- 子条件组列表 -->
  73. <div
  74. v-if="trigger.conditionGroups && trigger.conditionGroups.length > 0"
  75. class="space-y-16px"
  76. >
  77. <!-- 逻辑关系说明 -->
  78. <div class="relative">
  79. <div
  80. v-for="(subGroup, subGroupIndex) in trigger.conditionGroups"
  81. :key="`sub-group-${subGroupIndex}`"
  82. class="relative"
  83. >
  84. <!-- 子条件组容器 -->
  85. <div
  86. class="border-2 border-orange-200 rounded-8px bg-orange-50 shadow-sm hover:shadow-md transition-shadow"
  87. >
  88. <div
  89. class="flex items-center justify-between p-16px bg-gradient-to-r from-orange-50 to-yellow-50 border-b border-orange-200 rounded-t-6px"
  90. >
  91. <div class="flex items-center gap-12px">
  92. <div class="flex items-center gap-8px text-16px font-600 text-orange-700">
  93. <div
  94. class="w-24px h-24px bg-orange-500 text-white rounded-full flex items-center justify-center text-12px font-bold"
  95. >
  96. {{ subGroupIndex + 1 }}
  97. </div>
  98. <span>子条件组 {{ subGroupIndex + 1 }}</span>
  99. </div>
  100. <el-tag size="small" type="warning" class="font-500">组内条件为"且"关系</el-tag>
  101. <el-tag size="small" type="info"> {{ subGroup?.length || 0 }}个条件 </el-tag>
  102. </div>
  103. <el-button
  104. type="danger"
  105. size="small"
  106. text
  107. @click="removeSubGroup(subGroupIndex)"
  108. class="hover:bg-red-50"
  109. >
  110. <Icon icon="ep:delete" />
  111. 删除组
  112. </el-button>
  113. </div>
  114. <SubConditionGroupConfig
  115. :model-value="subGroup"
  116. @update:model-value="(value) => updateSubGroup(subGroupIndex, value)"
  117. :trigger-type="trigger.type"
  118. :max-conditions="maxConditionsPerGroup"
  119. />
  120. </div>
  121. <!-- 子条件组间的"或"连接符 -->
  122. <div
  123. v-if="subGroupIndex < trigger.conditionGroups!.length - 1"
  124. class="flex items-center justify-center py-12px"
  125. >
  126. <div class="flex items-center gap-8px">
  127. <!-- 连接线 -->
  128. <div class="w-32px h-1px bg-orange-300"></div>
  129. <!-- 或标签 -->
  130. <div class="px-16px py-6px bg-orange-100 border-2 border-orange-300 rounded-full">
  131. <span class="text-14px font-600 text-orange-600">或</span>
  132. </div>
  133. <!-- 连接线 -->
  134. <div class="w-32px h-1px bg-orange-300"></div>
  135. </div>
  136. </div>
  137. </div>
  138. </div>
  139. </div>
  140. <!-- 空状态 -->
  141. <div
  142. v-else
  143. class="p-24px border-2 border-dashed border-orange-200 rounded-8px text-center bg-orange-50"
  144. >
  145. <div class="flex flex-col items-center gap-12px">
  146. <Icon icon="ep:plus" class="text-32px text-orange-400" />
  147. <div class="text-orange-600">
  148. <p class="text-14px font-500 mb-4px">暂无子条件组</p>
  149. <p class="text-12px">点击上方"添加子条件组"按钮开始配置</p>
  150. </div>
  151. </div>
  152. </div>
  153. </div>
  154. </div>
  155. </div>
  156. </template>
  157. <script setup lang="ts">
  158. import { useVModel } from '@vueuse/core'
  159. import MainConditionInnerConfig from './MainConditionInnerConfig.vue'
  160. import SubConditionGroupConfig from './SubConditionGroupConfig.vue'
  161. import type { Trigger } from '@/api/iot/rule/scene'
  162. /** 设备触发配置组件 */
  163. defineOptions({ name: 'DeviceTriggerConfig' })
  164. const props = defineProps<{
  165. modelValue: Trigger
  166. index: number
  167. }>()
  168. const emit = defineEmits<{
  169. (e: 'update:modelValue', value: Trigger): void
  170. (e: 'trigger-type-change', type: number): void
  171. }>()
  172. const trigger = useVModel(props, 'modelValue', emit)
  173. const maxSubGroups = 3 // 最多 3 个子条件组
  174. const maxConditionsPerGroup = 3 // 每组最多 3 个条件
  175. /**
  176. * 更新条件
  177. * @param condition 条件对象
  178. */
  179. const updateCondition = (condition: Trigger) => {
  180. trigger.value = condition
  181. }
  182. /**
  183. * 处理触发器类型变化事件
  184. * @param type 触发器类型
  185. */
  186. const handleTriggerTypeChange = (type: number) => {
  187. trigger.value.type = type
  188. emit('trigger-type-change', type)
  189. }
  190. /** 添加子条件组 */
  191. const addSubGroup = async () => {
  192. if (!trigger.value.conditionGroups) {
  193. trigger.value.conditionGroups = []
  194. }
  195. // 检查是否达到最大子组数量限制
  196. if (trigger.value.conditionGroups?.length >= maxSubGroups) {
  197. return
  198. }
  199. // 使用 nextTick 确保响应式更新完成后再添加新的子组
  200. await nextTick()
  201. if (trigger.value.conditionGroups) {
  202. trigger.value.conditionGroups.push([])
  203. }
  204. }
  205. /**
  206. * 移除子条件组
  207. * @param index 子条件组索引
  208. */
  209. const removeSubGroup = (index: number) => {
  210. if (trigger.value.conditionGroups) {
  211. trigger.value.conditionGroups.splice(index, 1)
  212. }
  213. }
  214. /**
  215. * 更新子条件组
  216. * @param index 子条件组索引
  217. * @param subGroup 子条件组数据
  218. */
  219. const updateSubGroup = (index: number, subGroup: any) => {
  220. if (trigger.value.conditionGroups) {
  221. trigger.value.conditionGroups[index] = subGroup
  222. }
  223. }
  224. /** 移除整个条件组 */
  225. const removeConditionGroup = () => {
  226. trigger.value.conditionGroups = undefined
  227. }
  228. </script>