feat: ad manager
This commit is contained in:
@@ -14,23 +14,27 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
CreateReq = ad.CreateReq
|
||||
EditReq = ad.EditReq
|
||||
Item = ad.Item
|
||||
ItemsData = ad.ItemsData
|
||||
ItemsReq = ad.ItemsReq
|
||||
Response = ad.Response
|
||||
StatusReq = ad.StatusReq
|
||||
CreateReq = ad.CreateReq
|
||||
EditReq = ad.EditReq
|
||||
Item = ad.Item
|
||||
ItemsData = ad.ItemsData
|
||||
ItemsReq = ad.ItemsReq
|
||||
Response = ad.Response
|
||||
StatusReq = ad.StatusReq
|
||||
WebItem = ad.WebItem
|
||||
WebItemsReq = ad.WebItemsReq
|
||||
|
||||
Ad interface {
|
||||
// 添加广告
|
||||
Create(ctx context.Context, in *CreateReq, opts ...grpc.CallOption) (*Response, error)
|
||||
// 编辑广告
|
||||
Edit(ctx context.Context, in *EditReq, opts ...grpc.CallOption) (*Response, error)
|
||||
// 广告列表
|
||||
// 广告列表(后台)
|
||||
Items(ctx context.Context, in *ItemsReq, opts ...grpc.CallOption) (*Response, error)
|
||||
// 广告状态
|
||||
Status(ctx context.Context, in *StatusReq, opts ...grpc.CallOption) (*Response, error)
|
||||
// 广告列表(C 端)
|
||||
WebItems(ctx context.Context, in *WebItemsReq, opts ...grpc.CallOption) (*Response, error)
|
||||
}
|
||||
|
||||
defaultAd struct {
|
||||
@@ -56,7 +60,7 @@ func (m *defaultAd) Edit(ctx context.Context, in *EditReq, opts ...grpc.CallOpti
|
||||
return client.Edit(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 广告列表
|
||||
// 广告列表(后台)
|
||||
func (m *defaultAd) Items(ctx context.Context, in *ItemsReq, opts ...grpc.CallOption) (*Response, error) {
|
||||
client := ad.NewAdClient(m.cli.Conn())
|
||||
return client.Items(ctx, in, opts...)
|
||||
@@ -67,3 +71,9 @@ func (m *defaultAd) Status(ctx context.Context, in *StatusReq, opts ...grpc.Call
|
||||
client := ad.NewAdClient(m.cli.Conn())
|
||||
return client.Status(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 广告列表(C 端)
|
||||
func (m *defaultAd) WebItems(ctx context.Context, in *WebItemsReq, opts ...grpc.CallOption) (*Response, error) {
|
||||
client := ad.NewAdClient(m.cli.Conn())
|
||||
return client.WebItems(ctx, in, opts...)
|
||||
}
|
||||
|
||||
@@ -8,31 +8,39 @@ const (
|
||||
// type: 1、商城首页banner 2、商城首页视频 3、商城首页公告 4、商城首页教程
|
||||
|
||||
type AdCreate struct {
|
||||
Id int `gorm:"column:id;primaryKey;autoIncrement"`
|
||||
Type uint8 `gorm:"column:type"`
|
||||
Image string `gorm:"column:image"`
|
||||
Content string `gorm:"column:content"`
|
||||
Url string `gorm:"column:url"`
|
||||
AdminId int `gorm:"column:admin_id"`
|
||||
AdminName string `gorm:"column:admin_name"`
|
||||
Id int `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
|
||||
Type uint8 `gorm:"column:type" json:"type"`
|
||||
Image string `gorm:"column:image" json:"image"`
|
||||
Content string `gorm:"column:content" json:"content"`
|
||||
Url string `gorm:"column:url" json:"url"`
|
||||
AdminId int `gorm:"column:admin_id" json:"admin_id"`
|
||||
AdminName string `gorm:"column:admin_name" json:"admin_name"`
|
||||
}
|
||||
|
||||
type AdInfo struct {
|
||||
Id int `gorm:"column:id"`
|
||||
Type uint8 `gorm:"column:type"`
|
||||
Image string `gorm:"column:image"`
|
||||
Content string `gorm:"column:content"`
|
||||
Reason string `gorm:"column:reason"`
|
||||
Status uint8 `gorm:"column:status"`
|
||||
Url string `gorm:"column:url"`
|
||||
UpdateTime string `gorm:"column:update_time"`
|
||||
CreateTime string `gorm:"column:create_time"`
|
||||
Id int `gorm:"column:id" json:"id"`
|
||||
Type uint8 `gorm:"column:type" json:"type"`
|
||||
Image string `gorm:"column:image" json:"image"`
|
||||
Content string `gorm:"column:content" json:"content"`
|
||||
Reason string `gorm:"column:reason" json:"reason"`
|
||||
Status uint8 `gorm:"column:status" json:"status"`
|
||||
Url string `gorm:"column:url" json:"url"`
|
||||
UpdateTime string `gorm:"column:update_time" json:"update_time"`
|
||||
CreateTime string `gorm:"column:create_time" json:"create_time"`
|
||||
}
|
||||
|
||||
type AdStatus struct {
|
||||
Id int `gorm:"column:id;primaryKey"`
|
||||
Reason string `gorm:"column:reason"`
|
||||
Status uint8 `gorm:"column:status"`
|
||||
AdminId int `gorm:"column:admin_id"`
|
||||
AdminName string `gorm:"column:admin_name"`
|
||||
Id int `gorm:"column:id;primaryKey" json:"id"`
|
||||
Reason string `gorm:"column:reason" json:"reason"`
|
||||
Status uint8 `gorm:"column:status" json:"status"`
|
||||
AdminId int `gorm:"column:admin_id" json:"admin_id"`
|
||||
AdminName string `gorm:"column:admin_name" json:"admin_name"`
|
||||
}
|
||||
|
||||
type AdWebInfo struct {
|
||||
Id int `gorm:"column:id" json:"id"`
|
||||
Type uint8 `gorm:"column:type" json:"type"`
|
||||
Image string `gorm:"column:image" json:"image"`
|
||||
Content string `gorm:"column:content" json:"content"`
|
||||
Url string `gorm:"column:url" json:"url"`
|
||||
}
|
||||
|
||||
@@ -18,3 +18,13 @@ func toAdItem(row dao.AdInfo) *ad.Item {
|
||||
CreateTime: row.CreateTime,
|
||||
}
|
||||
}
|
||||
|
||||
func toAdWebItem(row dao.AdWebInfo) *ad.WebItem {
|
||||
return &ad.WebItem{
|
||||
Id: int64(row.Id),
|
||||
Type: uint32(row.Type),
|
||||
Image: row.Image,
|
||||
Content: row.Content,
|
||||
Url: row.Url,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,7 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"lone-services/pkg/log"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/pkg/validate"
|
||||
ad "lone-services/rpc/ad/pb"
|
||||
@@ -50,9 +48,9 @@ func (l *CreateLogic) Create(in *ad.CreateReq) (*ad.Response, error) {
|
||||
AdminName: adminInfo.Name,
|
||||
}
|
||||
if err := (model.AdModel{}.Init().Create(&data)); err != nil {
|
||||
log.Errorf("create ad: %v", err)
|
||||
l.Errorf("create ad: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
return okResponse(strconv.Itoa(data.Id)), nil
|
||||
return okResponse(utils.StringEmpty), nil
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"lone-services/pkg/log"
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/pkg/validate"
|
||||
@@ -46,7 +45,7 @@ func (l *EditLogic) Edit(in *ad.EditReq) (*ad.Response, error) {
|
||||
var info dao.AdCreate
|
||||
m := model.AdModel{}.Init()
|
||||
if err := m.GetOne(w, &info); err != nil {
|
||||
log.Errorf("edit ad get: %v", err)
|
||||
l.Errorf("edit ad get: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
if info.Id < utils.NumberOne {
|
||||
@@ -61,7 +60,7 @@ func (l *EditLogic) Edit(in *ad.EditReq) (*ad.Response, error) {
|
||||
info.AdminName = adminInfo.Name
|
||||
|
||||
if _, err := m.Edit(w, &info); err != nil {
|
||||
log.Errorf("edit ad: %v", err)
|
||||
l.Errorf("edit ad: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package logic
|
||||
import (
|
||||
"context"
|
||||
|
||||
"lone-services/pkg/log"
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/pkg/validate"
|
||||
@@ -54,7 +53,7 @@ func (l *ItemsLogic) Items(in *ad.ItemsReq) (*ad.Response, error) {
|
||||
var info []dao.AdInfo
|
||||
result, err := model.AdModel{}.Init().Page(w, &info)
|
||||
if err != nil {
|
||||
log.Errorf("ad items: %v", err)
|
||||
l.Errorf("ad items: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"lone-services/pkg/log"
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/pkg/validate"
|
||||
@@ -41,7 +40,7 @@ func (l *StatusLogic) Status(in *ad.StatusReq) (*ad.Response, error) {
|
||||
var info dao.AdStatus
|
||||
m := model.AdModel{}.Init()
|
||||
if err := m.GetOne(w, &info); err != nil {
|
||||
log.Errorf("ad status get: %v", err)
|
||||
l.Errorf("ad status get: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
if info.Id < utils.NumberOne {
|
||||
@@ -59,7 +58,7 @@ func (l *StatusLogic) Status(in *ad.StatusReq) (*ad.Response, error) {
|
||||
info.AdminName = adminInfo.Name
|
||||
|
||||
if _, err := m.Edit(w, &info); err != nil {
|
||||
log.Errorf("ad status edit: %v", err)
|
||||
l.Errorf("ad status edit: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/pkg/validate"
|
||||
ad "lone-services/rpc/ad/pb"
|
||||
"lone-services/services/ad/internal/dao"
|
||||
"lone-services/services/ad/internal/model"
|
||||
"lone-services/services/ad/internal/svc"
|
||||
"lone-services/services/ad/validator"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type WebItemsLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewWebItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WebItemsLogic {
|
||||
return &WebItemsLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *WebItemsLogic) WebItems(in *ad.WebItemsReq) (*ad.Response, error) {
|
||||
var req validator.AdWebItemsValidator
|
||||
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
||||
return outResponse(utils.ErrorParams, msg), nil
|
||||
}
|
||||
|
||||
types := make([]string, 0)
|
||||
for _, t := range strings.Split(req.Type, utils.DecollatorComma) {
|
||||
t = strings.TrimSpace(t)
|
||||
if t != "" {
|
||||
types = append(types, t)
|
||||
}
|
||||
}
|
||||
if len(types) < 1 {
|
||||
return outResponse(utils.ErrorParams, "请选择类型"), nil
|
||||
}
|
||||
|
||||
w := modelbase.Params{
|
||||
Order: "type asc",
|
||||
In: map[string][]string{"type": types},
|
||||
Eq: map[string]string{"status": utils.StringStatusOk},
|
||||
}
|
||||
|
||||
var info []dao.AdWebInfo
|
||||
if err := (model.AdModel{}.Init().Items(w, &info)); err != nil {
|
||||
l.Errorf("ad web items: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
items := make([]*ad.WebItem, 0, len(info))
|
||||
for _, row := range info {
|
||||
items = append(items, toAdWebItem(row))
|
||||
}
|
||||
|
||||
return okResponse(utils.StructToJson(items)), nil
|
||||
}
|
||||
@@ -35,7 +35,7 @@ func (s *AdServer) Edit(ctx context.Context, in *ad.EditReq) (*ad.Response, erro
|
||||
return l.Edit(in)
|
||||
}
|
||||
|
||||
// 广告列表
|
||||
// 广告列表(后台)
|
||||
func (s *AdServer) Items(ctx context.Context, in *ad.ItemsReq) (*ad.Response, error) {
|
||||
l := logic.NewItemsLogic(ctx, s.svcCtx)
|
||||
return l.Items(in)
|
||||
@@ -46,3 +46,9 @@ func (s *AdServer) Status(ctx context.Context, in *ad.StatusReq) (*ad.Response,
|
||||
l := logic.NewStatusLogic(ctx, s.svcCtx)
|
||||
return l.Status(in)
|
||||
}
|
||||
|
||||
// 广告列表(C 端)
|
||||
func (s *AdServer) WebItems(ctx context.Context, in *ad.WebItemsReq) (*ad.Response, error) {
|
||||
l := logic.NewWebItemsLogic(ctx, s.svcCtx)
|
||||
return l.WebItems(in)
|
||||
}
|
||||
|
||||
@@ -11,13 +11,13 @@ type AdCreateValidator struct {
|
||||
|
||||
func (p AdCreateValidator) GetMessage() validate.ValidatorMessages {
|
||||
return validate.ValidatorMessages{
|
||||
"Type.required": "请选择类型",
|
||||
"Type.oneof": "类型不对",
|
||||
"Image.required_without": "图片与文本内容必须填写一个",
|
||||
"Content.required_without": "图片与文本内容必须填写一个",
|
||||
"Image.max": "图片长度不能超过255",
|
||||
"Content.max": "内容长度不能超过255",
|
||||
"Url.max": "链接长度不能超过255",
|
||||
"Type.required": "请选择类型",
|
||||
"Type.oneof": "类型不对",
|
||||
"Image.required_without": "图片与文本内容必须填写一个",
|
||||
"Content.required_without": "图片与文本内容必须填写一个",
|
||||
"Image.max": "图片长度不能超过255",
|
||||
"Content.max": "内容长度不能超过255",
|
||||
"Url.max": "链接长度不能超过255",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,15 +31,15 @@ type AdEditValidator struct {
|
||||
|
||||
func (p AdEditValidator) GetMessage() validate.ValidatorMessages {
|
||||
return validate.ValidatorMessages{
|
||||
"Id.required": "ID不能为空",
|
||||
"Id.gt": "ID必须大于0",
|
||||
"Type.required": "请选择类型",
|
||||
"Type.oneof": "类型不对",
|
||||
"Image.required_without": "图片与文本内容必须填写一个",
|
||||
"Content.required_without": "图片与文本内容必须填写一个",
|
||||
"Image.max": "图片长度不能超过255",
|
||||
"Content.max": "内容长度不能超过255",
|
||||
"Url.max": "链接长度不能超过255",
|
||||
"Id.required": "ID不能为空",
|
||||
"Id.gt": "ID必须大于0",
|
||||
"Type.required": "请选择类型",
|
||||
"Type.oneof": "类型不对",
|
||||
"Image.required_without": "图片与文本内容必须填写一个",
|
||||
"Content.required_without": "图片与文本内容必须填写一个",
|
||||
"Image.max": "图片长度不能超过255",
|
||||
"Content.max": "内容长度不能超过255",
|
||||
"Url.max": "链接长度不能超过255",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,11 +64,22 @@ type AdStatusValidator struct {
|
||||
|
||||
func (p AdStatusValidator) GetMessage() validate.ValidatorMessages {
|
||||
return validate.ValidatorMessages{
|
||||
"Id.required": "ID不能为空",
|
||||
"Id.gt": "ID必须大于0",
|
||||
"Status.required": "状态不能为空",
|
||||
"Status.oneof": "状态不对",
|
||||
"Reason.required_if": "禁用状态,理由不能为空",
|
||||
"Reason.max": "理由长度不能超过255",
|
||||
"Id.required": "ID不能为空",
|
||||
"Id.gt": "ID必须大于0",
|
||||
"Status.required": "状态不能为空",
|
||||
"Status.oneof": "状态不对",
|
||||
"Reason.required_if": "禁用状态,理由不能为空",
|
||||
"Reason.max": "理由长度不能超过255",
|
||||
}
|
||||
}
|
||||
|
||||
type AdWebItemsValidator struct {
|
||||
Type string `validate:"required,max=64"`
|
||||
}
|
||||
|
||||
func (p AdWebItemsValidator) GetMessage() validate.ValidatorMessages {
|
||||
return validate.ValidatorMessages{
|
||||
"Type.required": "请选择类型",
|
||||
"Type.max": "类型长度不能超过64",
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user