diff --git a/.idea/httpRequests/2019-11-05T074354.200.json b/.idea/httpRequests/2019-11-05T074354.200.json new file mode 100644 index 0000000..8fe2cc3 --- /dev/null +++ b/.idea/httpRequests/2019-11-05T074354.200.json @@ -0,0 +1,5 @@ +{ + "code": 40001, + "msg": "UserName required", + "error": "Key: 'UserLoginService.UserName' Error:Field validation for 'UserName' failed on the 'required' tag\nKey: 'UserLoginService.Password' Error:Field validation for 'Password' failed on the 'required' tag" +} \ No newline at end of file diff --git a/.idea/httpRequests/http-requests-log.http b/.idea/httpRequests/http-requests-log.http new file mode 100644 index 0000000..2568f76 --- /dev/null +++ b/.idea/httpRequests/http-requests-log.http @@ -0,0 +1,18 @@ +POST http://127.0.0.1:5000/Api/V3/User/Login +Accept: */* +Cache-Control: no-cache + +{} + +<> 2019-11-05T074354.200.json + +### + +POST http://127.0.0.1:5000/Api/V3/User/Login +Accept: */* +Cache-Control: no-cache + +<> 2019-11-05T074329.200.json + +### + diff --git a/go.mod b/go.mod index ba40eaf..993e688 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module Cloudreve go 1.12 -require github.com/gin-gonic/gin v1.4.0 +require ( + github.com/gin-gonic/gin v1.4.0 + gopkg.in/go-playground/validator.v8 v8.18.2 +) diff --git a/routers/controllers/main.go b/routers/controllers/main.go new file mode 100644 index 0000000..7e5a276 --- /dev/null +++ b/routers/controllers/main.go @@ -0,0 +1,25 @@ +package controllers + +import ( + "Cloudreve/serializer" + "encoding/json" + "fmt" + "gopkg.in/go-playground/validator.v8" +) + +// ErrorResponse 返回错误消息 +func ErrorResponse(err error) serializer.Response { + if ve, ok := err.(validator.ValidationErrors); ok { + for _, e := range ve { + return serializer.ParamErr( + fmt.Sprintf("%s %s", e.Field, e.Tag), + err, + ) + } + } + if _, ok := err.(*json.UnmarshalTypeError); ok { + return serializer.ParamErr("JSON类型不匹配", err) + } + + return serializer.ParamErr("参数错误", err) +} diff --git a/routers/controllers/user.go b/routers/controllers/user.go new file mode 100644 index 0000000..bb61c49 --- /dev/null +++ b/routers/controllers/user.go @@ -0,0 +1,22 @@ +package controllers + +import ( + "Cloudreve/serializer" + "Cloudreve/service/user" + "github.com/gin-gonic/gin" +) + +// UserLogin 用户登录 +func UserLogin(c *gin.Context) { + var service service.UserLoginService + if err := c.ShouldBindJSON(&service); err == nil { + //res := service.Login(c) + c.JSON(200, serializer.Response{ + Code: 0, + Msg: "OK", + }) + } else { + c.JSON(200, ErrorResponse(err)) + } + +} diff --git a/routers/router.go b/routers/router.go index fec749e..a8332b6 100644 --- a/routers/router.go +++ b/routers/router.go @@ -8,10 +8,13 @@ import ( func InitRouter() *gin.Engine { r := gin.Default() - // 路由 + // 顶层路由分组 v3 := r.Group("/Api/V3") { + // 测试用路由 v3.GET("Ping", controllers.Ping) + // 用户登录 + v3.POST("User/Login", controllers.UserLogin) } return r diff --git a/serializer/common.go b/serializer/common.go index b4931ec..6656100 100644 --- a/serializer/common.go +++ b/serializer/common.go @@ -1,5 +1,7 @@ package serializer +import "github.com/gin-gonic/gin" + // Response 基础序列化器 type Response struct { Code int `json:"code"` @@ -7,3 +9,49 @@ type Response struct { Msg string `json:"msg"` Error string `json:"error,omitempty"` } + +// 三位数错误编码为复用http原本含义 +// 五位数错误编码为应用自定义错误 +// 五开头的五位数错误编码为服务器端错误,比如数据库操作失败 +// 四开头的五位数错误编码为客户端错误,有时候是客户端代码写错了,有时候是用户操作错误 +const ( + // CodeCheckLogin 未登录 + CodeCheckLogin = 401 + // CodeNoRightErr 未授权访问 + CodeNoRightErr = 403 + // CodeDBError 数据库操作失败 + CodeDBError = 50001 + // CodeEncryptError 加密失败 + CodeEncryptError = 50002 + //CodeParamErr 各种奇奇怪怪的参数错误 + CodeParamErr = 40001 +) + +// DBErr 数据库操作失败 +func DBErr(msg string, err error) Response { + if msg == "" { + msg = "数据库操作失败" + } + return Err(CodeDBError, msg, err) +} + +// ParamErr 各种参数错误 +func ParamErr(msg string, err error) Response { + if msg == "" { + msg = "参数错误" + } + return Err(CodeParamErr, msg, err) +} + +// Err 通用错误处理 +func Err(errCode int, msg string, err error) Response { + res := Response{ + Code: errCode, + Msg: msg, + } + // 生产环境隐藏底层报错 + if err != nil && gin.Mode() != gin.ReleaseMode { + res.Error = err.Error() + } + return res +} diff --git a/service/user/user_login.go b/service/user/user_login.go new file mode 100644 index 0000000..4e74324 --- /dev/null +++ b/service/user/user_login.go @@ -0,0 +1,7 @@ +package service + +// UserLoginService 管理用户登录的服务 +type UserLoginService struct { + UserName string `form:"user_name" json:"user_name" binding:"required,min=5,max=30"` + Password string `form:"password" json:"password" binding:"required,min=8,max=40"` +}