Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
107 lines
2.3 KiB
Go
107 lines
2.3 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"videoweb/app/service/authser"
|
|
"videoweb/app/service/playletser"
|
|
"videoweb/pub/common/log"
|
|
"videoweb/pub/common/useragent"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type chapterUnlockBody struct {
|
|
ChapterID int64 `json:"chapterId"`
|
|
PlayletID int64 `json:"playletId"`
|
|
}
|
|
|
|
// ChapterUnlock 代理上游 POST /api/app/playlet/chapter/unlock。
|
|
func ChapterUnlock(c *gin.Context) {
|
|
if useragent.TokenFromRequest(c.Request) == "" {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 401,
|
|
"msg": "unauthorized",
|
|
"tip": "请先登录后再解锁",
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
var body chapterUnlockBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 1001,
|
|
"msg": "参数错误",
|
|
"tip": "参数错误",
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
if body.ChapterID <= 0 || body.PlayletID <= 0 {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 1001,
|
|
"msg": "参数错误",
|
|
"tip": "缺少 chapterId 或 playletId",
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
env, err := playletser.ChapterUnlock(c, playletser.PlayletChapterUnlockReq{
|
|
ChapterID: body.ChapterID,
|
|
PlayletID: body.PlayletID,
|
|
})
|
|
if err != nil {
|
|
log.Error("chapter unlock upstream failed", log.E(err))
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 500,
|
|
"msg": "chapter unlock failed",
|
|
"tip": "解锁失败,请稍后重试",
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
if env != nil && TryJSONSessionRecover(c, env.Code) {
|
|
env, err = playletser.ChapterUnlock(c, playletser.PlayletChapterUnlockReq{
|
|
ChapterID: body.ChapterID,
|
|
PlayletID: body.PlayletID,
|
|
})
|
|
if err != nil {
|
|
log.Error("chapter unlock retry failed", log.E(err))
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 500,
|
|
"msg": "chapter unlock failed",
|
|
"tip": "解锁失败,请稍后重试",
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
}
|
|
if env == nil || env.Code != 200 {
|
|
tip := "解锁失败"
|
|
msg := ""
|
|
code := 500
|
|
if env != nil {
|
|
code = env.Code
|
|
msg = env.Msg
|
|
tip = firstNonEmpty(env.Tip, env.Msg, tip)
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": code,
|
|
"msg": msg,
|
|
"tip": tip,
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
authser.SetRechargeCookie(c, env.Data.Balance)
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"msg": firstNonEmpty(env.Msg, "success"),
|
|
"tip": firstNonEmpty(env.Tip, "解锁成功"),
|
|
"data": env.Data,
|
|
})
|
|
}
|