Bladeren bron

Merge branch 'master' of http://git.shengws.com/csx/XT_New

csx 4 jaren geleden
bovenliggende
commit
6f1a2f229b

+ 11 - 6
controllers/mobile_api_controllers/mobile_api_base_controller.go Bestand weergeven

@@ -66,13 +66,18 @@ func (this *MobileBaseAPIAuthController) Prepare() {
66 66
 	//		this.StopRun()
67 67
 	//	}
68 68
 	//}
69
+
69 70
 	if adminUserInfo.AppRole != nil {
70
-		app_role, _ := service.FindAppRoleById(adminUserInfo.AppRole.Id)
71
-		if app_role.Status != 1 {
72
-			this.DelSession("mobile_admin_user_info")
73
-			this.Ctx.SetCookie("token_cookie", "")
74
-			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeForbidden)
75
-			this.StopRun()
71
+		if adminUserInfo.AppRole.Id > 0 {
72
+			app_role, _ := service.FindAppRoleById(adminUserInfo.AppRole.Id)
73
+			if app_role != nil {
74
+				if app_role.Status != 1 {
75
+					this.DelSession("mobile_admin_user_info")
76
+					this.Ctx.SetCookie("token_cookie", "")
77
+					this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeForbidden)
78
+					this.StopRun()
79
+				}
80
+			}
76 81
 		}
77 82
 
78 83
 	}

+ 69 - 9
controllers/role_controller.go Bestand weergeven

@@ -1,6 +1,7 @@
1 1
 package controllers
2 2
 
