edit.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <template>
  2. <div class="system-edit-dic-container">
  3. <el-dialog :title="(ruleForm.sourceId !== 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="请输入数据源名称" />
  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="description">
  12. <el-input v-model="ruleForm.description" type="textarea" placeholder="请输入内容"></el-input>
  13. </el-form-item>
  14. <el-form-item label="数据来源" prop="from">
  15. <el-radio-group v-model="ruleForm.from">
  16. <el-radio :label="1">api导入</el-radio>
  17. <el-radio :label="2">数据库</el-radio>
  18. <el-radio :label="3">文件</el-radio>
  19. </el-radio-group>
  20. </el-form-item>
  21. <el-divider content-position="left">规则表达式</el-divider>
  22. <div v-for="(item, index) in rule" :key="index">
  23. <el-form-item label="正则表达式">
  24. <el-input v-model="item.expression" placeholder="请输入正则表达式" />
  25. </el-form-item>
  26. <el-form-item label="替换内容">
  27. <el-input v-model="item.replace" placeholder="请输入替换内容" class="w-35" />
  28. <!-- <el-input v-model="item.params.name" placeholder="请输入键值" class="w-35" />
  29. <el-input v-model="item.params.value" placeholder="请输入值" class="w-35" /> -->
  30. <div class="conicon">
  31. <el-icon @click="delRule(index)" v-if="index > 0"><Delete /></el-icon>
  32. </div>
  33. </el-form-item>
  34. </div>
  35. <div style="padding: 10px">
  36. <el-button type="primary" class="addbutton" @click="addRule">增加</el-button>
  37. </div>
  38. <el-divider content-position="left">数据源配置</el-divider>
  39. <el-form-item label="请求方法">
  40. <el-select v-model="config.method" placeholder="请选择请求方法">
  41. <el-option v-for="item in methodData" :key="item.value" :label="item.label" :value="item.value" />
  42. </el-select>
  43. </el-form-item>
  44. <el-form-item label="请求地址">
  45. <el-input v-model="config.url" placeholder="请输入请求地址" />
  46. </el-form-item>
  47. <el-form-item label="更新时间">
  48. <el-input v-model="config.interval" placeholder="请输入更新时间" class="w-35" />
  49. <el-select v-model="config.intervalUnit" placeholder="请选择单位">
  50. <el-option v-for="item in unitData" :key="item.value" :label="item.label" :value="item.value" />
  51. </el-select>
  52. </el-form-item>
  53. <div class="box-content">
  54. <el-divider content-position="left">请求参数</el-divider>
  55. <div class="content-f" v-for="(item, index) in requestParams" :key="index">
  56. <el-select v-model="item.type" placeholder="参数类型" style="width: 320px">
  57. <el-option v-for="item in paramData" :key="item.value" :label="item.label" :value="item.value" />
  58. </el-select>
  59. <el-input v-model="item.name" placeholder="请输入参数标题" style="width: 320px" />
  60. <el-input v-model="item.key" placeholder="请输入参数名" style="width: 320px" />
  61. <el-input v-model="item.value" placeholder="请输入参数值" style="width: 320px" />
  62. <div class="conicon">
  63. <el-icon @click="delParams(index)" v-if="index > 0"><Delete /></el-icon>
  64. </div>
  65. </div>
  66. <el-button type="primary" class="addbutton" @click="addParams">增加</el-button>
  67. </div>
  68. </el-form>
  69. <template #footer>
  70. <span class="dialog-footer">
  71. <el-button @click="onTest" type="warning" size="default">测试</el-button>
  72. <el-button @click="onCancel" size="default">取 消</el-button>
  73. <el-button type="primary" @click="onSubmit" size="default">{{ ruleForm.sourceId !== 0 ? '修 改' : '添 加' }}</el-button>
  74. </span>
  75. </template>
  76. </el-dialog>
  77. <el-dialog
  78. v-model="dialogVisible"
  79. title="返回Json数据"
  80. width="30%"
  81. :before-close="handleClose"
  82. >
  83. <json-viewer :value="jsonData" boxed sort :expand-depth=20 theme="my-awesome-json-theme" />
  84. <template #footer>
  85. <span class="dialog-footer">
  86. <el-button @click="dialogVisible = false">关闭</el-button>
  87. </span>
  88. </template>
  89. </el-dialog>
  90. </div>
  91. </template>
  92. <script lang="ts">
  93. import { reactive, toRefs, defineComponent, ref, unref } from 'vue';
  94. import api from '/@/api/datahub';
  95. import { ElMessage } from 'element-plus';
  96. import { Delete, Minus, Right } from '@element-plus/icons-vue';
  97. interface RuleFormState {
  98. sourceId: number;
  99. name: string;
  100. from: number;
  101. key: string;
  102. userName: string;
  103. password: string;
  104. description: string;
  105. status: number;
  106. }
  107. interface DicState {
  108. isShowDialog: boolean;
  109. ruleForm: RuleFormState;
  110. rules: {};
  111. }
  112. export default defineComponent({
  113. name: 'Edit',
  114. components: { Delete, Minus, Right },
  115. setup(prop, { emit }) {
  116. const formRef = ref<HTMLElement | null>(null);
  117. const state = reactive<DicState>({
  118. isShowDialog: false,
  119. dialogVisible:false,
  120. config: {},
  121. sourceId:0,
  122. jsonData:'',
  123. ruledata: [
  124. {
  125. expression: '',
  126. replace: '',
  127. //params: {},
  128. },
  129. ],
  130. rule: [
  131. {
  132. expression: '',
  133. replace: '',
  134. // params: {
  135. // name: '',
  136. // value: '',
  137. // },
  138. },
  139. ],
  140. requestParams: [
  141. {
  142. type: '',
  143. key: '',
  144. name: '',
  145. value: '',
  146. },
  147. ],
  148. methodData: [
  149. {
  150. label: 'GET',
  151. value: 'get',
  152. },
  153. {
  154. label: 'POST',
  155. value: 'post',
  156. },
  157. {
  158. label: 'PUT',
  159. value: 'put',
  160. },
  161. ],
  162. unitData: [
  163. {
  164. label: '秒',
  165. value: 'second',
  166. },
  167. {
  168. label: '分',
  169. value: 'minute',
  170. },
  171. {
  172. label: '时',
  173. value: 'hour',
  174. },
  175. {
  176. label: '天',
  177. value: 'day',
  178. },
  179. ],
  180. paramData: [
  181. {
  182. lable: 'header',
  183. value: 'header',
  184. },
  185. {
  186. lable: 'body',
  187. value: 'body',
  188. },
  189. {
  190. lable: 'param',
  191. value: 'param',
  192. },
  193. ],
  194. ruleForm: {
  195. sourceId: 0,
  196. name: '',
  197. from: 1,
  198. key: '',
  199. rule: [],
  200. config: {
  201. method: '',
  202. url: '',
  203. interval: '',
  204. intervalUnit: '',
  205. requestParams: [],
  206. },
  207. description: '',
  208. },
  209. rules: {
  210. key: [{ required: true, message: '数据源标识不能为空', trigger: 'blur' }],
  211. name: [{ required: true, message: '数据源名称不能为空', trigger: 'blur' }],
  212. from: [{ required: true, message: '数据源类型不能为空', trigger: 'blur' }],
  213. },
  214. });
  215. const delParams = (index) => {
  216. state.requestParams.splice(index, 1);
  217. };
  218. const addParams = () => {
  219. state.requestParams.push({
  220. type: '',
  221. key: '',
  222. name: '',
  223. value: '',
  224. });
  225. };
  226. const delRule = (index) => {
  227. state.rule.splice(index, 1);
  228. };
  229. const addRule = () => {
  230. state.rule.push({
  231. expression: '',
  232. replace: '',
  233. // params: {
  234. // name: '',
  235. // value: '',
  236. // },
  237. });
  238. };
  239. // 打开弹窗
  240. const openDialog = (row: RuleFormState | null) => {
  241. resetForm();
  242. if (row) {
  243. state.sourceId=row.sourceId
  244. api.common.detail(row.sourceId).then((res: any) => {
  245. state.ruleForm = res.data;
  246. state.config = res.data.apiConfig;
  247. state.requestParams = res.data.apiConfig.requestParams;
  248. res.data.sourceRule.forEach((item, index) => {
  249. state.rule[index].expression = item.expression;
  250. state.rule[index].replace = item.replace;
  251. // state.rule[index].params.name = Object.keys(item.params);
  252. // state.rule[index].params.value = item.params[Object.keys(item.params)];
  253. });
  254. });
  255. }
  256. state.isShowDialog = true;
  257. };
  258. const resetForm = () => {
  259. state.ruleForm = {
  260. sourceId: 0,
  261. name: '',
  262. from: 1,
  263. key: '',
  264. rule: [],
  265. config: {
  266. method: '',
  267. url: '',
  268. interval: '',
  269. intervalUnit: '',
  270. requestParams: [],
  271. },
  272. description: '',
  273. };
  274. };
  275. // 关闭弹窗
  276. const closeDialog = () => {
  277. state.isShowDialog = false;
  278. };
  279. // 取消
  280. const onCancel = () => {
  281. closeDialog();
  282. };
  283. const onTest=()=>{
  284. api.common.api(state.sourceId).then((res: any) => {
  285. state.jsonData=JSON.parse(res.data);
  286. state.dialogVisible=true
  287. console.log(res);
  288. })
  289. };
  290. // 新增
  291. const onSubmit = () => {
  292. const formWrap = unref(formRef) as any;
  293. if (!formWrap) return;
  294. formWrap.validate((valid: boolean) => {
  295. if (valid) {
  296. //修改rule数据
  297. // state.rule.forEach((item, index) => {
  298. // item.params[item.params.name] = item.params.value;
  299. // delete item.params.name;
  300. // delete item.params.value;
  301. // });
  302. state.ruleForm.rule = state.rule;
  303. state.config.requestParams = state.requestParams;
  304. state.ruleForm.config = state.config;
  305. if (state.ruleForm.sourceId !== 0) {
  306. //修改
  307. api.common.edit(state.ruleForm).then(() => {
  308. ElMessage.success('数据源类型修改成功');
  309. closeDialog(); // 关闭弹窗
  310. emit('typeList');
  311. });
  312. } else {
  313. //添加
  314. api.common.add(state.ruleForm).then(() => {
  315. ElMessage.success('数据源类型添加成功');
  316. closeDialog(); // 关闭弹窗
  317. emit('typeList');
  318. });
  319. }
  320. }
  321. });
  322. };
  323. return {
  324. onTest,
  325. addRule,
  326. delRule,
  327. addParams,
  328. delParams,
  329. openDialog,
  330. closeDialog,
  331. onCancel,
  332. onSubmit,
  333. formRef,
  334. ...toRefs(state),
  335. };
  336. },
  337. });
  338. </script>
  339. <style>
  340. .el-input__wrapper {
  341. width: 98%;
  342. }
  343. .box-content {
  344. border: 1px solid #e8e8e8;
  345. margin: 10px;
  346. padding: 10px;
  347. }
  348. .content-f {
  349. display: flex;
  350. margin-bottom: 10px;
  351. }
  352. .content-f .el-input__wrapper {
  353. margin-right: 5px;
  354. }
  355. .addbutton {
  356. width: 100%;
  357. margin-top: 10px;
  358. }
  359. .conicon {
  360. width: 55px;
  361. height: 25px;
  362. font-size: 28px;
  363. line-height: 28px;
  364. cursor: pointer;
  365. }
  366. .jv-node{
  367. margin-left: 25px;
  368. }
  369. .jv-my-awesome-json-theme {
  370. background: #d9f1e9;
  371. white-space: nowrap;
  372. color: #525252;
  373. font-size: 14px;
  374. font-family: Consolas, Menlo, Courier, monospace;
  375. }
  376. </style>