作者 tangxuhui

更新

  1 +package command
  2 +
  3 +type AccessTokenCommand struct {
  4 + AuthCode string `json:"authCode"`
  5 +}
  1 +package command
  2 +
  3 +type LoginCommand struct {
  4 + Phone string `json:"phone"`
  5 + GrantType string `json:"grantType"` //登录方式(signInPassword 密码登录、signInCaptcha 验证码登录)
  6 + Password string `json:"password"`
  7 + Captcha string `json:"captcha"`
  8 +}
  1 +package service
  2 +
  3 +import (
  4 + "errors"
  5 + "time"
  6 +
  7 + "github.com/linmadan/egglib-go/core/application"
  8 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/factory"
  9 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/mobile/auth/command"
  10 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
  11 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
  12 +)
  13 +
  14 +// 组织管理
  15 +type AuthService struct {
  16 +}
  17 +
  18 +//AuthLogin 用户登录
  19 +func (srv AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{}, error) {
  20 + var (
  21 + result interface{}
  22 + err error
  23 + )
  24 + switch loginCommand.GrantType {
  25 + case "signInPassword":
  26 + result, err = srv.SignInPassword(loginCommand.Phone, loginCommand.Password)
  27 + case "signInCaptcha":
  28 + default:
  29 + err = errors.New("登录方式无法解析")
  30 + }
  31 + return result, err
  32 +}
  33 +
  34 +//SignInPassword 使用账号密码校验
  35 +func (srv AuthService) SignInPassword(account string, password string) (interface{}, error) {
  36 + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(0, 0, 0)
  37 + _, err := creationUserGateway.AuthCheckPassword(allied_creation_user.ReqAuthCheckPassword{
  38 + Password: password,
  39 + Phone: account,
  40 + })
  41 + if err != nil {
  42 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  43 + }
  44 + ltoken := domain.LoginToken{
  45 + UserId: 0,
  46 + Account: account,
  47 + Platform: domain.LoginPlatformApp,
  48 + CompanyId: 0,
  49 + }
  50 + authcode, err := ltoken.GenerateAuthCode()
  51 + if err != nil {
  52 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  53 + }
  54 + result := map[string]string{
  55 + "authCode": authcode,
  56 + }
  57 + return result, nil
  58 +}
  59 +
  60 +//SignInCaptcha 使用手机验证码登录
  61 +func (srv AuthService) SignInCaptcha(phone string, captcha string) (interface{}, error) {
  62 + return nil, nil
  63 +}
  64 +
  65 +//GetAuthAccessToken 获取令牌Token
  66 +func (srv AuthService) GetAuthAccessToken(accessTokenCommand *command.AccessTokenCommand) (interface{}, error) {
  67 + ltoken := domain.LoginToken{}
  68 + err := ltoken.ParseToken(accessTokenCommand.AuthCode)
  69 + if err != nil {
  70 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  71 + }
  72 + phone := ltoken.Account
  73 + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(0, 0, 0)
  74 + userSeachResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
  75 + Phone: phone,
  76 + })
  77 + if err != nil {
  78 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  79 + }
  80 + if len(userSeachResult.Users) == 0 {
  81 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "获取用户信息失败")
  82 + }
  83 + transactionContext, err := factory.CreateTransactionContext(nil)
  84 + if err != nil {
  85 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  86 + }
  87 + if err := transactionContext.StartTransaction(); err != nil {
  88 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  89 + }
  90 + defer func() {
  91 + transactionContext.RollbackTransaction()
  92 + }()
  93 + var loginAccessRepository domain.LoginAccessRepository
  94 + if loginAccessRepository, err = factory.CreateLoginAccessRepository(map[string]interface{}{
  95 + "transactionContext": transactionContext,
  96 + }); err != nil {
  97 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  98 + }
  99 + _, lAccess, err := loginAccessRepository.Find(map[string]interface{}{
  100 + "account": phone,
  101 + "platform": domain.LoginPlatformApp,
  102 + })
  103 + if err != nil {
  104 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  105 + }
  106 + var currentAccess *domain.LoginAccess
  107 + if len(lAccess) > 0 {
  108 + currentAccess = lAccess[0]
  109 + currentAccess.UpdatedTime = time.Now()
  110 + } else {
  111 + currentAccess = &domain.LoginAccess{
  112 + UserBaseId: int64(userSeachResult.Users[0].UserBaseId),
  113 + UserId: int64(userSeachResult.Users[0].UserId),
  114 + Account: userSeachResult.Users[0].UserInfo.Phone,
  115 + Platform: domain.LoginPlatformApp,
  116 + CompanyId: int64(userSeachResult.Users[0].Company.CompanyId),
  117 + OrganizationId: int64(userSeachResult.Users[0].Org.OrgId),
  118 + AccessToken: "",
  119 + RefreshToken: "",
  120 + AccessExpired: 0,
  121 + RefreshExpired: 0,
  122 + CreatedTime: time.Now(),
  123 + UpdatedTime: time.Now(),
  124 + }
  125 + }
  126 + loginToken := domain.LoginToken{
  127 + UserId: currentAccess.UserId,
  128 + Account: currentAccess.Account,
  129 + CompanyId: currentAccess.CompanyId,
  130 + OrgId: currentAccess.OrganizationId,
  131 + Platform: currentAccess.Platform,
  132 + }
  133 +
  134 + accessTokenStr, err := loginToken.GenerateAccessToken()
  135 + if err != nil {
  136 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  137 + }
  138 + currentAccess.AccessToken = accessTokenStr
  139 + currentAccess.AccessExpired = loginToken.ExpiresAt
  140 + refreshTokenStr, err := loginToken.GenerateRefreshToken()
  141 + if err != nil {
  142 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  143 + }
  144 + currentAccess.RefreshToken = refreshTokenStr
  145 + currentAccess.AccessExpired = loginToken.ExpiresAt
  146 +
  147 + //先存数据库
  148 + if err := transactionContext.CommitTransaction(); err != nil {
  149 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  150 + }
  151 + //先删缓存
  152 + //后加缓存
  153 + return nil, nil
  154 +}
