|
@@ -0,0 +1,376 @@
|
|
1
|
+请求`api/auth/token`接口。
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+如果用户不存在,创建写字楼版的用户以及用户角色中间表。在`aspnetusers`中增加一条记录,在`aspnetuserroles`增加一条记录,角色为`OfficeGuest`。
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+如果用户存在,会根据手机号去`projectuserroles`中找角色为`HdDg`的记录。
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+如果找到,说明是总部电工(办公楼版的超级管理员)。
|
|
14
|
+
|
|
15
|
+```
|
|
16
|
+dto.ProjectId = currentUser.ProjectIds;
|
|
17
|
+dto.ProjectName = string.Empty;
|
|
18
|
+```
|
|
19
|
+
|
|
20
|
+以上,可以看出,当原先的总部运营后台设置总部电工和项目关系时,实际上是保存在了`aspnetusers`表的`ProjectIds`字段中。而在新版的运营后台,与总部电工有关的项目要去`HdDgProject`中去找。
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+如果找不到,说明是办公楼版的其它角色用户。会去项目数据库的`CompanyUser`中去找当前用户的相关公司。
|
|
25
|
+
|
|
26
|
+```
|
|
27
|
+var currentCompanyUsers = _companyUserRepo.GetCompanyUsersByUserIdAsync(currentUser.Id.ToString()).ToList();
|
|
28
|
+```
|
|
29
|
+
|
|
30
|
+也就是说,一个用户可以在不同的`Company`中。
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+以上,出现了`aspnetusers`, `aspnetuserroles`,``projectuserroles`,`companyuser` 这4张表。
|
|
35
|
+
|
|
36
|
+```
|
|
37
|
+aspnetusers
|
|
38
|
+ Id
|
|
39
|
+ UserName
|
|
40
|
+ ProjectIds
|
|
41
|
+
|
|
42
|
+aspnetuserroles
|
|
43
|
+ UserId
|
|
44
|
+ RoleId
|
|
45
|
+
|
|
46
|
+projectuserroles
|
|
47
|
+ UserId
|
|
48
|
+ RoleId
|
|
49
|
+ ProjectId
|
|
50
|
+ CompanyId
|
|
51
|
+
|
|
52
|
+companyuser
|
|
53
|
+ CompanyId
|
|
54
|
+ UserId
|
|
55
|
+ RoleId
|
|
56
|
+ RoleName
|
|
57
|
+ IsAllowed: 是否允许新朋友加入
|
|
58
|
+ HasRole:是否具有角色
|
|
59
|
+```
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+如果是手机端的用户:`13800000000`
|
|
64
|
+
|
|
65
|
+```
|
|
66
|
+aspnetusers
|
|
67
|
+ Id:1
|
|
68
|
+ UserName:13800000000
|
|
69
|
+ ProjectIds
|
|
70
|
+
|
|
71
|
+aspnetuserroles
|
|
72
|
+ UserId:1
|
|
73
|
+ RoleId:角色为OfficeGuest的aspnetroles的主键
|
|
74
|
+```
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+如果总部运营后台添加了一个电工:13811111111
|
|
79
|
+
|
|
80
|
+```
|
|
81
|
+aspnetusers
|
|
82
|
+ Id:2
|
|
83
|
+ UserName:13811111111
|
|
84
|
+ ProjectIds: 9,10
|
|
85
|
+
|
|
86
|
+aspnetuserroles
|
|
87
|
+ UserId:2
|
|
88
|
+ RoleId:角色为HdDg的aspnetroles的主键
|
|
89
|
+```
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+电工`13811111111`,打开`APP`
|
|
94
|
+
|
|
95
|
+![](F:\SourceCodes\DDWiki\专题\后端\office6.png)
|
|
96
|
+
|
|
97
|
+电工`13811111111`,点击某个项目,展示公司列表。
|
|
98
|
+
|
|
99
|
+![](F:\SourceCodes\DDWiki\专题\后端\office7.png)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+电工`13811111111`,添加公司。
|
|
104
|
+
|
|
105
|
+![](F:\SourceCodes\DDWiki\专题\后端\office8.png)
|
|
106
|
+
|
|
107
|
+对应的接口在`api/super/addUpdateCompany`
|
|
108
|
+
|
|
109
|
+```
|
|
110
|
+ var newCompany = new Company(request.ProjectId, request.Name,request.LocationIds,request.IsAdmin, request.UserId);
|
|
111
|
+ _companyRepo.Add(newCompany);
|
|
112
|
+ await _companyRepo.UnitOfWork.SaveChangesAsync();
|
|
113
|
+ var myCompanyId = newCompany.Id.ToString();//新增公司ID
|
|
114
|
+ //获取当前用户信息,并更新相应的用户权限表
|
|
115
|
+ var user = await _auth.GetUserByIdAsync(request.UserId);
|
|
116
|
+ //添加用户
|
|
117
|
+ var rolestr = await _auth.CreatOfficeAdmin(user.Mobile, request.ProjectId, myCompanyId, user.UserName);
|
|
118
|
+ string[] roleInfo = rolestr.Split(',');
|
|
119
|
+ //添加公司用户
|
|
120
|
+ if (roleInfo.Length == 3)
|
|
121
|
+ {
|
|
122
|
+ CompanyUser newUser = new CompanyUser(myCompanyId, roleInfo[0], roleInfo[1], roleInfo[2]);
|
|
123
|
+ _companyUser.Add(newUser);
|
|
124
|
+ await _companyUser.UnitOfWork.SaveChangesAsync();
|
|
125
|
+ }
|
|
126
|
+```
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+数据库中对应的:
|
|
131
|
+
|
|
132
|
+```
|
|
133
|
+aspnetusers
|
|
134
|
+ Id:2
|
|
135
|
+ UserName:13811111111
|
|
136
|
+ ProjectIds: 9,10
|
|
137
|
+
|
|
138
|
+aspnetuserroles
|
|
139
|
+ UserId:2
|
|
140
|
+ RoleId:角色为HdDg的aspnetroles的主键
|
|
141
|
+
|
|
142
|
+projectuserroles
|
|
143
|
+ UserId:2
|
|
144
|
+ RoleId:角色为HdDg的aspnetroles的主键
|
|
145
|
+ ProjectId:9
|
|
146
|
+ CompanyId:1
|
|
147
|
+
|
|
148
|
+companyuser
|
|
149
|
+ CompanyId:1
|
|
150
|
+ UserId:2
|
|
151
|
+ RoleId:角色为HdDg的aspnetroles的主键
|
|
152
|
+ RoleName:HdDg
|
|
153
|
+ IsAllowed: false
|
|
154
|
+ HasRole:true
|
|
155
|
+```
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+电工`13811111111`,点击"我的",点击项目管理。
|
|
160
|
+
|
|
161
|
+![](F:\SourceCodes\DDWiki\专题\后端\office1.png)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+展示项目列表。其实对后端开发人员来说,项目列表就是`Company`列表。
|
|
166
|
+
|
|
167
|
+![](F:\SourceCodes\DDWiki\专题\后端\office2.png)
|
|
168
|
+
|
|
169
|
+电工点击"邀请好友",展示二维码。目前角色为`OfficeGuest`的用户`13800000000`扫描二维码。
|
|
170
|
+
|
|
171
|
+![](F:\SourceCodes\DDWiki\专题\后端\office3.png)
|
|
172
|
+
|
|
173
|
+目前角色为`OfficeGuest`的用户`13800000000`点击"加入该项目"。
|
|
174
|
+
|
|
175
|
+![](F:\SourceCodes\DDWiki\专题\后端\office4.png)
|
|
176
|
+
|
|
177
|
+对应的接口在`api/mine/joinProject`中。
|
|
178
|
+
|
|
179
|
+```
|
|
180
|
+ var guestRole = await _auth.GetRoleByNameAsync(OfficeConstants.Office_Role_Guest);
|
|
181
|
+
|
|
182
|
+ var ef = await _officeHelper.GetOfficeContextByProjectIdAsync(request.ProjectId);
|
|
183
|
+ _companyUserRepo = new CompanyUserRepository(ef);
|
|
184
|
+
|
|
185
|
+ //先检查这个用户是否已经在CompanyUser中有记录,因为用户可以多次扫码加入,这种情况就不在数据库中再添加记录了
|
|
186
|
+ if (_companyUserRepo.GetCompanyUsersByUserIdAsync(request.UserId).ToList().Any(t => t.CompanyId == request.CompanyId))//已在本公司中的用户无法再加入公司
|
|
187
|
+ result.Message = "用户已经加入该项目了";
|
|
188
|
+ else
|
|
189
|
+ {
|
|
190
|
+ //管理员不能扫码加入自己的项目
|
|
191
|
+ var AdminRole = await _auth.GetRoleByNameAsync(OfficeConstants.Office_Role_Admin);
|
|
192
|
+ var companyAdmin = _companyUserRepo.GetUsersByCompanyId(request.CompanyId).FirstOrDefault(t => t.RoleId == AdminRole.Id);
|
|
193
|
+ if (companyAdmin != null && companyAdmin.UserId == request.UserId)
|
|
194
|
+ {
|
|
195
|
+ result.Message = "管理员不能扫码加入自己管理的项目";
|
|
196
|
+ return await Task.FromResult(result);
|
|
197
|
+ }
|
|
198
|
+
|
|
199
|
+ //var newCompanyUser = new CompanyUser(request.CompanyId, request.UserId, guestRole.Id, guestRole.Name);
|
|
200
|
+ var newCompanyUser = new CompanyUser(request.CompanyId, request.UserId);
|
|
201
|
+ _companyUserRepo.Add(newCompanyUser);
|
|
202
|
+ await _companyUserRepo.UnitOfWork.SaveChangesAsync(cancellationToken);
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+ }
|
|
206
|
+```
|
|
207
|
+
|
|
208
|
+以上,排除了用户`13800000000`已经在`CompanyUser`中有记录,以及管理员不能扫码进入当前`Company`这两种情况,然后再`CompanyUser`中添加了一条记录。
|
|
209
|
+
|
|
210
|
+```
|
|
211
|
+aspnetusers
|
|
212
|
+ Id:1
|
|
213
|
+ UserName:13800000000
|
|
214
|
+ ProjectIds
|
|
215
|
+
|
|
216
|
+aspnetuserroles
|
|
217
|
+ UserId:1
|
|
218
|
+ RoleId:角色为OfficeGuest的aspnetroles的主键
|
|
219
|
+
|
|
220
|
+companyuser
|
|
221
|
+ CompanyId:1
|
|
222
|
+ UserId:1
|
|
223
|
+ RoleId
|
|
224
|
+ RoleName
|
|
225
|
+ IsAllowed:
|
|
226
|
+ HasRole:false
|
|
227
|
+```
|
|
228
|
+
|
|
229
|
+电工`13811111111`,点击"新朋友"。
|
|
230
|
+
|
|
231
|
+![](F:\SourceCodes\DDWiki\专题\后端\office2.png)
|
|
232
|
+
|
|
233
|
+展示新朋友列表。
|
|
234
|
+
|
|
235
|
+![](F:\SourceCodes\DDWiki\专题\后端\office5.png)
|
|
236
|
+
|
|
237
|
+对应的接口在`api/mine/newFriends`
|
|
238
|
+
|
|
239
|
+```
|
|
240
|
+ if (await _auth.IsOfficeSuperAdminAsync(request.UserId))//超级管理员
|
|
241
|
+ {
|
|
242
|
+ var superAdminRole = await _auth.GetRoleByNameAsync(OfficeConstants.Office_Role_SuperAdmin);
|
|
243
|
+ adminCompanyIds = companyUsers.Where(t => t.UserId == request.UserId && t.RoleId == superAdminRole.Id).Select(t => t.CompanyId).ToList();
|
|
244
|
+
|
|
245
|
+ }
|
|
246
|
+ else//管理员
|
|
247
|
+ {
|
|
248
|
+ //获取管理员角色的主键
|
|
249
|
+ var officeAdminRole = await _auth.GetRoleByNameAsync(OfficeConstants.Office_Role_Admin);
|
|
250
|
+
|
|
251
|
+ //找出管理员所在的公司主键集合
|
|
252
|
+ adminCompanyIds = companyUsers.Where(t => t.UserId == request.UserId && t.RoleId == officeAdminRole.Id.ToString()).Select(t => t.CompanyId).ToList();
|
|
253
|
+
|
|
254
|
+ }
|
|
255
|
+
|
|
256
|
+ companyUsers = companyUsers.Where(t => t.IsAllowed == false && adminCompanyIds.Any(x => x == t.CompanyId)).ToList();
|
|
257
|
+ foreach (var companyUser in companyUsers)
|
|
258
|
+ {
|
|
259
|
+ dto.Items.Add(HandleItem(companyUser, request, _companyRepo));
|
|
260
|
+ }
|
|
261
|
+
|
|
262
|
+ dto.Items = dto.Items.OrderByDescending(t => DateTime.Parse(t.CreateTime)).ToList();
|
|
263
|
+ result.data = dto;
|
|
264
|
+```
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+点击"同意",对应的接口在:`api/mine/acceptNewFriend`
|
|
269
|
+
|
|
270
|
+```
|
|
271
|
+ if (!_companyUserRepo.HasCompanyRole(request.ObjUserId, request.CompanyId, guestRole.Name))//保证多次扫码只加一次
|
|
272
|
+ {
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+ var companyUsers = _companyUserRepo.GetAll();
|
|
276
|
+ var currentCompanyUser = companyUsers.FirstOrDefault(t => t.CompanyId == request.CompanyId && t.UserId == request.ObjUserId);//肯定存在
|
|
277
|
+ currentCompanyUser.AcceptNewFriendNew(guestRole.Id,guestRole.Name);
|
|
278
|
+
|
|
279
|
+ _companyUserRepo.Update(currentCompanyUser);
|
|
280
|
+
|
|
281
|
+ await _companyUserRepo.UnitOfWork.SaveChangesAsync(cancellationToken);
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+ //TODO:需要保证事务的原子性、持久性、隔离性
|
|
285
|
+ //获取总部用户设置ProjectId
|
|
286
|
+ await _auth.SetProjectIdForUserAsync(request.ObjUserId, request.ProjectId);
|
|
287
|
+
|
|
288
|
+ await _auth.AddProjectUserRoleAsync(request.UserId, guestRole.Id, request.ProjectId, request.CompanyId);
|
|
289
|
+ }
|
|
290
|
+```
|
|
291
|
+
|
|
292
|
+数据库方面
|
|
293
|
+
|
|
294
|
+```
|
|
295
|
+aspnetusers
|
|
296
|
+ Id:1
|
|
297
|
+ UserName:13800000000
|
|
298
|
+ ProjectIds:9
|
|
299
|
+
|
|
300
|
+aspnetuserroles
|
|
301
|
+ UserId:1
|
|
302
|
+ RoleId:角色为OfficeGuest的aspnetroles的主键
|
|
303
|
+
|
|
304
|
+companyuser
|
|
305
|
+ CompanyId:1
|
|
306
|
+ UserId:1
|
|
307
|
+ RoleId:角色为OfficeGuest的aspnetuserroles主键
|
|
308
|
+ RoleName:OfficeGuest
|
|
309
|
+ IsAllowed: true
|
|
310
|
+ HasRole:true
|
|
311
|
+
|
|
312
|
+projectuserroles
|
|
313
|
+ UserId:1
|
|
314
|
+ RoleId:角色为OfficeGuest的aspnetroles的主键
|
|
315
|
+ ProjectId:9
|
|
316
|
+ CompanyId:1
|
|
317
|
+```
|
|
318
|
+
|
|
319
|
+启用"设置为管理员"
|
|
320
|
+
|
|
321
|
+![](F:\SourceCodes\DDWiki\专题\后端\office9.png)
|
|
322
|
+
|
|
323
|
+点击`确定`按钮。对应的接口在:`api/mine/setUserRole`中。
|
|
324
|
+
|
|
325
|
+```
|
|
326
|
+ var ef = await _dbHelper.GetOfficeContextByProjectIdAsync(request.ProjectId);
|
|
327
|
+ _companyUserRepo = new CompanyUserRepository(ef);
|
|
328
|
+
|
|
329
|
+ var currentCompanyUser = _companyUserRepo.GetAll().FirstOrDefault(t => t.UserId == request.ObjectUserId && t.CompanyId == request.CompanyId);
|
|
330
|
+ var currentRole = await _auth.GetRoleByNameAsync(request.RoleName);
|
|
331
|
+
|
|
332
|
+ if(await _auth.IsOfficeSuperAdminAsync(request.UserId)|| request.RoleName != OfficeConstants.Office_Role_Admin)//超级管理员
|
|
333
|
+ {
|
|
334
|
+ currentCompanyUser.ChangeRole(currentRole.Id, currentRole.Name);
|
|
335
|
+ _companyUserRepo.Update(currentCompanyUser);
|
|
336
|
+ await _companyUserRepo.UnitOfWork.SaveChangesAsync(cancellationToken);
|
|
337
|
+
|
|
338
|
+ await _auth.UpdateProjectUserRoleByRoleNameAsync(currentRole.Id, request.ObjectUserId, request.ProjectId, request.CompanyId);
|
|
339
|
+ }
|
|
340
|
+```
|
|
341
|
+
|
|
342
|
+数据库方面
|
|
343
|
+
|
|
344
|
+```
|
|
345
|
+aspnetusers
|
|
346
|
+ Id:1
|
|
347
|
+ UserName:13800000000
|
|
348
|
+ ProjectIds:9
|
|
349
|
+
|
|
350
|
+aspnetuserroles(似乎后来没用)
|
|
351
|
+ UserId:1
|
|
352
|
+ RoleId:角色为OfficeGuest的aspnetroles的主键
|
|
353
|
+
|
|
354
|
+companyuser
|
|
355
|
+ CompanyId:1
|
|
356
|
+ UserId:1
|
|
357
|
+ RoleId:角色为OfficeAdmin的aspnetuserroles主键
|
|
358
|
+ RoleName:OfficeAdmin
|
|
359
|
+ IsAllowed: true
|
|
360
|
+ HasRole:true
|
|
361
|
+
|
|
362
|
+projectuserroles
|
|
363
|
+ UserId:1
|
|
364
|
+ RoleId:角色为OfficeAdmin的aspnetuserroles主键
|
|
365
|
+ ProjectId:9
|
|
366
|
+ CompanyId:1
|
|
367
|
+```
|
|
368
|
+
|
|
369
|
+至此,总部电工为项目下的某个Company设置Company管理员成功。
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+另外还有
|
|
374
|
+
|
|
375
|
+- Company的管理员设置新的管理员,
|
|
376
|
+- Company的管理员设置操作员
|