mirror of
				https://gitee.com/dromara/mayfly-go
				synced 2025-11-04 08:20:25 +08:00 
			
		
		
		
	feat: sql执行改为选中鼠标右击显示执行按钮等
This commit is contained in:
		@@ -41,7 +41,7 @@ const service = Axios.create({
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// request interceptor
 | 
					// request interceptor
 | 
				
			||||||
service.interceptors.request.use(
 | 
					service.interceptors.request.use(
 | 
				
			||||||
    config => {
 | 
					    (config: any) => {
 | 
				
			||||||
        // do something before request is sent
 | 
					        // do something before request is sent
 | 
				
			||||||
        const token = getSession("token")
 | 
					        const token = getSession("token")
 | 
				
			||||||
        if (token) {
 | 
					        if (token) {
 | 
				
			||||||
@@ -99,7 +99,7 @@ service.interceptors.response.use(
 | 
				
			|||||||
 * @param {Object} uri    uri
 | 
					 * @param {Object} uri    uri
 | 
				
			||||||
 * @param {Object} params 参数
 | 
					 * @param {Object} params 参数
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
function request(method: string, url: string, params: any, headers: any, options: any): Promise<any> {
 | 
					function request(method: string, url: string, params: any = null, headers: any = null, options: any = null): Promise<any> {
 | 
				
			||||||
    if (!url)
 | 
					    if (!url)
 | 
				
			||||||
        throw new Error('请求url不能为空');
 | 
					        throw new Error('请求url不能为空');
 | 
				
			||||||
    // 简单判断该url是否是restful风格
 | 
					    // 简单判断该url是否是restful风格
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,9 @@
 | 
				
			|||||||
<template>
 | 
					<template>
 | 
				
			||||||
    <div>
 | 
					    <div>
 | 
				
			||||||
 | 
					        <el-button-group :style="btnStyle">
 | 
				
			||||||
 | 
					            <el-button @click="onRunSql" type="success" icon="video-play" size="small" plain>执行</el-button>
 | 
				
			||||||
 | 
					            <el-button @click="formatSql" type="primary" icon="magic-stick" size="small" plain>格式化</el-button>
 | 
				
			||||||
 | 
					        </el-button-group>
 | 
				
			||||||
        <div class="toolbar">
 | 
					        <div class="toolbar">
 | 
				
			||||||
            <el-row type="flex" justify="space-between">
 | 
					            <el-row type="flex" justify="space-between">
 | 
				
			||||||
                <el-col :span="24">
 | 
					                <el-col :span="24">
 | 
				
			||||||
@@ -82,12 +86,8 @@
 | 
				
			|||||||
                            </div>
 | 
					                            </div>
 | 
				
			||||||
                        </div>
 | 
					                        </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        <div class="mt5 sqlEditor" @mousemove="listenMouse">
 | 
					                        <div @click="closeExecBtns" class="mt5 sqlEditor" @contextmenu="showExecBtns">
 | 
				
			||||||
                            <textarea ref="codeTextarea"></textarea>
 | 
					                            <textarea ref="codeTextarea"></textarea>
 | 
				
			||||||
                            <el-button-group :style="btnStyle">
 | 
					 | 
				
			||||||
                                <el-button @click="onRunSql" type="success" icon="video-play" size="small" plain>执行</el-button>
 | 
					 | 
				
			||||||
                                <el-button @click="formatSql" type="primary" icon="magic-stick" size="small" plain>格式化</el-button>
 | 
					 | 
				
			||||||
                            </el-button-group>
 | 
					 | 
				
			||||||
                        </div>
 | 
					                        </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        <div class="mt5">
 | 
					                        <div class="mt5">
 | 
				
			||||||
@@ -189,7 +189,6 @@
 | 
				
			|||||||
<script lang="ts">
 | 
					<script lang="ts">
 | 
				
			||||||
import { onMounted, toRefs, reactive, defineComponent, ref } from 'vue';
 | 
					import { onMounted, toRefs, reactive, defineComponent, ref } from 'vue';
 | 
				
			||||||
import { dbApi } from './api';
 | 
					import { dbApi } from './api';
 | 
				
			||||||
import _ from 'lodash';
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import 'codemirror/addon/hint/show-hint.css';
 | 
					import 'codemirror/addon/hint/show-hint.css';
 | 
				
			||||||
// import base style
 | 
					// import base style
 | 
				
			||||||
@@ -297,15 +296,27 @@ export default defineComponent({
 | 
				
			|||||||
                // 将sql提示去除
 | 
					                // 将sql提示去除
 | 
				
			||||||
                changeObj.text[0] = text.split('  ')[0];
 | 
					                changeObj.text[0] = text.split('  ')[0];
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
            // 默认300px
 | 
					            
 | 
				
			||||||
            codemirror.setSize('auto', `${window.innerHeight - 538}px`);
 | 
					 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        onMounted(() => {
 | 
					        onMounted(() => {
 | 
				
			||||||
            initCodemirror();
 | 
					            initCodemirror();
 | 
				
			||||||
            state.dataTabsTableHeight = window.innerHeight - 258;
 | 
					            setHeight();
 | 
				
			||||||
 | 
					            // 监听浏览器窗口大小变化,更新对应组件高度
 | 
				
			||||||
 | 
					            window.onresize = () => (() => {
 | 
				
			||||||
 | 
					                setHeight();
 | 
				
			||||||
 | 
					            })();
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /**
 | 
				
			||||||
 | 
					         * 设置codemirror高度和数据表高度
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        const setHeight = () => {
 | 
				
			||||||
 | 
					            // 默认300px
 | 
				
			||||||
 | 
					            codemirror.setSize('auto', `${window.innerHeight - 538}px`);
 | 
				
			||||||
 | 
					            state.dataTabsTableHeight = window.innerHeight - 258;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /**
 | 
					        /**
 | 
				
			||||||
         * 项目及环境更改后的回调事件
 | 
					         * 项目及环境更改后的回调事件
 | 
				
			||||||
         */
 | 
					         */
 | 
				
			||||||
@@ -332,7 +343,7 @@ export default defineComponent({
 | 
				
			|||||||
            notBlank(state.dbId, '请先选择数据库');
 | 
					            notBlank(state.dbId, '请先选择数据库');
 | 
				
			||||||
            // 没有选中的文本,则为全部文本
 | 
					            // 没有选中的文本,则为全部文本
 | 
				
			||||||
            let sql = getSql();
 | 
					            let sql = getSql();
 | 
				
			||||||
            notBlank(sql.trim(), 'sql内容不能为空');
 | 
					            isTrue(sql && sql.trim(), '请选中需要执行的sql');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            state.queryTab.loading = true;
 | 
					            state.queryTab.loading = true;
 | 
				
			||||||
            // 即只有以该字符串开头的sql才可修改表数据内容
 | 
					            // 即只有以该字符串开头的sql才可修改表数据内容
 | 
				
			||||||
@@ -360,6 +371,7 @@ export default defineComponent({
 | 
				
			|||||||
            } catch (e: any) {
 | 
					            } catch (e: any) {
 | 
				
			||||||
                state.queryTab.loading = false;
 | 
					                state.queryTab.loading = false;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            closeExecBtns();
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /**
 | 
					        /**
 | 
				
			||||||
@@ -499,7 +511,7 @@ export default defineComponent({
 | 
				
			|||||||
        const getSql = () => {
 | 
					        const getSql = () => {
 | 
				
			||||||
            // 没有选中的文本,则为全部文本
 | 
					            // 没有选中的文本,则为全部文本
 | 
				
			||||||
            let selectSql = codemirror.getSelection();
 | 
					            let selectSql = codemirror.getSelection();
 | 
				
			||||||
            if (selectSql == '') {
 | 
					            if (!selectSql) {
 | 
				
			||||||
                selectSql = getCodermirrorValue();
 | 
					                selectSql = getCodermirrorValue();
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            return selectSql;
 | 
					            return selectSql;
 | 
				
			||||||
@@ -730,6 +742,7 @@ export default defineComponent({
 | 
				
			|||||||
            state.queryTab.execRes.tableColumn = [];
 | 
					            state.queryTab.execRes.tableColumn = [];
 | 
				
			||||||
            state.cmOptions.hintOptions.tables = [];
 | 
					            state.cmOptions.hintOptions.tables = [];
 | 
				
			||||||
            tableMap.clear();
 | 
					            tableMap.clear();
 | 
				
			||||||
 | 
					            closeExecBtns();
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const onDataSelectionChange = (datas: []) => {
 | 
					        const onDataSelectionChange = (datas: []) => {
 | 
				
			||||||
@@ -872,13 +885,9 @@ export default defineComponent({
 | 
				
			|||||||
         */
 | 
					         */
 | 
				
			||||||
        const formatSql = () => {
 | 
					        const formatSql = () => {
 | 
				
			||||||
            let selectSql = codemirror.getSelection();
 | 
					            let selectSql = codemirror.getSelection();
 | 
				
			||||||
            // 有选中sql则只格式化选中部分,否则格式化全部
 | 
					            isTrue(selectSql, '请选中需要格式化的sql');
 | 
				
			||||||
            if (selectSql != '') {
 | 
					 | 
				
			||||||
            codemirror.replaceSelection(sqlFormatter(selectSql));
 | 
					            codemirror.replaceSelection(sqlFormatter(selectSql));
 | 
				
			||||||
            } else {
 | 
					            closeExecBtns();
 | 
				
			||||||
                /* 将sql内容进行格式后放入编辑器中*/
 | 
					 | 
				
			||||||
                setCodermirrorValue(sqlFormatter(getCodermirrorValue()));
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const search = async () => {
 | 
					        const search = async () => {
 | 
				
			||||||
@@ -887,25 +896,28 @@ export default defineComponent({
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /**
 | 
					        /**
 | 
				
			||||||
         * 获取选择文字,显示隐藏按钮,防抖
 | 
					         * 显示执行sql和格式化按钮
 | 
				
			||||||
         */
 | 
					         */
 | 
				
			||||||
        const getSelection = _.debounce((e: any) => {
 | 
					        const showExecBtns = (event: any) => {
 | 
				
			||||||
            let temp = codemirror.getSelection();
 | 
					            if (event.preventDefault) {
 | 
				
			||||||
            if (temp) {
 | 
					                event.preventDefault();
 | 
				
			||||||
                state.btnStyle.display = 'block';
 | 
					 | 
				
			||||||
                if (!state.btnStyle.left) {
 | 
					 | 
				
			||||||
                    state.btnStyle.left = e.target.getBoundingClientRect().left;
 | 
					 | 
				
			||||||
                    state.btnStyle.top = e.target.getBoundingClientRect().top - 160 + 'px';
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
 | 
					                event.returnValue = false;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            state.btnStyle.display = 'inline';
 | 
				
			||||||
 | 
					            state.btnStyle.left = event.offsetX + 15 + 'px';
 | 
				
			||||||
 | 
					            state.btnStyle.top = event.clientY - 80 + 'px';
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /**
 | 
				
			||||||
 | 
					         * 关闭执行sql和格式化按钮
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					        const closeExecBtns = () => {
 | 
				
			||||||
 | 
					            if (state.btnStyle.left) {
 | 
				
			||||||
                state.btnStyle.display = 'none';
 | 
					                state.btnStyle.display = 'none';
 | 
				
			||||||
                state.btnStyle.left = '';
 | 
					                state.btnStyle.left = '';
 | 
				
			||||||
                state.btnStyle.top = '';
 | 
					                state.btnStyle.top = '';
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }, 100);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const listenMouse = (e: any) => {
 | 
					 | 
				
			||||||
            getSelection(e);
 | 
					 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return {
 | 
					        return {
 | 
				
			||||||
@@ -929,7 +941,6 @@ export default defineComponent({
 | 
				
			|||||||
            clearDb,
 | 
					            clearDb,
 | 
				
			||||||
            formatSql,
 | 
					            formatSql,
 | 
				
			||||||
            onBeforeChange,
 | 
					            onBeforeChange,
 | 
				
			||||||
            listenMouse,
 | 
					 | 
				
			||||||
            onRefresh,
 | 
					            onRefresh,
 | 
				
			||||||
            selectByCondition,
 | 
					            selectByCondition,
 | 
				
			||||||
            onCommit,
 | 
					            onCommit,
 | 
				
			||||||
@@ -937,6 +948,8 @@ export default defineComponent({
 | 
				
			|||||||
            onDataSelectionChange,
 | 
					            onDataSelectionChange,
 | 
				
			||||||
            onDeleteData,
 | 
					            onDeleteData,
 | 
				
			||||||
            onTableSortChange,
 | 
					            onTableSortChange,
 | 
				
			||||||
 | 
					            showExecBtns,
 | 
				
			||||||
 | 
					            closeExecBtns,
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -189,7 +189,7 @@ import { recommendList } from './mock.ts';
 | 
				
			|||||||
import { useStore } from '@/store/index.ts';
 | 
					import { useStore } from '@/store/index.ts';
 | 
				
			||||||
import { personApi } from './api';
 | 
					import { personApi } from './api';
 | 
				
			||||||
export default {
 | 
					export default {
 | 
				
			||||||
    name: 'personal',
 | 
					    name: 'PersonalPage',
 | 
				
			||||||
    setup() {
 | 
					    setup() {
 | 
				
			||||||
        const store = useStore();
 | 
					        const store = useStore();
 | 
				
			||||||
        const state = reactive({
 | 
					        const state = reactive({
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user