function.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package utils
  2. import (
  3. "gfast/app/service/user_service"
  4. "gfast/library/response"
  5. "github.com/gogf/gf/crypto/gaes"
  6. "github.com/gogf/gf/encoding/gbase64"
  7. "github.com/gogf/gf/net/ghttp"
  8. "github.com/gogf/gf/os/glog"
  9. "github.com/gogf/gf/util/gvalid"
  10. "github.com/mojocn/base64Captcha"
  11. )
  12. const AESPublicKey = "HqmP1KLMuz09Q0Bu"
  13. //获取验证码
  14. func GetVerifyImg() (idKeyC string, base64stringC string) {
  15. //字符,公式,验证码配置
  16. var configC = base64Captcha.ConfigCharacter{
  17. Height: 60,
  18. Width: 240,
  19. //const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合.
  20. Mode: base64Captcha.CaptchaModeNumberAlphabet,
  21. ComplexOfNoiseText: base64Captcha.CaptchaComplexLower,
  22. ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower,
  23. IsShowHollowLine: false,
  24. IsShowNoiseDot: false,
  25. IsShowNoiseText: false,
  26. IsShowSlimeLine: false,
  27. IsShowSineLine: true,
  28. CaptchaLen: 4,
  29. }
  30. //创建字符公式验证码.
  31. //GenerateCaptcha 第一个参数为空字符串,包会自动在服务器一个随机种子给你产生随机uiid.
  32. var capC base64Captcha.CaptchaInterface
  33. idKeyC, capC = base64Captcha.GenerateCaptcha("8nM77YhE2xOvU6GMQ33A", configC)
  34. //以base64编码
  35. base64stringC = base64Captcha.CaptchaWriteToBase64Encoding(capC)
  36. return idKeyC, base64stringC
  37. }
  38. //AdminLogin 后台用户登陆验证
  39. func AdminLogin(r *ghttp.Request) (string, interface{}) {
  40. data := r.GetPostMapStrStr()
  41. //判断验证码是否正确
  42. /*if !base64Captcha.VerifyCaptchaAndIsClear(data["idKeyC"], data["idValueC"], true) {
  43. response.JsonExit(r, response.ErrorCode, "验证码输入错误")
  44. }*/
  45. rules := map[string]string{
  46. "username": "required",
  47. "password": "required",
  48. }
  49. msgs := map[string]interface{}{
  50. "username": "账号不能为空",
  51. "password": "密码不能为空",
  52. }
  53. if e := gvalid.CheckMap(data, rules, msgs); e != nil {
  54. response.JsonExit(r, response.ErrorCode, e.String())
  55. }
  56. if err, user := user_service.SignIn(data["username"], EncryptCBC(data["password"]), r.Session); err != nil {
  57. response.JsonExit(r, response.NotAcceptableCode, err.Error())
  58. } else {
  59. return data["username"], user
  60. }
  61. return data["username"], nil
  62. }
  63. //后台退出登陆
  64. func AdminLoginOut(r *ghttp.Request) bool {
  65. return true
  66. }
  67. //字符串加密
  68. func EncryptCBC(plainText string) string {
  69. key := []byte(AESPublicKey)
  70. b, e := gaes.EncryptCBC([]byte(plainText), key, key)
  71. if e != nil {
  72. glog.Error(e.Error())
  73. return ""
  74. }
  75. return gbase64.EncodeToString(b)
  76. }