| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package services
- import (
- "app/commons/constant"
- "app/commons/core"
- "app/commons/core/redisclient"
- "context"
- "fmt"
- "github.com/demdxx/gocast"
- "github.com/gin-gonic/gin"
- "time"
- )
- type Context struct {
- *gin.Context
- }
- func (ctx *Context) Resp(args ...any) {
- ctx.Header("Content-Type", "application/json; charset=utf-8")
- core.Resp(ctx.Context, args...)
- }
- func (ctx *Context) Fail(message string) {
- ctx.Header("Content-Type", "application/json; charset=utf-8")
- core.Resp(ctx.Context, message)
- }
- func (ctx *Context) UserId() int64 {
- return ctx.GetInt64("userId")
- }
- func (ctx *Context) AdminId() int64 {
- return gocast.ToInt64(ctx.GetHeader("authId"))
- }
- func (ctx *Context) Uid() string {
- return ctx.GetString("uid")
- }
- func (ctx *Context) OpenId() string {
- return ctx.GetString("openId")
- }
- func (ctx *Context) unique(a string) string {
- return fmt.Sprintf("CRC:%s", a)
- }
- func (ctx *Context) RepeatFilter(serviceName string, timeSt time.Duration) bool {
- // 尝试设置KEY
- nxBool, err := redisclient.DefaultClient().SetNX(context.Background(), ctx.unique(serviceName), true, timeSt).Result()
- if err != nil {
- return false
- }
- if !nxBool {
- return false
- }
- // 获取过期时间 -- 可用于验证码获取上 暂五使用场景
- _, err = redisclient.DefaultClient().TTL(context.Background(), ctx.unique(serviceName)).Result()
- if err != nil {
- return false
- }
- return true
- }
- func (ctx *Context) I18n(name string) string {
- lang := ctx.GetHeader(constant.HeaderKeyLanguage)
- if lang == "" {
- lang = ctx.GetHeader(constant.HeaderKeyLang)
- }
- return GetI18nService().Translate(lang, name)
- }
- func (ctx *Context) OK(data any) {
- ctx.Header("Content-Type", "application/json; charset=utf-8")
- core.Resp(ctx.Context, data)
- }
- func (ctx *Context) QueryInt64(key string, defaultVal int64) int64 {
- val := ctx.Query(key)
- if val == "" {
- return defaultVal
- }
- return gocast.ToInt64(val)
- }
- func (ctx *Context) QueryString(key string, defaultVal string) string {
- val := ctx.Query(key)
- if val == "" {
- return defaultVal
- }
- return val
- }
|