diff --git a/main.go b/main.go index 892795f..479338a 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "message-nest/pkg/table" + "message-nest/pkg/util" "message-nest/service/cron_service" "message-nest/service/env_service" "net/http" @@ -12,7 +13,6 @@ import ( "message-nest/models" "message-nest/pkg/logging" "message-nest/pkg/setting" - "message-nest/pkg/util" "message-nest/routers" ) diff --git a/models/auth.go b/models/auth.go index 1d65a6f..623aab1 100644 --- a/models/auth.go +++ b/models/auth.go @@ -8,7 +8,7 @@ type Auth struct { Password string `json:"password"` } -// CheckAuth checks if authentication information exists +// CheckAuth 检查用户信息 func CheckAuth(username, password string) (bool, error) { var auth Auth err := db.Select("id").Where(Auth{Username: username, Password: password}).First(&auth).Error diff --git a/pkg/app/form.go b/pkg/app/form.go index f8d4d86..cb6b564 100644 --- a/pkg/app/form.go +++ b/pkg/app/form.go @@ -2,35 +2,13 @@ package app import ( "fmt" - "github.com/astaxie/beego/validation" "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" - "message-nest/pkg/e" "message-nest/pkg/util" "net/http" "strings" ) -// BindAndValid binds and validates data -func BindAndValid(c *gin.Context, form interface{}) (int, int) { - err := c.Bind(form) - if err != nil { - return http.StatusBadRequest, e.INVALID_PARAMS - } - - valid := validation.Validation{} - check, err := valid.Valid(form) - if err != nil { - return http.StatusInternalServerError, e.ERROR - } - if !check { - MarkErrors(valid.Errors) - return http.StatusBadRequest, e.INVALID_PARAMS - } - - return http.StatusOK, e.SUCCESS -} - func CommonPlaygroundValid(obj interface{}) (int, string) { if err := util.CustomerValidate.Struct(obj); err != nil { errs := err.(validator.ValidationErrors) diff --git a/pkg/app/request.go b/pkg/app/request.go index dfbea40..c0ddfdd 100644 --- a/pkg/app/request.go +++ b/pkg/app/request.go @@ -2,20 +2,9 @@ package app import ( "fmt" - "github.com/astaxie/beego/validation" "github.com/gin-gonic/gin" - - "message-nest/pkg/logging" ) -// MarkErrors logs error logs -func MarkErrors(errors []*validation.Error) { - for _, err := range errors { - logging.Logger.Error(err.Key, err.Message) - } - return -} - // 从请求中获取当前用户 func GetCurrentUserName(c *gin.Context) string { userName, ok := c.Get("currentUserName") diff --git a/pkg/app/response.go b/pkg/app/response.go index 3978e46..deb5ba6 100644 --- a/pkg/app/response.go +++ b/pkg/app/response.go @@ -3,8 +3,6 @@ package app import ( "github.com/gin-gonic/gin" "net/http" - - "message-nest/pkg/e" ) type Gin struct { @@ -17,16 +15,6 @@ type Response struct { Data interface{} `json:"data"` } -// Response setting gin.JSON -func (g *Gin) Response(httpCode, errCode int, data interface{}) { - g.C.JSON(httpCode, Response{ - Code: errCode, - Msg: e.GetMsg(errCode), - Data: data, - }) - return -} - func (g *Gin) CResponse(errCode int, Msg string, data interface{}) { g.C.JSON(http.StatusOK, Response{ Code: errCode, diff --git a/pkg/constant/constant.go b/pkg/constant/constant.go index b2c2fa2..abd7a8f 100644 --- a/pkg/constant/constant.go +++ b/pkg/constant/constant.go @@ -1,11 +1,11 @@ package constant -var CleanLogsTaskId = "00000000-0000-0000-0000-000000000001" -var SiteSettingSectionName = "site_config" +const CleanLogsTaskId = "00000000-0000-0000-0000-000000000001" +const SiteSettingSectionName = "site_config" -//var SiteSettingTitleKeyName = "title" -//var SiteSettingSloganKeyName = "slogan" -//var SiteSettingLogoKeyName = "logo" +//const SiteSettingTitleKeyName = "title" +//const SiteSettingSloganKeyName = "slogan" +//const SiteSettingLogoKeyName = "logo" // 站点信息默认值 var SiteSiteDefaultValueMap = map[string]string{ @@ -15,9 +15,9 @@ var SiteSiteDefaultValueMap = map[string]string{ } // 日志清理自定义 -var LogsCleanSectionName = "log_config" -var LogsCleanCronKeyName = "cron" -var LogsCleanKeepKeyName = "keep_num" +const LogsCleanSectionName = "log_config" +const LogsCleanCronKeyName = "cron" +const LogsCleanKeepKeyName = "keep_num" // 日志清理默认值 var LogsCleanDefaultValueMap = map[string]string{ @@ -25,4 +25,4 @@ var LogsCleanDefaultValueMap = map[string]string{ "keep_num": "1000", } -var AboutSectionName = "about" +const AboutSectionName = "about" diff --git a/pkg/constant/release_log.go b/pkg/constant/release_log.go index 6c1566c..817777e 100644 --- a/pkg/constant/release_log.go +++ b/pkg/constant/release_log.go @@ -2,8 +2,6 @@ package constant var LatestVersion = map[string]string{} -//var info map[string]string - var V1Version = "v1.0.0" var V1VersionDesc = `1. 支持邮件消息发送 2. 支持日志定时删除 diff --git a/pkg/e/code.go b/pkg/e/code.go index 97da84f..5219ae2 100644 --- a/pkg/e/code.go +++ b/pkg/e/code.go @@ -5,34 +5,9 @@ const ( ERROR = 500 INVALID_PARAMS = 400 - ERROR_EXIST_TAG = 10001 - ERROR_EXIST_TAG_FAIL = 10002 - ERROR_NOT_EXIST_TAG = 10003 - ERROR_GET_TAGS_FAIL = 10004 - ERROR_COUNT_TAG_FAIL = 10005 - ERROR_ADD_TAG_FAIL = 10006 - ERROR_EDIT_TAG_FAIL = 10007 - ERROR_DELETE_TAG_FAIL = 10008 - ERROR_EXPORT_TAG_FAIL = 10009 - ERROR_IMPORT_TAG_FAIL = 10010 - - ERROR_NOT_EXIST_ARTICLE = 10011 - ERROR_CHECK_EXIST_ARTICLE_FAIL = 10012 - ERROR_ADD_ARTICLE_FAIL = 10013 - ERROR_DELETE_ARTICLE_FAIL = 10014 - ERROR_EDIT_ARTICLE_FAIL = 10015 - ERROR_COUNT_ARTICLE_FAIL = 10016 - ERROR_GET_ARTICLES_FAIL = 10017 - ERROR_GET_ARTICLE_FAIL = 10018 - ERROR_GEN_ARTICLE_POSTER_FAIL = 10019 - ERROR_AUTH_CHECK_TOKEN_FAIL = 20001 ERROR_AUTH_CHECK_TOKEN_TIMEOUT = 20002 ERROR_AUTH_TOKEN = 20003 ERROR_AUTH = 20004 ERROR_AUTH_NO_TOKEN = 20005 - - ERROR_UPLOAD_SAVE_IMAGE_FAIL = 30001 - ERROR_UPLOAD_CHECK_IMAGE_FAIL = 30002 - ERROR_UPLOAD_CHECK_IMAGE_FORMAT = 30003 ) diff --git a/pkg/e/msg.go b/pkg/e/msg.go index 7907e50..1a1ec82 100644 --- a/pkg/e/msg.go +++ b/pkg/e/msg.go @@ -1,36 +1,14 @@ package e var MsgFlags = map[int]string{ - SUCCESS: "ok", - ERROR: "fail", - INVALID_PARAMS: "请求参数错误", - ERROR_EXIST_TAG: "已存在该标签名称", - ERROR_EXIST_TAG_FAIL: "获取已存在标签失败", - ERROR_NOT_EXIST_TAG: "该标签不存在", - ERROR_GET_TAGS_FAIL: "获取所有标签失败", - ERROR_COUNT_TAG_FAIL: "统计标签失败", - ERROR_ADD_TAG_FAIL: "新增标签失败", - ERROR_EDIT_TAG_FAIL: "修改标签失败", - ERROR_DELETE_TAG_FAIL: "删除标签失败", - ERROR_EXPORT_TAG_FAIL: "导出标签失败", - ERROR_IMPORT_TAG_FAIL: "导入标签失败", - ERROR_NOT_EXIST_ARTICLE: "该文章不存在", - ERROR_ADD_ARTICLE_FAIL: "新增文章失败", - ERROR_DELETE_ARTICLE_FAIL: "删除文章失败", - ERROR_CHECK_EXIST_ARTICLE_FAIL: "检查文章是否存在失败", - ERROR_EDIT_ARTICLE_FAIL: "修改文章失败", - ERROR_COUNT_ARTICLE_FAIL: "统计文章失败", - ERROR_GET_ARTICLES_FAIL: "获取多个文章失败", - ERROR_GET_ARTICLE_FAIL: "获取单个文章失败", - ERROR_GEN_ARTICLE_POSTER_FAIL: "生成文章海报失败", - ERROR_AUTH_CHECK_TOKEN_FAIL: "Token鉴权失败", - ERROR_AUTH_CHECK_TOKEN_TIMEOUT: "Token已超时", - ERROR_AUTH_TOKEN: "Token生成失败", - ERROR_AUTH_NO_TOKEN: "Token缺失", - ERROR_AUTH: "Token错误", - ERROR_UPLOAD_SAVE_IMAGE_FAIL: "保存图片失败", - ERROR_UPLOAD_CHECK_IMAGE_FAIL: "检查图片失败", - ERROR_UPLOAD_CHECK_IMAGE_FORMAT: "校验图片错误,图片格式或大小有问题", + SUCCESS: "ok", + ERROR: "fail", + INVALID_PARAMS: "请求参数错误", + ERROR_AUTH_CHECK_TOKEN_FAIL: "Token鉴权失败", + ERROR_AUTH_CHECK_TOKEN_TIMEOUT: "Token已超时", + ERROR_AUTH_TOKEN: "Token生成失败", + ERROR_AUTH_NO_TOKEN: "Token缺失", + ERROR_AUTH: "Token错误", } // GetMsg get error information based on Code diff --git a/pkg/setting/setting.go b/pkg/setting/setting.go index e4708db..8f3f1c1 100644 --- a/pkg/setting/setting.go +++ b/pkg/setting/setting.go @@ -9,26 +9,9 @@ import ( type App struct { JwtSecret string - PageSize int PrefixUrl string RuntimeRootPath string - - ImageSavePath string - ImageMaxSize int - ImageAllowExts []string - - ExportSavePath string - QrCodeSavePath string - FontSavePath string - - LogSavePath string - LogSaveName string - LogFileExt string - TimeFormat string - - //LogKeepNum int - //CleanTaskLogID string } var AppSetting = &App{} @@ -53,16 +36,6 @@ type Database struct { var DatabaseSetting = &Database{} -type Redis struct { - Host string - Password string - MaxIdle int - MaxActive int - IdleTimeout time.Duration -} - -var RedisSetting = &Redis{} - var cfg *ini.File // Setup initialize the configuration instance @@ -76,16 +49,9 @@ func Setup() { mapTo("app", AppSetting) mapTo("server", ServerSetting) mapTo("database", DatabaseSetting) - mapTo("redis", RedisSetting) - AppSetting.ImageMaxSize = AppSetting.ImageMaxSize * 1024 * 1024 ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second ServerSetting.WriteTimeout = ServerSetting.WriteTimeout * time.Second - RedisSetting.IdleTimeout = RedisSetting.IdleTimeout * time.Second - - //// 默认值 - //AppSetting.LogKeepNum = 1000 - //AppSetting.CleanTaskLogID = "00000000-0000-0000-0000-000000000001" } // mapTo map section diff --git a/pkg/util/pagination.go b/pkg/util/pagination.go index ea4d599..79f2993 100644 --- a/pkg/util/pagination.go +++ b/pkg/util/pagination.go @@ -3,21 +3,8 @@ package util import ( "github.com/gin-gonic/gin" "github.com/unknwon/com" - - "message-nest/pkg/setting" ) -// GetPage get page parameters -func GetPage(c *gin.Context) int { - result := 0 - page := com.StrTo(c.Query("page")).MustInt() - if page > 0 { - result = (page - 1) * setting.AppSetting.PageSize - } - - return result -} - func GetPageSize(c *gin.Context) (int, int) { result := 0 page := com.StrTo(c.Query("page")).MustInt() diff --git a/routers/api/auth.go b/routers/api/auth.go index 1b9885c..f5c8ccb 100644 --- a/routers/api/auth.go +++ b/routers/api/auth.go @@ -1,9 +1,9 @@ package api import ( + "fmt" "net/http" - "github.com/astaxie/beego/validation" "github.com/gin-gonic/gin" "message-nest/pkg/app" @@ -18,52 +18,40 @@ type auth struct { } type ReqAuth struct { - Username string `json:"username"` - Password string `json:"passwd"` + Username string `json:"username" validate:"required,max=36" label:"用户名"` + Password string `json:"passwd" validate:"required,max=36" label:"密码"` } func GetAuth(c *gin.Context) { - appG := app.Gin{C: c} - valid := validation.Validation{} - - var user ReqAuth - err := c.ShouldBindJSON(&user) - if err != nil { - app.MarkErrors(valid.Errors) - appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil) - return - } - username := user.Username - password := user.Password - - a := auth{Username: username, Password: password} - ok, _ := valid.Valid(&a) - - if !ok { - app.MarkErrors(valid.Errors) - appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil) + var ( + appG = app.Gin{C: c} + req ReqAuth + ) + add + errCode, errMsg := app.BindJsonAndPlayValid(c, &req) + if errCode != e.SUCCESS { + appG.CResponse(errCode, errMsg, nil) return } - authService := auth_service.Auth{Username: username, Password: password} + authService := auth_service.Auth{Username: req.Username, Password: req.Password} isExist, err := authService.Check() if err != nil { - appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, nil) + appG.CResponse(http.StatusInternalServerError, fmt.Sprintf("校验失败:%s", err), nil) return } - if !isExist { - appG.Response(http.StatusUnauthorized, e.ERROR_AUTH, nil) + appG.CResponse(http.StatusUnauthorized, "账号或密码不正确!", nil) return } - token, err := util.GenerateToken(username, password) + token, err := util.GenerateToken(req.Username, req.Password) if err != nil { - appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_TOKEN, nil) + appG.CResponse(http.StatusInternalServerError, fmt.Sprintf("生成token失败:%s", err), nil) return } - appG.Response(http.StatusOK, e.SUCCESS, map[string]string{ + appG.CResponse(http.StatusOK, "登录成功!", map[string]string{ "token": token, }) } diff --git a/web/src/api/api.js b/web/src/api/api.js index 0babc02..11aa81f 100644 --- a/web/src/api/api.js +++ b/web/src/api/api.js @@ -53,7 +53,10 @@ request.interceptors.response.use( if (error.response && error.response.status === 401) { // 未授权,可能是登录状态过期,执行登出操作 logout(); + } else if (20000 <= error.response.status <= 29999) { + logout(); } + handleException(error); // return Promise.reject(error); } @@ -82,8 +85,9 @@ const handleException = (error) => { // 登出系统 const logout = () => { - // 执行登出逻辑,清除本地存储的用户信息等 - console.log('User logged out'); + const pageState = usePageState(); + pageState.setIsLogin(true); + localStorage.setItem(CONSTANT.STORE_TOKEN_NAME, ""); }; export { request, handleException, logout }; diff --git a/web/src/views/home/login.vue b/web/src/views/home/login.vue index fdf3e37..91b93e8 100644 --- a/web/src/views/home/login.vue +++ b/web/src/views/home/login.vue @@ -42,8 +42,8 @@ export default { const pageState = usePageState(); const state = reactive({ - account: 'admin', - passwd: '123456', + account: '', + passwd: '', }); onMounted(() => {