2024-11-09 14:59:27 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"yitao/ecode"
|
|
|
|
"yitao/middleware"
|
|
|
|
"yitao/validate"
|
|
|
|
|
|
|
|
"github.com/kataras/iris/v12/mvc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ItemController struct {
|
|
|
|
BaseController
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ItemController) BeforeActivation(b mvc.BeforeActivation) {
|
2024-11-09 16:51:53 +08:00
|
|
|
b.Handle("GET", "/detail/{id:uint}", "Detail")
|
2024-11-11 22:06:24 +08:00
|
|
|
b.Handle("GET", "/list/type/{ttype:int}", "GetListByType")
|
2024-11-10 16:19:49 +08:00
|
|
|
b.Handle("GET", "/search", "Search")
|
2024-11-09 14:59:27 +08:00
|
|
|
|
2024-11-09 22:11:24 +08:00
|
|
|
b.Handle("GET", "types", "GetTypes")
|
|
|
|
|
2024-11-09 14:59:27 +08:00
|
|
|
// 管理员接口
|
|
|
|
{
|
|
|
|
b.Handle("POST", "/oper/create", "Create", middleware.JwtMiddleware.Serve)
|
2024-11-09 16:36:28 +08:00
|
|
|
b.Handle("DELETE", "/oper/delete", "Delete", middleware.JwtMiddleware.Serve)
|
2024-11-09 14:59:27 +08:00
|
|
|
//b.Handle("POST", "/admin/update", "Update", middleware.JwtMiddleware.Serve, middleware.AdminMiddleware)
|
|
|
|
//b.Handle("POST", "/admin/delete", "Delete", middleware.JwtMiddleware.Serve, middleware.AdminMiddleware)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary get item api
|
|
|
|
// @Description 获取相关ID的商品信息
|
|
|
|
// @Tags item api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path uint true "商品ID"
|
2024-11-09 21:24:02 +08:00
|
|
|
// @Success 200 {object} model.ItemModel "{"id":1}"
|
2024-11-09 14:59:27 +08:00
|
|
|
// @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}"
|
2024-11-09 21:24:02 +08:00
|
|
|
// @Router /api/item/{id} [get]
|
2024-11-09 16:51:53 +08:00
|
|
|
func (c *ItemController) Detail(id uint) mvc.Result {
|
|
|
|
item, e := c.Service.Item.GetItem(id)
|
|
|
|
if e.Error() {
|
|
|
|
return e.Response()
|
|
|
|
}
|
|
|
|
return mvc.Response{
|
|
|
|
Object: item,
|
|
|
|
}
|
2024-11-09 14:59:27 +08:00
|
|
|
}
|
|
|
|
|
2024-11-09 21:24:02 +08:00
|
|
|
// @Summary get item list api
|
|
|
|
// @Description 通过类型获取相关商品列表
|
|
|
|
// @Tags item api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param type path string true "商品类型"
|
2024-11-13 16:30:51 +08:00
|
|
|
// @Param pagingParam query validate.PagingParam true "分页信息"
|
2024-11-09 21:24:02 +08:00
|
|
|
// @Success 200 {object} []model.ItemModel "{["id":1],["id":2]}"
|
|
|
|
// @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}"
|
2024-11-11 22:06:24 +08:00
|
|
|
// @Router /api/item/list/type/{type} [get]
|
|
|
|
func (c *ItemController) GetListByType(ttype int) mvc.Result {
|
2024-11-13 16:30:51 +08:00
|
|
|
pagingParam := new(validate.PagingParam)
|
|
|
|
e := validate.ReadQuery(c.Ctx, pagingParam)
|
|
|
|
if e.Error() {
|
|
|
|
return e.Response()
|
|
|
|
}
|
|
|
|
items, e := c.Service.Item.GetItems(ttype, pagingParam.Page, pagingParam.PageSize)
|
2024-11-09 21:24:02 +08:00
|
|
|
if e.Error() {
|
|
|
|
return e.Response()
|
|
|
|
}
|
|
|
|
return mvc.Response{
|
|
|
|
Object: items,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-09 22:11:24 +08:00
|
|
|
// @Summary get item types api
|
|
|
|
// @Description 获取商品类型列表
|
|
|
|
// @Tags item api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} []model.TypeModel "[{id:1,name:类型1},{id:2,name:类型2}]"
|
|
|
|
// @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}"
|
|
|
|
// @Router /api/item/types [get]
|
|
|
|
func (c *ItemController) GetTypes() mvc.Result {
|
|
|
|
types, e := c.Service.Item.GetTypes()
|
|
|
|
if e.Error() {
|
|
|
|
return e.Response()
|
|
|
|
}
|
|
|
|
return mvc.Response{
|
|
|
|
Object: types,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-10 16:19:49 +08:00
|
|
|
// @Summary search item api
|
|
|
|
// @Description 搜索商品
|
|
|
|
// @Tags item api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param keyword query string true "搜索关键字"
|
|
|
|
// @Success 200 {object} []model.ItemModel "{["id":1],["id":2]}"
|
|
|
|
// @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}"
|
|
|
|
// @Router /api/item/search [get]
|
|
|
|
func (c *ItemController) Search() mvc.Result {
|
|
|
|
keyword := c.Ctx.URLParam("keyword")
|
2024-11-13 16:30:51 +08:00
|
|
|
pagineParam := new(validate.PagingParam)
|
|
|
|
e := validate.ReadQuery(c.Ctx, pagineParam)
|
|
|
|
if e.Error() {
|
|
|
|
return e.Response()
|
|
|
|
}
|
|
|
|
items, e := c.Service.Item.Search(keyword, pagingParam.Page, pagingParam.PageSize)
|
2024-11-10 16:19:49 +08:00
|
|
|
if e.Error() {
|
|
|
|
return e.Response()
|
|
|
|
}
|
|
|
|
return mvc.Response{
|
|
|
|
Object: items,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-09 14:59:27 +08:00
|
|
|
// @Summary 创建商品
|
|
|
|
// @Description 创建商品接口
|
|
|
|
// @Tags item api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param createItemParam body validate.CreateItemParam true "商品信息"
|
|
|
|
// @Success 200 {object} model.ItemModel "{"id": 1}"
|
|
|
|
// @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}"
|
|
|
|
// @Router /api/item/oper/create [post]
|
|
|
|
func (c *ItemController) Create() mvc.Result {
|
|
|
|
e := ecode.OK()
|
|
|
|
create_item_param := new(validate.CreateItemParam)
|
|
|
|
e = validate.ReadJSON(c.Ctx, create_item_param)
|
|
|
|
if e.Error() {
|
|
|
|
return e.Response()
|
|
|
|
}
|
|
|
|
|
2024-11-09 16:36:28 +08:00
|
|
|
// 验证图片文件是否存在
|
|
|
|
_, e = c.Service.File.GetFileId(create_item_param.Img)
|
|
|
|
if e.Error() {
|
|
|
|
return e.Response()
|
|
|
|
}
|
|
|
|
|
|
|
|
uid := GetUidFromCtx(c.Ctx)
|
|
|
|
item, e := c.Service.Item.CreateItem(uid, create_item_param.Itemname, create_item_param.Desc, create_item_param.Types, create_item_param.Img, create_item_param.Price)
|
2024-11-09 14:59:27 +08:00
|
|
|
if e.Error() {
|
|
|
|
return e.Response()
|
|
|
|
}
|
|
|
|
|
|
|
|
return mvc.Response{
|
|
|
|
Object: item,
|
|
|
|
}
|
|
|
|
}
|
2024-11-09 16:36:28 +08:00
|
|
|
|
|
|
|
// @Summary 删除商品
|
|
|
|
// @Description 删除商品接口
|
|
|
|
// @Tags item api
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param deleteItemParam body validate.DeleteItemParam true "商品信息"
|
|
|
|
// @Success 200 {object} map[string]interface{} "{"msg": "商品操作成功"}"
|
|
|
|
// @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}"
|
|
|
|
// @Router /api/item/oper/delete [delete]
|
|
|
|
func (c *ItemController) Delete() mvc.Result {
|
|
|
|
e := ecode.OK()
|
|
|
|
deleteItemParam := new(validate.DeleteItemParam)
|
|
|
|
e = validate.ReadJSON(c.Ctx, deleteItemParam)
|
|
|
|
if e.Error() {
|
|
|
|
return e.Response()
|
|
|
|
}
|
|
|
|
|
|
|
|
uid := GetUidFromCtx(c.Ctx)
|
|
|
|
c.Service.Item.DeleteItem(uid, 1)
|
|
|
|
return e.Response()
|
|
|
|
}
|