Browse Source

数据中台

xiaochan 1 year ago
parent
commit
a8de831751

+ 3 - 2
src/ts-type/data-base-type.ts

@@ -21,7 +21,6 @@ export interface TDataBaseSqlApi {
     id: string;
     name: string
     config: string;
-    dataBaseId: number;
     sql: string;
     requestMapping: string;
     enable: number
@@ -38,8 +37,10 @@ export interface TDataBaseSqlApi {
             page: boolean
             pageSize: number
         },
-        removeSpaces: boolean
+        removeSpaces: boolean,
+        dataSources: string[]
     }
     children?: TDataBaseSqlApi[]
     params?: any
+    currentDataSource?: string
 }

+ 54 - 0
src/utils/genergic-token-parser.ts

@@ -0,0 +1,54 @@
+class GenericTokenParser {
+    private readonly openToken: string;
+    private readonly closeToken: string;
+    private readonly handler: (content: string) => string;
+
+    constructor(openToken: string, closeToken: string, handler: (content: string) => string) {
+        this.openToken = openToken;
+        this.closeToken = closeToken;
+        this.handler = handler;
+    }
+
+    parse(text: string) {
+        let builder = '';
+        if (text != null && text !== '') {
+            let offset = 0;
+            let start = text.indexOf(this.openToken, offset);
+            while (start > -1) {
+                if (start > 0 && text[start - 1] === '\\') {
+                    // the variable is escaped. remove the backslash.
+                    builder += text.substring(offset, start - 1) + this.openToken;
+                    offset = start + this.openToken.length;
+                } else {
+                    let end = text.indexOf(this.closeToken, start);
+                    if (end === -1) {
+                        builder += text.substring(offset, text.length);
+                        offset = text.length;
+                    } else {
+                        builder += text.substring(offset, start - offset);
+                        offset = start + this.openToken.length;
+                        let content = text.substring(offset, end);
+                        builder += this.handler(content);
+                        offset = end + this.closeToken.length;
+                    }
+                }
+                start = text.indexOf(this.openToken, offset);
+            }
+            if (offset < text.length) {
+                builder += text.substring(offset, text.length);
+            }
+        }
+        return builder;
+    }
+}
+
+// const tokenParser = new GenericTokenParser("${", "}",
+//     (content) => {
+//         // 处理占位符的逻辑
+//         console.log('content', content)
+//         return 'woqi';
+//     });
+//
+// const parsedText = tokenParser.parse('这是一个示例,占位符为${  placeholder}。 ${aa}');
+
+export default GenericTokenParser

+ 46 - 28
src/views/data-base/data-base-api/components/ApiTabPane.vue

@@ -1,7 +1,7 @@
 <script setup lang="ts">
 import {useVModel} from "@vueuse/core";
 import {nextTick, onMounted, onUnmounted, ref, Ref, unref} from 'vue'
-import {TDataBaseSqlApi} from "@/ts-type/data-base-type";
+import type {TDataBaseSqlApi} from "@/ts-type/data-base-type";
 import * as monaco from "monaco-editor";
 import JsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker'
 import CssWorker from 'monaco-editor/esm/vs/language/css/css.worker?worker'
@@ -15,6 +15,7 @@ import {FormInstance} from "element-plus";
 import {xcMessage} from "@/utils/xiaochan-element-plus";
 import {copyStrFunc, isDev} from "@/utils/public";
 import {windowSizeStore} from "@/utils/store-public";
+import GenericTokenParser from "@/utils/genergic-token-parser";
 
 // 代码提示
 self.MonacoEnvironment = {
@@ -46,7 +47,7 @@ const props = defineProps({
   }
 })
 
-const urlPrefix = isDev ? `http://172.16.30.61:8706/dataBase/api/data` : `http://172.16.32.160:8077/dataBase/api/data`;
+const urlPrefix = isDev ? `http://172.16.30.61:8706/dataBase/api` : `http://172.16.32.160:8077/dataBase/api`;
 
 const emits = defineEmits(['update:modelValue'])
 const modelValue: Ref<TDataBaseSqlApi> = useVModel(props, 'modelValue', emits) as Ref<TDataBaseSqlApi>
