Basic check for User controller

pull/247/head
HFO4 5 years ago
parent 74b5bf24a8
commit 6539ae20fa

@ -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"
}

@ -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
###

@ -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
)

@ -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)
}

@ -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))
}
}

@ -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

@ -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
}

@ -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"`
}
Loading…
Cancel
Save