userService.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package service
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "github.com/gin-gonic/gin"
  6. "github.com/google/uuid"
  7. "gorm.io/gorm"
  8. "iot_manager_service/app/user/dao"
  9. "iot_manager_service/util/cache"
  10. "iot_manager_service/util/common"
  11. "iot_manager_service/util/rsa"
  12. "iot_manager_service/util/token"
  13. "strconv"
  14. )
  15. // 用户管理服务
  16. var UserService = new(userService)
  17. type userService struct{}
  18. func (s *userService) Login(acc string, pwd string) *common.Errors {
  19. userdao := &dao.User{}
  20. USER, err := userdao.LoginFindAccount(acc)
  21. if err != nil {
  22. if err == gorm.ErrRecordNotFound {
  23. return common.ParamsInvalidResponse("很抱歉,由于您的账号未完成注册或已被禁用,暂时无法使用。", nil)
  24. }
  25. return common.FailResponse(err.Error(), nil)
  26. }
  27. //加密
  28. //encryption := rsa.Encryption([]byte("123456"))
  29. //base64.StdEncoding.EncodeToString(encryption))
  30. //解密前端密文
  31. a, _ := base64.StdEncoding.DecodeString(pwd)
  32. client := rsa.Decryption(a)
  33. //解密数据库密文
  34. b, _ := base64.StdEncoding.DecodeString(USER.Password)
  35. mysql := rsa.Decryption(b)
  36. if !bytes.Equal(client, mysql) {
  37. return common.ParamsInvalidResponse("密码错误", err)
  38. }
  39. //登录成功则生成token
  40. UUID := uuid.New().String()
  41. USERID := strconv.Itoa(USER.ID)
  42. jwtToken, _ := token.JwtClaims.CreateJwtToken(USERID, USER.UserName, UUID)
  43. cache.SetToken(USERID, UUID, USER)
  44. return common.SuccessResponse("登录成功", gin.H{"token": jwtToken})
  45. }
  46. func (s *userService) GetPublicKey() *common.Errors {
  47. pub, err := rsa.GetPubKey()
  48. if err != nil {
  49. return common.FailResponse(err.Error(), nil)
  50. }
  51. return common.SuccessResponse("获取公钥成功", gin.H{"publicKey": pub})
  52. }
  53. func (s *userService) GetDeptUsers(deptIDs []int) *common.Errors {
  54. deptdao := &dao.Dept{}
  55. depts, _ := deptdao.GetDepts(deptIDs)
  56. userdao := &dao.User{}
  57. users, _ := userdao.GetUsers()
  58. deptUsersMap := make(map[int][]dao.User)
  59. for _, user := range users {
  60. deptUsersMap[user.DeptId] = append(deptUsersMap[user.DeptId], user)
  61. }
  62. var deptUsers []dao.Dept
  63. for _, dept := range depts {
  64. users := deptUsersMap[dept.ID]
  65. deptUsers = append(deptUsers, dao.Dept{
  66. ID: dept.ID,
  67. DeptName: dept.DeptName,
  68. Users: users,
  69. })
  70. }
  71. //for i, dept := range depts {
  72. // users1, _ := userdao.GetUsers1(dept.ID)
  73. // depts[i].Users = users1
  74. //}
  75. return common.SuccessResponse("获取成功", gin.H{"deptUsers": deptUsers})
  76. }