editFun.vue 8.9 KB

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