This commit is contained in:
meilin.huang
2023-10-12 12:14:56 +08:00
parent 2c2c0ff40b
commit d300f604f1
31 changed files with 204 additions and 271 deletions

View File

@@ -1,76 +1,76 @@
module.exports = {
root: true,
env: {
browser: true,
es2021: true,
node: true,
},
parser: 'vue-eslint-parser',
parserOptions: {
ecmaVersion: 12,
parser: '@typescript-eslint/parser',
sourceType: 'module',
},
extends: ['plugin:vue/vue3-essential', 'plugin:vue/essential', 'eslint:recommended'],
plugins: ['vue', '@typescript-eslint'],
overrides: [
{
files: ['*.ts', '*.tsx', '*.vue'],
rules: {
'no-undef': 'off',
},
},
],
rules: {
// http://eslint.cn/docs/rules/
// https://eslint.vuejs.org/rules/
// https://typescript-eslint.io/rules/no-unused-vars/
'@typescript-eslint/ban-ts-ignore': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/ban-types': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-redeclare': 'error',
'@typescript-eslint/no-non-null-asserted-optional-chain': 'off',
'@typescript-eslint/no-unused-vars': [2],
'vue/custom-event-name-casing': 'off',
'vue/attributes-order': 'off',
'vue/one-component-per-file': 'off',
'vue/html-closing-bracket-newline': 'off',
'vue/max-attributes-per-line': 'off',
'vue/multiline-html-element-content-newline': 'off',
'vue/singleline-html-element-content-newline': 'off',
'vue/attribute-hyphenation': 'off',
'vue/html-self-closing': 'off',
'vue/no-multiple-template-root': 'off',
'vue/require-default-prop': 'off',
'vue/no-v-model-argument': 'off',
'vue/no-arrow-functions-in-watch': 'off',
'vue/no-template-key': 'off',
'vue/no-v-html': 'off',
'vue/comment-directive': 'off',
'vue/no-parsing-error': 'off',
'vue/no-deprecated-v-on-native-modifier': 'off',
'vue/multi-word-component-names': 'off',
'no-useless-escape': 'off',
'no-sparse-arrays': 'off',
'no-prototype-builtins': 'off',
'no-constant-condition': 'off',
'no-use-before-define': 'off',
'no-restricted-globals': 'off',
'no-restricted-syntax': 'off',
'generator-star-spacing': 'off',
'no-unreachable': 'off',
'no-multiple-template-root': 'off',
'no-unused-vars': 'error',
'no-v-model-argument': 'off',
'no-case-declarations': 'off',
'no-console': 'error',
'no-redeclare': 'off',
},
root: true,
env: {
browser: true,
es2021: true,
node: true,
},
parser: 'vue-eslint-parser',
parserOptions: {
ecmaVersion: 12,
parser: '@typescript-eslint/parser',
sourceType: 'module',
},
extends: ['plugin:vue/vue3-essential', 'plugin:vue/essential', 'eslint:recommended'],
plugins: ['vue', '@typescript-eslint'],
overrides: [
{
files: ['*.ts', '*.tsx', '*.vue'],
rules: {
'no-undef': 'off',
},
},
],
rules: {
// http://eslint.cn/docs/rules/
// https://eslint.vuejs.org/rules/
// https://typescript-eslint.io/rules/no-unused-vars/
'@typescript-eslint/ban-ts-ignore': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/ban-types': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-redeclare': 'error',
'@typescript-eslint/no-non-null-asserted-optional-chain': 'off',
'@typescript-eslint/no-unused-vars': [2],
'vue/custom-event-name-casing': 'off',
'vue/attributes-order': 'off',
'vue/one-component-per-file': 'off',
'vue/html-closing-bracket-newline': 'off',
'vue/max-attributes-per-line': 'off',
'vue/multiline-html-element-content-newline': 'off',
'vue/singleline-html-element-content-newline': 'off',
'vue/attribute-hyphenation': 'off',
'vue/html-self-closing': 'off',
'vue/no-multiple-template-root': 'off',
'vue/require-default-prop': 'off',
'vue/no-v-model-argument': 'off',
'vue/no-arrow-functions-in-watch': 'off',
'vue/no-template-key': 'off',
'vue/no-v-html': 'off',
'vue/comment-directive': 'off',
'vue/no-parsing-error': 'off',
'vue/no-deprecated-v-on-native-modifier': 'off',
'vue/multi-word-component-names': 'off',
'no-useless-escape': 'off',
'no-sparse-arrays': 'off',
'no-prototype-builtins': 'off',
'no-constant-condition': 'off',
'no-use-before-define': 'off',
'no-restricted-globals': 'off',
'no-restricted-syntax': 'off',
'generator-star-spacing': 'off',
'no-unreachable': 'off',
'no-multiple-template-root': 'off',
'no-unused-vars': 'error',
'no-v-model-argument': 'off',
'no-case-declarations': 'off',
// 'no-console': 'error',
'no-redeclare': 'off',
},
};

