正在显示
7 个修改的文件
包含
348 行增加
和
235 行删除
pkg/application/auth/service/app_auth.go
0 → 100644
1 | +package service | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/linmadan/egglib-go/core/application" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/auth/command" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant" | ||
8 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" | ||
9 | +) | ||
10 | + | ||
11 | +// 员工绩效 手机端登录,来源于能力展示app | ||
12 | +func (service *AuthService) MobileLogin(param *command.MobileLoginCommand) (map[string]interface{}, error) { | ||
13 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
14 | + if err != nil { | ||
15 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
16 | + } | ||
17 | + if errStart := transactionContext.StartTransaction(); errStart != nil { | ||
18 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, errStart.Error()) | ||
19 | + } | ||
20 | + defer func() { | ||
21 | + _ = transactionContext.RollbackTransaction() | ||
22 | + }() | ||
23 | + // 统一用户中心登录 | ||
24 | + authCodeReply, err := factory.UCenterApi().AppAuthCode(param.Credentials, param.Cuid, param.Cid) | ||
25 | + if err != nil || !authCodeReply.IsOk() { | ||
26 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "统一用户中心认证失败") | ||
27 | + } | ||
28 | + // 用户权限校验 | ||
29 | + // 登录平台ID,28-绩效管理后台 29-员工绩效 | ||
30 | + userAuthReply, err := factory.BusinessAdminApi().GetUserAuth(int64(param.Muid), constant.PLATFORM_FONT_ID) | ||
31 | + if err != nil { | ||
32 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户鉴权失败") | ||
33 | + } | ||
34 | + if !userAuthReply.IsOk() { | ||
35 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, userAuthReply.Message()) | ||
36 | + } | ||
37 | + //获取公司数据 | ||
38 | + companyRepository := factory.CreateCompanyRepository(map[string]interface{}{ | ||
39 | + "transactionContext": transactionContext, | ||
40 | + }) | ||
41 | + company, err := companyRepository.FindOne(map[string]interface{}{ | ||
42 | + "id": param.Cid, | ||
43 | + }) | ||
44 | + if err != nil { | ||
45 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取公司数据失败") | ||
46 | + } | ||
47 | + userRepository := factory.CreateUserRepository(map[string]interface{}{ | ||
48 | + "transactionContext": transactionContext, | ||
49 | + }) | ||
50 | + user, err := userRepository.FindOne(map[string]interface{}{ | ||
51 | + "id": param.Muid, | ||
52 | + "companyId": company.Id, | ||
53 | + }) | ||
54 | + if err != nil { | ||
55 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户数据失败") | ||
56 | + } | ||
57 | + if user.Status != domain.UserStatusEnable { | ||
58 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户被禁用") | ||
59 | + } | ||
60 | + userAuth := &domain.UserAuth{ | ||
61 | + UserId: user.Id, | ||
62 | + CompanyId: user.CompanyId, | ||
63 | + CompanyName: company.Name, | ||
64 | + Phone: user.Account, | ||
65 | + PlatformId: constant.PLATFORM_FONT_ID, | ||
66 | + Name: user.Name, | ||
67 | + AdminType: user.AdminType, | ||
68 | + } | ||
69 | + accessToken, err := userAuth.CreateAccessToken() | ||
70 | + if err != nil { | ||
71 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
72 | + } | ||
73 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
74 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
75 | + } | ||
76 | + result := map[string]interface{}{ | ||
77 | + "access": map[string]interface{}{ | ||
78 | + "accessToken": accessToken, | ||
79 | + "expiresIn": domain.JWTExpiresSecond, | ||
80 | + }, | ||
81 | + } | ||
82 | + return result, nil | ||
83 | +} | ||
84 | + | ||
85 | +// Authorize 移动端授权登录 | ||
86 | +func (srv *AuthService) Authorize(param *command.AuthorizeCommand) (map[string]interface{}, error) { | ||
87 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
88 | + if err != nil { | ||
89 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
90 | + } | ||
91 | + if errStart := transactionContext.StartTransaction(); errStart != nil { | ||
92 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, errStart.Error()) | ||
93 | + } | ||
94 | + defer func() { | ||
95 | + _ = transactionContext.RollbackTransaction() | ||
96 | + }() | ||
97 | + // 统一用户中心登录 | ||
98 | + authCodeReply, err := factory.UCenterApi().AppAuthCode(param.Credentials, param.Cuid, param.Cid) | ||
99 | + if err != nil || !authCodeReply.IsOk() { | ||
100 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "统一用户中心认证失败") | ||
101 | + } | ||
102 | + // 用户权限校验 | ||
103 | + // 登录平台ID, 29-员工绩效 | ||
104 | + userAuthReply, err := factory.BusinessAdminApi().GetUserAuth(int64(param.Muid), constant.PLATFORM_FONT_ID) | ||
105 | + if err != nil { | ||
106 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户鉴权失败") | ||
107 | + } | ||
108 | + if !userAuthReply.IsOk() { | ||
109 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, userAuthReply.Message()) | ||
110 | + } | ||
111 | + //获取公司数据 | ||
112 | + companyRepository := factory.CreateCompanyRepository(map[string]interface{}{ | ||
113 | + "transactionContext": transactionContext, | ||
114 | + }) | ||
115 | + company, err := companyRepository.FindOne(map[string]interface{}{ | ||
116 | + "id": param.Cid, | ||
117 | + }) | ||
118 | + if err != nil { | ||
119 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取公司数据失败") | ||
120 | + } | ||
121 | + userRepository := factory.CreateUserRepository(map[string]interface{}{ | ||
122 | + "transactionContext": transactionContext, | ||
123 | + }) | ||
124 | + user, err := userRepository.FindOne(map[string]interface{}{ | ||
125 | + "id": param.Muid, | ||
126 | + "companyId": company.Id, | ||
127 | + }) | ||
128 | + if err != nil { | ||
129 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户数据失败") | ||
130 | + } | ||
131 | + if user.Status != domain.UserStatusEnable { | ||
132 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户被禁用") | ||
133 | + } | ||
134 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
135 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
136 | + } | ||
137 | + userAuth := &domain.UserAuth{ | ||
138 | + UserId: user.Id, | ||
139 | + CompanyId: user.CompanyId, | ||
140 | + CompanyName: company.Name, | ||
141 | + Phone: user.Account, | ||
142 | + PlatformId: constant.PLATFORM_FONT_ID, | ||
143 | + Name: user.Name, | ||
144 | + AdminType: user.AdminType, | ||
145 | + } | ||
146 | + accessToken, err := userAuth.CreateAccessToken() | ||
147 | + if err != nil { | ||
148 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
149 | + } | ||
150 | + respData := map[string]interface{}{ | ||
151 | + "authCode": accessToken, | ||
152 | + } | ||
153 | + return respData, nil | ||
154 | +} | ||
155 | + | ||
156 | +func (srv *AuthService) AccessToken(param *command.AccessTokenCommand) (map[string]interface{}, error) { | ||
157 | + userAuth := domain.UserAuth{} | ||
158 | + _, err := userAuth.ParseAccessToken(param.AuthCode) | ||
159 | + if err != nil { | ||
160 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "authcode 失效") | ||
161 | + } | ||
162 | + | ||
163 | + accessToken, err := userAuth.CreateAccessToken() | ||
164 | + if err != nil { | ||
165 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "生成 accessToken 失败") | ||
166 | + } | ||
167 | + refreshToken, err := userAuth.CreateRefreshToken() | ||
168 | + if err != nil { | ||
169 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "生成 refreshToken 解析失败") | ||
170 | + } | ||
171 | + respData := map[string]interface{}{ | ||
172 | + "refreshToken": refreshToken, | ||
173 | + "accessToken": accessToken, | ||
174 | + "expiresIn": domain.JWTExpiresSecond, | ||
175 | + } | ||
176 | + return respData, nil | ||
177 | +} | ||
178 | + | ||
179 | +// 刷新token | ||
180 | +func (srv *AuthService) RefreshToken(param *command.RefreshTokenCommand) (map[string]interface{}, error) { | ||
181 | + userAuth := domain.UserAuth{} | ||
182 | + _, err := userAuth.ParseAccessToken(param.RefreshToken) | ||
183 | + if err != nil { | ||
184 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "refresh_token 失效") | ||
185 | + } | ||
186 | + accessToken, err := userAuth.CreateAccessToken() | ||
187 | + if err != nil { | ||
188 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "生成 accessToken 失败") | ||
189 | + } | ||
190 | + refreshToken, err := userAuth.CreateRefreshToken() | ||
191 | + if err != nil { | ||
192 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "生成 refreshToken 解析失败") | ||
193 | + } | ||
194 | + respData := map[string]interface{}{ | ||
195 | + "refreshToken": refreshToken, | ||
196 | + "accessToken": accessToken, | ||
197 | + "expiresIn": domain.JWTExpiresSecond, | ||
198 | + } | ||
199 | + return respData, nil | ||
200 | +} | ||
201 | + | ||
202 | +func (srv *AuthService) UserInfo(param *command.GetMeInfo) (map[string]interface{}, error) { | ||
203 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
204 | + if err != nil { | ||
205 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
206 | + } | ||
207 | + if errStart := transactionContext.StartTransaction(); errStart != nil { | ||
208 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, errStart.Error()) | ||
209 | + } | ||
210 | + defer func() { | ||
211 | + _ = transactionContext.RollbackTransaction() | ||
212 | + }() | ||
213 | + | ||
214 | + userRepository := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext}) | ||
215 | + companyRepository := factory.CreateCompanyRepository(map[string]interface{}{"transactionContext": transactionContext}) | ||
216 | + depRepository := factory.CreateDepartmentRepository(map[string]interface{}{"transactionContext": transactionContext}) | ||
217 | + userData, err := userRepository.FindOne(map[string]interface{}{ | ||
218 | + "id": param.UserId, | ||
219 | + }) | ||
220 | + if err != nil { | ||
221 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取员工数据"+err.Error()) | ||
222 | + } | ||
223 | + companyData, err := companyRepository.FindOne(map[string]interface{}{ | ||
224 | + "id": param.CompanyId, | ||
225 | + }) | ||
226 | + if err != nil { | ||
227 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取公司数据"+err.Error()) | ||
228 | + } | ||
229 | + var departmentList []*domain.Department | ||
230 | + if len(userData.DepartmentId) > 0 { | ||
231 | + _, departmentList, err = depRepository.Find(map[string]interface{}{"ids": userData.DepartmentId}) | ||
232 | + if err != nil { | ||
233 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取部门数据"+err.Error()) | ||
234 | + } | ||
235 | + } | ||
236 | + | ||
237 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
238 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
239 | + } | ||
240 | + respData := map[string]interface{}{ | ||
241 | + "user": map[string]interface{}{ | ||
242 | + "uid": userData.Id, | ||
243 | + "muid": userData.Id, | ||
244 | + "uname": userData.Name, | ||
245 | + "phone": userData.Account, | ||
246 | + "image": map[string]interface{}{ | ||
247 | + "path": userData.AvatarUrl, | ||
248 | + "w": 0, | ||
249 | + "h": 0, | ||
250 | + }, | ||
251 | + "company": map[string]interface{}{ | ||
252 | + "id": companyData.Id, | ||
253 | + "name": companyData.Name, | ||
254 | + }, | ||
255 | + "dep": map[string]interface{}{ | ||
256 | + "id": 0, | ||
257 | + "name": "", | ||
258 | + }, | ||
259 | + }, | ||
260 | + } | ||
261 | + if len(departmentList) > 0 { | ||
262 | + respData["dep"] = map[string]interface{}{ | ||
263 | + "id": departmentList[0].Id, | ||
264 | + "name": departmentList[0].Name, | ||
265 | + } | ||
266 | + } | ||
267 | + | ||
268 | + return respData, nil | ||
269 | +} |
@@ -6,7 +6,6 @@ import ( | @@ -6,7 +6,6 @@ import ( | ||
6 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/auth/command" | 6 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/auth/command" |
7 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory" | 7 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory" |
8 | roleService "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role" | 8 | roleService "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role" |
9 | - "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant" | ||
10 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" | 9 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" |
11 | ) | 10 | ) |
12 | 11 | ||
@@ -85,80 +84,6 @@ func (service *AuthService) Login(loginCommand *command.LoginCommand) (interface | @@ -85,80 +84,6 @@ func (service *AuthService) Login(loginCommand *command.LoginCommand) (interface | ||
85 | }, nil | 84 | }, nil |
86 | } | 85 | } |
87 | 86 | ||
88 | -// 员工绩效 手机端登录,来源于能力展示app | ||
89 | -func (service *AuthService) MobileLogin(param *command.MobileLoginCommand) (map[string]interface{}, error) { | ||
90 | - transactionContext, err := factory.CreateTransactionContext(nil) | ||
91 | - if err != nil { | ||
92 | - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
93 | - } | ||
94 | - if errStart := transactionContext.StartTransaction(); errStart != nil { | ||
95 | - return nil, application.ThrowError(application.TRANSACTION_ERROR, errStart.Error()) | ||
96 | - } | ||
97 | - defer func() { | ||
98 | - _ = transactionContext.RollbackTransaction() | ||
99 | - }() | ||
100 | - // 统一用户中心登录 | ||
101 | - authCodeReply, err := factory.UCenterApi().AppAuthCode(param.Credentials, param.Cuid, param.Cid) | ||
102 | - if err != nil || !authCodeReply.IsOk() { | ||
103 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "统一用户中心认证失败") | ||
104 | - } | ||
105 | - // 用户权限校验 | ||
106 | - // 登录平台ID,28-绩效管理后台 29-员工绩效 | ||
107 | - userAuthReply, err := factory.BusinessAdminApi().GetUserAuth(int64(param.Muid), constant.PLATFORM_FONT_ID) | ||
108 | - if err != nil { | ||
109 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户鉴权失败") | ||
110 | - } | ||
111 | - if !userAuthReply.IsOk() { | ||
112 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, userAuthReply.Message()) | ||
113 | - } | ||
114 | - //获取公司数据 | ||
115 | - companyRepository := factory.CreateCompanyRepository(map[string]interface{}{ | ||
116 | - "transactionContext": transactionContext, | ||
117 | - }) | ||
118 | - company, err := companyRepository.FindOne(map[string]interface{}{ | ||
119 | - "id": param.Cid, | ||
120 | - }) | ||
121 | - if err != nil { | ||
122 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取公司数据失败") | ||
123 | - } | ||
124 | - userRepository := factory.CreateUserRepository(map[string]interface{}{ | ||
125 | - "transactionContext": transactionContext, | ||
126 | - }) | ||
127 | - user, err := userRepository.FindOne(map[string]interface{}{ | ||
128 | - "id": param.Muid, | ||
129 | - "companyId": company.Id, | ||
130 | - }) | ||
131 | - if err != nil { | ||
132 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户数据失败") | ||
133 | - } | ||
134 | - if user.Status != domain.UserStatusEnable { | ||
135 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户被禁用") | ||
136 | - } | ||
137 | - userAuth := &domain.UserAuth{ | ||
138 | - UserId: user.Id, | ||
139 | - CompanyId: user.CompanyId, | ||
140 | - CompanyName: company.Name, | ||
141 | - Phone: user.Account, | ||
142 | - PlatformId: constant.PLATFORM_FONT_ID, | ||
143 | - Name: user.Name, | ||
144 | - AdminType: user.AdminType, | ||
145 | - } | ||
146 | - accessToken, err := userAuth.CreateAccessToken() | ||
147 | - if err != nil { | ||
148 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
149 | - } | ||
150 | - if err := transactionContext.CommitTransaction(); err != nil { | ||
151 | - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
152 | - } | ||
153 | - result := map[string]interface{}{ | ||
154 | - "access": map[string]interface{}{ | ||
155 | - "accessToken": accessToken, | ||
156 | - "expiresIn": domain.JWTExpiresSecond, | ||
157 | - }, | ||
158 | - } | ||
159 | - return result, nil | ||
160 | -} | ||
161 | - | ||
162 | // 获取我的 | 87 | // 获取我的 |
163 | func (service *AuthService) MeInfo(param *command.GetMeInfo) (map[string]interface{}, error) { | 88 | func (service *AuthService) MeInfo(param *command.GetMeInfo) (map[string]interface{}, error) { |
164 | 89 | ||
@@ -229,119 +154,3 @@ func (service *AuthService) MeInfo(param *command.GetMeInfo) (map[string]interfa | @@ -229,119 +154,3 @@ func (service *AuthService) MeInfo(param *command.GetMeInfo) (map[string]interfa | ||
229 | "user": info, | 154 | "user": info, |
230 | }, nil | 155 | }, nil |
231 | } | 156 | } |
232 | - | ||
233 | -// Authorize 移动端授权登录 | ||
234 | -func (srv *AuthService) Authorize(param *command.AuthorizeCommand) (map[string]interface{}, error) { | ||
235 | - transactionContext, err := factory.CreateTransactionContext(nil) | ||
236 | - if err != nil { | ||
237 | - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
238 | - } | ||
239 | - if errStart := transactionContext.StartTransaction(); errStart != nil { | ||
240 | - return nil, application.ThrowError(application.TRANSACTION_ERROR, errStart.Error()) | ||
241 | - } | ||
242 | - defer func() { | ||
243 | - _ = transactionContext.RollbackTransaction() | ||
244 | - }() | ||
245 | - // 统一用户中心登录 | ||
246 | - authCodeReply, err := factory.UCenterApi().AppAuthCode(param.Credentials, param.Cuid, param.Cid) | ||
247 | - if err != nil || !authCodeReply.IsOk() { | ||
248 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "统一用户中心认证失败") | ||
249 | - } | ||
250 | - // 用户权限校验 | ||
251 | - // 登录平台ID, 29-员工绩效 | ||
252 | - userAuthReply, err := factory.BusinessAdminApi().GetUserAuth(int64(param.Muid), constant.PLATFORM_FONT_ID) | ||
253 | - if err != nil { | ||
254 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户鉴权失败") | ||
255 | - } | ||
256 | - if !userAuthReply.IsOk() { | ||
257 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, userAuthReply.Message()) | ||
258 | - } | ||
259 | - //获取公司数据 | ||
260 | - companyRepository := factory.CreateCompanyRepository(map[string]interface{}{ | ||
261 | - "transactionContext": transactionContext, | ||
262 | - }) | ||
263 | - company, err := companyRepository.FindOne(map[string]interface{}{ | ||
264 | - "id": param.Cid, | ||
265 | - }) | ||
266 | - if err != nil { | ||
267 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取公司数据失败") | ||
268 | - } | ||
269 | - userRepository := factory.CreateUserRepository(map[string]interface{}{ | ||
270 | - "transactionContext": transactionContext, | ||
271 | - }) | ||
272 | - user, err := userRepository.FindOne(map[string]interface{}{ | ||
273 | - "id": param.Muid, | ||
274 | - "companyId": company.Id, | ||
275 | - }) | ||
276 | - if err != nil { | ||
277 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户数据失败") | ||
278 | - } | ||
279 | - if user.Status != domain.UserStatusEnable { | ||
280 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户被禁用") | ||
281 | - } | ||
282 | - if err := transactionContext.CommitTransaction(); err != nil { | ||
283 | - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
284 | - } | ||
285 | - userAuth := &domain.UserAuth{ | ||
286 | - UserId: user.Id, | ||
287 | - CompanyId: user.CompanyId, | ||
288 | - CompanyName: company.Name, | ||
289 | - Phone: user.Account, | ||
290 | - PlatformId: constant.PLATFORM_FONT_ID, | ||
291 | - Name: user.Name, | ||
292 | - AdminType: user.AdminType, | ||
293 | - } | ||
294 | - accessToken, err := userAuth.CreateAccessToken() | ||
295 | - if err != nil { | ||
296 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
297 | - } | ||
298 | - respData := map[string]interface{}{ | ||
299 | - "authCode": accessToken, | ||
300 | - } | ||
301 | - return respData, nil | ||
302 | -} | ||
303 | - | ||
304 | -func (srv *AuthService) AccessToken(param *command.AccessTokenCommand) (map[string]interface{}, error) { | ||
305 | - userAuth := domain.UserAuth{} | ||
306 | - _, err := userAuth.ParseAccessToken(param.AuthCode) | ||
307 | - if err != nil { | ||
308 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "authcode 失效") | ||
309 | - } | ||
310 | - | ||
311 | - accessToken, err := userAuth.CreateAccessToken() | ||
312 | - if err != nil { | ||
313 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "生成 accessToken 失败") | ||
314 | - } | ||
315 | - refreshToken, err := userAuth.CreateRefreshToken() | ||
316 | - if err != nil { | ||
317 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "生成 refreshToken 解析失败") | ||
318 | - } | ||
319 | - respData := map[string]interface{}{ | ||
320 | - "refreshToken": refreshToken, | ||
321 | - "accessToken": accessToken, | ||
322 | - "expiresIn": domain.JWTExpiresSecond, | ||
323 | - } | ||
324 | - return respData, nil | ||
325 | -} | ||
326 | - | ||
327 | -func (srv *AuthService) RefreshToken(param *command.RefreshTokenCommand) (map[string]interface{}, error) { | ||
328 | - userAuth := domain.UserAuth{} | ||
329 | - _, err := userAuth.ParseAccessToken(param.RefreshToken) | ||
330 | - if err != nil { | ||
331 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "refresh_token 失效") | ||
332 | - } | ||
333 | - accessToken, err := userAuth.CreateAccessToken() | ||
334 | - if err != nil { | ||
335 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "生成 accessToken 失败") | ||
336 | - } | ||
337 | - refreshToken, err := userAuth.CreateRefreshToken() | ||
338 | - if err != nil { | ||
339 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "生成 refreshToken 解析失败") | ||
340 | - } | ||
341 | - respData := map[string]interface{}{ | ||
342 | - "refreshToken": refreshToken, | ||
343 | - "accessToken": accessToken, | ||
344 | - "expiresIn": domain.JWTExpiresSecond, | ||
345 | - } | ||
346 | - return respData, nil | ||
347 | -} |
1 | package controllers | 1 | package controllers |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | + "errors" | ||
5 | + | ||
4 | "github.com/linmadan/egglib-go/web/beego" | 6 | "github.com/linmadan/egglib-go/web/beego" |
5 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/auth/command" | 7 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/auth/command" |
6 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/auth/service" | 8 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/auth/service" |
9 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant" | ||
7 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" | 10 | "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" |
8 | ) | 11 | ) |
9 | 12 | ||
@@ -50,7 +53,7 @@ func (controller *AuthController) Authorize() { | @@ -50,7 +53,7 @@ func (controller *AuthController) Authorize() { | ||
50 | controller.Response(resp, err) | 53 | controller.Response(resp, err) |
51 | } | 54 | } |
52 | 55 | ||
53 | -// Login 获取token | 56 | +// AccessToken 手机端 获取token |
54 | func (controller *AuthController) AccessToken() { | 57 | func (controller *AuthController) AccessToken() { |
55 | authService := &service.AuthService{} | 58 | authService := &service.AuthService{} |
56 | loginCommand := &command.AccessTokenCommand{} | 59 | loginCommand := &command.AccessTokenCommand{} |
@@ -67,3 +70,28 @@ func (controller *AuthController) RefreshToken() { | @@ -67,3 +70,28 @@ func (controller *AuthController) RefreshToken() { | ||
67 | resp, err := authService.RefreshToken(loginCommand) | 70 | resp, err := authService.RefreshToken(loginCommand) |
68 | controller.Response(resp, err) | 71 | controller.Response(resp, err) |
69 | } | 72 | } |
73 | + | ||
74 | +// 手机端 获取个人信息 | ||
75 | +func (controller *AuthController) UserInfos() { | ||
76 | + tokenStr := controller.Ctx.Input.Header("x-mmm-accesstoken") | ||
77 | + if tokenStr == "" { //没有带token | ||
78 | + controller.Response(nil, errors.New("token 错误")) | ||
79 | + return | ||
80 | + } | ||
81 | + userAuth, err := (&domain.UserAuth{}).ParseAccessToken(tokenStr) | ||
82 | + if err != nil || userAuth.UserId <= 0 { | ||
83 | + controller.Response(nil, errors.New("token 错误")) | ||
84 | + return | ||
85 | + } | ||
86 | + if userAuth.PlatformId != constant.PLATFORM_FONT_ID { | ||
87 | + controller.Response(nil, errors.New("token 错误")) | ||
88 | + return | ||
89 | + } | ||
90 | + authService := &service.AuthService{} | ||
91 | + param := &command.GetMeInfo{ | ||
92 | + UserId: userAuth.UserId, | ||
93 | + CompanyId: userAuth.CompanyId, | ||
94 | + } | ||
95 | + resp, err := authService.UserInfo(param) | ||
96 | + controller.Response(resp, err) | ||
97 | +} |
1 | package middlewares | 1 | package middlewares |
2 | 2 | ||
3 | -// | ||
4 | -//import ( | ||
5 | -// "github.com/beego/beego/v2/server/web/context" | ||
6 | -//) | ||
7 | -// | ||
8 | -//func setUserId(userId int64, ctx *context.Context) { | 3 | +// func setUserId(userId int64, ctx *context.Context) { |
9 | // ctx.Input.SetData("_UserId", userId) | 4 | // ctx.Input.SetData("_UserId", userId) |
10 | -//} | 5 | +// } |
11 | // | 6 | // |
12 | -//func GetUserId(ctx *context.Context) int64 { | 7 | +// func GetUserId(ctx *context.Context) int64 { |
13 | // userId := ctx.Input.GetData("_UserId") | 8 | // userId := ctx.Input.GetData("_UserId") |
14 | // return userId.(int64) | 9 | // return userId.(int64) |
15 | -//} | 10 | +// } |
16 | // | 11 | // |
17 | -//func setCompanyId(companyId int64, ctx *context.Context) { | 12 | +// func setCompanyId(companyId int64, ctx *context.Context) { |
18 | // ctx.Input.SetData("_CompanyId", companyId) | 13 | // ctx.Input.SetData("_CompanyId", companyId) |
19 | -//} | 14 | +// } |
20 | // | 15 | // |
21 | -//func GetCompanyId(ctx *context.Context) int64 { | 16 | +// func GetCompanyId(ctx *context.Context) int64 { |
22 | // companyId := ctx.Input.GetData("_CompanyId") | 17 | // companyId := ctx.Input.GetData("_CompanyId") |
23 | // return companyId.(int64) | 18 | // return companyId.(int64) |
24 | -//} | 19 | +// } |
25 | // | 20 | // |
26 | -//func setCompanyType(companyId int, ctx *context.Context) { | 21 | +// func setCompanyType(companyId int, ctx *context.Context) { |
27 | // ctx.Input.SetData("_CompanyType", companyId) | 22 | // ctx.Input.SetData("_CompanyType", companyId) |
28 | -//} | 23 | +// } |
29 | // | 24 | // |
30 | -//func GetCompanyType(ctx *context.Context) int { | 25 | +// func GetCompanyType(ctx *context.Context) int { |
31 | // companyId := ctx.Input.GetData("_CompanyType") | 26 | // companyId := ctx.Input.GetData("_CompanyType") |
32 | // return companyId.(int) | 27 | // return companyId.(int) |
33 | -//} | ||
34 | -// | ||
35 | -//func invalidOrExpired(ctx *context.Context) { | 28 | +// } |
29 | +// func invalidOrExpired(ctx *context.Context) { | ||
36 | // resp := map[string]interface{}{ | 30 | // resp := map[string]interface{}{ |
37 | // "code": 902, | 31 | // "code": 902, |
38 | // "msg": "Authorization过期或无效,需要进行重新获取令牌", | 32 | // "msg": "Authorization过期或无效,需要进行重新获取令牌", |
39 | // } | 33 | // } |
40 | // _ = ctx.Output.JSON(resp, false, false) | 34 | // _ = ctx.Output.JSON(resp, false, false) |
41 | -//} | ||
42 | -// | ||
43 | -//func CheckToken() func(ctx *context.Context) { | 35 | +// } |
36 | + | ||
37 | +// // 适配手机端的token 处理 | ||
38 | +// func CheckTokenForApp() func(ctx *context.Context) { | ||
44 | // return func(ctx *context.Context) { | 39 | // return func(ctx *context.Context) { |
45 | // tokenStr := ctx.Input.Header("x-mmm-accesstoken") | 40 | // tokenStr := ctx.Input.Header("x-mmm-accesstoken") |
46 | // if tokenStr == "" { //没有带token | 41 | // if tokenStr == "" { //没有带token |
47 | // invalidOrExpired(ctx) | 42 | // invalidOrExpired(ctx) |
48 | // return | 43 | // return |
49 | // } | 44 | // } |
50 | -// | ||
51 | -// //userServe := service.UserService{} | ||
52 | -// //userTk, err := userServe.ValidLoginToken(tokenStr) | ||
53 | -// //if err != nil { | ||
54 | -// // invalidOrExpired(ctx) | ||
55 | -// // return | ||
56 | -// //} | ||
57 | -// //setUserId(userTk.UserId, ctx) | ||
58 | -// //setCompanyId(userTk.CompanyId, ctx) | ||
59 | -// //setCompanyType(userTk.CompanyType, ctx) | ||
60 | -// } | ||
61 | -//} | 45 | +// userAuth, err := (&domain.UserAuth{}).ParseAccessToken(tokenStr) |
46 | +// if err != nil || userAuth.UserId <= 0 { | ||
47 | +// forbidden(ctx) | ||
48 | +// return | ||
49 | +// } | ||
50 | +// if userAuth.PlatformId != constant.PLATFORM_FONT_ID { | ||
51 | +// forbidden(ctx) | ||
52 | +// return | ||
53 | +// } | ||
54 | +// ctx.Input.SetData(domain.UserAuth{}, userAuth) | ||
55 | +// } | ||
56 | +// } |
pkg/port/beego/routers/app_auth.go
0 → 100644
1 | +package routers | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/beego/beego/v2/server/web" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/controllers" | ||
6 | +) | ||
7 | + | ||
8 | +func init() { | ||
9 | + //手机模块登录-旧 | ||
10 | + web.Router("/login/mobile", &controllers.AuthController{}, "Post:MobileLogin") | ||
11 | + | ||
12 | + { | ||
13 | + //手机模块登录-新 | ||
14 | + web.CtrlPost("/v1/auth/authorize", (*controllers.AuthController).Authorize) | ||
15 | + web.CtrlPost("/v1/auth/accessToken", (*controllers.AuthController).AccessToken) | ||
16 | + web.CtrlPost("/v1/auth/refreshToken", (*controllers.AuthController).RefreshToken) | ||
17 | + } | ||
18 | + // 手机端登录后获取用户信息 | ||
19 | + web.CtrlPost("/v2/user/userInfo", (*controllers.AuthController).UserInfos) | ||
20 | +} |
@@ -8,8 +8,6 @@ import ( | @@ -8,8 +8,6 @@ import ( | ||
8 | 8 | ||
9 | func init() { | 9 | func init() { |
10 | web.Router("/login", &controllers.AuthController{}, "Post:Login") | 10 | web.Router("/login", &controllers.AuthController{}, "Post:Login") |
11 | - //手机模块登录-旧 | ||
12 | - web.Router("/login/mobile", &controllers.AuthController{}, "Post:MobileLogin") | ||
13 | 11 | ||
14 | web.InsertFilter("/auth/admin/*", web.BeforeExec, middlewares.CheckAdminToken()) | 12 | web.InsertFilter("/auth/admin/*", web.BeforeExec, middlewares.CheckAdminToken()) |
15 | web.Router("/auth/admin/user", &controllers.AuthController{}, "Get:User") | 13 | web.Router("/auth/admin/user", &controllers.AuthController{}, "Get:User") |
@@ -17,11 +15,4 @@ func init() { | @@ -17,11 +15,4 @@ func init() { | ||
17 | web.InsertFilter("/auth/font/*", web.BeforeExec, middlewares.CheckFontToken()) | 15 | web.InsertFilter("/auth/font/*", web.BeforeExec, middlewares.CheckFontToken()) |
18 | web.Router("/auth/font/user", &controllers.AuthController{}, "Get:User") | 16 | web.Router("/auth/font/user", &controllers.AuthController{}, "Get:User") |
19 | 17 | ||
20 | - { | ||
21 | - //手机模块登录-新 | ||
22 | - web.CtrlPost("/v1/auth/authorize", (*controllers.AuthController).Authorize) | ||
23 | - web.CtrlPost("/v1/auth/accessToken", (*controllers.AuthController).AccessToken) | ||
24 | - web.CtrlPost("/v1/auth/refreshToken", (*controllers.AuthController).RefreshToken) | ||
25 | - } | ||
26 | - | ||
27 | } | 18 | } |
@@ -16,4 +16,5 @@ func init() { | @@ -16,4 +16,5 @@ func init() { | ||
16 | web.NSRouter("/import-parent", &controllers.UserController{}, "Post:ImportParentUser"), // 直接上级导入 | 16 | web.NSRouter("/import-parent", &controllers.UserController{}, "Post:ImportParentUser"), // 直接上级导入 |
17 | ) | 17 | ) |
18 | web.AddNamespace(ns) | 18 | web.AddNamespace(ns) |
19 | + | ||
19 | } | 20 | } |
-
请 注册 或 登录 后发表评论