264 lines
10 KiB
Go
264 lines
10 KiB
Go
package adsctrl
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"91porn-server/common"
|
|
"91porn-server/common/constant"
|
|
"91porn-server/common/log"
|
|
"91porn-server/common/stderr"
|
|
"91porn-server/common/ysurl"
|
|
"91porn-server/models/commod"
|
|
"91porn-server/models/l/operatorlgmod"
|
|
"91porn-server/models/v/adreviewmod"
|
|
"91porn-server/models/v/adsmod"
|
|
"91porn-server/web/service/adser"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
// List doc
|
|
// @Summary 广告列表
|
|
// @Description 广告列表
|
|
// @Tags ads
|
|
// @Accept mpfd,json
|
|
// @Produce json,html
|
|
// @Param id formData string true "id"
|
|
// @Param position formData integer true "广告位置"
|
|
// @Param pageNumber formData integer true "查询页码"
|
|
// @Param pageSize formData integer true "页码大小"
|
|
// @Param sort formData string true "排序的字段"
|
|
// @Param desc formData integer true "1 正序 -1倒序"
|
|
// @Success 200 {string} json "{"msg": "操作成功"}"
|
|
// @Failure 400 {string} json "{"msg": "操作失败"}"
|
|
// @Router /api/web/admin/ads/list [get]
|
|
func List(ctx *gin.Context) {
|
|
type Query struct {
|
|
ID *string `form:"id" json:"id"` //id
|
|
AdsType *adsmod.AdsType `form:"adsType" json:"adsType"` //广告类型
|
|
Position *adsmod.AdPosition `form:"position" json:"position"` //广告位置
|
|
DiscCode *string `form:"discCode" json:"discCode"` //商区码
|
|
}
|
|
var args struct {
|
|
commod.Page
|
|
Query
|
|
}
|
|
if err := ctx.ShouldBind(&args); err != nil {
|
|
common.ServeJSON(ctx, stderr.ErrParamError, "adsctrl List arg error "+err.Error())
|
|
return
|
|
}
|
|
skip := int64((args.PageNumber - 1) * args.PageSize)
|
|
limit := int64(args.PageSize)
|
|
var id *primitive.ObjectID
|
|
if args.ID != nil {
|
|
i, err := primitive.ObjectIDFromHex(*args.ID)
|
|
if err != nil {
|
|
common.ServeJSON(ctx, stderr.ErrParamError, "adsctrl List arg error "+err.Error())
|
|
return
|
|
}
|
|
id = &i
|
|
}
|
|
page, err := adser.AdPages(skip, limit,
|
|
adsmod.IDMatch{ID: id},
|
|
adsmod.AdsTypeMatch{AdsType: args.AdsType},
|
|
adsmod.AdPositionMatch{Position: args.Position},
|
|
adsmod.DistrictCodeMatch{DistrictCode: args.DiscCode},
|
|
)
|
|
if err != nil {
|
|
common.ServeJSON(ctx, stderr.Failure, "adsctrl AdPages error: "+err.Error())
|
|
return
|
|
}
|
|
common.ServeJSON(ctx, stderr.Success, page)
|
|
}
|
|
|
|
// Update doc
|
|
// @Summary 更新广告信息
|
|
// @Description 更新广告信息
|
|
// @Tags vid
|
|
// @Accept mpfd,json
|
|
// @Produce json,html
|
|
// @Param id formData string true "ID"
|
|
// @Param title formData string false "广告标题"
|
|
// @Param cover formData string false "广告封面图"
|
|
// @Param sortCode formData integer false "排序号"
|
|
// @Param type formData integer false "广告类型"
|
|
// @Param href formData string false "广告跳转地址"
|
|
// @Param position formData integer false "广告位置"
|
|
// @Param active formData boolean false "激活状态"
|
|
// @Param remark formData string false "备注"
|
|
// @Success 200 {string} json "{"msg": "操作成功"}"
|
|
// @Failure 400 {string} json "{"msg": "操作失败"}"
|
|
// @Router /api/web/admin/ads/update [post]
|
|
func Update(ctx *gin.Context) {
|
|
manager, err := common.GetAdminAct(ctx)
|
|
if err != nil {
|
|
common.ServeJSON(ctx, stderr.AdminIDErr, err.Error())
|
|
return
|
|
}
|
|
type Update struct {
|
|
Title *string `form:"title" json:"title" binding:""` //广告标题
|
|
Cover *string `form:"cover" json:"cover" binding:"required_with=Position"` //广告封面图
|
|
HrefType *adsmod.URLJumpType `form:"hrefType" json:"hrefType" binding:"required_with=Href"` //广告类型
|
|
Href *string `form:"href" json:"href" binding:"required_with=HrefType"` //广告跳转地址
|
|
Position *adsmod.AdPosition `form:"position" json:"position" binding:"required_with=Cover"` //广告位置
|
|
SortCode *int `form:"sortCode" json:"sortCode" binding:""` //排序号
|
|
Active *bool `form:"active" json:"active" binding:""` //激活状态
|
|
Remark *string `form:"remark" json:"remark" binding:""` //备注
|
|
Start *time.Time `form:"start" json:"start" binding:""` //开始时间
|
|
End *time.Time `form:"end" json:"end" binding:""` //结束时间
|
|
}
|
|
var args struct {
|
|
ID primitive.ObjectID `form:"id" json:"id" binding:"required"`
|
|
Update
|
|
}
|
|
if err := ctx.ShouldBind(&args); err != nil {
|
|
common.ServeJSON(ctx, stderr.ErrParamError, "adsctrl Update arg error "+err.Error())
|
|
return
|
|
}
|
|
if args.HrefType != nil && args.Href != nil {
|
|
if *args.Href != "" && !ysurl.IsValidURL(*args.HrefType, *args.Href) {
|
|
common.ServeJSON(ctx, stderr.URLInvalid, "")
|
|
return
|
|
}
|
|
}
|
|
doc := adsmod.UpdateDoc{
|
|
Title: args.Title,
|
|
Cover: args.Cover,
|
|
HrefType: args.HrefType,
|
|
Href: args.Href,
|
|
Position: args.Position,
|
|
SortCode: args.SortCode,
|
|
Active: args.Active,
|
|
Remark: args.Remark,
|
|
Start: args.Start,
|
|
End: args.End,
|
|
}
|
|
if doc.Cover != nil {
|
|
prop := common.GetPictureProp(common.ImgUrlHost + *doc.Cover)
|
|
doc.CoverProp = prop
|
|
}
|
|
if err = adsmod.UpdateOneByID(nil, args.ID, doc); err != nil {
|
|
switch err.(type) {
|
|
case adsmod.ADSNotExistError:
|
|
common.ServeJSON(ctx, stderr.ADSNotExist, err.Error())
|
|
default:
|
|
log.Error("adsctrl Update failed", log.E(err))
|
|
common.ServeJSON(ctx, stderr.Failure, fmt.Errorf("adsctrl Update error: %+v", err))
|
|
}
|
|
return
|
|
}
|
|
log, _ := json.Marshal(args)
|
|
_ = operatorlgmod.RecordOperation(manager, constant.AdsManageAdsList, constant.Modify, string(log), ctx.Request.URL.RequestURI())
|
|
common.ServeJSON(ctx, stderr.Success, nil)
|
|
}
|
|
|
|
// Delete doc
|
|
// @Summary 删除广告
|
|
// @Description 删除广告
|
|
// @Tags follow
|
|
// @Accept mpfd,json
|
|
// @Produce json,html
|
|
// @Param ids formData array true "广告id数组"
|
|
// @Success 200 {string} json "{"msg": "操作成功"}"
|
|
// @Failure 400 {string} json "{"msg": "操作失败"}"
|
|
// @Router /api/web/admin/ads/del [delete]
|
|
func Delete(ctx *gin.Context) {
|
|
manager, err := common.GetAdminAct(ctx)
|
|
if err != nil {
|
|
common.ServeJSON(ctx, stderr.AdminIDErr, err.Error())
|
|
return
|
|
}
|
|
var args struct {
|
|
ID primitive.ObjectID `form:"id" json:"id" binding:"required"`
|
|
ArID *primitive.ObjectID `form:"arID" json:"arID" binding:""`
|
|
}
|
|
if err = ctx.ShouldBind(&args); err != nil {
|
|
common.ServeJSON(ctx, stderr.ErrParamError, "adsctrl Delete arg error "+err.Error())
|
|
return
|
|
}
|
|
if err = adser.DeleteAd(&args.ID, args.ArID); err != nil {
|
|
switch err.(type) {
|
|
case adreviewmod.ADReviewNotExistError:
|
|
common.ServeJSON(ctx, stderr.ADReviewNotExist, err.Error())
|
|
case adsmod.ADSNotExistError:
|
|
common.ServeJSON(ctx, stderr.ADSNotExist, err.Error())
|
|
default:
|
|
common.ServeJSON(ctx, stderr.Failure, err.Error())
|
|
}
|
|
return
|
|
}
|
|
log, _ := json.Marshal(args)
|
|
_ = operatorlgmod.RecordOperation(manager, constant.AdsManageAdsList, constant.Delete, string(log), ctx.Request.URL.RequestURI())
|
|
common.ServeJSON(ctx, stderr.Success, nil)
|
|
}
|
|
|
|
// AddSysAds doc
|
|
// @Summary 增加一条广告
|
|
// @Description 增加一条广告
|
|
// @Tags vid
|
|
// @Accept mpfd,json
|
|
// @Produce json,html
|
|
// @Param title formData string false "广告标题"
|
|
// @Param cover formData string false "广告封面图"
|
|
// @Param sortCode formData integer false "排序号"
|
|
// @Param type formData integer false "广告类型"
|
|
// @Param href formData string false "广告跳转地址"
|
|
// @Param position formData integer false "广告位置"
|
|
// @Param active formData bool false "激活状态"
|
|
// @Param remark formData string false "备注"
|
|
// @Success 200 {string} json "{"msg": "操作成功"}"
|
|
// @Failure 400 {string} json "{"msg": "操作失败"}"
|
|
// @Router /api/web/admin/ads/add [post]
|
|
func Add(ctx *gin.Context) {
|
|
manager, err := common.GetAdminAct(ctx)
|
|
if err != nil {
|
|
common.ServeJSON(ctx, stderr.AdminIDErr, err.Error())
|
|
return
|
|
}
|
|
var args struct {
|
|
Title string `form:"title" json:"title" binding:"required"` //广告标题
|
|
Cover string `form:"cover" json:"cover" binding:"required"` //广告封面图
|
|
HrefType *adsmod.URLJumpType `form:"hrefType" json:"hrefType" binding:"required"` //广告跳转类型,
|
|
Href *string `form:"href" json:"href" binding:"required"` //广告跳转地址
|
|
Position *adsmod.AdPosition `form:"position" json:"position" binding:"required"` //广告位置
|
|
Active *bool `form:"active" json:"active" binding:"required"` //激活状态
|
|
SortCode *int `form:"sortCode" json:"sortCode" binding:"required"` //排序号
|
|
Remark string `form:"remark" json:"remark" binding:""` //备注
|
|
Start *time.Time `form:"start" json:"start" binding:""` //开始时间
|
|
End *time.Time `form:"end" json:"end" binding:""` //结束时间
|
|
}
|
|
if err = ctx.ShouldBind(&args); err != nil {
|
|
common.ServeJSON(ctx, stderr.ErrParamError, err)
|
|
return
|
|
}
|
|
if *args.Href != "" && !ysurl.IsValidURL(*args.HrefType, *args.Href) {
|
|
common.ServeJSON(ctx, stderr.URLInvalid, "")
|
|
return
|
|
}
|
|
doc := adsmod.InsertDoc{
|
|
AdsType: adsmod.SysAds,
|
|
Cover: args.Cover,
|
|
Title: args.Title,
|
|
HrefType: *args.HrefType,
|
|
Href: *args.Href,
|
|
Position: *args.Position,
|
|
SortCode: *args.SortCode,
|
|
Active: *args.Active,
|
|
Remark: args.Remark,
|
|
Start: *args.Start,
|
|
End: *args.End,
|
|
}
|
|
prop := common.GetPictureProp(common.ImgUrlHost + doc.Cover)
|
|
doc.CoverProp = prop
|
|
if _, err = adsmod.InsertOne(nil, doc); err != nil {
|
|
common.ServeJSON(ctx, stderr.Failure, fmt.Errorf("adsctrl Add error: %+v", err))
|
|
return
|
|
}
|
|
log, _ := json.Marshal(args)
|
|
_ = operatorlgmod.RecordOperation(manager, constant.AdsManageAdsList, constant.Add, string(log), ctx.Request.URL.RequestURI())
|
|
common.ServeJSON(ctx, stderr.Success, nil)
|
|
}
|