editFun.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <template>
  2. <div class="system-edit-dic-container">
  3. <!-- 新增功能定义/编辑功能定义 -->
  4. <el-dialog :title="ruleForm.id !== 0 ? $t('message.device.dialogI18n.editFunctionDefinition') : $t('message.device.dialogI18n.addFunctionDefinition')" v-model="isShowDialog" width="769px">
  5. <el-form :model="ruleForm" ref="formRef" :rules="rules" label-width="120px">
  6. <!-- 功能定义标识 -->
  7. <el-form-item :label="$t('message.device.formI18nLabel.functionKey')" prop="key">
  8. <!-- 请输入功能定义标识 -->
  9. <el-input v-model="ruleForm.key" :placeholder="$t('message.device.formI18nPlaceholder.functionKey')" :disabled="ruleForm.id !== 0 ? true : false" />
  10. </el-form-item>
  11. <!-- 功能定义名称 -->
  12. <el-form-item :label="$t('message.device.formI18nLabel.functionName')" prop="name">
  13. <!-- 请输入功能定义名称 -->
  14. <el-input v-model="ruleForm.name" :placeholder="$t('message.device.formI18nPlaceholder.functionName')" />
  15. </el-form-item>
  16. <!-- 输入参数 -->
  17. <el-form-item :label="$t('message.device.formI18nLabel.functionInput')" prop="maxLength">
  18. <div v-for="(item, index) in inputsdata" :key="index" class="jslist">
  19. <div class="jsonlist">
  20. <!-- 参数标识 -->
  21. <div>{{ $t('message.device.formI18nLabel.parameterIdentifier') }}:{{ item.key }}</div>
  22. <!-- 参数名称 -->
  23. <div>{{ $t('message.device.formI18nLabel.parameterName') }}:{{ item.name }}</div>
  24. <!-- 数据类型 -->
  25. <div>{{ $t('message.device.formI18nLabel.dataType') }}:{{ item.valueType.type }}</div>
  26. <div class="jsonoption">
  27. <!-- 编辑 -->
  28. <el-link type="primary" @click="editjson(index, 'fun')">{{ $t('message.tableI18nAction.edit') }}</el-link>
  29. <!-- 删除 -->
  30. <el-link type="primary" @click="deljson(index, 'fun')">{{ $t('message.tableI18nAction.delete') }}</el-link>
  31. </div>
  32. </div>
  33. </div>
  34. <div style="display: block; width: 100%">
  35. <div class="input-options" @click="addJson('fun')">
  36. <el-icon>
  37. <Plus />
  38. </el-icon>
  39. <!-- 添加参数 -->
  40. <div>{{ $t('message.device.formI18nButton.addParameter') }}</div>
  41. </div>
  42. </div>
  43. </el-form-item>
  44. <!-- 输出参数 -->
  45. <el-form-item :label="$t('message.device.formI18nLabel.functionOutput')" prop="">
  46. <div v-for="(item, index) in outputsdata" :key="index" class="jslist">
  47. <div class="jsonlist">
  48. <!-- 参数标识 -->
  49. <div>{{ $t('message.device.formI18nLabel.parameterIdentifier') }}:{{ item.key }}</div>
  50. <!-- 参数名称 -->
  51. <div>{{ $t('message.device.formI18nLabel.parameterName') }}:{{ item.name }}</div>
  52. <!-- 数据类型 -->
  53. <div>{{ $t('message.device.formI18nLabel.dataType') }}:{{ item.valueType.type }}</div>
  54. <div class="jsonoption">
  55. <!-- 编辑 -->
  56. <el-link type="primary" @click="editjsonOut(index)">{{ $t('message.tableI18nAction.edit') }}</el-link>
  57. <!-- 删除 -->
  58. <el-link type="primary" @click="deljsonOut(index, 'fun')">{{ $t('message.tableI18nAction.delete') }}</el-link>
  59. </div>
  60. </div>
  61. </div>
  62. <div style="display: block; width: 100%">
  63. <div class="input-options" @click="addJsonOut('fun')">
  64. <el-icon>
  65. <Plus />
  66. </el-icon>
  67. <!-- 添加参数 -->
  68. <div>{{ $t('message.device.formI18nButton.addParameter') }}</div>
  69. </div>
  70. </div>
  71. </el-form-item>
  72. <!-- 功能定义描述 -->
  73. <el-form-item :label="$t('message.device.formI18nLabel.functionDescription')" prop="desc">
  74. <!-- 请输入功能定义描述 -->
  75. <el-input v-model="ruleForm.desc" type="textarea" :placeholder="$t('message.device.formI18nPlaceholder.functionDescription')"></el-input>
  76. </el-form-item>
  77. </el-form>
  78. <template #footer>
  79. <span class="dialog-footer">
  80. <!-- 取消 -->
  81. <el-button @click="onCancel">{{ $t('message.tableI18nAction.cancel') }}</el-button>
  82. <!-- 编辑 / 新增 -->
  83. <el-button type="primary" @click="onSubmit">{{ ruleForm.id !== 0 ? $t('message.tableI18nAction.edit') : $t('message.tableI18nAction.add') }}</el-button>
  84. </span>
  85. </template>
  86. </el-dialog>
  87. <EditOption ref="editOptionRef" key="editOptionRef" @typeList="getOptionData" />
  88. <EditOption ref="editOptionOutRef" key="editOptionOutRef" @typeList="getOptionDataOut" />
  89. </div>
  90. </template>
  91. <script lang="ts" setup>
  92. import { reactive, toRefs, defineComponent, ref, unref, computed } from 'vue'
  93. import api from '/@/api/device'
  94. import { Plus, Minus, Right } from '@element-plus/icons-vue'
  95. import EditOption from './editOption.vue'
  96. import { validateNoSpace } from '/@/utils/validator'
  97. import { ElMessage } from 'element-plus'
  98. import { useI18n } from 'vue-i18n';
  99. const { locale, t } = useI18n();
  100. interface RuleFormState {
  101. id?: number
  102. key: string
  103. productKey: string
  104. name: string
  105. type: string
  106. dictType: string
  107. valueType: Object
  108. inputs: any
  109. outputs: any
  110. status: number
  111. desc: string
  112. }
  113. const form = {
  114. productKey: '',
  115. type: '',
  116. name: '',
  117. key: '',
  118. status: 1,
  119. dictType: '',
  120. inputs: [],
  121. outputs: [],
  122. valueType: {
  123. type: '',
  124. maxLength: '',
  125. },
  126. desc: '',
  127. }
  128. const emit = defineEmits(['typeList'])
  129. const formRef = ref<HTMLElement | null>(null)
  130. const editOptionRef = ref()
  131. const editOptionOutRef = ref()
  132. const isShowDialog = ref(false)
  133. const type = ref('')
  134. const types = ref('')
  135. const productKey = ref('')
  136. // const valueType = ref<any>({
  137. // type: '',
  138. // maxLength: '',
  139. // trueText: '是',
  140. // trueValue: 'true',
  141. // falseText: '否',
  142. // falseValue: 'false',
  143. // })
  144. const elementType = ref({
  145. type: '',
  146. maxLength: '',
  147. })
  148. const jsondata = ref<any>([])
  149. const inputsdata = ref<any>([])
  150. const outputsdata = ref<any>([])
  151. const ruleForm = ref(JSON.parse(JSON.stringify(form)))
  152. const rules = computed(() => ({
  153. name: [
  154. // 功能定义名称不能为空
  155. { required: true, message: t('message.device.rules.functionName'), trigger: 'blur' },
  156. // 功能定义名称不能超过32个字符
  157. { max: 32, message: t('message.device.rules.functionNameMax32'), trigger: 'blur' },
  158. // 功能定义名称不能包含空格
  159. { validator: validateNoSpace, message: t('message.device.rules.functionNameValidator'), trigger: 'blur' },
  160. ],
  161. // 功能定义标识不能为空
  162. key: [{ required: true, message: t('message.device.rules.functionKey'), trigger: 'blur' }],
  163. // 请选择数据类型
  164. type: [{ required: true, message: t('message.device.rules.functionType'), trigger: 'blur' }],
  165. }))
  166. // 打开弹窗
  167. const openDialog = (row: RuleFormState, productKeyData: string) => {
  168. resetForm()
  169. ruleForm.value = row
  170. productKey.value = productKeyData
  171. inputsdata.value = row.inputs || []
  172. outputsdata.value = row.outputs || []
  173. isShowDialog.value = true
  174. }
  175. const resetForm = () => {
  176. ruleForm.value = JSON.parse(JSON.stringify(form))
  177. type.value = ''
  178. types.value = ''
  179. inputsdata.value = []
  180. outputsdata.value = []
  181. elementType.value = {
  182. type: '',
  183. maxLength: '',
  184. }
  185. }
  186. const editjson = (index: number, type: string) => {
  187. if (type == 'fun') {
  188. editOptionRef.value.openDialog(inputsdata.value[index])
  189. } else {
  190. editOptionRef.value.openDialog(jsondata.value[index])
  191. }
  192. }
  193. const deljson = (index: number, type: string) => {
  194. if (type == 'fun') {
  195. inputsdata.value.splice(index, 1)
  196. } else {
  197. jsondata.value.splice(index, 1)
  198. }
  199. }
  200. const deljsonOut = (index: number, type: string) => {
  201. if (type == 'fun') {
  202. outputsdata.value.splice(index, 1)
  203. } else {
  204. outputsdata.value.splice(index, 1)
  205. }
  206. }
  207. const editjsonOut = (index: number) => {
  208. editOptionOutRef.value.openDialog(outputsdata.value[index])
  209. }
  210. const addJson = (type: string) => {
  211. editOptionRef.value.openDialog({ productKey: '', id: 0, type_data: type })
  212. }
  213. const addJsonOut = (type: string) => {
  214. editOptionOutRef.value.openDialog({ productKey: '', id: 0, type_data: type })
  215. }
  216. const getOptionData = (data: any, type_data: any) => {
  217. if (type_data == 'fun') {
  218. inputsdata.value.push(data)
  219. } else {
  220. jsondata.value.push(data)
  221. }
  222. }
  223. const getOptionDataOut = (data: any, type_data: any) => {
  224. if (type_data == 'fun') {
  225. outputsdata.value.push(data)
  226. } else {
  227. outputsdata.value.push(data)
  228. }
  229. }
  230. // 关闭弹窗
  231. const closeDialog = () => {
  232. isShowDialog.value = false
  233. }
  234. // 取消
  235. const onCancel = () => {
  236. closeDialog()
  237. }
  238. // 新增
  239. const onSubmit = () => {
  240. const formWrap = unref(formRef) as any
  241. if (!formWrap) return
  242. formWrap.validate((valid: boolean) => {
  243. if (valid) {
  244. ruleForm.value.inputs = inputsdata.value
  245. ruleForm.value.outputs = outputsdata.value
  246. if (ruleForm.value.id !== 0) {
  247. ruleForm.value.productKey = productKey.value
  248. api.model.functionedit(ruleForm.value).then(() => {
  249. // 编辑成功
  250. ElMessage.success(t('message.tableI18nConfirm.editSuccess'))
  251. closeDialog() // 关闭弹窗
  252. emit('typeList')
  253. })
  254. } else {
  255. api.model.functionadd(ruleForm.value).then(() => {
  256. // 新增成功
  257. ElMessage.success(t('message.tableI18nConfirm.addSuccess'))
  258. closeDialog() // 关闭弹窗
  259. emit('typeList')
  260. })
  261. }
  262. }
  263. })
  264. }
  265. defineExpose({ openDialog })
  266. </script>
  267. <style scoped lang="scss">
  268. [data-theme="dark"] {
  269. .jslist {
  270. border-color: var(--next-border-color-light) !important;
  271. }
  272. }
  273. .input-box {
  274. display: flex;
  275. flex-direction: row;
  276. justify-content: space-between;
  277. margin-top: 10px;
  278. }
  279. .input-option {
  280. line-height: 30px;
  281. padding-top: 5px;
  282. width: 140px;
  283. }
  284. .input-option i {
  285. margin: 0px 5px;
  286. border: 1px solid #c3c3c3;
  287. font-size: 16px;
  288. }
  289. .input-options {
  290. display: flex;
  291. align-items: center;
  292. color: #409eff;
  293. cursor: pointer;
  294. }
  295. .jslist {
  296. width: 100%;
  297. border: 1px solid #e8e8e8;
  298. padding: 10px;
  299. margin-bottom: 10px;
  300. }
  301. .jsonlist {
  302. display: flex;
  303. flex-direction: row;
  304. justify-content: space-between;
  305. }
  306. .jsonoption a {
  307. margin: 0px 10px;
  308. }
  309. </style>