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

160 lines
4.1 KiB

package LoginController
import (
"dsSdsf/Business/Login/CaptchaController"
"dsSdsf/Business/Login/LoginDao"
"dsSdsf/Utils/CommonUtil"
"dsSdsf/Utils/RedisUtil"
"dsSdsf/Utils/RsaUtil"
"encoding/base64"
"github.com/dchest/captcha"
"github.com/gin-gonic/gin"
"net/http"
)
//模块的路由配置
func Routers(r *gin.RouterGroup) {
rr := r.Group("/login")
rr.GET("/test", test)
rr.GET("/test1", test1)
rr.GET("/testSql", testSql)
rr.GET("/getCaptchaId", getCaptchaId)
rr.GET("/getCaptchaPng", getCaptchaPng)
rr.GET("/verifyCaptcha", verifyCaptcha)
rr.POST("/doLogin", doLogin)
return
}
func test(c *gin.Context) {
b := "eWgaeStLsS3Kh/M2oaL3rEzndYZtk5mdnEGyKScOuD40iUnC/a4I0N6gRhNbHRrQplZixM8C0Ng8B3gsy70Abg=="
enb, _ := base64.StdEncoding.DecodeString(b)
decryptPwd, err := RsaUtil.RsaDecrypt(enb)
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "info": "无法解密!"})
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "info": string(decryptPwd)})
}
func test1(c *gin.Context) {
//redirectUri := c.Query("redirect_uri")
token := CommonUtil.GetUUID()
RedisUtil.SET(token, "1", 3000)
c.SetCookie("token", token, 0, "/", "", false, true)
//c.Redirect(http.StatusMovedPermanently, redirectUri)
}
func doLogin(c *gin.Context) {
//用户名
user := c.PostForm("user")
//密码
pwd := c.PostForm("pwd")
//验证码ID
captchaId := c.PostForm("captchaId")
//验证码值
captchaVal := c.PostForm("captchaVal")
if user == "" || pwd == "" {
c.JSON(http.StatusOK, gin.H{"success": false, "info": "用户名或密码不允许为空!"})
return
}
if captchaId == "" || captchaVal == "" {
c.JSON(http.StatusOK, gin.H{"success": false, "info": "验证码不允许为空!"})
return
}
//验证码校验
var redisStore CaptchaController.RedisStore
verifyFlag := redisStore.VerifyString(captchaId, captchaVal)
if !verifyFlag {
c.JSON(http.StatusOK, gin.H{"success": verifyFlag, "info": "验证码不正确!"})
return
}
//RSA解密密码
base64Pwd, _ := base64.StdEncoding.DecodeString(pwd)
decryptPwdByte, err := RsaUtil.RsaDecrypt(base64Pwd)
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "info": "用户名或密码不正确!"})
return
}
//前台密码经过RSA解密和MD5加密
md5Pwd := CommonUtil.MD5(string(decryptPwdByte))
//校验用户名和密码是否存在
if !LoginDao.GetLoginPwdExists(user, md5Pwd) {
c.JSON(http.StatusOK, gin.H{"success": false, "info": "用户名或密码不正确!"})
return
}
//生成token
token := CommonUtil.GetUUID()
//将token放到redis设置30分钟过期
RedisUtil.SET(token, "1", 1800)
//写cookie
c.SetCookie("token", token, 0, "/", "", false, true)
c.JSON(http.StatusOK, gin.H{"success": true, "info": "登录成功!"})
}
func getCaptchaId(c *gin.Context) {
captcha.SetCustomStore(&CaptchaController.RedisStoreBean)
captchaId := captcha.NewLen(4)
c.JSON(http.StatusOK, gin.H{"success": true, "captchaId": captchaId})
}
func getCaptchaPng(c *gin.Context) {
CaptchaController.ServeHTTP(c.Writer, c.Request)
}
func verifyCaptcha(c *gin.Context) {
captchaId := c.Query("captchaId")
captchaVal := c.Query("captchaVal")
var redisStore CaptchaController.RedisStore
verifyFlag := redisStore.VerifyString(captchaId, captchaVal)
if verifyFlag {
c.JSON(http.StatusOK, gin.H{"success": verifyFlag, "info": "验证成功!"})
} else {
c.JSON(http.StatusOK, gin.H{"success": verifyFlag, "info": "验证失败!"})
}
}
func testSql(c *gin.Context) {
/*
results, err := LoginDao.TestSqlJson()
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "info": "无法解密!"})
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "info": CommonUtil.ConvertJsonStringToMapArray(results)})
*/
/*
record := LoginDao.TestSqlSingle()
c.JSON(http.StatusOK, gin.H{"success": true, "info": record["pct_tel"].String()})
*/
jsonStr := `{"user":"admin","pwd":"dsideal","captchaId":"Av7CyMklC3EBXv4BVevW","captchaVal":"3698"}`
myMap, err := CommonUtil.JsonStringToMap(jsonStr)
c.JSON(http.StatusOK, gin.H{"success": err, "info": myMap["captchaId"]})
}