72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
package imctrl
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"91porn-server/app/service/imadser"
|
|
"91porn-server/common"
|
|
"91porn-server/common/enum/imad"
|
|
"91porn-server/common/stderr"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type IMAdReq struct {
|
|
Position string `json:"position"`
|
|
Positions []string `json:"positions"`
|
|
}
|
|
|
|
type IMAdResp struct {
|
|
Groups []imadser.AdPositionGroup `json:"groups"`
|
|
}
|
|
|
|
func GetIMAd(ctx *gin.Context) {
|
|
if _, err := common.GetUID(ctx); err != nil {
|
|
common.ServeJSON(ctx, stderr.ErrNoToken, err)
|
|
return
|
|
}
|
|
|
|
var req IMAdReq
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
common.ServeJSON(ctx, stderr.ErrParamError, err)
|
|
return
|
|
}
|
|
positions := normalizeIMAdPositions(req)
|
|
if len(positions) == 0 {
|
|
common.ServeJSON(ctx, stderr.ErrParamError, nil)
|
|
return
|
|
}
|
|
for _, position := range positions {
|
|
if !imad.IsPositionCode(position) {
|
|
common.ServeJSON(ctx, stderr.ErrParamError, nil)
|
|
return
|
|
}
|
|
}
|
|
|
|
common.ServeJSON(ctx, stderr.Success, &IMAdResp{
|
|
Groups: imadser.GetAdsByPositionCodes(positions),
|
|
})
|
|
}
|
|
|
|
func normalizeIMAdPositions(req IMAdReq) []string {
|
|
positions := make([]string, 0, len(req.Positions)+1)
|
|
if position := strings.TrimSpace(req.Position); position != "" {
|
|
positions = append(positions, position)
|
|
}
|
|
positions = append(positions, req.Positions...)
|
|
seen := make(map[string]struct{}, len(positions))
|
|
normalized := make([]string, 0, len(positions))
|
|
for _, position := range positions {
|
|
position = strings.TrimSpace(position)
|
|
if position == "" {
|
|
continue
|
|
}
|
|
if _, ok := seen[position]; ok {
|
|
continue
|
|
}
|
|
seen[position] = struct{}{}
|
|
normalized = append(normalized, position)
|
|
}
|
|
return normalized
|
|
}
|