auth.go 949 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package user
  2. import (
  3. "authService/model"
  4. "authService/response"
  5. "authService/service/auth"
  6. "authService/util"
  7. )
  8. // 登录
  9. func Login(account, password string) (string, int, *response.ErrCode) {
  10. var token = ""
  11. var expireIn = 0
  12. existUser := FindByAccount(account)
  13. if existUser == nil {
  14. return token, expireIn, &response.ErrCode{
  15. Code: response.ERROR,
  16. Msg: "账号或密码不正确",
  17. }
  18. }
  19. // 如果被禁用,禁止登录
  20. if existUser.Status == model.UserStatusDisable {
  21. return token, expireIn, response.ErrAccountForbid
  22. }
  23. if existUser.Password != util.Md5(password) {
  24. return token, expireIn, &response.ErrCode{
  25. Code: response.ERROR,
  26. Msg: "账号或密码不正确",
  27. }
  28. }
  29. formatedUser := Format(existUser)
  30. token, expireIn, err := auth.Generate(formatedUser)
  31. if err != nil {
  32. return token, expireIn, err
  33. }
  34. return token, expireIn, nil
  35. }
  36. // 退出登录
  37. func Logout(userId int64) {
  38. auth.Exit(userId)
  39. }