1 -package query  
2 -  
3 -type GetAccessTokenCommand struct {  
4 - AuthCode string `json:"authCode"`  
5 -}  
6 -  
7 -func (cmd GetAccessTokenCommand) Valid() error {  
8 - return nil  
9 -}  
1 -package services  
2 -  
3 -import (  
4 - "fmt"  
5 -  
6 - "github.com/dgrijalva/jwt-go"  
7 - "github.com/sony/sonyflake"  
8 -)  
9 -  
10 -var idwork *sonyflake.Sonyflake  
11 -  
12 -func init() {  
13 - setting := sonyflake.Settings{  
14 - MachineID: func() (uint16, error) {  
15 - return 1, nil  
16 - },  
17 - CheckMachineID: func(u uint16) bool {  
18 - return true  
19 - },  
20 - }  
21 - idwork = sonyflake.NewSonyflake(setting)  
22 -}  
23 -  
24 -const (  
25 - qrcodeExpires int64 = 3600 //二维码的有效时长,单位:秒  
26 - qrcodeSecret string = "27e6741f0e0b658c"  
27 -)  
28 -  
29 -type qrcodeMessage struct {  
30 - Key string `json:"key"`  
31 - jwt.StandardClaims  
32 -}  
33 -  
34 -func (msg *qrcodeMessage) GenrateToken() (string, error) {  
35 - id, err := idwork.NextID()  
36 - if err != nil {  
37 - return "", fmt.Errorf("生成二维信息失败:%w", err)  
38 - }  
39 - msg.Key = fmt.Sprint(id)  
40 - //TODO jwt  
41 - return "", nil  
42 -}  
43 -  
44 -func (msg *qrcodeMessage) ParseToken(token string) error {  
45 - //TODO jwt  
46 - return nil  
47 -}  
1 -package services  
2 -  
3 -import (  
4 - "bytes"  
5 - "context"  
6 - "encoding/base64"  
7 - "image/png"  
8 -  
9 - "github.com/boombuler/barcode"  
10 - "github.com/boombuler/barcode/qr"  
11 -)  
12 -  
13 -//LoginAccessService 登录功能  
14 -type LoginAccessService struct {  
15 -}  
16 -  
17 -//LoginByAccount 账号登录获取authcode和可选择列表  
18 -func (srv LoginAccessService) LoginByAccount(ctx context.Context) error {  
19 - return nil  
20 -}  
21 -  
22 -//LoginBySmsCode 短信验证码登录获取authcode和可选择列表  
23 -func (srv LoginAccessService) LoginBySmsCode(ctx context.Context) error {  
24 - return nil  
25 -}  
26 -  
27 -//LoginByAuthCode 使用authCode进行登录获取accessToken和用户权限信息  
28 -func (srv LoginAccessService) LoginInfoByAuthCode(ctx context.Context) error {  
29 - return nil  
30 -}  
31 -  
32 -//GetQrcodeLogin 获取用于登录的二维码以及相应的key数据  
33 -func (srv LoginAccessService) GetQrcodeForLogin(ctx context.Context) error {  
34 -  
35 - qrCode, err := qr.Encode("Hello World", qr.M, qr.Auto)  
36 - if err != nil {  
37 - return err  
38 - }  
39 - // Scale the barcode to 200x200 pixels  
40 - qrCode, err = barcode.Scale(qrCode, 200, 200)  
41 - if err != nil {  
42 - return err  
43 - }  
44 - var imgByte bytes.Buffer  
45 - // encode the barcode as png  
46 - err = png.Encode(&imgByte, qrCode)  
47 - if err != nil {  
48 - return err  
49 - }  
50 - imgBase64 := base64.StdEncoding.EncodeToString(imgByte.Bytes())  
51 - _ = imgBase64  
52 - return err  
53 -}  
54 -  
55 -//ValidLoginForQrcode 检查以扫描二维码方式进行登录的状态  
56 -func (srv LoginAccessService) LoginByScanQrcode(ctx context.Context) error {  
57 - return nil  
58 -}  
59 -  
60 -// ValidToken 检查token信息  
61 -func (srv LoginAccessService) ValidAccessToken(ctx context.Context) error {  
62 - return nil  
63 -}  
64 -  
65 -// AuthCodeToAccessToken 用authcode交换accessToken  
66 -func (srv LoginAccessService) AuthCodeToAccessToken(ctx context.Context) error {  
67 - return nil  
68 -}  
@@ -7,5 +7,11 @@ type RoleItem struct { @@ -7,5 +7,11 @@ type RoleItem struct {
7 RoleName string `json:"roleName"` 7 RoleName string `json:"roleName"`
8 Describe string `json:"describe"` 8 Describe string `json:"describe"`
9 OrgName string `json:"orgName"` 9 OrgName string `json:"orgName"`
10 - RoleType int `json:"roleType"` 10 + RoleType int `json:"roleType"` //角色类型 1.普通角色 1024:超级管理员
  11 +}
  12 +
  13 +type RoleUser struct {
  14 + DepartmentName string `json:"departmentName"`
  15 + UserId string `json:"userId"`
  16 + UserName string `json:"userName"`
11 } 17 }
@@ -155,8 +155,8 @@ func (rolesService *RolesService) RoleRemove(roleRemoveCommand *command.RoleRemo @@ -155,8 +155,8 @@ func (rolesService *RolesService) RoleRemove(roleRemoveCommand *command.RoleRemo
155 roleIds = append(roleIds, int64(id)) 155 roleIds = append(roleIds, int64(id))
156 } 156 }
157 } 157 }
158 - _, err := creationUserGateway.RoleRemove(allied_creation_user.ReqRoleRemove{  
159 - //TODO 修改 为 切片类型 158 + _, err := creationUserGateway.RoleBatchRemove(allied_creation_user.ReqRoleBatchRemove{
  159 + RoleIds: roleIds,
160 }) 160 })
161 if err != nil { 161 if err != nil {
162 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 162 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
@@ -126,11 +126,11 @@ func (usersService *UsersService) CompanyUserAdd(companyUserAddCommand *command. @@ -126,11 +126,11 @@ func (usersService *UsersService) CompanyUserAdd(companyUserAddCommand *command.
126 UserRole: userRole, 126 UserRole: userRole,
127 // 启用状态(启用:1 禁用:2) 127 // 启用状态(启用:1 禁用:2)
128 EnableStatus: companyUserAddCommand.EnableStatus, 128 EnableStatus: companyUserAddCommand.EnableStatus,
129 - Password: "",  
130 UserName: companyUserAddCommand.UsersName, 129 UserName: companyUserAddCommand.UsersName,
131 Phone: companyUserAddCommand.Phone, 130 Phone: companyUserAddCommand.Phone,
132 Avatar: companyUserAddCommand.Avator, 131 Avatar: companyUserAddCommand.Avator,
133 Email: companyUserAddCommand.Avator, 132 Email: companyUserAddCommand.Avator,
  133 + Password: "", //TODO 填充密码
134 }) 134 })
135 135
136 data := struct { 136 data := struct {
@@ -201,7 +201,7 @@ func (usersService *UsersService) CompanyUserResetPassword(companyUserResetPassw @@ -201,7 +201,7 @@ func (usersService *UsersService) CompanyUserResetPassword(companyUserResetPassw
201 companyUserResetPasswordCommand.Operator.OrgId, 201 companyUserResetPasswordCommand.Operator.OrgId,
202 companyUserResetPasswordCommand.Operator.UserId) 202 companyUserResetPasswordCommand.Operator.UserId)
203 _, err := creationUserGateway.UserBatchResetPassword(allied_creation_user.ReqBatchResetPasswordUser{ 203 _, err := creationUserGateway.UserBatchResetPassword(allied_creation_user.ReqBatchResetPasswordUser{
204 - Password: "", 204 + Password: "", //TODO 填充密码
205 UserIds: companyUserResetPasswordCommand.UsersIds, 205 UserIds: companyUserResetPasswordCommand.UsersIds,
206 }) 206 })
207 return companyUserResetPasswordCommand, err 207 return companyUserResetPasswordCommand, err
@@ -358,7 +358,7 @@ func (usersService *UsersService) CooperationUserResetPassword(cooperationUserRe @@ -358,7 +358,7 @@ func (usersService *UsersService) CooperationUserResetPassword(cooperationUserRe
358 cooperationUserResetPasswordCommand.Operator.OrgId, 358 cooperationUserResetPasswordCommand.Operator.OrgId,
359 cooperationUserResetPasswordCommand.Operator.UserId) 359 cooperationUserResetPasswordCommand.Operator.UserId)
360 _, err := creationUserGateway.UserBatchResetPassword(allied_creation_user.ReqBatchResetPasswordUser{ 360 _, err := creationUserGateway.UserBatchResetPassword(allied_creation_user.ReqBatchResetPasswordUser{
361 - Password: "", 361 + Password: "", //TODO 填充密码
362 UserIds: cooperationUserResetPasswordCommand.UsersIds, 362 UserIds: cooperationUserResetPasswordCommand.UsersIds,
363 }) 363 })
364 return cooperationUserResetPasswordCommand, err 364 return cooperationUserResetPasswordCommand, err
@@ -2,6 +2,7 @@ package domain @@ -2,6 +2,7 @@ package domain
2 2
3 import "time" 3 import "time"
4 4
  5 +//TODO 移除文件
5 // 公司信息 6 // 公司信息
6 type CompanyInfo struct { 7 type CompanyInfo struct {
7 CompanyId string `json:"companyId"` 8 CompanyId string `json:"companyId"`
1 package domain 1 package domain
2 2
  3 +//TODO 移除文件
3 type CooperationInfo struct { 4 type CooperationInfo struct {
4 // 共创公司 5 // 共创公司
5 CooperationCompany string `json:"cooperationCompany"` 6 CooperationCompany string `json:"cooperationCompany"`
1 package domain 1 package domain
2 2
  3 +//TODO 移除文件
3 // 部门 4 // 部门
4 type Department struct { 5 type Department struct {
5 // 组织ID 6 // 组织ID
1 package domain 1 package domain
2 2
  3 +//TODO 移除文件
3 //Dictionary 字典 4 //Dictionary 字典
4 type Dictionary struct { 5 type Dictionary struct {
5 // 字典编号 主键 6 // 字典编号 主键
1 package domain 1 package domain
2 2
  3 +//TODO 移除文件
3 //DictionaryItem 字典明细项 4 //DictionaryItem 字典明细项
4 type DictionaryItem struct { 5 type DictionaryItem struct {
5 // 项编码 6 // 项编码
@@ -2,9 +2,17 @@ package domain @@ -2,9 +2,17 @@ package domain
2 2
3 import "time" 3 import "time"
4 4
  5 +//登录的平台
  6 +const (
  7 + LoginPlatformApp string = "app"
  8 + LoginPlatformWeb string = "web"
  9 +)
  10 +
5 // 登录凭证存储 11 // 登录凭证存储
6 type LoginAccess struct { 12 type LoginAccess struct {
7 LoginAccessId int64 `json:"loginAccessId"` 13 LoginAccessId int64 `json:"loginAccessId"`
  14 + UserBaseId int64 `json:"userBaseId"`
  15 + UserId int64 `json:"userId"`
8 // 账号 16 // 账号
9 Account string `json:"account"` 17 Account string `json:"account"`
10 // 对应平台 18 // 对应平台
@@ -17,9 +25,9 @@ type LoginAccess struct { @@ -17,9 +25,9 @@ type LoginAccess struct {
17 AccessToken string `json:"accessToken"` 25 AccessToken string `json:"accessToken"`
18 // 刷新登录凭证 26 // 刷新登录凭证
19 RefreshToken string `json:"refreshToken"` 27 RefreshToken string `json:"refreshToken"`
20 - // 登录凭证期时间,时间戳精度秒 28 + // 登录凭证期时间,时间戳精度秒
21 AccessExpired int64 `json:"accessExpired"` 29 AccessExpired int64 `json:"accessExpired"`
22 - // 刷新登录凭证期时间,时间戳精度秒 30 + // 刷新登录凭证期时间,时间戳精度秒
23 RefreshExpired int64 `json:"refreshExpired"` 31 RefreshExpired int64 `json:"refreshExpired"`
24 // 创建时间 32 // 创建时间
25 CreatedTime time.Time `json:"createdTime"` 33 CreatedTime time.Time `json:"createdTime"`
1 package domain 1 package domain
2 2
  3 +import (
  4 + "time"
  5 +
  6 + jwt "github.com/dgrijalva/jwt-go"
  7 +)
  8 +
3 const ( 9 const (
4 loginTokenSecret string = "bbe35ad433dd8e67" 10 loginTokenSecret string = "bbe35ad433dd8e67"
  11 + accessTokenExpiresAt int64 = 60 * 60 * 2 //两个小时过期
  12 + refreshTokenExpiresAt int64 = 3600 * 24 * 30 * 1 //刷新token 一个月过期
  13 + authCodeExpire int64 = 60 * 15 //15分钟过期
5 ) 14 )
6 15
7 type LoginToken struct { 16 type LoginToken struct {
  17 + jwt.StandardClaims
8 UserId int64 `json:"userId"` 18 UserId int64 `json:"userId"`
9 - UserBaseId int64 `json:"userBaseId"`  
10 // 账号 19 // 账号
11 Account string `json:"account"` 20 Account string `json:"account"`
12 // 对应平台 21 // 对应平台
@@ -14,18 +23,59 @@ type LoginToken struct { @@ -14,18 +23,59 @@ type LoginToken struct {
14 // 公司id 23 // 公司id
15 CompanyId int64 `json:"companyId"` 24 CompanyId int64 `json:"companyId"`
16 // 组织id 25 // 组织id
  26 + OrgId int64 `json:"orgId"`
17 } 27 }
18 28
19 -func (t *LoginToken) GenerateAccessToken() error {  
20 -  
21 - return nil 29 +func (t *LoginToken) GenerateAccessToken() (string, error) {
  30 + nowTime := time.Now().Unix()
  31 + t.StandardClaims = jwt.StandardClaims{
  32 + NotBefore: nowTime,
  33 + IssuedAt: nowTime,
  34 + ExpiresAt: nowTime + accessTokenExpiresAt,
  35 + Issuer: "allied_creation_gateway",
  36 + }
  37 + token := jwt.NewWithClaims(jwt.SigningMethodHS256, *t)
  38 + return token.SignedString([]byte(loginTokenSecret))
22 } 39 }
23 40
24 -func (t *LoginToken) GenerateRefreshToken() error { 41 +func (t *LoginToken) GenerateRefreshToken() (string, error) {
  42 + nowTime := time.Now().Unix()
  43 + t.StandardClaims = jwt.StandardClaims{
  44 + NotBefore: nowTime,
  45 + IssuedAt: nowTime,
  46 + ExpiresAt: nowTime + accessTokenExpiresAt,
  47 + Issuer: "allied_creation_gateway",
  48 + }
  49 + token := jwt.NewWithClaims(jwt.SigningMethodHS256, *t)
  50 + return token.SignedString([]byte(loginTokenSecret))
  51 +}
25 52
26 - return nil 53 +func (t *LoginToken) GenerateAuthCode() (string, error) {
  54 + nowTime := time.Now().Unix()
  55 + claims := LoginToken{
  56 + StandardClaims: jwt.StandardClaims{
  57 + NotBefore: nowTime,
  58 + IssuedAt: nowTime,
  59 + ExpiresAt: nowTime + authCodeExpire,
  60 + Issuer: "allied_creation_gateway",
  61 + },
  62 + }
  63 + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  64 + return token.SignedString([]byte(loginTokenSecret))
27 } 65 }
28 66
29 func (t *LoginToken) ParseToken(str string) error { 67 func (t *LoginToken) ParseToken(str string) error {
  68 + tokenClaims, err := jwt.ParseWithClaims(
  69 + str,
  70 + &LoginToken{},
  71 + func(token *jwt.Token) (interface{}, error) {
  72 + return loginTokenSecret, nil
  73 + })
  74 + if err != nil {
  75 + return err
  76 + }
  77 + if claim, ok := tokenClaims.Claims.(*LoginToken); ok && tokenClaims.Valid {
  78 + *t = *claim
  79 + }
30 return nil 80 return nil
31 } 81 }
1 package domain 1 package domain
2 2
  3 +//TODO 移除文件
3 // 系统菜单 4 // 系统菜单
4 type Menu struct { 5 type Menu struct {
5 // 菜单编号 6 // 菜单编号
1 package domain 1 package domain
2 2
  3 +//TODO 移除文件
3 // 组织organization 4 // 组织organization
4 type Orgs struct { 5 type Orgs struct {
5 // 组织ID 6 // 组织ID
1 package domain 1 package domain
2 2
  3 +//TODO 移除文件
3 // 区域信息值对象 4 // 区域信息值对象
4 type RegionInfo struct { 5 type RegionInfo struct {
5 // 区域编号 6 // 区域编号
1 package domain 1 package domain
2 2
  3 +//TODO 移除文件
3 // 角色 (base) 4 // 角色 (base)
4 type Roles struct { 5 type Roles struct {
5 // 角色ID 6 // 角色ID
1 package domain 1 package domain
2 2
  3 +//TODO 移除文件
3 // 用户基础信息 4 // 用户基础信息
4 type Users struct { 5 type Users struct {
5 // 用户id 6 // 用户id
@@ -2,6 +2,7 @@ package domain @@ -2,6 +2,7 @@ package domain
2 2
3 import "time" 3 import "time"
4 4
  5 +//TODO 移除文件
5 // 用户基础信息 6 // 用户基础信息
6 type UsersBase struct { 7 type UsersBase struct {
7 UserId string `json:"userId"` 8 UserId string `json:"userId"`
  1 +package cache
  2 +
  3 +//验证码缓存
1 package cache 1 package cache
2 2
3 -import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain" 3 +import (
  4 + "time"
4 5
5 -type LoginAccessCache struct { 6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
  7 +)
  8 +
  9 +//登录凭证缓存
  10 +type LoginTokenCache struct {
  11 +}
  12 +
  13 +func (ca LoginTokenCache) keyAccessToken(account string, platform string) string {
  14 + str := KEY_PREFIX + "accesstoken:" + account + ":" + platform
  15 + return str
  16 +}
  17 +
  18 +func (ca LoginTokenCache) keyRefreshToken(account string, platform string) string {
  19 + str := KEY_PREFIX + "refreshtoken" + account + ":" + platform
  20 + return str
  21 +}
  22 +
  23 +func (ca LoginTokenCache) SaveAccessToken(access *domain.LoginAccess) error {
  24 + nowTime := time.Now().Unix()
  25 + exp := access.AccessExpired - nowTime
  26 + if exp <= 0 {
  27 + exp = 60 * 60 * 2
  28 + }
  29 + key := ca.keyAccessToken(access.Account, access.Platform)
  30 + result := clientRedis.Set(key, access.AccessToken, time.Duration(exp))
  31 + return result.Err()
  32 +}
  33 +
  34 +func (ca LoginTokenCache) RemoveAccessToken(account string, platform string) error {
  35 + key := ca.keyAccessToken(account, platform)
  36 + result := clientRedis.Del(key)
  37 + return result.Err()
6 } 38 }
7 39
8 -func (ca LoginAccessCache) Save(param *domain.LoginAccess) (*domain.LoginAccess, error) {  
9 - return nil, nil 40 +func (ca LoginTokenCache) GetAccessToken(account string, platform string) (string, error) {
  41 + key := ca.keyAccessToken(account, platform)
  42 + result := clientRedis.Get(key)
  43 + return result.Result()
10 } 44 }
11 -func (ca LoginAccessCache) Remove(param *domain.LoginAccess) (*domain.LoginAccess, error) {  
12 - return nil, nil 45 +
  46 +func (ca LoginTokenCache) SaveRefreshToken(access *domain.LoginAccess) error {
  47 + nowTime := time.Now().Unix()
  48 + exp := access.RefreshExpired - nowTime
  49 + if exp <= 0 {
  50 + exp = 60 * 60 * 2
  51 + }
  52 + key := ca.keyAccessToken(access.Account, access.Platform)
  53 + result := clientRedis.Set(key, access.RefreshToken, time.Duration(exp))
  54 + return result.Err()
  55 +}
  56 +
  57 +func (ca LoginTokenCache) RemoveRefreshToken(account string, platform string) error {
  58 + key := ca.keyRefreshToken(account, platform)
  59 + result := clientRedis.Del(key)
  60 + return result.Err()
13 } 61 }
14 62
15 -func (ca LoginAccessCache) FindOne(account string, platform string) (*domain.LoginAccess, error) {  
16 - return nil, nil 63 +func (ca LoginTokenCache) GetRefreshToken(account string, platform string) (string, error) {
  64 + key := ca.keyRefreshToken(account, platform)
  65 + result := clientRedis.Get(key)
  66 + return result.Result()
17 } 67 }
1 package cache 1 package cache
2 2
  3 +//二维码信息缓存
3 type LoginQrcodeCache struct { 4 type LoginQrcodeCache struct {
4 } 5 }
@@ -9,7 +9,7 @@ import ( @@ -9,7 +9,7 @@ import (
9 var clientRedis *redis.Client 9 var clientRedis *redis.Client
10 10
11 const ( 11 const (
12 - keyPrefix string = "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway" 12 + KEY_PREFIX string = "allied:creation:gateway:"
13 ) 13 )
14 14
15 func Init() *redis.Client { 15 func Init() *redis.Client {
  1 +package repository
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/utils/snowflake"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
  6 +)
  7 +
  8 +var idWorker *snowflake.IdWorker
  9 +
  10 +func init() {
  11 + worker, err := snowflake.NewIdWorker(1)
  12 + if err != nil {
  13 + log.Logger.Panic("idWorker init err" + err.Error())
  14 + return
  15 + }
  16 + idWorker = worker
  17 +}
@@ -6,7 +6,6 @@ import ( @@ -6,7 +6,6 @@ import (
6 "github.com/go-pg/pg/v10" 6 "github.com/go-pg/pg/v10"
7 "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" 7 "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
8 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 8 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
9 - "github.com/linmadan/egglib-go/utils/snowflake"  
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/pg/models" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/pg/models"
12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/pg/transform" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/pg/transform"
@@ -17,11 +16,7 @@ type LoginAccessRepository struct { @@ -17,11 +16,7 @@ type LoginAccessRepository struct {
17 } 16 }
18 17
19 func (repository *LoginAccessRepository) nextIdentify() (int64, error) { 18 func (repository *LoginAccessRepository) nextIdentify() (int64, error) {
20 - IdWorker, err := snowflake.NewIdWorker(1)  
21 - if err != nil {  
22 - return 0, err  
23 - }  
24 - id, err := IdWorker.NextId() 19 + id, err := idWorker.NextId()
25 return id, err 20 return id, err
26 } 21 }
27 func (repository *LoginAccessRepository) Save(loginAccess *domain.LoginAccess) (*domain.LoginAccess, error) { 22 func (repository *LoginAccessRepository) Save(loginAccess *domain.LoginAccess) (*domain.LoginAccess, error) {
@@ -143,6 +138,8 @@ func (repository *LoginAccessRepository) Find(queryOptions map[string]interface{ @@ -143,6 +138,8 @@ func (repository *LoginAccessRepository) Find(queryOptions map[string]interface{
143 query := sqlbuilder.BuildQuery(tx.Model(&loginAccessModels), queryOptions) 138 query := sqlbuilder.BuildQuery(tx.Model(&loginAccessModels), queryOptions)
144 query.SetOffsetAndLimit(20) 139 query.SetOffsetAndLimit(20)
145 query.SetOrderDirect("login_access_id", "DESC") 140 query.SetOrderDirect("login_access_id", "DESC")
  141 + query.SetWhereByQueryOption("account", "account")
  142 + query.SetWhereByQueryOption("platform", "platform")
146 if count, err := query.SelectAndCount(); err != nil { 143 if count, err := query.SelectAndCount(); err != nil {
147 return 0, loginAccesss, err 144 return 0, loginAccesss, err
148 } else { 145 } else {
@@ -229,6 +229,37 @@ func (gateway HttplibAlliedCreationUser) RoleRemove(param ReqRoleRemove) (*DataR @@ -229,6 +229,37 @@ func (gateway HttplibAlliedCreationUser) RoleRemove(param ReqRoleRemove) (*DataR
229 return &data, err 229 return &data, err
230 } 230 }
231 231
  232 +// RoleRemove 批量移除角色
  233 +func (gateway HttplibAlliedCreationUser) RoleBatchRemove(param ReqRoleBatchRemove) (*DataRoleBatchRemove, error) {
  234 + urlStr := gateway.baseUrL + "/role/batch-delete"
  235 + method := "post"
  236 + req := gateway.CreateRequest(urlStr, method)
  237 + log.Logger.Debug("向用户模块请求数据:批量移除角色。", map[string]interface{}{
  238 + "api": method + ":" + urlStr,
  239 + "param": param,
  240 + })
  241 + req, err := req.JSONBody(param)
  242 + if err != nil {
  243 + return nil, fmt.Errorf("请求批量移除角色失败:%w", err)
  244 + }
  245 +
  246 + byteResult, err := req.Bytes()
  247 + if err != nil {
  248 + return nil, fmt.Errorf("获取批量移除角色失败:%w", err)
  249 + }
  250 + log.Logger.Debug("获取用户模块请求数据:批量移除角色。", map[string]interface{}{
  251 + "result": string(byteResult),
  252 + })
  253 + var result service_gateway.GatewayResponse
  254 + err = json.Unmarshal(byteResult, &result)
  255 + if err != nil {
  256 + return nil, fmt.Errorf("解析批量移除角色:%w", err)
  257 + }
  258 + var data DataRoleBatchRemove
  259 + err = gateway.GetResponseData(result, &data)
  260 + return &data, err
  261 +}
  262 +
232 // RoleGetRelatedUser 获取角色相关联的用户 263 // RoleGetRelatedUser 获取角色相关联的用户
233 func (gateway HttplibAlliedCreationUser) RoleGetRelatedUser(param ReqRoleGetRelatedUser) (*DataRoleGetRelatedUser, error) { 264 func (gateway HttplibAlliedCreationUser) RoleGetRelatedUser(param ReqRoleGetRelatedUser) (*DataRoleGetRelatedUser, error) {
234 orgId := strconv.FormatInt(param.OrgId, 10) 265 orgId := strconv.FormatInt(param.OrgId, 10)
@@ -30,9 +30,12 @@ type ( @@ -30,9 +30,12 @@ type (
30 //手机账号密码检查 30 //手机账号密码检查
31 type ( 31 type (
32 ReqAuthCheckPassword struct { 32 ReqAuthCheckPassword struct {
  33 + Password string `json:"password"`
  34 + Phone string `json:"phone"`
33 } 35 }
34 36
35 DataAuthCheckPassword struct { 37 DataAuthCheckPassword struct {
  38 + UserId int `json:"userId"`
36 } 39 }
37 ) 40 )
38 41
@@ -36,7 +36,7 @@ type ( @@ -36,7 +36,7 @@ type (
36 OrgID int `json:"orgId"` 36 OrgID int `json:"orgId"`
37 RoleID int `json:"roleId"` 37 RoleID int `json:"roleId"`
38 RoleName string `json:"roleName"` 38 RoleName string `json:"roleName"`
39 - RoleType int `json:"roleType"` 39 + RoleType int `json:"roleType"` //角色类型 1.普通角色 1024:超级管理员
40 UpdatedAt string `json:"updatedAt"` 40 UpdatedAt string `json:"updatedAt"`
41 } `json:"role"` 41 } `json:"role"`
42 } 42 }
@@ -130,6 +130,16 @@ type ( @@ -130,6 +130,16 @@ type (
130 } 130 }
131 ) 131 )
132 132
  133 +//批量移除角色
  134 +type (
  135 + ReqRoleBatchRemove struct {
  136 + RoleIds []int64 `json:"roleIds"`
  137 + }
  138 +
  139 + DataRoleBatchRemove struct {
  140 + }
  141 +)
  142 +
133 //获取角色相关联的用户 143 //获取角色相关联的用户
134 type ( 144 type (
135 ReqRoleGetRelatedUser struct { 145 ReqRoleGetRelatedUser struct {
@@ -139,6 +149,16 @@ type ( @@ -139,6 +149,16 @@ type (
139 } 149 }
140 150
141 DataRoleGetRelatedUser struct { 151 DataRoleGetRelatedUser struct {
  152 + NotInRoleUser []struct {
  153 + DepartmentName string `json:"departmentName"`
  154 + UserID int `json:"userId"`
  155 + UserName string `json:"userName"`
  156 + } `json:"notInRoleUser"`
  157 + RoleUser []struct {
  158 + DepartmentName string `json:"departmentName"`
  159 + UserID int `json:"userId"`
  160 + UserName string `json:"userName"`
  161 + } `json:"roleUser"`
142 } 162 }
143 ) 163 )
144 164