user.go 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package user
  2. import (
  3. "cdkj-check/helper"
  4. "net/http"
  5. "strings"
  6. "wechat-conf/models/model"
  7. "wechat-conf/models/user"
  8. "wechat-conf/utils"
  9. "github.com/yl10/kit/encrypt"
  10. )
  11. // UserServ 用户
  12. type UserServ struct {
  13. ctx *utils.Context
  14. dao *user.UserDAO
  15. }
  16. // NewUserServ 初始化
  17. func NewUserServ(ctx *utils.Context) *UserServ {
  18. return &UserServ{
  19. ctx: ctx,
  20. dao: user.NewUserDAO(ctx),
  21. }
  22. }
  23. // CheckUserRoute 判断用户理由
  24. func (s *UserServ) CheckUserRoute(userId, route, method string) (int, error) {
  25. if strings.Index(route, "admin") < 0 {
  26. return http.StatusOK, nil
  27. }
  28. // 如果没有登录
  29. if userId == "" {
  30. return http.StatusUnauthorized, utils.LogError("请先登录系统")
  31. }
  32. return http.StatusOK, nil
  33. }
  34. // ValidUserNameLogin 验证登陆
  35. func (s *UserServ) ValidUserNameLogin(userName, pass string) (*model.SysUser, error) {
  36. user, err := s.dao.GetUserByUserName(userName)
  37. if err != nil {
  38. return nil, err
  39. }
  40. if !s.ValidatePassword(&user, pass) {
  41. return nil, utils.LogError("用户密码不正确")
  42. }
  43. return &user, nil
  44. }
  45. // UpdatePassword 更新用户密码
  46. func (s *UserServ) UpdatePassword(userID, newPass string) error {
  47. return s.dao.UpdatePassword(userID, newPass)
  48. }
  49. // ValidatePassword 验证密码是否正确
  50. func (m *UserServ) ValidatePassword(user *model.SysUser, password string) bool {
  51. return user.Pwd == encrypt.Md5(password, user.UserId)
  52. }
  53. // GetUserByID 根据ID获取用户信息
  54. func (s *UserServ) GetUserByID(userid string) (*model.SysUser, error) {
  55. user, err := s.dao.GetUserByID(userid)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return user, nil
  60. }