106 lines
2.9 KiB
Go
106 lines
2.9 KiB
Go
package exchcodectrl
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"91porn-server/app/middleware/authuser"
|
|
"91porn-server/app/service/exchcodeser"
|
|
"91porn-server/common"
|
|
"91porn-server/common/log"
|
|
"91porn-server/common/stderr"
|
|
"91porn-server/models/commod"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// CodeExchange doc
|
|
// @Summary 兑换码兑换
|
|
// @Description 兑换码
|
|
// @Tags ExchangeCode
|
|
// @Accept mpfd,json
|
|
// @Produce json,html
|
|
// @Param code formData string true "兑换码"
|
|
// @Success 200 {string} json "{"msg": "操作成功"}"
|
|
// @Failure 400 {string} json "{"msg": "操作失败"}"
|
|
// @Router /api/app/code/exchange [post]
|
|
func CodeExchange(ctx *gin.Context) {
|
|
uid, err := common.GetUID(ctx)
|
|
if err != nil {
|
|
common.ServeJSON(ctx, stderr.ErrNoToken, nil)
|
|
return
|
|
}
|
|
var param struct {
|
|
Code string `json:"code" binding:"required"`
|
|
}
|
|
if err = ctx.ShouldBind(¶m); err != nil {
|
|
common.ServeJSON(ctx, stderr.ErrParamError, err)
|
|
return
|
|
}
|
|
code, data, err := exchcodeser.CodeExchange(uid, param.Code)
|
|
if err != nil {
|
|
common.ServeJSON(ctx, code, err)
|
|
return
|
|
}
|
|
common.ServeJSON(ctx, code, data)
|
|
}
|
|
|
|
// UserRecord doc
|
|
// @Summary 查询用户兑换记录
|
|
// @Description 查询用户兑换记录
|
|
// @Tags UserRecord
|
|
// @Accept mpfd,json
|
|
// @Produce json,html
|
|
// @Param pageNumber formData int true "页码"
|
|
// @Param pageSize formData int true "每页数据量"
|
|
// @Success 200 {string} json "{"msg": "操作成功"}"
|
|
// @Failure 400 {string} json "{"msg": "操作失败"}"
|
|
// @Router /api/app/code/userRecord [get]
|
|
func UserRecord(ctx *gin.Context) {
|
|
uid, err := common.GetUID(ctx)
|
|
if err != nil {
|
|
common.ServeJSON(ctx, stderr.ErrNoToken, nil)
|
|
return
|
|
}
|
|
var req struct {
|
|
commod.Page
|
|
}
|
|
if err = ctx.ShouldBind(&req); err != nil {
|
|
common.ServeJSON(ctx, stderr.ErrParamError, err)
|
|
return
|
|
}
|
|
code, data, err := exchcodeser.UserExchageRecord(uid, req.Page)
|
|
if err != nil {
|
|
common.ServeJSON(ctx, code, err)
|
|
return
|
|
}
|
|
common.ServeJSON(ctx, code, data)
|
|
}
|
|
|
|
func WebCodeExchange(ctx *gin.Context) {
|
|
token := ctx.Request.Header.Get("X-Authorization") //为兼容H5带APP token
|
|
if token == "" {
|
|
ctx.AbortWithStatusJSON(http.StatusOK, stderr.ErrNoToken.Struct())
|
|
return
|
|
}
|
|
claims, err := authuser.ParseWebClaims(token)
|
|
if err != nil {
|
|
log.Error("activity CodeExchange ParseWebClaims error", log.Any("token", fmt.Sprintf("%+v", token)), log.E(err))
|
|
ctx.JSON(http.StatusBadRequest, stderr.InvalidToken.Struct())
|
|
return
|
|
}
|
|
var param struct {
|
|
Code string `json:"code" binding:"required"`
|
|
}
|
|
if err = ctx.ShouldBind(¶m); err != nil {
|
|
common.ServeJSON(ctx, stderr.ErrParamError, err)
|
|
return
|
|
}
|
|
code, _, err := exchcodeser.CodeExchange(claims.UID, param.Code)
|
|
if err != nil {
|
|
common.ServeJSON(ctx, code, err)
|
|
return
|
|
}
|
|
common.ServeJSON(ctx, code, gin.H{})
|
|
}
|