[2024-07-12](UPDATE): 用户登录后台接口

This commit is contained in:
june 2024-07-12 20:34:06 +08:00
parent 90b4e437fe
commit 35cb1f9025
14 changed files with 633 additions and 0 deletions

51
.air.toml Normal file
View File

@ -0,0 +1,51 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ."
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = "./tmp/main picgo ./deploy/picgo-dev.yml"
include_dir = []
include_ext = ["go", "html", "js", "css", "yml"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false
[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
[log]
main_only = false
time = false
[misc]
clean_on_exit = false
[proxy]
app_port = 0
enabled = false
proxy_port = 0
[screen]
clear_on_rebuild = false
keep_scroll = true

2
.gitignore vendored
View File

@ -220,3 +220,5 @@ $RECYCLE.BIN/
*.lnk
.idea
tmp

8
configs/captcha.go Normal file
View File

@ -0,0 +1,8 @@
package configs
type Captcha struct {
Expire int `yaml:"expire"`
Width int `yaml:"width"`
Hight int `yaml:"hight"`
Length int `yaml:"length"`
}

7
configs/mysql.go Normal file
View File

@ -0,0 +1,7 @@
package configs
type Mysql struct {
MysqlDNS string `yaml:"mysql_dns"`
MaxIdleConns int `yaml:"max_idle_conns"`
MaxOpenConns int `yaml:"max_open_conns"`
}

9
configs/redis.go Normal file
View File

@ -0,0 +1,9 @@
package configs
type Redis struct {
Addr string `yaml:"addr"`
Password string `yaml:"password"`
DB int `yaml:"db"`
PoolSize int `yaml:"pool_size"`
MinidleConns int `yaml:"minidle_conns"`
}

14
configs/server.go Normal file
View File

@ -0,0 +1,14 @@
package configs
type Server struct {
Port int64 `yaml:"port"`
Host string `yaml:"host"`
NodeId int64 `yaml:"node_id"`
LogPath string `yaml:"log_path"`
Environment string `yaml:"environment"`
StaticPath string `yaml:"static_path"`
UploadPath string `yaml:"upload_path"`
UploadMaxSize int64 `yaml:"upload_max_size"`
SessionsKey string `yaml:"sessions_key"`
SessionName string `yaml:"session_name"`
}

View File

@ -0,0 +1,68 @@
package captcha
import (
"image"
"os"
"path/filepath"
"picgo/configs"
"picgo/corelib"
"strings"
)
// GenerateCaptcha 图形验证码
func GenerateCaptcha(cid string) (img *image.RGBA, err error) {
// 字体路径
dir, err := os.Getwd()
if err != nil {
return
}
var fontPath = filepath.Join(dir, "corelib", "captcha", "fonts")
// 随机字体
var fontNames = []string{"DENNEthree-dee", "3Dumb"}
fontName := RandChooseOne(fontNames)
// 随机模式
var modes = []int{0, 1}
mode := RandChooseOne(modes)
// 生成图片验证码
captchaLen := configs.Settings.Captcha.Length
w := configs.Settings.Captcha.Width
h := configs.Settings.Captcha.Hight
cp := NewCaptcha(w, h, captchaLen)
cp.SetFontPath(fontPath) //指定字体目录
cp.SetFontName(fontName) //指定字体名字
cp.SetMode(mode) //1设置为简单的数学算术运算公式 其他为普通字符串
code, img := cp.OutPut()
// 备注code 存储到redis并在使用时取出验证
corelib.Logger.Infoln("图片验证码: ", code)
// 配置文件中获取过期时间
exp := configs.Settings.Captcha.Expire
// 缓存生成的验证码
if err = SetCode(cid, code, int64(exp)); err != nil {
return
}
//core.Logger.Infoln("验证结果:", Verify(cid, code))
return
}
// Verify 验证
func Verify(id string, code string) (ok bool) {
ok = false
var (
err error
redisCode string
)
// 验证字符串是否合法
if code == "" || !corelib.IsCaptcha(code) {
return
}
code = strings.ToLower(code)
// redis取验证码
if redisCode, err = GetCode(id); err != nil {
return
}
// redis存储验证码和用户输入验证码对比
if strings.ToLower(redisCode) == code {
return true
}
return
}

Binary file not shown.

Binary file not shown.

339
corelib/captcha/image.go Normal file
View File

@ -0,0 +1,339 @@
package captcha
import (
"crypto/rand"
"github.com/golang/freetype"
"github.com/llgcode/draw2d"
"github.com/llgcode/draw2d/draw2dimg"
"image"
"image/color"
"log"
"math"
"math/big"
"os"
"path/filepath"
"strconv"
)
const (
chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
operator = "+-*/"
defaultLen = 4
defaultFontSize = 20
defaultDpi = 72
)
// Captcha 图形验证码 使用字体默认ttf格式
// w 图片宽度, h图片高度CodeLen验证码的个数
// FontSize 字体大小, Dpi 清晰度FontPath 字体目录, FontName 字体名字
// mode 验证模式 0普通字符串110以内简单数学公式
type Captcha struct {
W, H, CodeLen int
FontSize float64
Dpi int
FontPath, FontName string
mode int
}
// NewCaptcha 实例化验证码
func NewCaptcha(w, h, CodeLen int) *Captcha {
return &Captcha{W: w, H: h, CodeLen: CodeLen}
}
// OutPut 输出
func (captcha *Captcha) OutPut() (string, *image.RGBA) {
img := captcha.initCanvas()
return captcha.doImage(img)
}
// RangeRand 获取区间[-m, n]的随机数
func (captcha *Captcha) RangeRand(min, max int64) int64 {
if min > max {
panic("the min is greater than max!")
}
if min < 0 {
f64Min := math.Abs(float64(min))
i64Min := int64(f64Min)
result, _ := rand.Int(rand.Reader, big.NewInt(max+1+i64Min))
return result.Int64() - i64Min
} else {
result, _ := rand.Int(rand.Reader, big.NewInt(max-min+1))
return min + result.Int64()
}
}
// 随机字符串
func (captcha *Captcha) getRandCode() string {
if captcha.CodeLen <= 0 {
captcha.CodeLen = defaultLen
}
var code = ""
for l := 0; l < captcha.CodeLen; l++ {
charsPos := captcha.RangeRand(0, int64(len(chars)-1))
code += string(chars[charsPos])
}
return code
}
// 获取算术运算公式
func (captcha *Captcha) getFormulaMixData() (string, []string) {
num1 := int(captcha.RangeRand(11, 20))
num2 := int(captcha.RangeRand(1, 10))
opArr := []rune(operator)
opRand := opArr[captcha.RangeRand(0, 3)]
strNum1 := strconv.Itoa(num1)
strNum2 := strconv.Itoa(num2)
var ret int
var opRet string
switch string(opRand) {
case "+":
ret = num1 + num2
opRet = "+"
case "-":
if num1-num2 > 0 {
ret = num1 - num2
opRet = "-"
} else {
ret = num1 + num2
opRet = "+"
}
case "*":
if num1*num2 < 100 {
ret = num1 * num2
//opRet = "×"
opRet = "×"
} else {
ret = num1 + num2
opRet = "+"
}
case "/":
if num1%num2 == 0 {
ret = num1 / num2
opRet = "÷"
} else {
ret = num1 + num2
opRet = "+"
}
}
return strconv.Itoa(ret), []string{strNum1, opRet, strNum2, "=", "?"}
}
// 初始化画布
func (captcha *Captcha) initCanvas() *image.RGBA {
dest := image.NewRGBA(image.Rect(0, 0, captcha.W, captcha.H))
// 随机色
r := uint8(255) // uint8(captcha.RangeRand(50, 250))
g := uint8(255) // uint8(captcha.RangeRand(50, 250))
b := uint8(255) // uint8(captcha.RangeRand(50, 250))
// 填充背景色
for x := 0; x < captcha.W; x++ {
for y := 0; y < captcha.H; y++ {
dest.Set(x, y, color.RGBA{R: r, G: g, B: b, A: 255}) //设定alpha图片的透明度
}
}
return dest
}
// 处理图像
func (captcha *Captcha) doImage(dest *image.RGBA) (string, *image.RGBA) {
gc := draw2dimg.NewGraphicContext(dest)
defer gc.Close()
defer gc.FillStroke()
captcha.setFont(gc)
captcha.doPoint(gc)
captcha.doLine(gc)
captcha.doSinLine(gc)
var codeStr string
if captcha.mode == 1 {
ret, formula := captcha.getFormulaMixData()
// log.Println("算数验证码: ", formula)
codeStr = ret
captcha.doFormula(gc, formula)
} else {
codeStr = captcha.getRandCode()
captcha.doCode(gc, codeStr)
}
return codeStr, dest
}
// 验证码字符设置到图像上
func (captcha *Captcha) doCode(gc *draw2dimg.GraphicContext, code string) {
for l := 0; l < len(code); l++ {
y := captcha.RangeRand(int64(captcha.FontSize)-1, int64(captcha.H)+6)
x := captcha.RangeRand(1, 20)
// 随机色
r := uint8(captcha.RangeRand(0, 200))
g := uint8(captcha.RangeRand(0, 200))
b := uint8(captcha.RangeRand(0, 200))
gc.SetFillColor(color.RGBA{R: r, G: g, B: b, A: 255})
gc.FillStringAt(string(code[l]), float64(x)+captcha.FontSize*float64(l), float64(int64(captcha.H)-y)+captcha.FontSize)
gc.Stroke()
}
}
// 验证码字符设置到图像上
func (captcha *Captcha) doFormula(gc *draw2dimg.GraphicContext, formulaArr []string) {
for l := 0; l < len(formulaArr); l++ {
y := captcha.RangeRand(0, 10)
x := captcha.RangeRand(5, 10)
// 随机色
r := uint8(captcha.RangeRand(10, 200))
g := uint8(captcha.RangeRand(10, 200))
b := uint8(captcha.RangeRand(10, 200))
gc.SetFillColor(color.RGBA{R: r, G: g, B: b, A: 255})
gc.FillStringAt(formulaArr[l], float64(x)+captcha.FontSize*float64(l), captcha.FontSize+float64(y))
gc.Stroke()
}
}
// 增加干扰线
func (captcha *Captcha) doLine(gc *draw2dimg.GraphicContext) {
// 设置干扰线
for n := 0; n < 3; n++ {
gc.SetLineWidth(float64(captcha.RangeRand(1, 2)))
//gc.SetLineWidth(1)
// 随机背景色
r := uint8(captcha.RangeRand(0, 255))
g := uint8(captcha.RangeRand(0, 255))
b := uint8(captcha.RangeRand(0, 255))
gc.SetStrokeColor(color.RGBA{R: r, G: g, B: b, A: 255})
// 初始化位置
gc.MoveTo(float64(captcha.RangeRand(0, int64(captcha.W)+10)), float64(captcha.RangeRand(0, int64(captcha.H)+5)))
gc.LineTo(float64(captcha.RangeRand(0, int64(captcha.W)+10)), float64(captcha.RangeRand(0, int64(captcha.H)+5)))
gc.Stroke()
}
}
// 增加干扰点
func (captcha *Captcha) doPoint(gc *draw2dimg.GraphicContext) {
for n := 0; n < 10; n++ {
gc.SetLineWidth(float64(captcha.RangeRand(1, 3)))
// 随机色
r := uint8(captcha.RangeRand(0, 255))
g := uint8(captcha.RangeRand(0, 255))
b := uint8(captcha.RangeRand(0, 255))
gc.SetStrokeColor(color.RGBA{R: r, G: g, B: b, A: 255})
x := captcha.RangeRand(0, int64(captcha.W)+10) + 1
y := captcha.RangeRand(0, int64(captcha.H)+5) + 1
gc.MoveTo(float64(x), float64(y))
gc.LineTo(float64(x+captcha.RangeRand(1, 2)), float64(y+captcha.RangeRand(1, 2)))
gc.Stroke()
}
}
// 增加正弦干扰线
func (captcha *Captcha) doSinLine(gc *draw2dimg.GraphicContext) {
h1 := captcha.RangeRand(-12, 12)
h2 := captcha.RangeRand(-1, 1)
w2 := captcha.RangeRand(5, 20)
h3 := captcha.RangeRand(5, 10)
h := float64(captcha.H)
w := float64(captcha.W)
// 随机色
r := uint8(captcha.RangeRand(128, 255))
g := uint8(captcha.RangeRand(128, 255))
b := uint8(captcha.RangeRand(128, 255))
gc.SetStrokeColor(color.RGBA{R: r, G: g, B: b, A: 255})
gc.SetLineWidth(float64(captcha.RangeRand(1, 2)))
var i float64
for i = -w / 2; i < w/2; i = i + 0.1 {
y := h/float64(h3)*math.Sin(i/float64(w2)) + h/2 + float64(h1)
gc.LineTo(i+w/2, y)
if h2 == 0 {
gc.LineTo(i+w/2, y+float64(h2))
}
}
gc.Stroke()
}
// SetMode 设置模式
func (captcha *Captcha) SetMode(mode int) {
captcha.mode = mode
}
// SetFontPath 设置字体路径
func (captcha *Captcha) SetFontPath(FontPath string) {
captcha.FontPath = FontPath
}
// SetFontName 设置字体名称
func (captcha *Captcha) SetFontName(FontName string) {
captcha.FontName = FontName
}
// SetFontSize 设置字体大小
func (captcha *Captcha) SetFontSize(fontSize float64) {
captcha.FontSize = fontSize
}
// 设置相关字体
func (captcha *Captcha) setFont(gc *draw2dimg.GraphicContext) {
if captcha.FontPath == "" {
panic("the font path is empty!")
}
if captcha.FontName == "" {
panic("the font name is empty!")
}
// 字体文件
fontFile := filepath.Join(captcha.FontPath, captcha.FontName+".ttf")
fontBytes, err := os.ReadFile(fontFile)
if err != nil {
log.Println(err)
return
}
font, err := freetype.ParseFont(fontBytes)
if err != nil {
log.Println(err)
return
}
// 设置自定义字体相关信息
gc.FontCache = draw2d.NewSyncFolderFontCache(captcha.FontPath)
gc.FontCache.Store(draw2d.FontData{Name: captcha.FontName, Family: 0, Style: draw2d.FontStyleNormal}, font)
gc.SetFontData(draw2d.FontData{Name: captcha.FontName, Style: draw2d.FontStyleNormal})
//设置清晰度
if captcha.Dpi <= 0 {
captcha.Dpi = defaultDpi
}
gc.SetDPI(captcha.Dpi)
// 设置字体大小
if captcha.FontSize <= 0 {
captcha.FontSize = defaultFontSize
}
gc.SetFontSize(captcha.FontSize)
}

24
corelib/captcha/random.go Normal file
View File

@ -0,0 +1,24 @@
package captcha
import (
"math/rand"
"time"
)
// RandDigit 随机数
func RandDigit(max int) int {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Intn(max)
}
// RandDigitRange 随机数范围
func RandDigitRange(max int, min int) int {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Intn(max-min) + min
}
// RandChooseOne 数组随机选一个元素
func RandChooseOne[T any](reasons []T) T {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return reasons[r.Intn(len(reasons))]
}

46
corelib/captcha/store.go Normal file
View File

@ -0,0 +1,46 @@
package captcha
import (
"context"
"picgo/corelib"
"time"
)
// Store 存储验证码接口
type Store interface {
// SetCode cid 当前id
// oid 旧id
// code 验证码
// expired 过期时间(毫秒)
// return nil/error
SetCode(cid string, oid string, code string, expired int)
// GetCode cid 当前id
// return code
GetCode(cid string) string
}
// SetCode 验证码保存到redis
func SetCode(cid string, code string, expired int64) (err error) {
ctx := context.Background()
cid = genRedisKey(cid)
if err = corelib.RdbClient.Set(ctx, cid, code, time.Duration(expired)*time.Second).Err(); err != nil {
return
}
return
}
// GetCode 从redis中查询验证码查询之后删除
func GetCode(cid string) (code string, err error) {
ctx := context.Background()
cid = genRedisKey(cid)
if code, err = corelib.RdbClient.Get(ctx, cid).Result(); err != nil {
return
}
//rdb.Del(ctx, cid)
return
}
// captcha缓存key拼接
func genRedisKey(id string) (res string) {
return corelib.AdminCaptchaKey + id
}

52
deploy/picgo-dev.yml Normal file
View File

@ -0,0 +1,52 @@
#------------------------
# ---- 公共配置 start ----
#------------------------
# 服务
server:
port: 8181
host: localhost
node_id: 1
log_path: tmp/admin-server.log
environment: dev
static_path: static
upload_path: static/pic
upload_max_size: 3
sessions_key: eyjhbgcioijiuzi1niisinr5cci6ikpx
session_name: picgo
# redis
redis:
addr: "localhost:6379"
password: Develop_123
db: 3
pool_size: 10
minidle_conns: 5
# 数据库
mysql:
mysql_dns: "root:Mysql_123@tcp(localhost:3306)/picgo?charset=utf8mb4&parseTime=True&loc=Local"
max_idle_conns: 10
max_open_conns: 100
#----------------------
# ---- 公共配置 end ----
#----------------------
#-------------------------------
# ---- 管理后台相关配置 start ----
#-------------------------------
# 验证码
captcha:
# 过期时间(s)
expire: 305000
width: 96
hight: 32
length: 4
# 鉴权
#jwt:
# signing_key: eyjhbgcioijiuzi1niisinr5cci6ikpxvcj9
# expires_time: 7d
# issuer: cnnm
# aes_key: 23624e296707b1eb20311c29969bc46e
# aes_iv: 29ca233cd1f42147
#-----------------------------
# ---- 管理后台相关配置 end ----
#-----------------------------

File diff suppressed because one or more lines are too long