From a93ae51e27f1d1ee2b643a8dd3589d4cb73628eb Mon Sep 17 00:00:00 2001 From: wzj Date: Sat, 9 Nov 2024 22:33:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Djwt=E7=9A=84=E9=83=A8?= =?UTF-8?q?=E5=88=86=E9=80=BB=E8=BE=91=20&&=20=E6=96=B0=E5=A2=9E=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=91=98=E6=8E=A5=E5=8F=A3=20&&=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=95=86=E5=93=81=E7=B1=BB=E5=9E=8B=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/admin_controller.go | 39 +++++++++++++++++++++++++++++++ controller/controller.go | 2 ++ docs/docs.go | 42 ++++++++++++++++++++++++++++++++++ docs/swagger.json | 42 ++++++++++++++++++++++++++++++++++ docs/swagger.yaml | 28 +++++++++++++++++++++++ ecode/item_ecode.go | 2 ++ middleware/admin.go | 1 + middleware/login.go | 25 ++++++++++++++++++++ service/item_service.go | 16 +++++++++++++ validate/admin_validate.go | 5 ++++ 10 files changed, 202 insertions(+) create mode 100644 controller/admin_controller.go create mode 100644 middleware/login.go create mode 100644 validate/admin_validate.go diff --git a/controller/admin_controller.go b/controller/admin_controller.go new file mode 100644 index 0000000..8c1ef8a --- /dev/null +++ b/controller/admin_controller.go @@ -0,0 +1,39 @@ +package controller + +import ( + "yitao/middleware" + "yitao/validate" + + "github.com/kataras/iris/v12/mvc" +) + +type AdminController struct { + BaseController +} + +func (c *AdminController) BeforeActivation(b mvc.BeforeActivation) { + b.Handle("POST", "/type/create", "CreateType", middleware.JwtMiddleware.Serve, middleware.AdminMiddleware) +} + +// @Summary 创建商品类型 +// @Description 创建商品类型 +// @Tags admin api +// @Accept json +// @Produce json +// @Param name body string true "类型名称" +// @Success 200 {object} map[string]interface{} "{"code": 0}" +// @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}"` +// @Router /api/admin/type/create [post] +func (c *AdminController) CreateType() mvc.Result { + createTypeParam := new(validate.CreateTypeParam) + validate.ReadJSON(c.Ctx, createTypeParam) + e := c.Service.Item.CreateType(createTypeParam.Name) + if e.Error() { + return e.Response() + } + return mvc.Response{ + Object: map[string]interface{}{ + "code": 0, + }, + } +} diff --git a/controller/controller.go b/controller/controller.go index 401f191..a37d895 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -30,6 +30,8 @@ func LoadRouter(app *iris.Application, service *service.Service) { registerController("/item", new(ItemController)) registerController("/file", new(FileController)) + registerController("/admin", new(AdminController)) + } func registerController(root string, controller ControllerInterface) { diff --git a/docs/docs.go b/docs/docs.go index fefcb92..6c09216 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -24,6 +24,48 @@ const docTemplate = `{ "host": "{{.Host}}", "basePath": "{{.BasePath}}", "paths": { + "/api/admin/type/create": { + "post": { + "description": "创建商品类型", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "admin api" + ], + "summary": "创建商品类型", + "parameters": [ + { + "description": "类型名称", + "name": "name", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "{\"code\": 0}", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "400": { + "description": "{\"msg\": \"错误信息\",\"code\":0}\"` + "`" + `", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + }, "/api/captcha/click/gen": { "get": { "description": "生成按次序点击验证码", diff --git a/docs/swagger.json b/docs/swagger.json index e65f47f..d786352 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -18,6 +18,48 @@ "host": "localhost:5001", "basePath": "/api", "paths": { + "/api/admin/type/create": { + "post": { + "description": "创建商品类型", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "admin api" + ], + "summary": "创建商品类型", + "parameters": [ + { + "description": "类型名称", + "name": "name", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "{\"code\": 0}", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "400": { + "description": "{\"msg\": \"错误信息\",\"code\":0}\"`", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + }, "/api/captcha/click/gen": { "get": { "description": "生成按次序点击验证码", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index f11913d..77882b7 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -137,6 +137,34 @@ info: title: Yitao Backend API version: "1.0" paths: + /api/admin/type/create: + post: + consumes: + - application/json + description: 创建商品类型 + parameters: + - description: 类型名称 + in: body + name: name + required: true + schema: + type: string + produces: + - application/json + responses: + "200": + description: '{"code": 0}' + schema: + additionalProperties: true + type: object + "400": + description: '{"msg": "错误信息","code":0}"`' + schema: + additionalProperties: true + type: object + summary: 创建商品类型 + tags: + - admin api /api/captcha/click/{key}/submit: post: consumes: diff --git a/ecode/item_ecode.go b/ecode/item_ecode.go index 5b72983..aa1a632 100644 --- a/ecode/item_ecode.go +++ b/ecode/item_ecode.go @@ -4,12 +4,14 @@ const ( ITEM_OK = iota ITEM_NOT_FOUND ITEM_PERMISSION_DENIED + ITEM_TYPE_EXIST ) var ITEM_MSG = map[int]string{ ITEM_OK: "商品操作成功", ITEM_NOT_FOUND: "商品不存在", ITEM_PERMISSION_DENIED: "操作权限不足", + ITEM_TYPE_EXIST: "商品类型已存在", } func Item(id int) *Ecode { diff --git a/middleware/admin.go b/middleware/admin.go index a55e475..8a5a184 100644 --- a/middleware/admin.go +++ b/middleware/admin.go @@ -13,6 +13,7 @@ var AdminMiddleware = iris.Handler(func(ctx iris.Context) { if !is_admin { ctx.StatusCode(iris.StatusForbidden) ctx.JSON(iris.Map{"message": "forbidden"}) + return } } else { ctx.StatusCode(iris.StatusUnauthorized) diff --git a/middleware/login.go b/middleware/login.go new file mode 100644 index 0000000..a190372 --- /dev/null +++ b/middleware/login.go @@ -0,0 +1,25 @@ +package middleware + +import ( + "github.com/iris-contrib/middleware/jwt" + "github.com/kataras/iris/v12" +) + +var LoginMiddleware = iris.Handler(func(ctx iris.Context) { + + if token, ok := ctx.Values().Get("jwt").(*jwt.Token); ok { + // Use the token if needed + _, ok := token.Claims.(jwt.MapClaims)["uid"].(bool) + if !ok { + ctx.StatusCode(iris.StatusUnauthorized) + ctx.JSON(iris.Map{"message": "unauthorized"}) + return + } + } else { + ctx.StatusCode(iris.StatusUnauthorized) + ctx.JSON(iris.Map{"message": "unauthorized"}) + return + } + + ctx.Next() +}) diff --git a/service/item_service.go b/service/item_service.go index 9e04c4e..1cd2f0a 100644 --- a/service/item_service.go +++ b/service/item_service.go @@ -51,8 +51,24 @@ func (s *ItemService) GetItems(ttype int) (items []model.ItemModel, e *ecode.Eco return } +// 类型服务模块 +/*********************************************************************************/ + func (s *ItemService) GetTypes() (types []model.TypeModel, e *ecode.Ecode) { e = ecode.OK() model.DB.Find(&types) return } + +func (s *ItemService) CreateType(name string) *ecode.Ecode { + t := new(model.TypeModel) + // 判断该名称是否已存在 + model.DB.Where("name=?", name).First(t) + if t.ID != 0 { + return ecode.Item(ecode.ITEM_TYPE_EXIST) + } + + t.Name = name + model.DB.Create(t) + return ecode.OK() +} diff --git a/validate/admin_validate.go b/validate/admin_validate.go new file mode 100644 index 0000000..4f9351c --- /dev/null +++ b/validate/admin_validate.go @@ -0,0 +1,5 @@ +package validate + +type CreateTypeParam struct { + Name string `json:"name" validate:"required,max=20"` +}