modify api limit auth

master
jager 3 years ago
parent 49037549cf
commit 8c8a544997

@ -19,8 +19,6 @@ import (
"idata/internal/service/user"
"github.com/gin-gonic/gin"
"github.com/jageros/hawox/errcode"
"github.com/jageros/hawox/httpx"
)
func Registry(engine *gin.Engine) {
@ -32,20 +30,7 @@ func Registry(engine *gin.Engine) {
ur.GET("/secret", jwt.CheckToken, user.Secret)
ur.GET("/open", jwt.CheckToken, user.OpenApi)
apiR := r.Group("/api", auth)
apiR := r.Group("/api")
apiR.GET("/soup", soup.GetRandSoup)
apiR.GET("/almanac", almanac.Almanac)
}
func auth(ctx *gin.Context) {
uid, ok1 := httpx.DecodeUrlVal(ctx, "uid")
secret, ok2 := httpx.DecodeUrlVal(ctx, "secret")
if !ok1 || !ok2 {
httpx.ErrInterrupt(ctx, errcode.InvalidParam)
return
}
if !user.Auth(uid, secret) {
httpx.ErrInterrupt(ctx, errcode.VerifyErr)
return
}
}

@ -17,10 +17,10 @@ import "strconv"
type ApiType int64
const (
Soup ApiType = 1 << iota
Weather
Obscenity
Astro
Soup ApiType = 1 << iota // 1鸡汤句子
Weather // 2天气
Obscenity // 3脏词检测
Almanac // 4黄历
)
func Type(ty string) ApiType {
@ -35,8 +35,8 @@ func Type(ty string) ApiType {
return Weather
case "obscenity":
return Obscenity
case "astro":
return Astro
case "almanac":
return Almanac
}
return ApiType(0)
}

@ -20,31 +20,13 @@ import (
"idata/internal/apity"
"idata/internal/consts"
"idata/internal/service/user"
"idata/internal/types"
"strconv"
"strings"
"time"
)
func Almanac(ctx *gin.Context) {
uid, ok := httpx.DecodeUrlVal(ctx, "uid")
if !ok {
return
}
var opened bool
err := user.OperateSync(uid, func(u types.RWUser) {
opened = u.ApiCanReq(apity.Astro)
if opened && !u.IsVip() {
u.SetScore(u.Score() - 1)
}
})
if err != nil {
httpx.ErrInterrupt(ctx, errcode.WithErrcode(1, err))
return
}
if !opened {
httpx.ErrInterrupt(ctx, errcode.New(1, "未获得此Api权限"))
if !user.Auth(ctx, apity.Almanac) {
return
}
date, ok := httpx.DecodeUrlVal(ctx, "date")
@ -60,6 +42,10 @@ func Almanac(ctx *gin.Context) {
}
}
dd := astro.GetDate(strs[0])
if dd == nil {
httpx.ErrInterrupt(ctx, errcode.InvalidParam)
return
}
data, err := dd.Map(hour)
if err != nil {
httpx.ErrInterrupt(ctx, errcode.WithErrcode(2, err))

@ -14,31 +14,13 @@ package soup
import (
"github.com/gin-gonic/gin"
"github.com/jageros/hawox/errcode"
"github.com/jageros/hawox/httpx"
"idata/internal/apity"
"idata/internal/service/user"
"idata/internal/types"
)
func GetRandSoup(ctx *gin.Context) {
uid, ok := httpx.DecodeUrlVal(ctx, "uid")
if !ok {
return
}
var opened bool
err := user.OperateSync(uid, func(u types.RWUser) {
opened = u.ApiCanReq(apity.Soup)
if opened && !u.IsVip() {
u.SetScore(u.Score() - 1)
}
})
if err != nil {
httpx.ErrInterrupt(ctx, errcode.WithErrcode(1, err))
return
}
if !opened {
httpx.ErrInterrupt(ctx, errcode.New(1, "未获得此Api权限"))
if !user.Auth(ctx, apity.Soup) {
return
}
text := getContent()

@ -164,12 +164,29 @@ func OpenApi(ctx *gin.Context) {
httpx.PkgMsgWrite(ctx, nil)
}
func Auth(uid, secret string) bool {
var ok bool
err := Operate(uid, func(u types.RUser) {
if u.Secret() == secret {
ok = true
func Auth(ctx *gin.Context, ty apity.ApiType) bool {
uid, ok1 := httpx.DecodeUrlVal(ctx, "uid")
secret, ok2 := httpx.DecodeUrlVal(ctx, "secret")
if !ok1 || !ok2 {
return false
}
var opened bool
err := OperateSync(uid, func(u types.RWUser) {
if u.Secret() != secret {
return
}
opened = u.ApiCanReq(ty)
if opened && !u.IsVip() {
u.SetScore(u.Score() - 1)
}
})
return err == nil && ok
if err != nil {
httpx.ErrInterrupt(ctx, errcode.WithErrcode(-1, err))
return false
}
if !opened {
httpx.ErrInterrupt(ctx, errcode.VerifyErr)
}
return opened
}

Loading…
Cancel
Save