editNode.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <template>
  2. <div class="system-edit-dic-container">
  3. <el-dialog :title="(ruleForm.nodeId !== 0 ? '修改' : '添加') + '数据节点'" v-model="isShowDialog" width="769px">
  4. <el-form :model="ruleForm" ref="formRef" :rules="rules" size="default" label-width="110px">
  5. <el-form-item label="数据节点标识" prop="key">
  6. <el-input v-model="ruleForm.key" placeholder="请输入数据节点名称" :disabled="ruleForm.nodeId" />
  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="isPk">
  12. <el-radio-group v-model="ruleForm.isPk">
  13. <el-radio :label="0">否</el-radio>
  14. <el-radio :label="1">是</el-radio>
  15. </el-radio-group>
  16. </el-form-item>
  17. <el-form-item label="数据类型" prop="dataType" v-if="detail.from == 1">
  18. <el-select v-model="ruleForm.dataType" filterable placeholder="请选择数据类型">
  19. <el-option v-for="item in tabData" :key="item.value" :label="item.label" :value="item.value" />
  20. </el-select>
  21. </el-form-item>
  22. <el-form-item label="取值项" prop="value" v-if="detail.from == 1">
  23. <el-input v-model="ruleForm.value" placeholder="请输入取值项" class="w-35" /><el-button type="success" @click="onTest">选择值</el-button>
  24. </el-form-item>
  25. <el-form-item label="取值项" prop="value" v-if="detail.from == 4">
  26. <el-select v-model="ruleForm.value" filterable placeholder="请选择数取值项" @change="getNodeList">
  27. <el-option v-for="item in propertyData" :key="item.key" :label="item.name" :value="item.key">
  28. <span style="float: left">{{ item.key }}</span>
  29. <span style="float: right; font-size: 13px">{{ item.name }}</span>
  30. </el-option>
  31. </el-select>
  32. </el-form-item>
  33. <el-divider content-position="left">规则表达式</el-divider>
  34. <div v-for="(item, index) in rule" :key="index">
  35. <el-form-item label="正则表达式">
  36. <el-input v-model="item.expression" placeholder="请输入规则表达式" />
  37. </el-form-item>
  38. <el-form-item label="替换内容">
  39. <el-input v-model="item.replace" placeholder="请输入替换内容" class="w-35" />
  40. <!-- <el-input v-model="rule[index].params.name" placeholder="请输入键值" class="w-35" />
  41. <el-input v-model="rule[index].params.value" placeholder="请输入值" class="w-35" /> -->
  42. <div class="conicon">
  43. <el-icon @click="delRule(index)" v-if="index > 0"><Delete /></el-icon>
  44. </div>
  45. </el-form-item>
  46. </div>
  47. <div style="padding: 10px">
  48. <el-button type="primary" class="addbutton" @click="addRule">增加</el-button>
  49. </div>
  50. </el-form>
  51. <template #footer>
  52. <span class="dialog-footer">
  53. <el-button @click="onCancel" size="default">取 消</el-button>
  54. <el-button type="primary" @click="onSubmit" size="default">{{ ruleForm.nodeId !== 0 ? '修 改' : '添 加' }}</el-button>
  55. </span>
  56. </template>
  57. </el-dialog>
  58. <el-dialog v-model="dialogVisible" title="点击蓝色key值进行选择" width="40%">
  59. <jsontree :data="jsonPathData" @handlePath="handlePath"></jsontree>
  60. <template #footer>
  61. <span class="dialog-footer">
  62. <el-button @click="dialogVisible = false">关闭</el-button>
  63. </span>
  64. </template>
  65. </el-dialog>
  66. </div>
  67. </template>
  68. <script lang="ts">
  69. import { reactive, toRefs, defineComponent, ref, unref } from 'vue';
  70. import api from '/@/api/datahub';
  71. import 'vue3-json-viewer/dist/index.css';
  72. import jsontree from '/@/components/jsontree/index.vue';
  73. import { ElMessage } from 'element-plus';
  74. import { Delete, Minus, Right } from '@element-plus/icons-vue';
  75. interface RuleFormState {
  76. nodeId: number;
  77. name: string;
  78. from: number;
  79. isPk: number;
  80. key: string;
  81. dataType: string;
  82. value: string;
  83. description: string;
  84. status: number;
  85. }
  86. interface DicState {
  87. isShowDialog: boolean;
  88. ruleForm: RuleFormState;
  89. rules: {};
  90. }
  91. export default defineComponent({
  92. name: 'Edit',
  93. components: { Delete, Minus, Right, jsontree },
  94. setup(prop, { emit }) {
  95. const editDicRef = ref();
  96. const formRef = ref<HTMLElement | null>(null);
  97. const state = reactive<DicState>({
  98. isShowDialog: false,
  99. dialogVisible: false,
  100. jsonPathData: [],
  101. jsonData: '',
  102. propertyData: [],
  103. config: {},
  104. detail: {},
  105. tabData: [
  106. {
  107. label: 'int',
  108. value: 'int',
  109. },
  110. {
  111. label: 'long',
  112. value: 'long',
  113. },
  114. {
  115. label: 'float',
  116. value: 'float',
  117. },
  118. {
  119. label: 'double',
  120. value: 'double',
  121. },
  122. {
  123. label: 'string',
  124. value: 'string',
  125. },
  126. {
  127. label: 'boolean',
  128. value: 'boolean',
  129. },
  130. {
  131. label: 'date',
  132. value: 'date',
  133. },
  134. ],
  135. ruledata: [
  136. {
  137. expression: '',
  138. replace: '',
  139. // params: {
  140. // name: '',
  141. // value: '',
  142. // },
  143. },
  144. ],
  145. rule: [
  146. {
  147. expression: '',
  148. replace: '',
  149. // params: {
  150. // name: '',
  151. // value: '',
  152. // },
  153. },
  154. ],
  155. ruleForm: {
  156. nodeId: 0,
  157. isPk: 0,
  158. name: '',
  159. key: '',
  160. dataType: '',
  161. value: '',
  162. rule: [],
  163. description: '',
  164. },
  165. rules: {
  166. key: [{ required: true, message: '数据节点标识不能为空', trigger: 'blur' }],
  167. name: [{ required: true, message: '数据节点名称不能为空', trigger: 'blur' }],
  168. isPk: [{ required: true, message: '请选择是否主键', trigger: 'blur' }],
  169. dataType: [{ required: true, message: '数据节点类型不能为空', trigger: 'blur' }],
  170. value: [{ required: true, message: '数据节点取值项不能为空', trigger: 'blur' }],
  171. },
  172. });
  173. const onTest = () => {
  174. if (state.detail.from == 1) {
  175. api.common.api(state.detail.sourceId).then((res: any) => {
  176. state.jsonData = JSON.parse(res.data);
  177. var jsonstr = jsonPath([], state.jsonData, '');
  178. state.jsonPathData = jsonstr;
  179. state.dialogVisible = true;
  180. });
  181. } else if (state.detail.from == 4) {
  182. api.common.devapi(state.detail.sourceId).then((res: any) => {
  183. state.jsonData = JSON.parse(res.data);
  184. var jsonstr = jsonPath([], state.jsonData, '');
  185. state.jsonPathData = jsonstr;
  186. state.dialogVisible = true;
  187. });
  188. }
  189. };
  190. const handlePath = (path) => {
  191. let data = path.slice(1);
  192. state.ruleForm.value = data;
  193. state.dialogVisible = false;
  194. };
  195. const delRule = (index) => {
  196. state.rule.splice(index, 1);
  197. };
  198. const addRule = () => {
  199. state.rule.push({
  200. expression: '',
  201. replace: '',
  202. // params: {
  203. // name: '',
  204. // value: '',
  205. // },
  206. });
  207. };
  208. // 打开弹窗
  209. const openDialog = (row: RuleFormState | null) => {
  210. resetForm();
  211. if (row?.nodeId) {
  212. state.ruleForm = row;
  213. var data = JSON.parse(row.rule);
  214. console.log(data);
  215. data.forEach((item, index) => {
  216. state.rule[index].expression = item.expression;
  217. state.rule[index].replace = item.replace;
  218. // state.rule[index].params.name = Object.keys(item.params);
  219. // state.rule[index].params.value = item.params[Object.keys(item.params)];
  220. });
  221. }
  222. api.common.detail(row.sourceId).then((res: any) => {
  223. state.detail = res.data;
  224. if (res.data.from == 4) {
  225. //propertyData
  226. api.node.getpropertyList({ key: res.data.deviceConfig.productKey }).then((re: any) => {
  227. state.propertyData = re;
  228. });
  229. }
  230. });
  231. state.ruleForm = row;
  232. state.isShowDialog = true;
  233. };
  234. const resetForm = () => {
  235. state.ruleForm = {
  236. nodeId: 0,
  237. name: '',
  238. isPk: 0,
  239. from: 1,
  240. key: '',
  241. rule: [],
  242. description: '',
  243. };
  244. };
  245. // 关闭弹窗
  246. const closeDialog = () => {
  247. state.isShowDialog = false;
  248. };
  249. // 取消
  250. const onCancel = () => {
  251. closeDialog();
  252. };
  253. // 新增
  254. const onSubmit = () => {
  255. const formWrap = unref(formRef) as any;
  256. if (!formWrap) return;
  257. formWrap.validate((valid: boolean) => {
  258. if (valid) {
  259. //修改rule数据
  260. // state.rule.forEach((item, index) => {
  261. // item.params[item.params.name] = item.params.value;
  262. // delete item.params.name;
  263. // delete item.params.value;
  264. // });
  265. state.ruleForm.rule = state.rule;
  266. if (state.ruleForm.nodeId !== 0) {
  267. //修改
  268. api.node.edit(state.ruleForm).then(() => {
  269. ElMessage.success('数据节点类型修改成功');
  270. closeDialog(); // 关闭弹窗
  271. emit('typeList');
  272. });
  273. } else {
  274. //添加
  275. api.node.add(state.ruleForm).then(() => {
  276. ElMessage.success('数据节点类型添加成功');
  277. closeDialog(); // 关闭弹窗
  278. emit('typeList');
  279. });
  280. }
  281. }
  282. });
  283. };
  284. const getNodeList = (e) => {
  285. state.propertyData.forEach((item, index) => {
  286. if (item.key === e) {
  287. state.ruleForm.dataType = item.valueType.type;
  288. }
  289. });
  290. };
  291. const onKeyclick = (e) => {
  292. //console.log(e);
  293. // if (e.target.innerText && e.target.className == 'jv-key') {
  294. // let str = e.target.innerText;
  295. // str = str.substr(0, str.length - 1);
  296. // state.ruleForm.value = str;
  297. // state.dialogVisible = false;
  298. // var con = {
  299. // ...state.jsonData,
  300. // };
  301. // // var jsonstr = getOrgIdArr([], str, con);
  302. // // state.ruleForm.value = jsonstr.join('.');
  303. // }
  304. };
  305. const jsonPath = (arr, json, basePath) => {
  306. // 生成jsonpath
  307. const type = validateType(json);
  308. if (type === 'object') {
  309. for (let key in json) {
  310. const item = {
  311. key,
  312. path: `${basePath}.${key}`,
  313. };
  314. const childType = validateType(json[key]);
  315. item.type = childType;
  316. if (childType === 'object' || childType === 'array') {
  317. item.leaf = true;
  318. item.children = [];
  319. jsonPath(item.children, json[key], item.path);
  320. } else {
  321. item.leaf = false;
  322. item.value = json[key];
  323. }
  324. arr.push(item);
  325. }
  326. } else if (type === 'array') {
  327. json.forEach((item, index) => {
  328. const childType = validateType(item);
  329. const obj = {
  330. key: index,
  331. };
  332. obj.type = childType;
  333. obj.path = `${basePath}.${index}`;
  334. if (childType === 'object' || childType === 'array') {
  335. (obj.leaf = true), (obj.children = []);
  336. jsonPath(obj.children, item, obj.path);
  337. } else {
  338. obj.value = item;
  339. obj.leaf = false;
  340. }
  341. arr.push(obj);
  342. });
  343. }
  344. return arr;
  345. };
  346. // 校验JSON数据类型
  347. const validateType = (val) => {
  348. let type = typeof val;
  349. if (type === 'object') {
  350. if (Array.isArray(val)) {
  351. return 'array';
  352. } else if (val === null) {
  353. return 'null';
  354. } else {
  355. return 'object';
  356. }
  357. } else {
  358. switch (type) {
  359. case 'boolean':
  360. return 'boolean';
  361. case 'string':
  362. return 'string';
  363. case 'number':
  364. return 'number';
  365. default:
  366. return 'error';
  367. }
  368. }
  369. };
  370. const getOrgIdArr = (parents, childNode, treeData) => {
  371. for (var key in treeData) {
  372. // 父节点查询条件
  373. if (key === childNode) {
  374. // 如果找到结果,保存当前节点
  375. parents.push(key);
  376. break;
  377. } else {
  378. if (treeData[key] instanceof Object) {
  379. // console.log(treeData[key]);
  380. parents.push(key);
  381. //没找到,遍历该节点的子节点
  382. getOrgIdArr(parents, childNode, treeData[key]);
  383. }
  384. }
  385. }
  386. return parents;
  387. };
  388. return {
  389. jsonPath,
  390. handlePath,
  391. validateType,
  392. getNodeList,
  393. onKeyclick,
  394. getOrgIdArr,
  395. addRule,
  396. onTest,
  397. delRule,
  398. openDialog,
  399. closeDialog,
  400. onCancel,
  401. onSubmit,
  402. formRef,
  403. ...toRefs(state),
  404. };
  405. },
  406. });
  407. </script>
  408. <style>
  409. .el-input__wrapper {
  410. width: 98%;
  411. }
  412. .box-content {
  413. border: 1px solid #e8e8e8;
  414. margin: 10px;
  415. padding: 10px;
  416. }
  417. .content-f {
  418. display: flex;
  419. margin-bottom: 10px;
  420. }
  421. .content-f .el-input__wrapper {
  422. margin-right: 5px;
  423. }
  424. .addbutton {
  425. width: 100%;
  426. margin-top: 10px;
  427. }
  428. .conicon {
  429. width: 55px;
  430. height: 25px;
  431. font-size: 28px;
  432. line-height: 28px;
  433. cursor: pointer;
  434. }
  435. .jv-key {
  436. cursor: pointer;
  437. color: #0034f1;
  438. }
  439. </style>