View File

@@ -1,14 +1,12 @@
export const buildProgressProps = () : any => {
export const buildProgressProps = (): any => {
return {
progress: {
sqlFileName: {
type: String
type: String,
},
executedStatements: {
type: Number
type: Number,
},
},
};
}
};

View File

@@ -1,41 +1,34 @@
<template>
<el-descriptions
border
size="small"
:title="`${progress.sqlFileName}`"
>
<el-descriptions border size="small" :title="`${progress.sqlFileName}`">
<el-descriptions-item label="时间">{{ state.elapsedTime }}</el-descriptions-item>
<el-descriptions-item label="已处理">{{ progress.executedStatements }}</el-descriptions-item>
</el-descriptions>
</template>
<script lang="ts" setup>
import {onMounted, onUnmounted, reactive} from "vue";
import {formatTime} from 'element-plus/es/components/countdown/src/utils';
import {buildProgressProps} from "./progress-notify";
import { onMounted, onUnmounted, reactive } from 'vue';
import { formatTime } from 'element-plus/es/components/countdown/src/utils';
import { buildProgressProps } from './progress-notify';
const props = defineProps(buildProgressProps());
const state = reactive({
elapsedTime: "00:00:00"
elapsedTime: '00:00:00',
});
let timer = undefined;
const startTime = Date.now()
let timer: any = undefined;
const startTime = Date.now();
onMounted(async () => {
timer = setInterval(() => {
const elapsed = Date.now() - startTime;
state.elapsedTime = formatTime(elapsed, 'HH:mm:ss')
}, 1000);
timer = setInterval(() => {
const elapsed = Date.now() - startTime;
state.elapsedTime = formatTime(elapsed, 'HH:mm:ss');
}, 1000);
});
onUnmounted(async () => {
if (timer != undefined) {
clearInterval(timer); // 在Vue实例销毁前清除我们的定时器
timer = undefined;
}
if (timer != undefined) {
clearInterval(timer); // 在Vue实例销毁前清除我们的定时器
timer = undefined;
}
});
</script>

View File

