提交 2160b283 编写于 作者: Mr.奇淼('s avatar Mr.奇淼(

二维码模式更换

上级 6ff5cfe5
package v1 package v1
import ( import (
"gin-vue-admin/global" "fmt"
"gin-vue-admin/global/response" "gin-vue-admin/global/response"
resp "gin-vue-admin/model/response" resp "gin-vue-admin/model/response"
"gin-vue-admin/utils"
"github.com/dchest/captcha"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/mojocn/base64Captcha"
) )
var store = base64Captcha.DefaultMemStore
// @Tags base // @Tags base
// @Summary 生成验证码 // @Summary 生成验证码
// @Security ApiKeyAuth // @Security ApiKeyAuth
...@@ -17,20 +18,17 @@ import ( ...@@ -17,20 +18,17 @@ import (
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}" // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /base/captcha [post] // @Router /base/captcha [post]
func Captcha(c *gin.Context) { func Captcha(c *gin.Context) {
captchaId := captcha.NewLen(global.GVA_CONFIG.Captcha.KeyLong) //字符,公式,验证码配置
response.OkDetailed(resp.SysCaptchaResponse{ // 生成默认数字的driver
CaptchaId: captchaId, driver := base64Captcha.NewDriverDigit(80, 240, 5, 0.7, 80)
PicPath: "/base/captcha/" + captchaId + ".png", cp := base64Captcha.NewCaptcha(driver, store)
}, "验证码获取成功", c) id, b64s, err := cp.Generate()
} if err != nil {
response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
// @Tags base } else {
// @Summary 生成验证码图片路径 response.OkDetailed(resp.SysCaptchaResponse{
// @Security ApiKeyAuth CaptchaId: id,
// @accept application/json PicPath: b64s,
// @Produce application/json }, "验证码获取成功", c)
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}" }
// @Router /base/captcha/:captchaId [get]
func CaptchaImg(c *gin.Context) {
utils.GinCaptchaServeHTTP(c.Writer, c.Request)
} }
...@@ -10,7 +10,6 @@ import ( ...@@ -10,7 +10,6 @@ import (
resp "gin-vue-admin/model/response" resp "gin-vue-admin/model/response"
"gin-vue-admin/service" "gin-vue-admin/service"
"gin-vue-admin/utils" "gin-vue-admin/utils"
"github.com/dchest/captcha"
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-redis/redis" "github.com/go-redis/redis"
...@@ -67,7 +66,7 @@ func Login(c *gin.Context) { ...@@ -67,7 +66,7 @@ func Login(c *gin.Context) {
response.FailWithMessage(UserVerifyErr.Error(), c) response.FailWithMessage(UserVerifyErr.Error(), c)
return return
} }
if captcha.VerifyString(L.CaptchaId, L.Captcha) { if store.Verify(L.CaptchaId, L.Captcha, true) {
U := &model.SysUser{Username: L.Username, Password: L.Password} U := &model.SysUser{Username: L.Username, Password: L.Password}
if err, user := service.Login(U); err != nil { if err, user := service.Login(U); err != nil {
response.FailWithMessage(fmt.Sprintf("用户名密码错误或%v", err), c) response.FailWithMessage(fmt.Sprintf("用户名密码错误或%v", err), c)
......
...@@ -26,6 +26,7 @@ require ( ...@@ -26,6 +26,7 @@ require (
github.com/lib/pq v1.3.0 // indirect github.com/lib/pq v1.3.0 // indirect
github.com/mailru/easyjson v0.7.1 // indirect github.com/mailru/easyjson v0.7.1 // indirect
github.com/mitchellh/mapstructure v1.2.2 // indirect github.com/mitchellh/mapstructure v1.2.2 // indirect
github.com/mojocn/base64Captcha v1.3.1
github.com/onsi/ginkgo v1.7.0 // indirect github.com/onsi/ginkgo v1.7.0 // indirect
github.com/onsi/gomega v1.4.3 // indirect github.com/onsi/gomega v1.4.3 // indirect
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
......
...@@ -11,7 +11,6 @@ func InitBaseRouter(Router *gin.RouterGroup) (R gin.IRoutes) { ...@@ -11,7 +11,6 @@ func InitBaseRouter(Router *gin.RouterGroup) (R gin.IRoutes) {
BaseRouter.POST("register", v1.Register) BaseRouter.POST("register", v1.Register)
BaseRouter.POST("login", v1.Login) BaseRouter.POST("login", v1.Login)
BaseRouter.POST("captcha", v1.Captcha) BaseRouter.POST("captcha", v1.Captcha)
BaseRouter.GET("captcha/:captchaId", v1.CaptchaImg)
} }
return BaseRouter return BaseRouter
} }
package utils
import (
"bytes"
"fmt"
"gin-vue-admin/global"
"github.com/dchest/captcha"
"net/http"
"path"
"strings"
"time"
)
// 这里需要自行实现captcha 的gin模式
func GinCaptchaServeHTTP(w http.ResponseWriter, r *http.Request) {
dir, file := path.Split(r.URL.Path)
ext := path.Ext(file)
id := file[:len(file)-len(ext)]
if ext == "" || id == "" {
http.NotFound(w, r)
return
}
fmt.Println("reload : " + r.FormValue("reload"))
if r.FormValue("reload") != "" {
captcha.Reload(id)
}
lang := strings.ToLower(r.FormValue("lang"))
download := path.Base(dir) == "download"
if Serve(w, r, id, ext, lang, download, global.GVA_CONFIG.Captcha.ImgWidth, global.GVA_CONFIG.Captcha.ImgHeight) == captcha.ErrNotFound {
http.NotFound(w, r)
}
}
func Serve(w http.ResponseWriter, r *http.Request, id, ext, lang string, download bool, width, height int) error {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
var content bytes.Buffer
switch ext {
case ".png":
w.Header().Set("Content-Type", "image/png")
_ = captcha.WriteImage(&content, id, width, height)
case ".wav":
w.Header().Set("Content-Type", "audio/x-wav")
_ = captcha.WriteAudio(&content, id, lang)
default:
return captcha.ErrNotFound
}
if download {
w.Header().Set("Content-Type", "application/octet-stream")
}
http.ServeContent(w, r, id+ext, time.Time{}, bytes.NewReader(content.Bytes()))
return nil
}
...@@ -52,7 +52,9 @@ ...@@ -52,7 +52,9 @@
<div class="vPic"> <div class="vPic">
<img <img
v-if="picPath" v-if="picPath"
:src="path + picPath" :src="picPath"
width="100%"
height="100%"
alt="请输入验证码" alt="请输入验证码"
@click="loginVefify()" @click="loginVefify()"
/> />
...@@ -92,7 +94,6 @@ ...@@ -92,7 +94,6 @@
<script> <script>
import { mapActions } from "vuex"; import { mapActions } from "vuex";
import { captcha } from "@/api/user"; import { captcha } from "@/api/user";
const path = process.env.VUE_APP_BASE_API;
export default { export default {
name: "Login", name: "Login",
data() { data() {
...@@ -123,7 +124,6 @@ export default { ...@@ -123,7 +124,6 @@ export default {
username: [{ validator: checkUsername, trigger: "blur" }], username: [{ validator: checkUsername, trigger: "blur" }],
password: [{ validator: checkPassword, trigger: "blur" }], password: [{ validator: checkPassword, trigger: "blur" }],
}, },
path: path,
logVerify: "", logVerify: "",
picPath: "", picPath: "",
}; };
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册