mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-09-17 08:46:39 +08:00
添加验证码选择,可自由选择字符或计算
This commit is contained in:
parent
c377b6b92d
commit
6cb0fcfd93
6
server/internal/consts/captcha.go
Normal file
6
server/internal/consts/captcha.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package consts
|
||||||
|
|
||||||
|
const (
|
||||||
|
CaptchaTypeString = iota + 1 // 字符串
|
||||||
|
CaptchaTypeMath // 数字计算
|
||||||
|
)
|
@ -95,7 +95,11 @@ func (c *cSite) LoginConfig(ctx context.Context, _ *common.SiteLoginConfigReq) (
|
|||||||
|
|
||||||
// Captcha 登录验证码
|
// Captcha 登录验证码
|
||||||
func (c *cSite) Captcha(ctx context.Context, _ *common.LoginCaptchaReq) (res *common.LoginCaptchaRes, err error) {
|
func (c *cSite) Captcha(ctx context.Context, _ *common.LoginCaptchaReq) (res *common.LoginCaptchaRes, err error) {
|
||||||
cid, base64 := captcha.Generate(ctx)
|
loginConf, err := service.SysConfig().GetLogin(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cid, base64 := captcha.Generate(ctx, loginConf.CaptchaType)
|
||||||
res = &common.LoginCaptchaRes{Cid: cid, Base64: base64}
|
res = &common.LoginCaptchaRes{Cid: cid, Base64: base64}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ package captcha
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"hotgo/internal/consts"
|
||||||
"image/color"
|
"image/color"
|
||||||
|
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
@ -18,44 +19,52 @@ import (
|
|||||||
var store = base64Captcha.DefaultMemStore
|
var store = base64Captcha.DefaultMemStore
|
||||||
|
|
||||||
// Generate 生成验证码
|
// Generate 生成验证码
|
||||||
func Generate(ctx context.Context) (id string, base64 string) {
|
func Generate(ctx context.Context, captchaType int) (id string, base64 string) {
|
||||||
// 字符
|
var err error
|
||||||
// driver := &base64Captcha.DriverString{
|
|
||||||
// Height: 42,
|
|
||||||
// Width: 100,
|
|
||||||
// //NoiseCount: 50,
|
|
||||||
// //ShowLineOptions: 20,
|
|
||||||
// Length: 4,
|
|
||||||
// BgColor: &color.RGBA{
|
|
||||||
// R: 255,
|
|
||||||
// G: 250,
|
|
||||||
// B: 250,
|
|
||||||
// A: 250,
|
|
||||||
// },
|
|
||||||
// Source: "0123456789", // abcdefghjkmnpqrstuvwxyz23456789
|
|
||||||
// Fonts: []string{"chromohv.ttf"},
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
switch captchaType {
|
||||||
// 算数
|
// 算数
|
||||||
driver := &base64Captcha.DriverMath{
|
case consts.CaptchaTypeMath:
|
||||||
Height: 42,
|
driver := &base64Captcha.DriverMath{
|
||||||
Width: 100,
|
Height: 42,
|
||||||
NoiseCount: 0,
|
Width: 100,
|
||||||
ShowLineOptions: 0,
|
NoiseCount: 0,
|
||||||
BgColor: &color.RGBA{
|
ShowLineOptions: 0,
|
||||||
R: 255,
|
BgColor: &color.RGBA{
|
||||||
G: 250,
|
R: 255,
|
||||||
B: 250,
|
G: 250,
|
||||||
A: 250,
|
B: 250,
|
||||||
},
|
A: 250,
|
||||||
Fonts: []string{"chromohv.ttf"},
|
},
|
||||||
|
Fonts: []string{"chromohv.ttf"},
|
||||||
|
}
|
||||||
|
c := base64Captcha.NewCaptcha(driver.ConvertFonts(), store)
|
||||||
|
id, base64, _, err = c.Generate()
|
||||||
|
// 字符
|
||||||
|
default:
|
||||||
|
driver := &base64Captcha.DriverString{
|
||||||
|
Height: 42,
|
||||||
|
Width: 100,
|
||||||
|
//NoiseCount: 50,
|
||||||
|
//ShowLineOptions: 20,
|
||||||
|
Length: 4,
|
||||||
|
BgColor: &color.RGBA{
|
||||||
|
R: 255,
|
||||||
|
G: 250,
|
||||||
|
B: 250,
|
||||||
|
A: 250,
|
||||||
|
},
|
||||||
|
Source: "abcdefghjkmnpqrstuvwxyz23456789", // abcdefghjkmnpqrstuvwxyz23456789
|
||||||
|
Fonts: []string{"chromohv.ttf"},
|
||||||
|
}
|
||||||
|
c := base64Captcha.NewCaptcha(driver.ConvertFonts(), store)
|
||||||
|
id, base64, _, err = c.Generate()
|
||||||
}
|
}
|
||||||
|
|
||||||
c := base64Captcha.NewCaptcha(driver.ConvertFonts(), store)
|
|
||||||
id, base64, _, err := c.Generate()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
g.Log().Errorf(ctx, "captcha.Generate err:%+v", err)
|
g.Log().Errorf(ctx, "captcha.Generate err:%+v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,6 +176,7 @@ type WechatConfig struct {
|
|||||||
type LoginConfig struct {
|
type LoginConfig struct {
|
||||||
RegisterSwitch int `json:"loginRegisterSwitch"`
|
RegisterSwitch int `json:"loginRegisterSwitch"`
|
||||||
CaptchaSwitch int `json:"loginCaptchaSwitch"`
|
CaptchaSwitch int `json:"loginCaptchaSwitch"`
|
||||||
|
CaptchaType int `json:"loginCaptchaType"`
|
||||||
Avatar string `json:"loginAvatar"`
|
Avatar string `json:"loginAvatar"`
|
||||||
RoleId int64 `json:"loginRoleId"`
|
RoleId int64 `json:"loginRoleId"`
|
||||||
DeptId int64 `json:"loginDeptId"`
|
DeptId int64 `json:"loginDeptId"`
|
||||||
|
Loading…
Reference in New Issue
Block a user