3 3
 import (
4
+	"github.com/jinzhu/gorm"
4 5
 	"strconv"
5 6
 	"strings"
6 7
 	"time"
@@ -75,7 +76,7 @@ func (this *RoleAPIController) GetRoles() {
75 76
 func (this *RoleAPIController) CreateRole() {
76 77
 	name := this.GetString("name")
77 78
 	intro := this.GetString("intro")
78
-	if len(name) == 0 || len(intro) == 0 {
79
+	if len(name) == 0 {
79 80
 		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
80 81
 		return
81 82
 	}
@@ -172,7 +173,7 @@ func (this *RoleAPIController) ModifyRoleStatus() {
172 173
 	}
173 174
 
174 175
 	if enable == false {
175
-		if count, _ := service.RoleAdminUserCount(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleID); count != 0 {
176
+		if count, _ := service.RoleAdminUserCountTwo(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleID); count != 0 {
176 177
 			this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCannotRemoveRole)
177 178
 			return
178 179
 		}
@@ -207,7 +208,44 @@ func (this *RoleAPIController) EditPurviewInitData() {
207 208
 		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
208 209
 		return
209 210
 	}
210
-	purviews, getPurviewsErr := service.GetAllGeneralPurviewVMsProcessed()
211
+
212
+	role, _ := service.GetRoleByRoleID(roleId)
213
+
214
+	purviews_xt, getPurviewsErr := service.GetAllGeneralPurviewVMsProcessed(3)
215
+	purviews_scrm, getPurviewsErr := service.GetAllGeneralPurviewVMsProcessed(6)
216
+	purviews_cdm, getPurviewsErr := service.GetAllGeneralPurviewVMsProcessed(4)
217
+	purviews_mall, getPurviewsErr := service.GetAllGeneralPurviewVMsProcessed(7)
218
+
219
+	purviews_func, getPurviewsErr := service.GetAllGeneralFuncPurviewVMsProcessed()
220
+	for _, item := range purviews_xt {
221
+		for _, childItem := range item.Childs {
222
+			if childItem.Name == "透析记录" {
223
+				childItem.Childs = purviews_func
224
+			}
225
+		}
226
+	}
227
+	scrm := &service.PurviewTreeViewModel{
228
+		ID:     0,
229
+		PID:    0,
230
+		Name:   "SCRM",
231
+		Number: 3,
232
+		Childs: purviews_scrm,
233
+	}
234
+
235
+	cdm := &service.PurviewTreeViewModel{
236
+		ID:     0,
237
+		PID:    0,
238
+		Number: 3,
239
+		Name:   "慢病管理",
240
+		Childs: purviews_cdm,
241
+	}
242
+
243
+	purviews_xt = append(purviews_xt, scrm)
244
+	purviews_xt = append(purviews_xt, cdm)
245
+	purviews_xt = append(purviews_xt, purviews_mall...)
246
+
247
+	//purviews_xt = append(purviews_xt,purviews_cdm...)
248
+
211 249
 	if getPurviewsErr != nil {
212 250
 		//beego.Error("获取所有权限时出错:", getPurviewsErr)
213 251
 		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
@@ -220,9 +258,31 @@ func (this *RoleAPIController) EditPurviewInitData() {
220 258
 		return
221 259
 	}
222 260
 
261
+	roleFuncPurview, getFuncRPIdsErr := service.GetRoleFuncPurview(roleId)
262
+	if getFuncRPIdsErr == gorm.ErrRecordNotFound {
263
+		//beego.Error("获取角色的权限时出错:", getRPIdsErr)
264
+		if roleFuncPurview.ID == 0 {
265
+			rolePurviewIdStr = rolePurviewIdStr
266
+
267
+		} else {
268
+			rolePurviewIdStr = rolePurviewIdStr + "," + roleFuncPurview.PurviewIds
269
+		}
270
+	} else if getFuncRPIdsErr != nil {
271
+		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
272
+		return
273
+
274
+	} else {
275
+		if roleFuncPurview.ID == 0 {
276
+			rolePurviewIdStr = rolePurviewIdStr
277
+		} else {
278
+			rolePurviewIdStr = rolePurviewIdStr + "," + roleFuncPurview.PurviewIds
279
+		}
280
+	}
281
+
223 282
 	this.ServeSuccessJSON(map[string]interface{}{
224
-		"purviews":         purviews,
225
-		"role_purview_ids": rolePurviewIdStr,
283
+		"purviews":         purviews_xt,
284
+		"role":             role,
285
+		"role_purview_ids": rolePurviewIdStr + "," + roleFuncPurview.PurviewIds,
226 286
 	})
227 287
 }
228 288
 
@@ -231,18 +291,18 @@ func (this *RoleAPIController) EditPurviewInitData() {
231 291
 // @param purview_ids:string
232 292
 func (this *RoleAPIController) EditPurview() {
233 293
 	adminUserInfo := this.GetAdminUserInfo()
234
-	//if adminUserInfo.AdminUser.IsSuperAdmin == false {
235
-	//	this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
236
-	//	return
237
-	//}
238 294
 	roleId, _ := this.GetInt64("role_id")
239 295
 	purviewIds := this.GetString("purview_ids")
296
+	funcPurviewIds := this.GetString("func_purview_ids")
297
+
240 298
 	if roleId <= 0 {
241 299
 		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
242 300
 		return
243 301
 	}
244 302
 
245 303
 	err := service.SaveRolePurviewIds(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId, purviewIds)
304
+	err = service.SaveFuncRolePurviewIds(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, roleId, funcPurviewIds)
305
+
246 306
 	if err != nil {
247 307
 		//beego.Error("设置角色的权限时出错:", err)
248 308
 		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)

+ 65 - 14
service/role_service.go Bestand weergeven

@@ -2,7 +2,6 @@ package service
2 2
 
3 3
 import (
4 4
 	"database/sql"
5
-	"fmt"
6 5
 	"strconv"
7 6
 	"strings"
8 7
 	"time"
@@ -349,11 +348,12 @@ type PurviewTreeViewModel struct {
349 348
 	PID    int64                   `json:"pid"`
350 349
 	Name   string                  `json:"name"`
351 350
 	Childs []*PurviewTreeViewModel `json:"childs"`
351
+	Number int64                   `json:"number"`
352 352
 }
353 353
 
354
-func GetAllGeneralPurviewVMsProcessed() ([]*PurviewTreeViewModel, error) {
354
+func GetAllGeneralPurviewVMsProcessed(module int) ([]*PurviewTreeViewModel, error) {
355 355
 	var originPurviews []*models.Purview
356
-	getPurviewErr := readUserDb.Model(models.Purview{}).Where("module = 3 AND status = 1 AND super_admin_exclusive = 0").Order("listorder asc").Order("id asc").Find(&originPurviews).Error
356
+	getPurviewErr := readUserDb.Model(models.Purview{}).Where("module = ? AND status = 1 AND super_admin_exclusive = 0", module).Order("listorder asc").Order("id asc").Find(&originPurviews).Error
357 357
 	if getPurviewErr != nil {
358 358
 		return nil, getPurviewErr
359 359
 	}
@@ -364,9 +364,10 @@ func GetAllGeneralPurviewVMsProcessed() ([]*PurviewTreeViewModel, error) {
364 364
 		// warning:下面这个算法只适用最多两层树形结构的菜单,对于两层以上的会丢失掉第三层及其以下的节点
365 365
 		// 因为取出 originPurviews 的时候已经排过序了,所以顶级节点肯定最先处理,不需要担心子节点比父节点先处理
366 366
 		pvm := &PurviewTreeViewModel{
367
-			ID:   purview.Id,
368
-			PID:  purview.Parentid,
369
-			Name: purview.Name,
367
+			ID:     purview.Id,
368
+			PID:    purview.Parentid,
369
+			Name:   purview.Name,
370
+			Number: 2,
370 371
 		}
371 372
 		if purview.Parentid == 0 {
372 373
 			purviewVMs = append(purviewVMs, pvm)
@@ -387,6 +388,29 @@ func GetAllGeneralPurviewVMsProcessed() ([]*PurviewTreeViewModel, error) {
387 388
 	return purviewVMs, nil
388 389
 }
389 390
 
391
+func GetAllGeneralFuncPurviewVMsProcessed() ([]*PurviewTreeViewModel, error) {
392
+	var originPurviews []*models.SgjUserOperatePurview
393
+	getPurviewErr := readUserDb.Model(models.SgjUserOperatePurview{}).Where(" status = 1").Order("id asc").Find(&originPurviews).Error
394
+	if getPurviewErr != nil {
395
+		return nil, getPurviewErr
396
+	}
397
+	// 加工这些规则:树形化
398
+	purviewVMs := make([]*PurviewTreeViewModel, 0)
399
+	for _, purview := range originPurviews {
400
+		// warning:下面这个算法只适用最多两层树形结构的菜单,对于两层以上的会丢失掉第三层及其以下的节点
401
+		// 因为取出 originPurviews 的时候已经排过序了,所以顶级节点肯定最先处理,不需要担心子节点比父节点先处理
402
+		pvm := &PurviewTreeViewModel{
403
+			ID:   purview.ID,
404
+			PID:  purview.Parentid,
405
+			Name: purview.Name,
406
+		}
407
+		purviewVMs = append(purviewVMs, pvm)
408
+
409
+	}
410
+
411
+	return purviewVMs, nil
412
+}
413
+
390 414
 func GetRolePurviewIds(roleID int64) (string, error) {
391 415
 	var rolePurview models.RolePurview
392 416
 	err := readUserDb.Where("role_id = ?", roleID).First(&rolePurview).Error
@@ -426,6 +450,32 @@ func SaveRolePurviewIds(orgID int64, appID int64, roleID int64, purviewIds strin
426 450
 	return tx.Commit().Error
427 451
 }
428 452
 
453
+func SaveFuncRolePurviewIds(orgID int64, appID int64, roleID int64, funcPurviewIds string) error {
454
+	var rolePurview models.SgjUserRoleFuncPurview
455
+	getRPErr := readUserDb.Where("org_id = ? AND app_id = ? AND role_id = ?", orgID, appID, roleID).First(&rolePurview).Error
456
+	if getRPErr != nil {
457
+		if getRPErr == gorm.ErrRecordNotFound {
458
+			rolePurview = models.SgjUserRoleFuncPurview{
459
+				RoleId: roleID,
460
+				OrgId:  orgID,
461
+				AppId:  appID,
462
+				Status: 1,
463
+				Ctime:  time.Now().Unix(),
464
+			}
465
+		} else {
466
+			return getRPErr
467
+		}
468
+	}
469
+	rolePurview.PurviewIds = funcPurviewIds
470
+	rolePurview.Mtime = time.Now().Unix()
471
+	tx := writeUserDb.Begin()
472
+	if err := tx.Save(&rolePurview).Error; err != nil {
473
+		tx.Rollback()
474
+		return err
475
+	}
476
+	return tx.Commit().Error
477
+}
478
+
429 479
 func ModifyAdminUserInfo(adminUserID int64, orgID int64, appID int64, name string, avatar string, newPassword string) error {
430 480
 	tx := writeUserDb.Begin()
431 481
 	editInfoErr := tx.Exec("update sgj_user_admin_role set user_name = ?, avatar = ?, mtime = ? where admin_user_id = ? and org_id = ?", name, avatar, time.Now().Unix(), adminUserID, orgID).Error
@@ -457,7 +507,6 @@ func GetAllOrgValidRoles(orgID int64, isSubSuperAdmin bool) ([]*models.Role, err
457 507
 	var roles []*models.Role
458 508
 	db := readUserDb.Model(models.Org{})
459 509
 	if isSubSuperAdmin {
460
-		fmt.Println("1111")
461 510
 		db = db.Where("role_name != '子管理员' AND is_system  != 1")
462 511
 	}
463 512
 	err := db.Where("org_id = ? AND status = 1", orgID).
@@ -557,12 +606,14 @@ func GetAllAdminUsersAndRole(orgID int64, appID int64, page int, count int) ([]*
557 606
 	}
558 607
 
559 608
 	for _, items := range viewModels {
560
-		ids := strings.Split(items.RoleIds, ",")
561
-		for _, ids := range ids {
562
-			id, _ := strconv.ParseInt(ids, 10, 64)
563
-			role, _ := GetRoleByRoleID(id)
564
-			if role.IsSystem == 1 && role.RoleName == "子管理员" {
565
-				items.IsSubAdmin = true
609
+		if len(items.RoleIds) > 0 {
610
+			ids := strings.Split(items.RoleIds, ",")
611
+			for _, ids := range ids {
612
+				id, _ := strconv.ParseInt(ids, 10, 64)
613
+				role, _ := GetRoleByRoleID(id)
614
+				if role.IsSystem == 1 && role.RoleName == "子管理员" {
615
+					items.IsSubAdmin = true
616
+				}
566 617
 			}
567 618
 		}
568 619
 	}
@@ -590,7 +641,7 @@ func SaveRole(role *models.Role) (err error) {
590 641
 func RoleAdminUserCountTwo(orgID int64, appID int64, roleID int64) (int, error) {
591 642
 	var count int
592 643
 	err := readUserDb.Model(models.App_Role{}).
593
-		Where("org_id = ? AND app_id = ? AND status = 1 AND find_in_set(?, role_ids)", orgID, appID, roleID).
644
+		Where("org_id = ? AND app_id = ? AND find_in_set(?, role_ids)", orgID, appID, roleID).
594 645
 		Count(&count).
595 646
 		Error
596 647
 	if err != nil {