1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package service
- import (
- "bytes"
- "encoding/base64"
- "github.com/gin-gonic/gin"
- "github.com/google/uuid"
- "gorm.io/gorm"
- "iot_manager_service/app/user/dao"
- "iot_manager_service/util/cache"
- "iot_manager_service/util/common"
- "iot_manager_service/util/rsa"
- "iot_manager_service/util/token"
- "strconv"
- )
- // 用户管理服务
- var UserService = new(userService)
- type userService struct{}
- func (s *userService) Login(acc string, pwd string) *common.Errors {
- userdao := &dao.User{}
- USER, err := userdao.LoginFindAccount(acc)
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return common.ParamsInvalidResponse("很抱歉,由于您的账号未完成注册或已被禁用,暂时无法使用。", nil)
- }
- return common.FailResponse(err.Error(), nil)
- }
- //加密
- //encryption := rsa.Encryption([]byte("123456"))
- //base64.StdEncoding.EncodeToString(encryption))
- //解密前端密文
- a, _ := base64.StdEncoding.DecodeString(pwd)
- client := rsa.Decryption(a)
- //解密数据库密文
- b, _ := base64.StdEncoding.DecodeString(USER.Password)
- mysql := rsa.Decryption(b)
- if !bytes.Equal(client, mysql) {
- return common.ParamsInvalidResponse("密码错误", err)
- }
- //登录成功则生成token
- UUID := uuid.New().String()
- USERID := strconv.Itoa(USER.ID)
- jwtToken, _ := token.JwtClaims.CreateJwtToken(USERID, USER.UserName, UUID)
- cache.SetToken(USERID, UUID, USER)
- return common.SuccessResponse("登录成功", gin.H{"token": jwtToken})
- }
- func (s *userService) GetPublicKey() *common.Errors {
- pub, err := rsa.GetPubKey()
- if err != nil {
- return common.FailResponse(err.Error(), nil)
- }
- return common.SuccessResponse("获取公钥成功", gin.H{"publicKey": pub})
- }
- func (s *userService) GetDeptUsers(deptIDs []int) *common.Errors {
- deptdao := &dao.Dept{}
- depts, _ := deptdao.GetDepts(deptIDs)
- userdao := &dao.User{}
- users, _ := userdao.GetUsers()
- deptUsersMap := make(map[int][]dao.User)
- for _, user := range users {
- deptUsersMap[user.DeptId] = append(deptUsersMap[user.DeptId], user)
- }
- var deptUsers []dao.Dept
- for _, dept := range depts {
- users := deptUsersMap[dept.ID]
- deptUsers = append(deptUsers, dao.Dept{
- ID: dept.ID,
- DeptName: dept.DeptName,
- Users: users,
- })
- }
- //for i, dept := range depts {
- // users1, _ := userdao.GetUsers1(dept.ID)
- // depts[i].Users = users1
- //}
- return common.SuccessResponse("获取成功", gin.H{"deptUsers": deptUsers})
- }
|