You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.5 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package system
import (
"pychr/plugin/upyun"
"time"
"github.com/gin-gonic/gin"
"github.com/mojocn/base64Captcha"
"go.uber.org/zap"
"pychr/global"
"pychr/model/common/response"
systemRes "pychr/model/system/response"
)
// 当开启多服务器部署时替换下面的配置使用redis共享存储验证码
// var store = captcha.NewDefaultRedisStore()
var store = base64Captcha.DefaultMemStore
type BaseApi struct{}
// Captcha
// @Tags Base
// @Summary 生成验证码
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=systemRes.SysCaptchaResponse,msg=string} "生成验证码,返回包括随机数id,base64,验证码长度,是否开启验证码"
// @Router /base/captcha [post]
func (b *BaseApi) Captcha(c *gin.Context) {
// 判断验证码是否开启
openCaptcha := global.GVA_CONFIG.Captcha.OpenCaptcha // 是否开启防爆次数
openCaptchaTimeOut := global.GVA_CONFIG.Captcha.OpenCaptchaTimeOut // 缓存超时时间
key := c.ClientIP()
v, ok := global.BlackCache.Get(key)
if !ok {
global.BlackCache.Set(key, 1, time.Second*time.Duration(openCaptchaTimeOut))
}
var oc bool
if openCaptcha == 0 || openCaptcha < interfaceToInt(v) {
oc = true
}
// 字符,公式,验证码配置
// 生成默认数字的driver
driver := base64Captcha.NewDriverDigit(global.GVA_CONFIG.Captcha.ImgHeight, global.GVA_CONFIG.Captcha.ImgWidth, global.GVA_CONFIG.Captcha.KeyLong, 0.7, 80)
// cp := base64Captcha.NewCaptcha(driver, store.UseWithCtx(c)) // v8下使用redis
cp := base64Captcha.NewCaptcha(driver, store)
id, b64s, answer, err := cp.Generate()
if err != nil {
global.GVA_LOG.Error("验证码获取失败!", zap.Error(err))
response.FailWithMessage("验证码获取失败", c)
return
}
global.GVA_LOG.Debug(answer)
response.OkWithDetailed(systemRes.SysCaptchaResponse{
CaptchaId: id,
PicPath: b64s,
CaptchaLength: global.GVA_CONFIG.Captcha.KeyLong,
OpenCaptcha: oc,
}, "验证码获取成功", c)
}
type GetCDNTokenQuery struct {
Method string `form:"method"`
SaveKey string `form:"saveKey"`
}
type GetCDNTokenData struct {
Authorization string `json:"authorization"`
Bucket string `json:"bucket"`
Method string `json:"method"`
Policy string `json:"policy"`
Expiration int64 `json:"expiration"`
}
// @Tags CDN
// @Security ApiKeyAuth
// @Summary 获取CDN操作令牌
// @Produce json
// @Param method query int false "方法"
// @Param saveKey query int false "保存关键字"
// @Success 200 {object} structure.GetLoginInformationData{}
// @Router /base/cdn/token [get]
func (b *BaseApi) GetCDNToken(c *gin.Context) {
// method := c.Query("method")
// saveKey := c.Query("saveKey")
var (
q = GetCDNTokenQuery{}
data = GetCDNTokenData{}
token upyun.FormAPIToken
)
err := c.ShouldBindQuery(&q)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
token, err = upyun.GetUpyunToken(q.Method, q.SaveKey)
if err != nil {
global.GVA_LOG.Error("获取失败!", zap.Error(err))
response.FailWithMessage("获取失败", c)
return
} else {
data.Authorization = token.Authorization
data.Bucket = token.Bucket
data.Method = token.Method
data.Policy = token.Policy
data.Expiration = token.XUpYunExpire
response.OkWithData(data, c)
}
}
// 类型转换
func interfaceToInt(v interface{}) (i int) {
switch v := v.(type) {
case int:
i = v
default:
i = 0
}
return
}