Skip to content

Commit

Permalink
move validator package to simple_admin inline
Browse files Browse the repository at this point in the history
  • Loading branch information
23233 committed May 6, 2020
1 parent ae92579 commit d925985
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
29 changes: 14 additions & 15 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package simple_admin

import (
"fmt"
"github.com/23233/simple_admin/validator"
"github.com/kataras/iris/v12"
"strconv"
)
Expand All @@ -15,7 +14,7 @@ func Index(ctx iris.Context) {

// 获取配置信息
func Configuration(ctx iris.Context) {
var resp validator.ConfigResp
var resp ConfigResp
resp.Name = NowSpAdmin.config.Name
resp.Prefix = NowSpAdmin.config.Prefix
resp.UserModelName = NowSpAdmin.config.UserModelSpecialUniqueName
Expand All @@ -24,7 +23,7 @@ func Configuration(ctx iris.Context) {

// 登录
func Login(ctx iris.Context) {
var req validator.UserLoginReq
var req UserLoginReq
// 引入数据
if err := ctx.ReadJSON(&req); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
Expand All @@ -34,7 +33,7 @@ func Login(ctx iris.Context) {
return
}
// 基础验证
if err := validator.GlobalValidator.Check(req); err != nil {
if err := GlobalValidator.Check(req); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
_, _ = ctx.JSON(iris.Map{
"detail": err.Error(),
Expand Down Expand Up @@ -81,7 +80,7 @@ func Login(ctx iris.Context) {
}
// 生成jwt
jwt := GenJwtToken(valuesMap["id"], req.UserName)
var resp validator.UserLoginResp
var resp UserLoginResp
resp.Token = jwt
resp.UserName = req.UserName
resp.Roles = roles
Expand All @@ -90,7 +89,7 @@ func Login(ctx iris.Context) {

// 注册
func Reg(ctx iris.Context) {
var req validator.UserLoginReq
var req UserLoginReq
// 引入数据
if err := ctx.ReadJSON(&req); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
Expand All @@ -100,7 +99,7 @@ func Reg(ctx iris.Context) {
return
}
// 基础验证
if err := validator.GlobalValidator.Check(req); err != nil {
if err := GlobalValidator.Check(req); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
_, _ = ctx.JSON(iris.Map{
"detail": err.Error(),
Expand All @@ -118,7 +117,7 @@ func Reg(ctx iris.Context) {
}
// 生成jwt
jwt := GenJwtToken(strconv.FormatInt(aff, 10), req.UserName)
var resp validator.UserLoginResp
var resp UserLoginResp
resp.Token = jwt
resp.UserName = req.UserName
resp.Roles = []string{"guest"}
Expand All @@ -129,7 +128,7 @@ func Reg(ctx iris.Context) {
func GetCurrentUser(ctx iris.Context) {
un := ctx.Values().Get("un").(string)
uid := ctx.Values().Get("uid").(string)
var resp validator.GetCurrentUserResp
var resp GetCurrentUserResp
resp.Name = un
resp.UserId = uid
resp.Avatar = "https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png"
Expand Down Expand Up @@ -287,7 +286,7 @@ func EditRouterData(ctx iris.Context) {
// 删除数据 -> 可以批量
func RemoveRouterData(ctx iris.Context) {
routerName := ctx.Params().Get("routerName")
var req validator.DeleteReq
var req DeleteReq
// 引入数据
if err := ctx.ReadJSON(&req); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
Expand All @@ -297,7 +296,7 @@ func RemoveRouterData(ctx iris.Context) {
return
}
// 基础验证
if err := validator.GlobalValidator.Check(req); err != nil {
if err := GlobalValidator.Check(req); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
_, _ = ctx.JSON(iris.Map{
"detail": err.Error(),
Expand All @@ -319,7 +318,7 @@ func RemoveRouterData(ctx iris.Context) {

// 变更用户密码
func ChangeUserPassword(ctx iris.Context) {
var req validator.UserChangePasswordReq
var req UserChangePasswordReq
// 引入数据
if err := ctx.ReadJSON(&req); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
Expand All @@ -329,7 +328,7 @@ func ChangeUserPassword(ctx iris.Context) {
return
}
// 基础验证
if err := validator.GlobalValidator.Check(req); err != nil {
if err := GlobalValidator.Check(req); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
_, _ = ctx.JSON(iris.Map{
"detail": err.Error(),
Expand Down Expand Up @@ -372,7 +371,7 @@ func ChangeUserPassword(ctx iris.Context) {

// 变更用户群组
func ChangeUserRoles(ctx iris.Context) {
var req validator.UserChangeRolesReq
var req UserChangeRolesReq
var err error
// 引入数据
if err = ctx.ReadJSON(&req); err != nil {
Expand All @@ -383,7 +382,7 @@ func ChangeUserRoles(ctx iris.Context) {
return
}
// 基础验证
if err = validator.GlobalValidator.Check(req); err != nil {
if err = GlobalValidator.Check(req); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
_, _ = ctx.JSON(iris.Map{
"detail": err.Error(),
Expand Down
1 change: 0 additions & 1 deletion simple_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ func (lib *SpAdmin) Router(router iris.Party) {
c.Put("/{routerName:string}/{id:uint64}", PolicyValidMiddleware, EditRouterData)
// 删除 delete模式在某些匹配时候有问题
c.Post("/{routerName:string}/delete", PolicyValidMiddleware, RemoveRouterData)

// 权限相关
c.Post("/change_user_role", PolicyRequireAdminMiddleware, ChangeUserRoles)

Expand Down
2 changes: 1 addition & 1 deletion validator/validator.go → validator.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package validator
package simple_admin

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion validator/global.go → validator_global.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package validator
package simple_admin

type ConfigResp struct {
Name string `json:"name"`
Expand Down
2 changes: 1 addition & 1 deletion validator/user.go → validator_user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package validator
package simple_admin

// 所有返回必须包裹一层resp
type GlobalResp struct {
Expand Down

0 comments on commit d925985

Please sign in to comment.