editOption.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <div class="system-edit-dic-container">
  3. <!-- 编辑参数 / 新增参数 -->
  4. <el-dialog :title="typeof ruleForm.valueType !== 'undefined' ? $t('message.device.dialogI18n.editOption') : $t('message.device.dialogI18n.addOption')" v-model="isShowDialog" width="769px">
  5. <el-form :model="ruleForm" ref="formRef" :rules="rules" :label-width="currentLocale == 'en' ? '150px' : '120px'">
  6. <!-- 参数标识 -->
  7. <el-form-item :label="$t('message.device.formI18nLabel.parameterIdentifier')" prop="key">
  8. <!-- 请输入参数标识 -->
  9. <el-input v-model="ruleForm.key" :placeholder="$t('message.device.formI18nPlaceholder.parameterIdentifier')" />
  10. </el-form-item>
  11. <!-- 参数名称 -->
  12. <el-form-item :label="$t('message.device.formI18nLabel.parameterName')" prop="name">
  13. <!-- 请输入参数名称 -->
  14. <el-input v-model="ruleForm.name" :placeholder="$t('message.device.formI18nPlaceholder.parameterName')" />
  15. </el-form-item>
  16. <!-- 数据类型 -->
  17. <el-form-item :label="$t('message.device.formI18nLabel.dataType')" prop="type">
  18. <!-- 请选择数据类型 -->
  19. <el-select v-model="valueType.type" :placeholder="$t('message.device.formI18nPlaceholder.dataType')" @change="seletChange">
  20. <el-option-group v-for="group in typeData" :key="group.label" :label="group.label">
  21. <el-option v-for="item in group.options" :key="item.type" :label="item.title" :value="item.type" />
  22. </el-option-group>
  23. </el-select>
  24. </el-form-item>
  25. <TypeItem :valueType="valueType" :typeData="typeData"></TypeItem>
  26. <div v-if="type == 'array'">
  27. <!-- 元素类型 -->
  28. <el-form-item :label="$t('message.device.formI18nLabel.elementType')" prop="type">
  29. <!-- 请选择元素类型 -->
  30. <el-select v-model="elementType.type" :placeholder="$t('message.device.formI18nPlaceholder.elementType')" @change="seletChanges">
  31. <el-option-group v-for="group in typeData" :key="group.label" :label="group.label">
  32. <el-option v-for="item in group.options" :key="item.type" :label="item.title" :value="item.type" :disabled="['array', 'enum'].includes(item.type)" />
  33. </el-option-group>
  34. </el-select>
  35. </el-form-item>
  36. <TypeItem :valueType="elementType" :typeData="typeData"></TypeItem>
  37. </div>
  38. <!-- 参数描述 -->
  39. <el-form-item :label="$t('message.device.formI18nLabel.parameterDescription')" prop="desc">
  40. <!-- 请输入参数描述 -->
  41. <el-input v-model="ruleForm.desc" type="textarea" :placeholder="$t('message.device.formI18nPlaceholder.parameterDescription')"></el-input>
  42. </el-form-item>
  43. </el-form>
  44. <template #footer>
  45. <span class="dialog-footer">
  46. <!-- 取 消 -->
  47. <el-button @click="onCancel">{{ $t('message.tableI18nAction.cancel') }}</el-button>
  48. <!-- 编辑 / 新增 -->
  49. <el-button type="primary" @click="onSubmit">{{ typeof ruleForm.valueType !== 'undefined' ? $t('message.tableI18nAction.edit') : $t('message.tableI18nAction.add') }}</el-button>
  50. </span>
  51. </template>
  52. </el-dialog>
  53. </div>
  54. </template>
  55. <script lang="ts">
  56. import { reactive, toRefs, defineComponent, ref, unref, computed } from 'vue';
  57. import api from '/@/api/device';
  58. import TypeItem from './typeItem.vue';
  59. import { Plus, Minus, Right } from '@element-plus/icons-vue';
  60. import { ElMessage } from 'element-plus';
  61. import { validateNoSpace } from '/@/utils/validator';
  62. import { useI18n } from 'vue-i18n';
  63. interface stateType {
  64. isShowDialog: boolean
  65. ruleForm: any
  66. valueType: any
  67. typeData: any
  68. elementType: any
  69. type: string
  70. [key: string]: any
  71. }
  72. const valueTypeBase = {
  73. // max: null,
  74. // min: null,
  75. unit: null,
  76. decimals: null,
  77. trueText: '是',
  78. falseText: '否',
  79. trueValue: true,
  80. falseValue: false,
  81. type: null,
  82. maxLength: null,
  83. }
  84. const valueType = {
  85. ...JSON.parse(JSON.stringify(valueTypeBase)),
  86. properties: [{
  87. 'key': '',
  88. 'name': '',
  89. 'desc': '',
  90. 'valueType': {
  91. ...JSON.parse(JSON.stringify(valueTypeBase)),
  92. elements: [{
  93. 'text': '',
  94. 'value': ''
  95. }]
  96. }
  97. }],
  98. elements: [{
  99. 'text': '',
  100. 'value': ''
  101. }]
  102. }
  103. export default defineComponent({
  104. name: 'deviceEditPro',
  105. components: { Plus, Minus, Right, TypeItem },
  106. setup(prop, { emit }) {
  107. const formRef = ref<HTMLElement | null>(null);
  108. const { locale, t } = useI18n();
  109. const currentLocale = computed(() => locale.value)
  110. const rules = computed(() => ({
  111. name: [
  112. // 参数名称不能为空
  113. { required: true, message: t('message.device.rules.parameterName'), trigger: 'blur' },
  114. // 参数名称不能超过32个字符
  115. { max: 32, message: t('message.device.rules.parameterNameMax32'), trigger: 'blur' },
  116. // 参数名称不能包含空格
  117. { validator: validateNoSpace, message: t('message.device.rules.parameterNameValidator'), trigger: 'blur' }
  118. ],
  119. // 参数标识不能为空
  120. key: [{ required: true, message: t('message.device.rules.parameterKey'), trigger: 'blur' }],
  121. // 请选择是否只读
  122. accessMode: [{ required: true, message: t('message.device.rules.accessMode'), trigger: 'blur' }],
  123. }));
  124. const state = reactive<stateType>({
  125. isShowDialog: false,
  126. typeData: [], //
  127. type: '',
  128. types: '',
  129. valueType: JSON.parse(JSON.stringify(valueType)),
  130. elementType: JSON.parse(JSON.stringify(valueType)),
  131. properties: [JSON.parse(JSON.stringify(valueType))],
  132. enumdata: [
  133. {
  134. 'text': '',
  135. 'value': '',
  136. },
  137. ],
  138. ruleForm: {
  139. id: 0,
  140. name: '',
  141. key: '',
  142. transportProtocol: '',
  143. accessMode: '0',
  144. status: 1,
  145. valueType: {},
  146. desc: '',
  147. }
  148. });
  149. // 打开弹窗
  150. const openDialog = (row?: any) => {
  151. resetForm();
  152. api.product.getDataType({ status: -1 }).then((res: any) => {
  153. const datat: any = Object.values(res.dataType);
  154. datat.forEach((item: any, index: number) => {
  155. if (index == 0) {
  156. // 基础类型
  157. datat[index]['label'] = t('message.device.baseType');
  158. datat[index]['options'] = item;
  159. } else {
  160. // 基础类型
  161. datat[index]['label'] = t('message.device.extensionType');
  162. datat[index]['options'] = item;
  163. }
  164. });
  165. state.typeData = datat || [];
  166. });
  167. if (row) {
  168. if (typeof row.valueType !== 'undefined') {
  169. state.type = row.valueType.type;
  170. if (typeof row.valueType.elementType !== 'undefined') state.elementType = row.valueType.elementType;
  171. if (typeof row.valueType.elements !== 'undefined') state.enumdata = row.valueType.elements;
  172. if (typeof row.valueType.properties !== 'undefined') state.properties = row.valueType.properties;
  173. if (typeof row.valueType.type !== 'undefined') state.valueType.type = row.valueType.type;
  174. const fieldCount = Object.keys(row.valueType).length;
  175. if (fieldCount > 1) state.valueType = row.valueType;
  176. }
  177. state.ruleForm = row;
  178. }
  179. state.isShowDialog = true;
  180. };
  181. const resetForm = () => {
  182. state.ruleForm = {
  183. name: '',
  184. desc: '',
  185. };
  186. state.valueType = JSON.parse(JSON.stringify(valueType));
  187. state.enumdata = [{
  188. 'text': '',
  189. 'value': '',
  190. }];
  191. state.elementType = JSON.parse(JSON.stringify(valueType));
  192. };
  193. const seletChange = (val: string) => {
  194. state.type = val;
  195. };
  196. const seletChanges = (val: string) => {
  197. state.types = val;
  198. };
  199. const addEnum = () => {
  200. state.enumdata.push({
  201. 'text': '',
  202. 'value': '',
  203. });
  204. };
  205. const delEnum = (index: number) => {
  206. state.enumdata.splice(index, 1);
  207. }
  208. // 关闭弹窗
  209. const closeDialog = () => {
  210. state.isShowDialog = false;
  211. };
  212. // 取消
  213. const onCancel = () => {
  214. closeDialog();
  215. };
  216. // 新增
  217. const onSubmit = () => {
  218. const formWrap = unref(formRef) as any;
  219. if (!formWrap) return;
  220. formWrap.validate((valid: boolean) => {
  221. if (valid) {
  222. if (typeof state.ruleForm.valueType !== 'undefined') {
  223. //修改
  224. if (state.type == 'array') {
  225. state.valueType.elementType = state.elementType;
  226. }
  227. state.ruleForm.valueType = state.valueType;
  228. // ElMessage.success('参数类型修改成功');
  229. closeDialog(); // 关闭弹窗
  230. emit('editTypeList', state.ruleForm, state.ruleForm.type_data);
  231. } else {
  232. // //添加
  233. if (state.type == 'array') {
  234. state.valueType.elementType = state.elementType;
  235. }
  236. state.ruleForm.valueType = state.valueType;
  237. // 新增成功
  238. ElMessage.success(t('message.tableI18nConfirm.addSuccess'));
  239. closeDialog(); // 关闭弹窗
  240. emit('typeList', state.ruleForm, state.ruleForm.type_data);
  241. }
  242. }
  243. });
  244. };
  245. return {
  246. currentLocale,
  247. rules,
  248. openDialog,
  249. addEnum,
  250. delEnum,
  251. seletChange,
  252. seletChanges,
  253. closeDialog,
  254. onCancel,
  255. onSubmit,
  256. formRef,
  257. ...toRefs(state),
  258. };
  259. },
  260. });
  261. </script>
  262. <style scoped>
  263. .input-box {
  264. display: flex;
  265. flex-direction: row;
  266. justify-content: space-between;
  267. margin-top: 10px;
  268. }
  269. .input-option {
  270. line-height: 30px;
  271. padding-top: 5px;
  272. width: 140px;
  273. }
  274. .input-option i {
  275. margin: 0px 5px;
  276. border: 1px solid #c3c3c3;
  277. font-size: 16px;
  278. }
  279. .input-options {
  280. display: flex;
  281. align-items: center;
  282. color: #409eff;
  283. cursor: pointer
  284. }
  285. </style>