大面积重构
Response部分 usersController部分 中间件部分
This commit is contained in:
		| @@ -3,7 +3,6 @@ package common | ||||
| import ( | ||||
| 	"drive-linked/config" | ||||
| 	"github.com/golang-jwt/jwt/v4" | ||||
| 	"strings" | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| @@ -13,16 +12,8 @@ type JwtClaims struct { | ||||
| } | ||||
|  | ||||
| //TODO:token解密验证 | ||||
| func ValidateLogin(authorization string) error { | ||||
| 	// 取出Bearer后的内容 | ||||
| 	var tokenString string | ||||
| 	if auths := strings.Split(authorization, " "); len(auths) > 1 { | ||||
| 		tokenString = auths[1] | ||||
| 	} else { | ||||
| 		return jwt.ErrInvalidKey | ||||
| 	} | ||||
|  | ||||
| 	token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { | ||||
| func VerifyToken(authorization string) error { | ||||
| 	token, err := jwt.Parse(authorization, func(token *jwt.Token) (interface{}, error) { | ||||
| 		return []byte(config.Cfg.Security.Jwt.Secret), nil | ||||
| 	}) | ||||
|  | ||||
|   | ||||
| @@ -19,7 +19,7 @@ func TestValidateLogin(t *testing.T) { | ||||
| 	config.SetupConfig() | ||||
|  | ||||
| 	tokenString, _ := NewToken("eigeen") | ||||
| 	err := ValidateLogin(tokenString) | ||||
| 	err := VerifyToken(tokenString) | ||||
| 	if err == nil { | ||||
| 		t.Log("验证通过") | ||||
| 	} else { | ||||
|   | ||||
| @@ -1,38 +0,0 @@ | ||||
| package controller | ||||
|  | ||||
| import ( | ||||
| 	"drive-linked/pkg/dto" | ||||
| 	"drive-linked/pkg/service" | ||||
| 	"encoding/json" | ||||
| 	"github.com/kataras/iris/v12" | ||||
| 	"io/ioutil" | ||||
| 	"net/http" | ||||
| ) | ||||
|  | ||||
| func UserProfile(ctx iris.Context) { | ||||
| 	serv := service.NewUsersService(ctx) | ||||
|  | ||||
| 	switch ctx.Request().Method { | ||||
| 	// GET | ||||
| 	case http.MethodGet: | ||||
| 		serv.GetOneUser(ctx.Params().GetString("name"), service.MethodUserName) | ||||
| 		return | ||||
| 	// POST | ||||
| 	case http.MethodPost: | ||||
| 		//TODO:错误处理 | ||||
| 		body, err := ioutil.ReadAll(ctx.Request().Body) | ||||
| 		if err != nil { | ||||
| 			return | ||||
| 		} | ||||
|  | ||||
| 		var req dto.QueryUserParams | ||||
| 		err = json.Unmarshal(body, &req) | ||||
| 		if err != nil { | ||||
| 			return | ||||
| 		} | ||||
|  | ||||
| 		serv.GetOneUser(req.Value, req.Method) | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| } | ||||
							
								
								
									
										37
									
								
								pkg/dao/sentences.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								pkg/dao/sentences.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| package dao | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"strings" | ||||
| ) | ||||
|  | ||||
| type Conditions *map[string]interface{} | ||||
|  | ||||
| //TODO:未完成的sql语句构建器 | ||||
| type QuerySql struct { | ||||
| 	Table      string | ||||
| 	Tail       string | ||||
| 	Conditions Conditions | ||||
| 	Rebind     int | ||||
| 	body       string | ||||
| } | ||||
|  | ||||
| func (b *QuerySql) Build() { | ||||
|  | ||||
| 	if b.Conditions == nil { | ||||
|  | ||||
| 	} | ||||
| 	var where []string | ||||
| 	var values []interface{} | ||||
|  | ||||
| 	for k, v := range *b.Conditions { | ||||
| 		values = append(values, v) | ||||
| 		where = append(where, fmt.Sprintf(`"%s"=%s`, k, "?")) | ||||
| 	} | ||||
|  | ||||
| 	b.body = "SELECT * FROM " + b.Table + " WHERE " + strings.Join(where, "AND") | ||||
| } | ||||
|  | ||||
| func (b *QuerySql) String() { | ||||
|  | ||||
| } | ||||
| @@ -2,6 +2,7 @@ package dto | ||||
|  | ||||
| import ( | ||||
| 	"drive-linked/pkg/serializer" | ||||
| 	"github.com/kataras/golog" | ||||
| 	"github.com/kataras/iris/v12" | ||||
| 	"net/http" | ||||
| ) | ||||
| @@ -10,21 +11,18 @@ type Response struct { | ||||
| 	Ctx iris.Context | ||||
| } | ||||
|  | ||||
| const errJsonUnmarshalMsg = "{\"code\":500,\"msg\":\"Json解析错误,请联系管理员\",\"data\":null}" | ||||
|  | ||||
| func NewResponse(ctx iris.Context) *Response { | ||||
| 	return &Response{Ctx: ctx} | ||||
| } | ||||
|  | ||||
| const errJsonUnmarshalMsg = "{\"code\":500,\"msg\":\"Json解析错误,请联系管理员\",\"data\":null}" | ||||
|  | ||||
| // 成功 统一处理 | ||||
| func (r *Response) Success(data interface{}) { | ||||
| 	res := serializer.Response{ | ||||
| 		Code: 200, | ||||
| 		Msg:  "", | ||||
| 		Data: data, | ||||
| 	} | ||||
| 	_, err := r.Ctx.JSON(res) | ||||
| 	resp := serializer.ResponseSerial{Code: http.StatusOK, Data: data} | ||||
| 	_, err := r.Ctx.JSON(resp) | ||||
| 	if err != nil { | ||||
| 		golog.Error("Json解析错误: ", resp) | ||||
| 		r.Ctx.Write([]byte(errJsonUnmarshalMsg)) | ||||
| 		return | ||||
| 	} | ||||
| @@ -32,12 +30,8 @@ func (r *Response) Success(data interface{}) { | ||||
|  | ||||
| // 失败 统一处理 | ||||
| func (r *Response) Error(code int, msg string) { | ||||
| 	res := serializer.Response{ | ||||
| 		Code: code, | ||||
| 		Msg:  msg, | ||||
| 		Data: nil, | ||||
| 	} | ||||
| 	_, err := r.Ctx.JSON(res) | ||||
| 	resp := serializer.ResponseSerial{Code: code, Msg: msg} | ||||
| 	_, err := r.Ctx.JSON(resp) | ||||
| 	if err != nil { | ||||
| 		r.Ctx.Write([]byte(errJsonUnmarshalMsg)) | ||||
| 		return | ||||
|   | ||||
| @@ -4,14 +4,23 @@ import ( | ||||
| 	"drive-linked/pkg/common" | ||||
| 	"drive-linked/pkg/dto" | ||||
| 	"github.com/kataras/iris/v12" | ||||
| 	"strings" | ||||
| ) | ||||
| 
 | ||||
| func SignRequired(ctx iris.Context) { | ||||
| 	auth := ctx.GetHeader("Authorization") | ||||
| 	//TODO:更详细的判断,包括请求格式是否正确 | ||||
| 
 | ||||
| 	// 取出Bearer后的内容 | ||||
| 	var tokenString string | ||||
| 	if auths := strings.Split(auth, " "); len(auths) > 1 { | ||||
| 		tokenString = auths[1] | ||||
| 	} else { | ||||
| 		ctx.Skip() | ||||
| 	} | ||||
| 
 | ||||
| 	// 验证token | ||||
| 	err := common.ValidateLogin(auth) | ||||
| 	err := common.VerifyToken(tokenString) | ||||
| 	if err == nil { | ||||
| 		ctx.Values().Set("logged_in", true) | ||||
| 		ctx.Next() | ||||
| @@ -1,8 +1,6 @@ | ||||
| package serializer | ||||
|  | ||||
| import "net/http" | ||||
|  | ||||
| type Response struct { | ||||
| type ResponseSerial struct { | ||||
| 	Code int         `json:"code"` | ||||
| 	Msg  string      `json:"msg"` | ||||
| 	Data interface{} `json:"data"` | ||||
| @@ -10,17 +8,9 @@ type Response struct { | ||||
|  | ||||
| /* | ||||
| 自定义错误码 | ||||
| 10000 用户操作 | ||||
| 40000 业务逻辑错误 | ||||
| 50000 服务器操作 | ||||
| */ | ||||
| const ( | ||||
| 	ErrNoUser = 10001 | ||||
| 	ErrNoUser = 40001 | ||||
| ) | ||||
|  | ||||
| func Success(data interface{}) Response { | ||||
| 	r := Response{ | ||||
| 		Code: http.StatusOK, | ||||
| 		Msg:  "", | ||||
| 		Data: data, | ||||
| 	} | ||||
| 	return r | ||||
| } | ||||
|   | ||||
							
								
								
									
										20
									
								
								pkg/service/controller/usersController.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								pkg/service/controller/usersController.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| package controller | ||||
|  | ||||
| import ( | ||||
| 	"drive-linked/pkg/service" | ||||
| 	"github.com/kataras/iris/v12" | ||||
| ) | ||||
|  | ||||
| func UserProfile(ctx iris.Context) { | ||||
| 	serv := service.NewUsersService(ctx) | ||||
|  | ||||
| 	// 获取所有查询条件参数 | ||||
| 	var conditions map[string]interface{} | ||||
| 	for _, field := range service.UserConditions { | ||||
| 		if ctx.Params().GetString(field) != "" { | ||||
| 			conditions[field] = ctx.Params().GetString(field) | ||||
| 		} | ||||
| 	} | ||||
| 	serv.GetOneUser(&conditions) | ||||
| 	ctx.Next() | ||||
| } | ||||
| @@ -13,26 +13,20 @@ type UsersService struct { | ||||
| 	Ctx iris.Context | ||||
| } | ||||
|  | ||||
| const ( | ||||
| 	MethodUserName  = "name" | ||||
| 	MethodUserEmail = "email" | ||||
| var ( | ||||
| 	UserConditions = [...]string{"id", "name", "email", "nickname"} | ||||
| ) | ||||
|  | ||||
| func NewUsersService(ctx iris.Context) *UsersService { | ||||
| 	return &UsersService{Ctx: ctx} | ||||
| } | ||||
|  | ||||
| func (serv *UsersService) GetOneUser(field, method string) { | ||||
| func (serv *UsersService) GetOneUser(conditions *map[string]interface{}) { | ||||
| 	var err error | ||||
| 	resp := dto.NewResponse(serv.Ctx) | ||||
| 	user := &model.User{} | ||||
|  | ||||
| 	switch method { | ||||
| 	case MethodUserName: | ||||
| 		err = user.GetByName(field) | ||||
| 	case MethodUserEmail: | ||||
| 		err = user.GetByEmail(field) | ||||
| 	} | ||||
| 	err = user.GetWithConditions(conditions) | ||||
|  | ||||
| 	switch err { | ||||
| 	case nil: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user