修复jwt的部分逻辑 && 新增管理员接口 && 新增添加商品类型接口
This commit is contained in:
parent
f94c3e1668
commit
a93ae51e27
39
controller/admin_controller.go
Normal file
39
controller/admin_controller.go
Normal file
@ -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,
|
||||
},
|
||||
}
|
||||
}
|
@ -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) {
|
||||
|
42
docs/docs.go
42
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": "生成按次序点击验证码",
|
||||
|
@ -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": "生成按次序点击验证码",
|
||||
|
@ -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:
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
|
25
middleware/login.go
Normal file
25
middleware/login.go
Normal file
@ -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()
|
||||
})
|
@ -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()
|
||||
}
|
||||
|
5
validate/admin_validate.go
Normal file
5
validate/admin_validate.go
Normal file
@ -0,0 +1,5 @@
|
||||
package validate
|
||||
|
||||
type CreateTypeParam struct {
|
||||
Name string `json:"name" validate:"required,max=20"`
|
||||
}
|
Loading…
Reference in New Issue
Block a user