@@ -110,6 +111,7 @@ async function handleTest() {
 
   const data = prepareData()
   data.params = unref(testParams)
+  data.currentDataSource = dataSource.value
 
   testSqlApi(data).then(res => {
     console.log(res)
@@ -174,19 +176,21 @@ function copyUrl(url: string) {
 }
 
 function fillingFunc() {
-  const str = modelValue.value.sql;
-  const regex = /\{(.*?)\}/g;
-  const matches = str.match(regex);
-  if (!matches) {
-    return
-  }
+  const addKeys = new Set()
+
+  new GenericTokenParser('#{', "}", (content) => {
+    addKeys.add(content)
+    return ""
+  }).parse(modelValue.value.sql)
+
+  new GenericTokenParser('${', "}", (content) => {
+    addKeys.add(content)
+    return ""
+  }).parse(modelValue.value.sql)
+
 
   const keys = new Set()
-  const addKeys = new Set()
 
-  matches.forEach(item => {
-    addKeys.add(item.slice(1, -1).trim())
-  })
 
   modelValue.value.configJson.params.forEach(item => {
     keys.add(item.key)
@@ -200,9 +204,11 @@ function fillingFunc() {
   })
 
   result1.forEach(item => {
+    const index = modelValue.value.sql.indexOf(`test="${item}`)
+
     modelValue.value.configJson.params.push({
       key: item,
-      required: false,
+      required: index === -1,
       describe: '',
       defaultValue: null
     })
@@ -215,6 +221,8 @@ function processingOpenProperties() {
   drawerDialog.value = true
 }
 
+const dataSource = ref('')
+
 onUnmounted(() => {
   monacoEditor.dispose()
 })
@@ -242,12 +250,25 @@ onMounted(async () => {
   monacoEditor.onDidChangeModelContent((val) => {
     modelValue.value.sql = monacoEditor.getValue();
   })
+  dataSource.value = modelValue.value.configJson.dataSources[0]
 })
 </script>
 
 <template>
   <div class="api_pane-container">
     <div class="api_pane-header">
+         <span style="font-size: 12px;margin: 0">
+          数据库:
+      </span>
+      <el-select v-model="modelValue.configJson.dataSources"
+                 placeholder="请选择"
+                 size="small"
+                 multiple
+                 collapse-tags
+                 style="width: 160px">
+        <el-option v-for="item in props.dataBase" :label="item.name" :value="item.alias"/>
+      </el-select>
+      <el-divider direction="vertical"/>
       <span class="iconfont icon-baocun" @click="handleSave" title="保存"/>
       <span class="iconfont icon-ceshi" title="测试" @click="handleDialogOpen"></span>
       <el-button
@@ -256,18 +277,6 @@ onMounted(async () => {
         {{ modelValue.enable ? '停用' : '启用' }}
       </el-button>
       <el-divider direction="vertical"/>
-      <span style="font-size: 12px;margin: 0">
-          数据库:
-      </span>
-
-      <!--   multiple   -->
-      <el-select v-model="modelValue.dataBaseId"
-                 placeholder="请选择"
-                 size="small"
-                 style="width: 120px">
-        <el-option v-for="item in props.dataBase" :label="item.name" :value="item.id"/>
-      </el-select>
-      <el-divider direction="vertical"/>
       <el-button @click="processingOpenProperties" type="primary">属性</el-button>
     </div>
 
@@ -355,7 +364,7 @@ onMounted(async () => {
               </el-col>
 
               <el-col :span="24" v-if="modelValue.configJson.pageInfo.page">
-                <el-form-item label="页大">
+                <el-form-item label="页大">
                   <el-select v-model="modelValue.configJson.pageInfo.pageSize">
                     <xc-el-option :data="pageSizeList"/>
                   </el-select>
@@ -378,9 +387,18 @@ onMounted(async () => {
         :column="3"
         size="small">
       <el-descriptions-item label="地址" :span="3">
-        {{ `${urlPrefix}/${modelValue.id}` }}
+        <el-select v-model="dataSource"
+                   placeholder="请选择"
+                   size="small"
+                   style="width: 120px">
+          <el-option v-for="item in props.dataBase"
+                     :label="item.name"
+                     :disabled="!modelValue.configJson.dataSources.includes(item.alias)"
+                     :value="item.alias"/>
+        </el-select>
+        {{ `${urlPrefix}/${dataSource}/${modelValue.id}` }}
         <el-button icon="CopyDocument" circle
-                   @click="copyUrl(`${urlPrefix}/${modelValue.id}`)"/>
+                   @click="copyUrl(`${urlPrefix}/${dataSource}/${modelValue.id}`)"/>
       </el-descriptions-item>
       <el-descriptions-item label="请求方式">
         {{ modelValue.requestMapping }}

+ 2 - 2
src/views/data-base/data-base-api/components/ApiTree.vue

@@ -145,14 +145,14 @@ function prepareData(type: 1 | 2, name: string, parentId: string = null) {
       page: true,
       pageSize: 50
     },
-    removeSpaces: true
+    removeSpaces: true,
+    dataSources: []
   }
 
   return {
     "id": null,
     name,
     "sql": "",
-    "dataBaseId": 0,
     "requestMapping": "GET",
     "config": JSON.stringify(a),
     "enable": 0,

+ 0 - 1
src/views/data-base/data-base-api/src/DataBaseApi.vue

@@ -46,7 +46,6 @@ function delApi(id) {
   if (index > -1) {
     tabsList.value.splice(index, 1)
   }
-
 }
 
 function query() {