@@ -160,7 +160,7 @@ const baseInfoFormRef: any = ref(null);
const state = reactive({
accountLoginSecurity: {
useCaptcha: true,
useCaptcha: false,
useOtp: false,
loginFailCount: 5,
loginFailMin: 10,

View File

@@ -28,13 +28,9 @@ require (
golang.org/x/oauth2 v0.13.0
gopkg.in/yaml.v3 v3.0.1
// gorm
gorm.io/driver/mysql v1.5.1
gorm.io/gorm v1.25.4
)
require (
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
gorm.io/driver/sqlite v1.5.1
gorm.io/driver/mysql v1.5.2
gorm.io/driver/sqlite v1.5.4
gorm.io/gorm v1.25.5
)
require (
@@ -61,7 +57,7 @@ require (
github.com/kr/fs v0.1.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-sqlite3 v1.14.16 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.7.0 // indirect

View File

@@ -15,8 +15,8 @@ import (
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/otp"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/cryptox"
"mayfly-go/pkg/utils/jsonx"
"mayfly-go/pkg/ws"
"strconv"
"time"
@@ -43,7 +43,7 @@ func (a *AccountLogin) Login(rc *req.Ctx) {
username := loginForm.Username
clientIp := getIpAndRegion(rc)
rc.ReqParam = jsonx.Kvs("username", username, "ip", clientIp)
rc.ReqParam = collx.Kvs("username", username, "ip", clientIp)
originPwd, err := cryptox.DefaultRsaDecrypt(loginForm.Password, true)
biz.ErrIsNilAppendErr(err, "解密密码错误: %s")

View File

@@ -11,6 +11,7 @@ import (
"mayfly-go/pkg/cache"
"mayfly-go/pkg/otp"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/jsonx"
"mayfly-go/pkg/utils/netx"
"mayfly-go/pkg/utils/stringx"
@@ -29,7 +30,7 @@ func LastLoginCheck(account *sysentity.Account, accountLoginSecurity *config.Acc
biz.IsTrue(account.IsEnable(), "该账号不可用")
username := account.Username
res := map[string]any{
res := collx.M{
"name": account.Name,
"username": username,
"lastLoginTime": account.LastLoginTime,

View File

@@ -13,8 +13,8 @@ import (
"mayfly-go/pkg/captcha"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/cryptox"
"mayfly-go/pkg/utils/jsonx"
"strconv"
"strings"
"time"
@@ -49,7 +49,7 @@ func (a *LdapLogin) Login(rc *req.Ctx) {
username := loginForm.Username
clientIp := getIpAndRegion(rc)
rc.ReqParam = jsonx.Kvs("username", username, "ip", clientIp)
rc.ReqParam = collx.Kvs("username", username, "ip", clientIp)
originPwd, err := cryptox.DefaultRsaDecrypt(loginForm.Password, true)
biz.ErrIsNilAppendErr(err, "解密密码错误: %s")

View File

@@ -14,6 +14,7 @@ import (
"mayfly-go/pkg/cache"
"mayfly-go/pkg/model"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/jsonx"
"mayfly-go/pkg/utils/stringx"
"net/http"
@@ -98,7 +99,7 @@ func (a *Oauth2Login) OAuth2Callback(rc *req.Ctx) {
account.Id = accountId
err = a.AccountApp.GetAccount(account, "username")
biz.ErrIsNilAppendErr(err, "该账号不存在")
rc.ReqParam = jsonx.Kvs("username", account.Username, "type", "bind")
rc.ReqParam = collx.Kvs("username", account.Username, "type", "bind")
err = a.Oauth2App.GetOAuthAccount(&entity.Oauth2Account{
AccountId: accountId,
@@ -118,7 +119,7 @@ func (a *Oauth2Login) OAuth2Callback(rc *req.Ctx) {
UpdateTime: &now,
})
biz.ErrIsNilAppendErr(err, "绑定用户失败: %s")
res := map[string]any{
res := collx.M{
"action": "oauthBind",
"bind": true,
}
@@ -173,7 +174,7 @@ func (a *Oauth2Login) doLoginAction(rc *req.Ctx, userId string, oauth *config.Oa
biz.ErrIsNilAppendErr(err, "获取用户信息失败: %s")
clientIp := getIpAndRegion(rc)
rc.ReqParam = jsonx.Kvs("username", account.Username, "ip", clientIp, "type", "login")
rc.ReqParam = collx.Kvs("username", account.Username, "ip", clientIp, "type", "login")
res := LastLoginCheck(account, config.GetAccountLoginSecurity(), clientIp)
res["action"] = "oauthLogin"
@@ -220,7 +221,7 @@ func (a *Oauth2Login) Oauth2Unbind(rc *req.Ctx) {
// 获取oauth2登录配置信息因为有些字段是敏感字段故单独使用接口获取
func (c *Oauth2Login) Oauth2Config(rc *req.Ctx) {
oauth2LoginConfig := config.GetOauth2Login()
rc.ResData = map[string]any{
rc.ResData = collx.M{
"enable": oauth2LoginConfig.Enable,
"name": oauth2LoginConfig.Name,
}

View File

@@ -11,6 +11,7 @@ import (
redisentity "mayfly-go/internal/redis/domain/entity"
tagapp "mayfly-go/internal/tag/application"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
)
type Index struct {
@@ -36,7 +37,7 @@ func (i *Index) Count(rc *req.Ctx) {
dbNum = i.DbApp.Count(&dbentity.DbQuery{TagIds: tagIds})
redisNum = i.RedisApp.Count(&redisentity.RedisQuery{TagIds: tagIds})
}
rc.ResData = map[string]any{
rc.ResData = collx.M{
"mongoNum": mongoNum,
"machineNum": machienNum,
"dbNum": dbNum,

View File

@@ -3,7 +3,6 @@ package application
import (
"encoding/json"
"fmt"
"github.com/kanzihuang/vitess/go/vt/sqlparser"
"mayfly-go/internal/db/config"
"mayfly-go/internal/db/domain/entity"
"mayfly-go/internal/db/domain/repository"
@@ -11,6 +10,8 @@ import (
"mayfly-go/pkg/model"
"strconv"
"strings"
"github.com/kanzihuang/vitess/go/vt/sqlparser"
)
type DbSqlExecReq struct {

View File

@@ -14,7 +14,7 @@ import (
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/model"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/jsonx"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/stringx"
"mayfly-go/pkg/ws"
"os"
@@ -86,7 +86,7 @@ func (m *Machine) ChangeStatus(rc *req.Ctx) {
g := rc.GinCtx
id := uint64(ginx.PathParamInt(g, "machineId"))
status := int8(ginx.PathParamInt(g, "status"))
rc.ReqParam = jsonx.Kvs("id", id, "status", status)
rc.ReqParam = collx.Kvs("id", id, "status", status)
m.MachineApp.ChangeStatus(id, status)
}

View File

@@ -16,7 +16,6 @@ import (
"mayfly-go/pkg/logx"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/jsonx"
"mayfly-go/pkg/utils/timex"
"mime/multipart"
"path/filepath"
@@ -68,7 +67,7 @@ func (m *MachineFile) CreateFile(rc *req.Ctx) {
form := ginx.BindJsonAndValid(g, new(form.MachineCreateFileForm))
path := form.Path
attrs := jsonx.Kvs("path", path)
attrs := collx.Kvs("path", path)
var mi *machine.Info
var err error
if form.Type == dir {
@@ -90,7 +89,7 @@ func (m *MachineFile) ReadFileContent(rc *req.Ctx) {
readType := g.Query("type")
sftpFile, mi, err := m.MachineFileApp.ReadFile(fid, readPath)
rc.ReqParam = jsonx.Kvs("machine", mi, "path", readPath)
rc.ReqParam = collx.Kvs("machine", mi, "path", readPath)
biz.ErrIsNilAppendErr(err, "打开文件失败: %s")
defer sftpFile.Close()
@@ -163,7 +162,7 @@ func (m *MachineFile) WriteFileContent(rc *req.Ctx) {
path := form.Path
mi, err := m.MachineFileApp.WriteFileContent(fid, path, []byte(form.Content))
rc.ReqParam = jsonx.Kvs("machine", mi, "path", path)
rc.ReqParam = collx.Kvs("machine", mi, "path", path)
biz.ErrIsNilAppendErr(err, "打开文件失败: %s")
}
@@ -193,7 +192,7 @@ func (m *MachineFile) UploadFile(rc *req.Ctx) {
}()
mi, err := m.MachineFileApp.UploadFile(fid, path, fileheader.Filename, file)
rc.ReqParam = jsonx.Kvs("machine", mi, "path", fmt.Sprintf("%s/%s", path, fileheader.Filename))
rc.ReqParam = collx.Kvs("machine", mi, "path", fmt.Sprintf("%s/%s", path, fileheader.Filename))
biz.ErrIsNilAppendErr(err, "创建文件失败: %s")
// 保存消息并发送文件上传成功通知
m.MsgApp.CreateAndSend(la, msgdto.SuccessSysMsg("文件上传成功", fmt.Sprintf("[%s]文件已成功上传至 %s[%s:%s]", fileheader.Filename, mi.Name, mi.Ip, path)))
@@ -226,7 +225,7 @@ func (m *MachineFile) UploadFolder(rc *req.Ctx) {
mcli := m.MachineFileApp.GetMachineCli(fid, basePath+"/"+folderName)
mi := mcli.GetMachine()
sftpCli := mcli.GetSftpCli()
rc.ReqParam = jsonx.Kvs("machine", mi, "path", fmt.Sprintf("%s/%s", basePath, folderName))
rc.ReqParam = collx.Kvs("machine", mi, "path", fmt.Sprintf("%s/%s", basePath, folderName))
folderFiles := make([]FolderFile, len(paths))
// 先创建目录并将其包装为folderFile结构
@@ -297,7 +296,7 @@ func (m *MachineFile) RemoveFile(rc *req.Ctx) {
ginx.BindJsonAndValid(g, rmForm)
mi, err := m.MachineFileApp.RemoveFile(fid, rmForm.Path...)
rc.ReqParam = jsonx.Kvs("machine", mi, "path", rmForm.Path)
rc.ReqParam = collx.Kvs("machine", mi, "path", rmForm.Path)
biz.ErrIsNilAppendErr(err, "删除文件失败: %s")
}
@@ -309,7 +308,7 @@ func (m *MachineFile) CopyFile(rc *req.Ctx) {
ginx.BindJsonAndValid(g, cpForm)
mi, err := m.MachineFileApp.Copy(fid, cpForm.ToPath, cpForm.Path...)
biz.ErrIsNilAppendErr(err, "文件拷贝失败: %s")
rc.ReqParam = jsonx.Kvs("machine", mi, "cp", cpForm)
rc.ReqParam = collx.Kvs("machine", mi, "cp", cpForm)
}
func (m *MachineFile) MvFile(rc *req.Ctx) {
@@ -319,7 +318,7 @@ func (m *MachineFile) MvFile(rc *req.Ctx) {
cpForm := new(form.MachineFileOpForm)
ginx.BindJsonAndValid(g, cpForm)
mi, err := m.MachineFileApp.Mv(fid, cpForm.ToPath, cpForm.Path...)
rc.ReqParam = jsonx.Kvs("machine", mi, "mv", cpForm)
rc.ReqParam = collx.Kvs("machine", mi, "mv", cpForm)
biz.ErrIsNilAppendErr(err, "文件移动失败: %s")
}
@@ -330,7 +329,7 @@ func (m *MachineFile) Rename(rc *req.Ctx) {
rename := new(form.MachineFileRename)
ginx.BindJsonAndValid(g, rename)
mi, err := m.MachineFileApp.Rename(fid, rename.Oldname, rename.Newname)
rc.ReqParam = jsonx.Kvs("machine", mi, "rename", rename)
rc.ReqParam = collx.Kvs("machine", mi, "rename", rename)
biz.ErrIsNilAppendErr(err, "文件重命名失败: %s")
}

View File

@@ -9,6 +9,7 @@ import (
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/jsonx"
"mayfly-go/pkg/utils/stringx"
"strconv"
@@ -70,7 +71,7 @@ func (m *MachineScript) RunMachineScript(rc *req.Ctx) {
res, err := cli.Run(script)
// 记录请求参数
rc.ReqParam = jsonx.Kvs("machine", cli.GetMachine(), "scriptId", scriptId, "name", ms.Name)
rc.ReqParam = collx.Kvs("machine", cli.GetMachine(), "scriptId", scriptId, "name", ms.Name)
if res == "" {
biz.ErrIsNilAppendErr(err, "执行命令失败:%s")
}

View File

@@ -10,7 +10,7 @@ import (
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/model"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/jsonx"
"mayfly-go/pkg/utils/collx"
"regexp"
"strconv"
"strings"
@@ -93,7 +93,7 @@ func (m *Mongo) RunCommand(rc *req.Ctx) {
ginx.BindJsonAndValid(rc.GinCtx, commandForm)
inst := m.MongoApp.GetMongoInst(m.GetMongoId(rc.GinCtx))
rc.ReqParam = jsonx.Kvs("mongo", inst.Info, "cmd", commandForm)
rc.ReqParam = collx.Kvs("mongo", inst.Info, "cmd", commandForm)
// 顺序执行
commands := bson.D{}
@@ -156,7 +156,7 @@ func (m *Mongo) UpdateByIdCommand(rc *req.Ctx) {
ginx.BindJsonAndValid(g, commandForm)
inst := m.MongoApp.GetMongoInst(m.GetMongoId(g))
rc.ReqParam = jsonx.Kvs("mongo", inst.Info, "cmd", commandForm)
rc.ReqParam = collx.Kvs("mongo", inst.Info, "cmd", commandForm)
// 解析docId文档id如果为string类型则使用ObjectId解析解析失败则为普通字符串
docId := commandForm.DocId
@@ -180,7 +180,7 @@ func (m *Mongo) DeleteByIdCommand(rc *req.Ctx) {
ginx.BindJsonAndValid(g, commandForm)
inst := m.MongoApp.GetMongoInst(m.GetMongoId(g))
rc.ReqParam = jsonx.Kvs("mongo", inst.Info, "cmd", commandForm)
rc.ReqParam = collx.Kvs("mongo", inst.Info, "cmd", commandForm)
// 解析docId文档id如果为string类型则使用ObjectId解析解析失败则为普通字符串
docId := commandForm.DocId
@@ -203,7 +203,7 @@ func (m *Mongo) InsertOneCommand(rc *req.Ctx) {
ginx.BindJsonAndValid(g, commandForm)
inst := m.MongoApp.GetMongoInst(m.GetMongoId(g))
rc.ReqParam = jsonx.Kvs("mongo", inst.Info, "cmd", commandForm)
rc.ReqParam = collx.Kvs("mongo", inst.Info, "cmd", commandForm)
res, err := inst.Cli.Database(commandForm.Database).Collection(commandForm.Collection).InsertOne(context.TODO(), commandForm.Doc)
biz.ErrIsNilAppendErr(err, "命令执行失败: %s")

View File

@@ -6,7 +6,7 @@ import (
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/jsonx"
"mayfly-go/pkg/utils/collx"
"time"
)
@@ -24,7 +24,7 @@ func (r *Redis) Hscan(rc *req.Ctx) {
keySize, err := cmdable.HLen(contextTodo, key).Result()
biz.ErrIsNilAppendErr(err, "hlen err: %s")
rc.ResData = map[string]any{
rc.ResData = collx.M{
"keys": keys,
"cursor": nextCursor,
"keySize": keySize,
@@ -35,7 +35,7 @@ func (r *Redis) Hdel(rc *req.Ctx) {
ri, key := r.checkKeyAndGetRedisIns(rc)
field := rc.GinCtx.Query("field")
rc.ReqParam = jsonx.Kvs("redis", ri.Info, "key", key, "field", field)
rc.ReqParam = collx.Kvs("redis", ri.Info, "key", key, "field", field)
delRes, err := ri.GetCmdable().HDel(context.TODO(), key, field).Result()
biz.ErrIsNilAppendErr(err, "hdel err: %s")
rc.ResData = delRes
@@ -57,7 +57,7 @@ func (r *Redis) Hset(rc *req.Ctx) {
hv := hashValue.Value[0]
ri := r.getRedisIns(rc)
rc.ReqParam = jsonx.Kvs("redis", ri.Info, "hash", hv)
rc.ReqParam = collx.Kvs("redis", ri.Info, "hash", hv)
res, err := ri.GetCmdable().HSet(context.TODO(), hashValue.Key, hv["field"].(string), hv["value"]).Result()
biz.ErrIsNilAppendErr(err, "hset失败: %s")
@@ -70,7 +70,7 @@ func (r *Redis) SetHashValue(rc *req.Ctx) {
ginx.BindJsonAndValid(g, hashValue)
ri := r.getRedisIns(rc)
rc.ReqParam = jsonx.Kvs("redis", ri.Info, "hash", hashValue)
rc.ReqParam = collx.Kvs("redis", ri.Info, "hash", hashValue)
cmd := ri.GetCmdable()
key := hashValue.Key

View File

@@ -8,7 +8,7 @@ import (
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/jsonx"
"mayfly-go/pkg/utils/collx"
"strings"
"sync"
"time"
@@ -132,7 +132,7 @@ func (r *Redis) TtlKey(rc *req.Ctx) {
func (r *Redis) DeleteKey(rc *req.Ctx) {
ri, key := r.checkKeyAndGetRedisIns(rc)
rc.ReqParam = jsonx.Kvs("redis", ri.Info, "key", key)
rc.ReqParam = collx.Kvs("redis", ri.Info, "key", key)
ri.GetCmdable().Del(context.Background(), key)
}
@@ -141,7 +141,7 @@ func (r *Redis) RenameKey(rc *req.Ctx) {
ginx.BindJsonAndValid(rc.GinCtx, form)
ri := r.getRedisIns(rc)
rc.ReqParam = jsonx.Kvs("redis", ri.Info, "rename", form)
rc.ReqParam = collx.Kvs("redis", ri.Info, "rename", form)
ri.GetCmdable().Rename(context.Background(), form.Key, form.NewKey)
}
@@ -150,14 +150,14 @@ func (r *Redis) ExpireKey(rc *req.Ctx) {
ginx.BindJsonAndValid(rc.GinCtx, form)
ri := r.getRedisIns(rc)
rc.ReqParam = jsonx.Kvs("redis", ri.Info, "expire", form)
rc.ReqParam = collx.Kvs("redis", ri.Info, "expire", form)
ri.GetCmdable().Expire(context.Background(), form.Key, time.Duration(form.Seconds)*time.Second)
}
// 移除过期时间
func (r *Redis) PersistKey(rc *req.Ctx) {
ri, key := r.checkKeyAndGetRedisIns(rc)
rc.ReqParam = jsonx.Kvs("redis", ri.Info, "key", key)
rc.ReqParam = collx.Kvs("redis", ri.Info, "key", key)
ri.GetCmdable().Persist(context.Background(), key)
}

View File

@@ -6,6 +6,7 @@ import (
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
)
func (r *Redis) GetListValue(rc *req.Ctx) {
@@ -22,7 +23,7 @@ func (r *Redis) GetListValue(rc *req.Ctx) {
res, err := cmdable.LRange(ctx, key, int64(start), int64(stop)).Result()
biz.ErrIsNilAppendErr(err, "获取list值失败: %s")
rc.ResData = map[string]any{
rc.ResData = collx.M{
"len": len,
"list": res,
}

View File

@@ -11,6 +11,7 @@ import (
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/model"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/cryptox"
"mayfly-go/pkg/utils/stringx"
"strconv"
@@ -196,7 +197,7 @@ func (r *Redis) ClusterInfo(rc *req.Ctx) {
}
nodesRes = append(nodesRes, node)
}
rc.ResData = map[string]any{
rc.ResData = collx.M{
"clusterInfo": info,
"clusterNodes": nodesRes,
}

View File

@@ -6,6 +6,7 @@ import (
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
"time"
)
@@ -49,7 +50,7 @@ func (r *Redis) Sscan(rc *req.Ctx) {
cmd := r.getRedisIns(rc).GetCmdable()
keys, cursor, err := cmd.SScan(context.TODO(), scan.Key, scan.Cursor, scan.Match, scan.Count).Result()
biz.ErrIsNilAppendErr(err, "sscan失败: %s")
rc.ResData = map[string]any{
rc.ResData = collx.M{
"keys": keys,
"cursor": cursor,
}

View File

@@ -6,7 +6,7 @@ import (
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/jsonx"
"mayfly-go/pkg/utils/collx"
"time"
)
@@ -24,7 +24,7 @@ func (r *Redis) SetStringValue(rc *req.Ctx) {
ri := r.getRedisIns(rc)
cmd := ri.GetCmdable()
rc.ReqParam = jsonx.Kvs("redis", ri.Info, "string", keyValue)
rc.ReqParam = collx.Kvs("redis", ri.Info, "string", keyValue)
str, err := cmd.Set(context.TODO(), keyValue.Key, keyValue.Value, time.Second*time.Duration(keyValue.Timed)).Result()
biz.ErrIsNilAppendErr(err, "保存字符串值失败: %s")

View File

@@ -6,6 +6,7 @@ import (
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
"github.com/redis/go-redis/v9"
)
@@ -28,7 +29,7 @@ func (r *Redis) ZScan(rc *req.Ctx) {
keys, cursor, err := ri.GetCmdable().ZScan(context.TODO(), key, cursor, match, int64(count)).Result()
biz.ErrIsNilAppendErr(err, "sscan失败: %s")
rc.ResData = map[string]any{
rc.ResData = collx.M{
"keys": keys,
"cursor": cursor,
}

View File

@@ -53,7 +53,7 @@ func (a *Account) GetPermissions(rc *req.Ctx) {
}
// 保存该账号的权限codes
req.SavePermissionCodes(account.Id, permissions)
rc.ResData = map[string]any{
rc.ResData = collx.M{
"menus": menus.ToTrees(0),
"permissions": permissions,
}

View File

@@ -3,9 +3,10 @@ package api
import (
"mayfly-go/pkg/captcha"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
)
func GenerateCaptcha(rc *req.Ctx) {
id, image := captcha.Generate()
rc.ResData = map[string]any{"base64Captcha": image, "cid": id}
rc.ResData = collx.M{"base64Captcha": image, "cid": id}
}

View File

@@ -7,6 +7,8 @@ import (
"fmt"
"io"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/utils/anyx"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/stringx"
"mime/multipart"
"net/http"
@@ -56,7 +58,7 @@ func (r *RequestWrapper) Timeout(timeout int) *RequestWrapper {
return r
}
func (r *RequestWrapper) GetByQuery(queryMap map[string]any) *ResponseWrapper {
func (r *RequestWrapper) GetByQuery(queryMap collx.M) *ResponseWrapper {
var params string
for k, v := range queryMap {
if params != "" {
@@ -104,7 +106,7 @@ func (r *RequestWrapper) PostForm(params string) *ResponseWrapper {
return sendRequest(r)
}
func (r *RequestWrapper) PostMulipart(files []MultipartFile, reqParams map[string]string) *ResponseWrapper {
func (r *RequestWrapper) PostMulipart(files []MultipartFile, reqParams collx.M) *ResponseWrapper {
buf := &bytes.Buffer{}
// 文件写入 buf
writer := multipart.NewWriter(buf)
@@ -130,7 +132,7 @@ func (r *RequestWrapper) PostMulipart(files []MultipartFile, reqParams map[strin
}
// 如果有其他参数则写入body
for k, v := range reqParams {
if err := writer.WriteField(k, v); err != nil {
if err := writer.WriteField(k, anyx.ConvString(v)); err != nil {
return &ResponseWrapper{err: err}
}
}

View File

@@ -2,6 +2,7 @@ package httpclient
import (
"fmt"
"mayfly-go/pkg/utils/collx"
"testing"
)
@@ -26,7 +27,7 @@ func TestGetBodyToMap(t *testing.T) {
func TestGetQueryBodyToMap(t *testing.T) {
res, err := NewRequest("http://go.mayfly.run/api/syslogs").
Header("Authorization", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2OTUzOTQ5NTIsImlkIjoxLCJ1c2VybmFtZSI6ImFkbWluIn0.pGrczVZqk5nlId-FZPkjW_O5Sw3-2yjgzACp_j4JEXY").
GetByQuery(map[string]any{"pageNum": 1, "pageSize": 10}).
GetByQuery(collx.M{"pageNum": 1, "pageSize": 10}).
BodyToMap()
fmt.Println(err)
fmt.Println(res["msg"])

View File

@@ -1,11 +1,11 @@
package sqlparser
import (
"github.com/kanzihuang/vitess/go/vt/sqlparser"
"github.com/stretchr/testify/require"
sqlparser_xwb1989 "github.com/xwb1989/sqlparser"
"strings"
"testing"
"github.com/kanzihuang/vitess/go/vt/sqlparser"
"github.com/stretchr/testify/require"
)
func Test_ParseNext_WithCurrentDate(t *testing.T) {
@@ -45,22 +45,22 @@ func Test_ParseNext_WithCurrentDate(t *testing.T) {
require.Equal(t, test.want, sqlparser.String(tree))
})
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
token := sqlparser_xwb1989.NewTokenizer(strings.NewReader(test.input))
tree, err := sqlparser_xwb1989.ParseNext(token)
if len(test.err) > 0 {
require.Error(t, err)
require.Contains(t, err.Error(), test.err)
return
}
require.NoError(t, err)
if len(test.want) == 0 {
test.want = test.input
}
require.Equal(t, test.wantXwb1989, sqlparser_xwb1989.String(tree))
})
}
// for _, test := range tests {
// t.Run(test.name, func(t *testing.T) {
// token := sqlparser_xwb1989.NewTokenizer(strings.NewReader(test.input))
// tree, err := sqlparser_xwb1989.ParseNext(token)
// if len(test.err) > 0 {
// require.Error(t, err)
// require.Contains(t, err.Error(), test.err)
// return
// }
// require.NoError(t, err)
// if len(test.want) == 0 {
// test.want = test.input
// }
// require.Equal(t, test.wantXwb1989, sqlparser_xwb1989.String(tree))
// })
// }
}
func Test_SplitSqls(t *testing.T) {

View File

@@ -1,44 +1,24 @@
package collx
import (
"reflect"
"strconv"
)
import "mayfly-go/pkg/utils/stringx"
func GetString4Map(m map[string]any, key string) string {
return m[key].(string)
}
// M is a shortcut for map[string]any
type M map[string]any
func GetInt4Map(m map[string]any, key string) int {
i := m[key]
iKind := reflect.TypeOf(i).Kind()
if iKind == reflect.Int {
return i.(int)
// 将偶数个元素转为对应的M (map[string]any)
//
// 偶数索引为key奇数为value
func Kvs(elements ...any) M {
myMap := make(map[string]any)
for i := 0; i < len(elements); i += 2 {
key := stringx.AnyToStr(elements[i])
if i+1 < len(elements) {
value := elements[i+1]
myMap[key] = value
} else {
myMap[key] = nil
}
}
if iKind == reflect.String {
i, _ := strconv.Atoi(i.(string))
return i
}
return 0
}
// map构造器
type mapBuilder struct {
m map[string]any
}
func MapBuilder(key string, value any) *mapBuilder {
mb := new(mapBuilder)
mb.m = make(map[string]any, 4)
mb.m[key] = value
return mb
}
func (mb *mapBuilder) Put(key string, value any) *mapBuilder {
mb.m[key] = value
return mb
}
func (mb *mapBuilder) ToMap() map[string]any {
return mb.m
return myMap
}

View File

@@ -3,7 +3,6 @@ package jsonx
import (
"encoding/json"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/utils/stringx"
"strings"
"github.com/buger/jsonparser"
@@ -34,31 +33,6 @@ func ToStr(val any) string {
}
}
// 将偶数个元素转为对应的map并转为json
//
// 偶数索引为key奇数为value
func AnysToStr(elements ...any) string {
return ToStr(Kvs(elements...))
}
// 将偶数个元素转为对应的map[string]any
//
// 偶数索引为json key奇数为value
func Kvs(elements ...any) map[string]any {
myMap := make(map[string]any)
for i := 0; i < len(elements); i += 2 {
key := stringx.AnyToStr(elements[i])
if i+1 < len(elements) {
value := elements[i+1]
myMap[key] = value
} else {
myMap[key] = nil
}
}
return myMap
}
// 根据json字节数组获取对应字段路径的string类型值
//
// @param fieldPath字段路径。如user.username等

View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"mayfly-go/pkg/utils/collx"
"reflect"
"strconv"
"strings"
@@ -324,7 +325,7 @@ func getInnerStructMaps(m map[string]any) map[string]map[string]any {
prefix := k[0:lastIndex]
m2 := key2map[prefix]
if m2 == nil {
key2map[prefix] = map[string]any{k[lastIndex+1:]: v}
key2map[prefix] = collx.M{k[lastIndex+1:]: v}
} else {
m2[k[lastIndex+1:]] = v
}
@@ -634,25 +635,3 @@ func Case2Camel(name string) string {
name = strings.Title(name)
return strings.Replace(name, " ", "", -1)
}
func IsBlank(value any) bool {
if value == nil {
return true
}
rValue := reflect.ValueOf(value)
switch rValue.Kind() {
case reflect.String:
return rValue.Len() == 0
case reflect.Bool:
return !rValue.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return rValue.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return rValue.Uint() == 0
case reflect.Float32, reflect.Float64:
return rValue.Float() == 0
case reflect.Interface, reflect.Ptr:
return rValue.IsNil()
}
return reflect.DeepEqual(rValue.Interface(), reflect.Zero(rValue.Type()).Interface())
}

View File

@@ -2,6 +2,7 @@ package structx
import (
"fmt"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/stringx"
"reflect"
"strings"
@@ -160,7 +161,7 @@ func getPrefixKeyMap(m map[string]any) map[string]map[string]any {
prefix := k[0:lastIndex]
m2 := key2map[prefix]
if m2 == nil {
key2map[prefix] = map[string]any{k[lastIndex+1:]: v}
key2map[prefix] = collx.M{k[lastIndex+1:]: v}
} else {
m2[k[lastIndex+1:]] = v
}