作者 tangxvhui

更新

@@ -3,6 +3,7 @@ module gitlab.fjmaimaimai.com/mmm-go/partnermg @@ -3,6 +3,7 @@ module gitlab.fjmaimaimai.com/mmm-go/partnermg
3 go 1.14 3 go 1.14
4 4
5 require ( 5 require (
  6 + github.com/GeeTeam/gt3-golang-sdk v0.0.0-20200116043922-446ca8a507d2
6 github.com/ajg/form v1.5.1 // indirect 7 github.com/ajg/form v1.5.1 // indirect
7 github.com/astaxie/beego v1.12.1 8 github.com/astaxie/beego v1.12.1
8 github.com/dgrijalva/jwt-go v3.2.0+incompatible 9 github.com/dgrijalva/jwt-go v3.2.0+incompatible
  1 +package query
  2 +
  3 +//ListAdminUserQuery 获取用户列表
  4 +type ListAdminPermissionQuery struct {
  5 + NotCode []string
  6 + ParentId interface{}
  7 +}
  1 +package service
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/query"
  5 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
  6 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
  7 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
  8 +)
  9 +
  10 +//AdminUserService 管理员相关服务
  11 +type AdminPermissionService struct {
  12 +}
  13 +
  14 +func NewAdminPermissionService(option map[string]interface{}) *AdminPermissionService {
  15 + newAdminPermissionService := new(AdminPermissionService)
  16 + return newAdminPermissionService
  17 +}
  18 +
  19 +func (adminPermissionSrv AdminPermissionService) ListAdminPermission(queryOption query.ListAdminPermissionQuery) ([]domain.AdminPermission, error) {
  20 + //实际业务
  21 + transactionContext, err := factory.CreateTransactionContext(nil)
  22 + if err != nil {
  23 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  24 + }
  25 + if err := transactionContext.StartTransaction(); err != nil {
  26 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  27 + }
  28 + defer func() {
  29 + transactionContext.RollbackTransaction()
  30 + }()
  31 + var (
  32 + permissionRepository domain.AdminPermissionRepository
  33 + permissions []domain.AdminPermission
  34 + )
  35 + if value, err := factory.CreateAdminPermissionRepository(map[string]interface{}{
  36 + "transactionContext": transactionContext,
  37 + }); err != nil {
  38 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  39 + } else {
  40 + permissionRepository = value
  41 + }
  42 + permissions, err = permissionRepository.Find(domain.AdminPermissionFindQuery{
  43 + NotCode: queryOption.NotCode,
  44 + ParentId: queryOption.ParentId,
  45 + })
  46 + if err != nil {
  47 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  48 + }
  49 + return permissions, nil
  50 +}
  1 +package command
  2 +
  3 +type SaveAdminUserCommand struct {
  4 + Id int64 `json:"id"`
  5 + // 员工姓名
  6 + Name string `json:"name"`
  7 + // 员工账号
  8 + Account string `json:"account" `
  9 + //密码
  10 + Password string `json:"password"`
  11 + // 员工角色
  12 + PermissionId []int `json:"PermissionId"`
  13 +
  14 + IsUsable bool `json:"isUsable"`
  15 +}
1 package query 1 package query
2 2
3 type GetAdminUserQuery struct { 3 type GetAdminUserQuery struct {
4 - Id int64 `json:"id"`  
5 - Admin_Account string `json:"admin_account"` 4 + Id int64 `json:"id"`
  5 + AdminAccount string `json:"adminAccount"`
6 } 6 }
@@ -3,9 +3,9 @@ package query @@ -3,9 +3,9 @@ package query
3 //ListAdminUserQuery 获取用户列表 3 //ListAdminUserQuery 获取用户列表
4 type ListAdminUserQuery struct { 4 type ListAdminUserQuery struct {
5 //账号匹配 5 //账号匹配
6 - AdminAccountMatch string `json:"admin_account_match" form:"account_match"` 6 + AdminAccountMatch string `json:"adminAccountMatch" `
7 // 查询偏离量 7 // 查询偏离量
8 - Offset int `json:"offset" form:"offset"` 8 + Offset int `json:"offset" `
9 // 查询限制 9 // 查询限制
10 - Limit int `json:"limit" form:"limit"` 10 + Limit int `json:"limit"`
11 } 11 }
1 package service 1 package service
2 2
3 import ( 3 import (
4 - "github.com/linmadan/egglib-go/core/application" 4 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/command"
5 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query" 5 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query"
  6 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
6 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" 7 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
  8 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
7 ) 9 )
8 10
9 //AdminUserService 管理员相关服务 11 //AdminUserService 管理员相关服务
@@ -15,12 +17,155 @@ func NewAdminUserService(option map[string]interface{}) *AdminUserService { @@ -15,12 +17,155 @@ func NewAdminUserService(option map[string]interface{}) *AdminUserService {
15 return newAdminUserService 17 return newAdminUserService
16 } 18 }
17 19
18 -func (adminUserSrv AdminUserService) GetAdminUser(getAdminUserQuery *query.GetAdminUserQuery) (interface{}, error) { 20 +func (adminUserSrv AdminUserService) GetAdminUser(getAdminUserQuery *query.GetAdminUserQuery) (*domain.AdminUser, error) {
19 //实际业务 21 //实际业务
20 - var err error 22 + transactionContext, err := factory.CreateTransactionContext(nil)
21 if err != nil { 23 if err != nil {
22 - return nil, application.ThrowError(application.BUSINESS_ERROR, "业务逻辑错误") 24 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  25 + }
  26 + if err := transactionContext.StartTransaction(); err != nil {
  27 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  28 + }
  29 + defer func() {
  30 + transactionContext.RollbackTransaction()
  31 + }()
  32 + var (
  33 + adminuserRepository domain.AdminUserRepository
  34 + adminuser *domain.AdminUser
  35 + )
  36 + if value, err := factory.CreateAdminUserRepository(map[string]interface{}{
  37 + "transactionContext": transactionContext,
  38 + }); err != nil {
  39 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  40 + } else {
  41 + adminuserRepository = value
  42 + }
  43 + adminuser, err = adminuserRepository.FindOne(domain.AdminUserFindOneQuery{
  44 + AccountEqual: getAdminUserQuery.AdminAccount,
  45 + AdminUserId: getAdminUserQuery.Id,
  46 + })
  47 + if err != nil {
  48 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  49 + }
  50 + return adminuser, nil
  51 +}
  52 +
  53 +func (adminUserSrv AdminUserService) SaveAdminUser(saveUserCmd *command.SaveAdminUserCommand) (*domain.AdminUser, error) {
  54 + //实际业务
  55 + transactionContext, err := factory.CreateTransactionContext(nil)
  56 + if err != nil {
  57 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  58 + }
  59 + if err := transactionContext.StartTransaction(); err != nil {
  60 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  61 + }
  62 + defer func() {
  63 + transactionContext.RollbackTransaction()
  64 + }()
  65 + var (
  66 + adminuserRepository domain.AdminUserRepository
  67 + adminuser *domain.AdminUser
  68 + )
  69 + if value, err := factory.CreateAdminUserRepository(map[string]interface{}{
  70 + "transactionContext": transactionContext,
  71 + }); err != nil {
  72 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  73 + } else {
  74 + adminuserRepository = value
  75 + }
  76 +
  77 + //获取权限
  78 + var (
  79 + permissionRepository domain.AdminPermissionRepository
  80 + permissions []domain.AdminPermission
  81 + )
  82 + if value, err := factory.CreateAdminPermissionRepository(map[string]interface{}{
  83 + "transactionContext": transactionContext,
  84 + }); err != nil {
  85 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  86 + } else {
  87 + permissionRepository = value
  88 + }
  89 + permissions, err = permissionRepository.Find(domain.AdminPermissionFindQuery{
  90 + IdsIn: saveUserCmd.PermissionId,
  91 + })
  92 + if err != nil {
  93 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
23 } 94 }
24 95
25 - return domain.AdminUser{}, nil 96 + permissionBases := []domain.AdminPermissionBase{}
  97 + for i := range permissions {
  98 + p := domain.AdminPermissionBase{
  99 + Id: permissions[i].Id, Code: permissions[i].Code,
  100 + }
  101 + permissionBases = append(permissionBases, p)
  102 + }
  103 +
  104 + if saveUserCmd.Id > 0 {
  105 + adminuser, err = adminuserRepository.FindOne(domain.AdminUserFindOneQuery{
  106 + AdminUserId: saveUserCmd.Id,
  107 + })
  108 + if err != nil {
  109 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  110 + }
  111 + adminuser.Account = saveUserCmd.Account
  112 + adminuser.AdminName = saveUserCmd.Name
  113 + adminuser.IsUsable = saveUserCmd.IsUsable
  114 + adminuser.Permission = permissionBases
  115 +
  116 + } else {
  117 + adminuser = &domain.AdminUser{
  118 + Id: saveUserCmd.Id,
  119 + Account: saveUserCmd.Account,
  120 + Password: saveUserCmd.Password,
  121 + AdminName: saveUserCmd.Name,
  122 + IsUsable: saveUserCmd.IsUsable,
  123 + Permission: permissionBases,
  124 + }
  125 + }
  126 + adminuser, err = adminuserRepository.Save(adminuser)
  127 + if err != nil {
  128 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  129 + }
  130 + return adminuser, nil
  131 +}
  132 +
  133 +func (adminUserSrv AdminUserService) PageListAdminUser(listAdminUserQuery *query.ListAdminUserQuery) ([]domain.AdminUser, int, error) {
  134 + transactionContext, err := factory.CreateTransactionContext(nil)
  135 + if err != nil {
  136 + return nil, 0, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  137 + }
  138 + if err := transactionContext.StartTransaction(); err != nil {
  139 + return nil, 0, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  140 + }
  141 + defer func() {
  142 + transactionContext.RollbackTransaction()
  143 + }()
  144 + var (
  145 + adminuserRepository domain.AdminUserRepository
  146 + adminusers []domain.AdminUser
  147 + cnt int
  148 + )
  149 + if value, err := factory.CreateAdminUserRepository(map[string]interface{}{
  150 + "transactionContext": transactionContext,
  151 + }); err != nil {
  152 + return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  153 + } else {
  154 + adminuserRepository = value
  155 + }
  156 + adminusers, err = adminuserRepository.Find(domain.AdminUserFindQuery{
  157 + AccountLike: listAdminUserQuery.AdminAccountMatch,
  158 + Offset: listAdminUserQuery.Offset,
  159 + Limit: listAdminUserQuery.Limit,
  160 + })
  161 + if err != nil {
  162 + return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  163 + }
  164 + cnt, err = adminuserRepository.CountAll(domain.AdminUserFindQuery{
  165 + AccountLike: listAdminUserQuery.AdminAccountMatch,
  166 + })
  167 + if err != nil {
  168 + return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  169 + }
  170 + return adminusers, cnt, nil
26 } 171 }
@@ -7,19 +7,28 @@ import ( @@ -7,19 +7,28 @@ import (
7 ) 7 )
8 8
9 //CreatePartnerInfoRepository 合伙人信息 9 //CreatePartnerInfoRepository 合伙人信息
10 -func CreatePartnerInfoRepository(options map[string]interface{}) (domain.PartnerInfoRepository, error) { 10 +// func CreatePartnerInfoRepository(options map[string]interface{}) (domain.PartnerInfoRepository, error) {
  11 +// var transactionContext *transaction.TransactionContext
  12 +// if value, ok := options["transactionContext"]; ok {
  13 +// transactionContext = value.(*transaction.TransactionContext)
  14 +// }
  15 +// return repository.NewPartnerInfoRepository(transactionContext)
  16 +// }
  17 +
  18 +//CreateAdminUserRepository 管理员信息
  19 +func CreateAdminUserRepository(options map[string]interface{}) (domain.AdminUserRepository, error) {
11 var transactionContext *transaction.TransactionContext 20 var transactionContext *transaction.TransactionContext
12 if value, ok := options["transactionContext"]; ok { 21 if value, ok := options["transactionContext"]; ok {
13 transactionContext = value.(*transaction.TransactionContext) 22 transactionContext = value.(*transaction.TransactionContext)
14 } 23 }
15 - return repository.NewPartnerInfoRepository(transactionContext) 24 + return repository.NewAdminUserRepository(transactionContext)
16 } 25 }
17 26
18 //CreateAdminUserRepository 管理员信息 27 //CreateAdminUserRepository 管理员信息
19 -func CreateAdminUserRepository(options map[string]interface{}) (domain.AdminUserRepository, error) { 28 +func CreateAdminPermissionRepository(options map[string]interface{}) (domain.AdminPermissionRepository, error) {
20 var transactionContext *transaction.TransactionContext 29 var transactionContext *transaction.TransactionContext
21 if value, ok := options["transactionContext"]; ok { 30 if value, ok := options["transactionContext"]; ok {
22 transactionContext = value.(*transaction.TransactionContext) 31 transactionContext = value.(*transaction.TransactionContext)
23 } 32 }
24 - return repository.NewAdminUserRepository(transactionContext) 33 + return repository.NewAdminPermissionRepository(transactionContext)
25 } 34 }
1 package domain 1 package domain
2 2
3 //权限代码 3 //权限代码
  4 +//权限配置 1合作管理人 2订单管理 3分红管理 4管理员管理
4 const ( 5 const (
5 - PERMINSSION_ADMIN_USER string = "ADMIN_USER"  
6 - PERMINSSION_PARTNER string = "PARTNER" 6 + PERMINSSION_PARTNER string = "1合作管理人" //1合作管理人
  7 + PERMISSION_ORDER string = "2订单管理" //2订单管理
  8 + PERMISSION_DIVIDEND string = "3分红管理" //3分红管理
  9 + PERMINSSION_ADMIN_USER string = "4管理员管理" //4管理员管理
7 ) 10 )
8 11
9 -//权限数据  
10 -var ConstAdminPermissions = map[string]AdminPermission{  
11 - PERMINSSION_ADMIN_USER: AdminPermission{  
12 - Code: PERMINSSION_ADMIN_USER,  
13 - Name: "管理员管理",  
14 - },  
15 - PERMINSSION_PARTNER: AdminPermission{  
16 - Code: PERMINSSION_PARTNER,  
17 - Name: "合伙人管理",  
18 - }, 12 +type AdminPermissionBase struct {
  13 + Id int `json:"id"`
  14 + Code string `json:"code"`
19 } 15 }
20 16
21 // 权限结构 17 // 权限结构
22 type AdminPermission struct { 18 type AdminPermission struct {
23 - Code string `json:"code"`  
24 - Name string `json:"name"` 19 + Id int `json:"id"`
  20 + ParentId int `json:"parentId"`
  21 + Code string `json:"code"`
  22 + Name string `json:"name"`
  23 + Sort int `json:"sort"`
  24 + Icon string `json:"icon"`
  25 +}
  26 +
  27 +type AdminPermissionFindQuery struct {
  28 + IdsIn []int
  29 + NotCode []string
  30 + ParentId interface{}
25 } 31 }
26 32
27 -func (permission AdminPermission) GetPermissions(codes []string) []AdminPermission {  
28 - newPermissions := []AdminPermission{}  
29 - if len(codes) == 0 {  
30 - return newPermissions  
31 - }  
32 - for i := range codes {  
33 - code := codes[i]  
34 - if _, ok := ConstAdminPermissions[code]; ok {  
35 - newPermissions = append(newPermissions, ConstAdminPermissions[code])  
36 - }  
37 - }  
38 - return newPermissions 33 +type AdminPermissionRepository interface {
  34 + Find(queryOptions AdminPermissionFindQuery) ([]AdminPermission, error)
39 } 35 }
1 package domain 1 package domain
2 2
  3 +import "time"
  4 +
3 //AdminUser 管理员 5 //AdminUser 管理员
4 type AdminUser struct { 6 type AdminUser struct {
5 //id 7 //id
@@ -9,15 +11,15 @@ type AdminUser struct { @@ -9,15 +11,15 @@ type AdminUser struct {
9 //密码 11 //密码
10 Password string `json:"password"` 12 Password string `json:"password"`
11 //管理员名称 13 //管理员名称
12 - AdminName string `json:"admin_name"` 14 + AdminName string `json:"adminName"`
13 //是否是默认系统账号 15 //是否是默认系统账号
14 - IsDefault bool `json:"is_default"` 16 + IsDefault bool `json:"isDefault"`
15 //账号是否可用 17 //账号是否可用
16 - IsUsable bool `json:"is_userable"` 18 + IsUsable bool `json:"isUserable"`
17 //创建时间 19 //创建时间
18 - CreateAt string `json:"create_at"`  
19 - //用户权限  
20 - Permission []AdminPermission `json:"permission"` 20 + CreateAt time.Time `json:"createAt"`
  21 + //用户权限id
  22 + Permission []AdminPermissionBase `json:"permission"`
21 } 23 }
22 24
23 type AdminUserFindQuery struct { 25 type AdminUserFindQuery struct {
@@ -32,8 +34,8 @@ type AdminUserFindOneQuery struct { @@ -32,8 +34,8 @@ type AdminUserFindOneQuery struct {
32 } 34 }
33 35
34 type AdminUserRepository interface { 36 type AdminUserRepository interface {
35 - // Save(*AdminUser) (*AdminUser, error)  
36 - // Remove(user *AdminUser) (*AdminUser, error) 37 + Save(*AdminUser) (*AdminUser, error)
37 FindOne(qureyOptions AdminUserFindOneQuery) (*AdminUser, error) 38 FindOne(qureyOptions AdminUserFindOneQuery) (*AdminUser, error)
38 - // Find(queryOptions AdminUserFindQuery) (int, []*AdminUser, error) 39 + Find(queryOptions AdminUserFindQuery) ([]AdminUser, error)
  40 + CountAll(queryOption AdminUserFindQuery) (int, error)
39 } 41 }
  1 +package models
  2 +
  3 +type AdminPermission struct {
  4 + tableName struct{} `pg:"admin_permisssion"`
  5 + //id
  6 + Id int `pg:",pk"`
  7 + //权限名称、菜单名称
  8 + Name string
  9 + //权限编码
  10 + Code string
  11 + //父级id
  12 + ParentId int
  13 + //排序编号
  14 + Sort int
  15 + //图标
  16 + Icon string
  17 +}
@@ -4,6 +4,8 @@ import ( @@ -4,6 +4,8 @@ import (
4 "context" 4 "context"
5 "time" 5 "time"
6 6
  7 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
  8 +
7 "github.com/go-pg/pg/v10" 9 "github.com/go-pg/pg/v10"
8 ) 10 )
9 11
@@ -12,7 +14,7 @@ type AdminUser struct { @@ -12,7 +14,7 @@ type AdminUser struct {
12 //id 14 //id
13 Id int64 `pg:",pk"` 15 Id int64 `pg:",pk"`
14 //用户账号 16 //用户账号
15 - AdminAccount string `pg:",unique"` 17 + Account string `pg:",unique"`
16 //用户名称 18 //用户名称
17 AdminName string 19 AdminName string
18 //账号密码 20 //账号密码
@@ -20,7 +22,9 @@ type AdminUser struct { @@ -20,7 +22,9 @@ type AdminUser struct {
20 //是否是默认账号 22 //是否是默认账号
21 IsDefault bool `pg:",use_zero"` 23 IsDefault bool `pg:",use_zero"`
22 //账号是否可用 24 //账号是否可用
23 - IsUsable bool `json:"is_userable"` 25 + IsUsable bool
  26 + //用户的权限
  27 + Permission []domain.AdminPermissionBase
24 28
25 CreateAt time.Time 29 CreateAt time.Time
26 UpdateAt time.Time 30 UpdateAt time.Time
  1 +package repository
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
  7 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
  8 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
  9 +)
  10 +
  11 +type AdminPermissionRepository struct {
  12 + transactionContext *transaction.TransactionContext
  13 +}
  14 +
  15 +var (
  16 + _ domain.AdminPermissionRepository = (*AdminPermissionRepository)(nil)
  17 +)
  18 +
  19 +func NewAdminPermissionRepository(transactionContext *transaction.TransactionContext) (*AdminPermissionRepository, error) {
  20 + if transactionContext == nil {
  21 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  22 + }
  23 + return &AdminPermissionRepository{transactionContext: transactionContext}, nil
  24 +}
  25 +func (reponsitory AdminPermissionRepository) transformPgModelToDomainModel(permissionModel *models.AdminPermission) (domain.AdminPermission, error) {
  26 + result := domain.AdminPermission{
  27 + Id: permissionModel.Id,
  28 + ParentId: permissionModel.ParentId,
  29 + Name: permissionModel.Name,
  30 + Icon: permissionModel.Icon,
  31 + Code: permissionModel.Code,
  32 + }
  33 + return result, nil
  34 +}
  35 +
  36 +func (reponsitory AdminPermissionRepository) Find(queryOptions domain.AdminPermissionFindQuery) ([]domain.AdminPermission, error) {
  37 + db := reponsitory.transactionContext.PgDd
  38 + permissionModels := make([]models.AdminPermission, 0)
  39 + query := db.Model(&permissionModels)
  40 + if queryOptions.ParentId != nil {
  41 + query = query.Where("parent_id=?", queryOptions.ParentId)
  42 + }
  43 + if len(queryOptions.NotCode) > 0 {
  44 + query = query.WhereIn("code not in (?) ", queryOptions.NotCode)
  45 + }
  46 + if len(queryOptions.IdsIn) > 0 {
  47 + query = query.WhereIn(" id in (?) ", queryOptions.IdsIn)
  48 + }
  49 + if err := query.Select(); err != nil {
  50 + return nil, err
  51 + }
  52 + var result []domain.AdminPermission
  53 + for i := range permissionModels {
  54 + v, _ := reponsitory.transformPgModelToDomainModel(&permissionModels[i])
  55 + result = append(result, v)
  56 + }
  57 + return result, nil
  58 +}
1 package repository 1 package repository
2 2
3 import ( 3 import (
  4 + "fmt"
  5 +
4 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" 6 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
5 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models" 7 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
6 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction" 8 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
@@ -16,12 +18,21 @@ var ( @@ -16,12 +18,21 @@ var (
16 18
17 func NewAdminUserRepository(transactionContext *transaction.TransactionContext) (*AdminUserRepository, error) { 19 func NewAdminUserRepository(transactionContext *transaction.TransactionContext) (*AdminUserRepository, error) {
18 if transactionContext == nil { 20 if transactionContext == nil {
19 - return nil, ERR_EMPTY_TC 21 + return nil, fmt.Errorf("transactionContext参数不能为nil")
20 } 22 }
21 return &AdminUserRepository{transactionContext: transactionContext}, nil 23 return &AdminUserRepository{transactionContext: transactionContext}, nil
22 } 24 }
23 func (reponsitory AdminUserRepository) transformPgModelToDomainModel(adminuserModel *models.AdminUser) (domain.AdminUser, error) { 25 func (reponsitory AdminUserRepository) transformPgModelToDomainModel(adminuserModel *models.AdminUser) (domain.AdminUser, error) {
24 - result := domain.AdminUser{} 26 + result := domain.AdminUser{
  27 + Id: adminuserModel.Id,
  28 + Account: adminuserModel.Account,
  29 + AdminName: adminuserModel.AdminName,
  30 + IsDefault: adminuserModel.IsDefault,
  31 + CreateAt: adminuserModel.CreateAt,
  32 + IsUsable: adminuserModel.IsUsable,
  33 + Password: adminuserModel.Password,
  34 + Permission: adminuserModel.Permission,
  35 + }
25 return result, nil 36 return result, nil
26 } 37 }
27 38
@@ -43,3 +54,94 @@ func (reponsitory AdminUserRepository) FindOne(queryOption domain.AdminUserFindO @@ -43,3 +54,94 @@ func (reponsitory AdminUserRepository) FindOne(queryOption domain.AdminUserFindO
43 adminUser, err := reponsitory.transformPgModelToDomainModel(adminuserModel) 54 adminUser, err := reponsitory.transformPgModelToDomainModel(adminuserModel)
44 return &adminUser, err 55 return &adminUser, err
45 } 56 }
  57 +
  58 +func (reponsitory AdminUserRepository) updateAdminUser(adminuser *domain.AdminUser) (*domain.AdminUser, error) {
  59 + tx := reponsitory.transactionContext.PgTx
  60 + adminUserModel := &models.AdminUser{
  61 + Id: adminuser.Id,
  62 + AdminName: adminuser.AdminName,
  63 + Password: adminuser.Password,
  64 + IsUsable: adminuser.IsUsable,
  65 + Permission: adminuser.Permission,
  66 + }
  67 + _, err := tx.Model(adminUserModel).
  68 + Where("id=?", adminUserModel.Id).
  69 + Column("admin_name", "password", "is_usable", "permission").
  70 + Update()
  71 + if err != nil {
  72 + return nil, fmt.Errorf("更新用户数据失败:%s", err)
  73 + }
  74 + return adminuser, nil
  75 +}
  76 +
  77 +func (reponsitory AdminUserRepository) addAdminUser(adminuser *domain.AdminUser) (*domain.AdminUser, error) {
  78 + tx := reponsitory.transactionContext.PgTx
  79 + adminuserModel := &models.AdminUser{
  80 + Account: adminuser.Account,
  81 + AdminName: adminuser.AdminName,
  82 + Password: adminuser.Password,
  83 + IsDefault: false,
  84 + IsUsable: true,
  85 + Permission: adminuser.Permission,
  86 + }
  87 + //添加用户数据
  88 + _, err := tx.Model(adminuserModel).Insert()
  89 + if err != nil {
  90 + return nil, err
  91 + }
  92 + return nil, nil
  93 +}
  94 +
  95 +func (reponsitory AdminUserRepository) Save(adminuser *domain.AdminUser) (*domain.AdminUser, error) {
  96 + if adminuser.Id == 0 {
  97 + return reponsitory.addAdminUser(adminuser)
  98 + } else {
  99 + return reponsitory.updateAdminUser(adminuser)
  100 + }
  101 + return adminuser, nil
  102 +}
  103 +
  104 +func (reponsitory AdminUserRepository) Find(queryOption domain.AdminUserFindQuery) ([]domain.AdminUser, error) {
  105 + db := reponsitory.transactionContext.PgDd
  106 + adminuserModels := []models.AdminUser{}
  107 + query := db.Model(&adminuserModels)
  108 + if len(queryOption.AccountLike) > 0 {
  109 + query = query.Where("admin_account like ?", "%"+queryOption.AccountLike)
  110 + }
  111 +
  112 + if queryOption.Offset > -1 {
  113 + query = query.Offset(queryOption.Offset)
  114 + }
  115 + if queryOption.Limit > 0 {
  116 + query = query.Limit(queryOption.Limit)
  117 + } else {
  118 + query = query.Limit(20)
  119 + }
  120 + var (
  121 + err error
  122 + adminuserReturn = make([]domain.AdminUser, 0)
  123 + )
  124 + err = query.Order("id DESC").Select()
  125 + if err != nil {
  126 + return adminuserReturn, err
  127 + }
  128 + for i := range adminuserModels {
  129 + domainAdminUser, err := reponsitory.transformPgModelToDomainModel(&adminuserModels[i])
  130 + if err != nil {
  131 + return adminuserReturn, err
  132 + }
  133 + adminuserReturn = append(adminuserReturn, domainAdminUser)
  134 + }
  135 + return adminuserReturn, nil
  136 +}
  137 +
  138 +func (reponsitory AdminUserRepository) CountAll(queryOption domain.AdminUserFindQuery) (int, error) {
  139 + db := reponsitory.transactionContext.PgDd
  140 + adminuserModels := []models.AdminUser{}
  141 + query := db.Model(&adminuserModels)
  142 + if len(queryOption.AccountLike) > 0 {
  143 + query = query.Where("admin_account like ?", "%"+queryOption.AccountLike)
  144 + }
  145 + cnt, err := query.Count()
  146 + return cnt, err
  147 +}
@@ -8,16 +8,18 @@ import ( @@ -8,16 +8,18 @@ import (
8 ) 8 )
9 9
10 var ( 10 var (
11 - key []byte = []byte("sx87sda0w7x7sd") 11 + key []byte = []byte("sx87sda0w7x7sd")
  12 + JWtExpiresSecond int64 = 60 * 60 * 3
12 ) 13 )
13 14
14 //MyToken ... 15 //MyToken ...
15 type MyToken struct { 16 type MyToken struct {
16 jwt.StandardClaims 17 jwt.StandardClaims
17 - UID int `json:"uid"` //管理员的id 18 + UID int64 `json:"uid"` //管理员的id
  19 +
18 } 20 }
19 21
20 -func NewMyToken(id int) *MyToken { 22 +func NewMyToken(id int64) *MyToken {
21 return &MyToken{UID: id} 23 return &MyToken{UID: id}
22 } 24 }
23 25
@@ -27,7 +29,7 @@ func (mytoken *MyToken) CreateJWTToken() (string, error) { @@ -27,7 +29,7 @@ func (mytoken *MyToken) CreateJWTToken() (string, error) {
27 mytoken.StandardClaims = jwt.StandardClaims{ 29 mytoken.StandardClaims = jwt.StandardClaims{
28 NotBefore: nowTime, 30 NotBefore: nowTime,
29 IssuedAt: nowTime, 31 IssuedAt: nowTime,
30 - ExpiresAt: 60 * 60 * 3, 32 + ExpiresAt: JWtExpiresSecond,
31 Issuer: "mmm_partnermg", 33 Issuer: "mmm_partnermg",
32 } 34 }
33 35
  1 +package lib
  2 +
  3 +import "strings"
  4 +
  5 +const (
  6 + INTERNAL_SERVER_ERROR = iota + 1
  7 + BAD_GATEWAY_ERROR
  8 + TRANSACTION_ERROR
  9 + ARG_ERROR
  10 + RES_NO_FIND_ERROR
  11 + BUSINESS_ERROR
  12 +)
  13 +
  14 +type ServiceError struct {
  15 + Prefix string
  16 + Code int
  17 + Message string
  18 +}
  19 +
  20 +func (serviceError ServiceError) Error() string {
  21 + return serviceError.Message
  22 +}
  23 +
  24 +func ThrowError(serviceErrorNo int, attachMessages ...string) error {
  25 + switch serviceErrorNo {
  26 + case INTERNAL_SERVER_ERROR:
  27 + return ServiceError{
  28 + Prefix: "内部服务出错:",
  29 + Code: INTERNAL_SERVER_ERROR,
  30 + Message: strings.Join(attachMessages, ""),
  31 + }
  32 + case BAD_GATEWAY_ERROR:
  33 + return ServiceError{
  34 + Prefix: "服务网关错误:",
  35 + Code: BAD_GATEWAY_ERROR,
  36 + Message: strings.Join(attachMessages, ""),
  37 + }
  38 + case TRANSACTION_ERROR:
  39 + return ServiceError{
  40 + Prefix: "事务处理错误:",
  41 + Code: TRANSACTION_ERROR,
  42 + Message: strings.Join(attachMessages, ""),
  43 + }
  44 + case ARG_ERROR:
  45 + return ServiceError{
  46 + Prefix: "参数认证错误:",
  47 + Code: ARG_ERROR,
  48 + Message: strings.Join(attachMessages, ""),
  49 + }
  50 + case RES_NO_FIND_ERROR:
  51 + return ServiceError{
  52 + Prefix: "找不到相关资源:",
  53 + Code: RES_NO_FIND_ERROR,
  54 + Message: strings.Join(attachMessages, ""),
  55 + }
  56 + case BUSINESS_ERROR:
  57 + return ServiceError{
  58 + Prefix: "业务逻辑错误:",
  59 + Code: BUSINESS_ERROR,
  60 + Message: strings.Join(attachMessages, ""),
  61 + }
  62 + default:
  63 + return ServiceError{
  64 + Prefix: "未定义错误:",
  65 + Code: 500,
  66 + Message: strings.Join(attachMessages, ""),
  67 + }
  68 + }
  69 +}
  1 +package controllers
  2 +
  3 +import (
  4 + "encoding/json"
  5 + "errors"
  6 + "fmt"
  7 + "time"
  8 +
  9 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
  10 +
  11 + "github.com/GeeTeam/gt3-golang-sdk/geetest"
  12 + "github.com/astaxie/beego/logs"
  13 + adminPermissionquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/query"
  14 + adminPermissionService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/service"
  15 + adminuserquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query"
  16 + adminuserservice "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/service"
  17 +)
  18 +
  19 +type AdminLoginController struct {
  20 + BaseController
  21 +}
  22 +
  23 +////Prepare 重写 BaseController 的Prepare方法
  24 +func (c *AdminLoginController) Prepare() {
  25 + c.BaseController.Prepare()
  26 + //排除登录 以及获取图形验证
  27 + urlIn := map[string]int{
  28 + "/v1/auth/captcha-init": 1,
  29 + "/v1/auth/login": 1,
  30 + }
  31 + nowUrl := c.Ctx.Input.URL()
  32 + if _, ok := urlIn[nowUrl]; ok {
  33 + //跳过校验
  34 + return
  35 + }
  36 + if ok := c.ValidJWTToken(); !ok {
  37 + return
  38 + }
  39 +}
  40 +
  41 +//Login 用户登录
  42 +func (c *AdminLoginController) Login() {
  43 + type Paramter struct {
  44 + Username string `json:"username"`
  45 + Password string `json:"password"`
  46 + }
  47 + var (
  48 + param Paramter
  49 + err error
  50 + )
  51 + if err = c.BindJsonData(&param); err != nil {
  52 + c.ResponseError(fmt.Errorf("json解析失败:%s", err))
  53 + return
  54 + }
  55 + newAdminuserquery := adminuserquery.GetAdminUserQuery{AdminAccount: param.Username}
  56 + newAdminUserService := adminuserservice.NewAdminUserService(nil)
  57 + adminuser, err := newAdminUserService.GetAdminUser(&newAdminuserquery)
  58 + if err != nil {
  59 + logs.Error("获取用户数据失败:%s", err)
  60 + c.ResponseError(errors.New("用户不存在"))
  61 + return
  62 + }
  63 + if adminuser.Password != param.Password {
  64 + c.ResponseError(errors.New("账号或密码错误"))
  65 + return
  66 + }
  67 + newJwt := lib.NewMyToken(adminuser.Id)
  68 + newToken, err := newJwt.CreateJWTToken()
  69 + if err != nil {
  70 + logs.Error("生成jwt数据失败:%s", err)
  71 + c.ResponseError(errors.New("服务异常"))
  72 + return
  73 + }
  74 + rspdata := map[string]interface{}{
  75 + "access": map[string]interface{}{
  76 + "accessToken": newToken,
  77 + "expiresIn": lib.JWtExpiresSecond,
  78 + },
  79 + }
  80 + c.ResponseData(rspdata)
  81 + return
  82 +}
  83 +
  84 +//CaptchaInit 极验初始化
  85 +func (c *AdminLoginController) CaptchaInit() {
  86 + const (
  87 + captchaID = "33a2abf9c5df0d6bc3b89fb39280114b"
  88 + privateKey = "13320fd2b10199e9a2440a4fbb4d46f7"
  89 + )
  90 + newGeetest := geetest.NewGeetestLib(captchaID, privateKey, 2*time.Second)
  91 + userip := c.Ctx.Input.IP()
  92 + _, responseBt := newGeetest.PreProcess("", userip)
  93 + // c.SetSession("geetest_status", status)
  94 + var geetestRsp geetest.FailbackRegisterRespnse
  95 + json.Unmarshal(responseBt, &geetestRsp)
  96 + //对前端定义的数据格式进行适配。。。
  97 + rspData := map[string]interface{}{
  98 + "success": geetestRsp.Success,
  99 + "gt": geetestRsp.GT,
  100 + "challenge": geetestRsp.Challenge,
  101 + "newCaptcha": geetestRsp.NewCaptcha,
  102 + }
  103 + c.ResponseData(rspData)
  104 + return
  105 +}
  106 +
  107 +//AdminpPofile 获取登录用户的权限配置
  108 +func (c *AdminLoginController) AdminpPofile() {
  109 + adminId := c.GetUserId()
  110 + newAdminuserquery := adminuserquery.GetAdminUserQuery{Id: adminId}
  111 + newAdminUserService := adminuserservice.NewAdminUserService(nil)
  112 + adminuser, err := newAdminUserService.GetAdminUser(&newAdminuserquery)
  113 + if err != nil {
  114 + logs.Error("获取用户数据失败:%s", err)
  115 + c.ResponseError(errors.New("用户不存在"))
  116 + return
  117 + }
  118 +
  119 + newPermissionSrv := adminPermissionService.NewAdminPermissionService(nil)
  120 + allPermission, err := newPermissionSrv.ListAdminPermission(adminPermissionquery.ListAdminPermissionQuery{})
  121 + if err != nil {
  122 + logs.Error("获取权限数据失败:%s", err)
  123 + c.ResponseError(errors.New("服务异常"))
  124 + return
  125 + }
  126 + //适配前端的数据格式
  127 + permissionSlice := make([]map[string]interface{}, 0)
  128 + for _, v := range allPermission {
  129 + m := map[string]interface{}{
  130 + "code": v.Code,
  131 + "icon": v.Icon,
  132 + "parentId": v.ParentId,
  133 + "sort": v.Sort,
  134 + "name": v.Name,
  135 + "id": v.Id,
  136 + "status": 0, //状态 1-启用 0-禁用,前端需要
  137 + }
  138 + permissionSlice = append(permissionSlice, m)
  139 + }
  140 + for index := range permissionSlice {
  141 + if adminuser.IsDefault {
  142 + permissionSlice[index]["status"] = 1
  143 + continue
  144 + }
  145 + for _, p := range adminuser.Permission {
  146 + if p == permissionSlice[index]["id"] {
  147 + permissionSlice[index]["status"] = 1
  148 + }
  149 + if p == permissionSlice[index]["parentId"] {
  150 + permissionSlice[index]["status"] = 1
  151 + }
  152 + }
  153 + }
  154 + userData := map[string]string{
  155 + "id": fmt.Sprint(adminuser.Id),
  156 + "name": adminuser.AdminName,
  157 + "adminType": "2", // 管理员类型 1-超级管理员 2-子管理员
  158 + }
  159 + if adminuser.IsDefault {
  160 + userData["adminType"] = "1"
  161 + }
  162 + respData := map[string]interface{}{
  163 + "user": userData,
  164 + "menus": permissionSlice,
  165 + }
  166 + c.ResponseData(respData)
  167 +}
@@ -4,6 +4,11 @@ import ( @@ -4,6 +4,11 @@ import (
4 "errors" 4 "errors"
5 5
6 "github.com/astaxie/beego/logs" 6 "github.com/astaxie/beego/logs"
  7 + adminPermissionquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/query"
  8 + adminPermissionService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/service"
  9 + adminuserCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/command"
  10 + adminuserquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query"
  11 + adminuserservice "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/service"
7 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" 12 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
8 ) 13 )
9 14
@@ -22,12 +27,53 @@ func (c *AdminUserController) Prepare() { @@ -22,12 +27,53 @@ func (c *AdminUserController) Prepare() {
22 } 27 }
23 } 28 }
24 29
25 -func (c *AdminUserController) Demo() { 30 +type adminDetailParam struct {
  31 + Id int64 `json:"id"`
  32 + PermissionType []int `json:"permissionType"`
  33 + Status int `json:"status"`
  34 + Pwd string `json:"pwd"`
  35 + Account string `json:"account"`
  36 +}
  37 +
  38 +func (c *AdminUserController) SaveAdminUser() {
  39 + //用与适配前端定义的数据结构
  40 + var (
  41 + param adminDetailParam
  42 + err error
  43 + )
  44 + if err = c.BindJsonData(&param); err != nil {
  45 + logs.Error(err)
  46 + c.ResponseError(errors.New("json数据解析失败"))
  47 + return
  48 + }
  49 + newAdminUserService := adminuserservice.NewAdminUserService(nil)
  50 + cmd := &adminuserCmd.SaveAdminUserCommand{
  51 + Id: param.Id,
  52 + Name: param.Account,
  53 + Account: param.Account,
  54 + Password: param.Pwd,
  55 + PermissionId: param.PermissionType,
  56 + IsUsable: false,
  57 + }
  58 + if param.Status == 1 {
  59 + cmd.IsUsable = true
  60 + }
  61 + _, err = newAdminUserService.SaveAdminUser(cmd)
  62 + if err != nil {
  63 + c.ResponseError(err)
  64 + return
  65 + }
  66 + c.ResponseData(nil)
  67 + return
  68 +}
  69 +
  70 +func (c *AdminUserController) GetAdminUser() {
26 //用与适配前端定义的数据结构 71 //用与适配前端定义的数据结构
27 - type Paramer struct { 72 + type Paramter struct {
  73 + Id int64 `json:"id"`
28 } 74 }
29 var ( 75 var (
30 - param Paramer 76 + param Paramter
31 err error 77 err error
32 ) 78 )
33 if err = c.BindJsonData(&param); err != nil { 79 if err = c.BindJsonData(&param); err != nil {
@@ -35,8 +81,91 @@ func (c *AdminUserController) Demo() { @@ -35,8 +81,91 @@ func (c *AdminUserController) Demo() {
35 c.ResponseError(errors.New("json数据解析失败")) 81 c.ResponseError(errors.New("json数据解析失败"))
36 return 82 return
37 } 83 }
38 - //Paramer转换为application要求的数据结构  
39 - //业务逻辑处理 84 + newAdminUserService := adminuserservice.NewAdminUserService(nil)
  85 + adminuser, err := newAdminUserService.GetAdminUser(&adminuserquery.GetAdminUserQuery{
  86 + Id: param.Id,
  87 + })
  88 + if err != nil {
  89 + c.ResponseError(err)
  90 + return
  91 + }
  92 + rspData := adminDetailParam{
  93 + Id: adminuser.Id,
  94 + Account: adminuser.Account,
  95 + Status: 0,
  96 + }
  97 + for _, v := range adminuser.Permission {
  98 + rspData.PermissionType = append(rspData.PermissionType, v.Id)
  99 + }
  100 + if adminuser.IsUsable {
  101 + rspData.Status = 1
  102 + }
  103 + c.ResponseData(rspData)
  104 + return
  105 +}
40 106
41 - c.ResponseData(nil) 107 +func (c *AdminUserController) ListAdminUser() {
  108 + //用与适配前端定义的数据结构
  109 + type Paramter struct {
  110 + SearchText string `json:"searchText"`
  111 + PageSize int `json::"pageSize"`
  112 + PageNumber int `json:"pageNumber"`
  113 + }
  114 + var (
  115 + param Paramter
  116 + err error
  117 + )
  118 + if err = c.BindJsonData(&param); err != nil {
  119 + logs.Error(err)
  120 + c.ResponseError(errors.New("json数据解析失败"))
  121 + return
  122 + }
  123 + if param.PageSize == 0 {
  124 + param.PageSize = 20
  125 + }
  126 + if param.PageNumber == 0 {
  127 + param.PageNumber = 1
  128 + }
  129 + newAdminUserService := adminuserservice.NewAdminUserService(nil)
  130 + queryOption := &adminuserquery.ListAdminUserQuery{
  131 + AdminAccountMatch: param.SearchText,
  132 + Limit: param.PageSize,
  133 + Offset: param.PageSize * (param.PageNumber - 1),
  134 + }
  135 + adminusers, cnt, err := newAdminUserService.PageListAdminUser(queryOption)
  136 + if err != nil {
  137 + c.ResponseError(err)
  138 + return
  139 + }
  140 + newPermissionSrv := adminPermissionService.NewAdminPermissionService(nil)
  141 + allPermission, err := newPermissionSrv.ListAdminPermission(adminPermissionquery.ListAdminPermissionQuery{
  142 + ParentId: 0,
  143 + })
  144 + if err != nil {
  145 + logs.Error("获取权限数据失败:%s", err)
  146 + c.ResponseError(errors.New("服务异常"))
  147 + return
  148 + }
  149 + permissionMap := map[int]domain.AdminPermission{}
  150 + for i := range allPermission {
  151 + permissionMap[allPermission[i].Id] = allPermission[i]
  152 + }
  153 + listData := []map[string]interface{}{}
  154 + //前端数据格式适配
  155 + for i := range adminusers {
  156 + permissionTypes := []string{}
  157 + for _, vv := range adminusers[i].Permission {
  158 + if pm, ok := permissionMap[vv.Id]; ok {
  159 + permissionTypes = append(permissionTypes, pm.Name)
  160 + }
  161 + }
  162 + m := map[string]interface{}{
  163 + "id": adminusers[i].Id,
  164 + "account": adminusers[i].Account,
  165 + "permission": permissionTypes,
  166 + }
  167 + listData = append(listData, m)
  168 + }
  169 + c.ResponsePageList(listData, cnt, param.PageNumber)
  170 + return
42 } 171 }
@@ -10,7 +10,9 @@ import ( @@ -10,7 +10,9 @@ import (
10 "github.com/astaxie/beego" 10 "github.com/astaxie/beego"
11 "github.com/astaxie/beego/logs" 11 "github.com/astaxie/beego/logs"
12 "github.com/prometheus/common/log" 12 "github.com/prometheus/common/log"
13 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/beego/lib" 13 + adminuserquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query"
  14 + adminuserservice "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/service"
  15 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
14 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/beego/protocol" 16 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/beego/protocol"
15 ) 17 )
16 18
@@ -28,6 +30,18 @@ func (controller BaseController) BindJsonData(v interface{}) error { @@ -28,6 +30,18 @@ func (controller BaseController) BindJsonData(v interface{}) error {
28 } 30 }
29 31
30 func (controller BaseController) ResponseError(err error) { 32 func (controller BaseController) ResponseError(err error) {
  33 + if e, ok := err.(lib.ServiceError); ok {
  34 + if !(e.Code == lib.ARG_ERROR || e.Code == lib.BUSINESS_ERROR) {
  35 + controller.Data["json"] = protocol.ResponseData{
  36 + Code: "-1",
  37 + Msg: "服务异常",
  38 + Data: struct{}{},
  39 + }
  40 + controller.ServeJSON()
  41 + logs.Error(err)
  42 + return
  43 + }
  44 + }
31 controller.Data["json"] = protocol.ResponseData{ 45 controller.Data["json"] = protocol.ResponseData{
32 Code: "-1", 46 Code: "-1",
33 Msg: err.Error(), 47 Msg: err.Error(),
@@ -35,6 +49,7 @@ func (controller BaseController) ResponseError(err error) { @@ -35,6 +49,7 @@ func (controller BaseController) ResponseError(err error) {
35 } 49 }
36 controller.ServeJSON() 50 controller.ServeJSON()
37 logs.Error(err) 51 logs.Error(err)
  52 + return
38 } 53 }
39 54
40 func (controller BaseController) ResponseData(data interface{}) { 55 func (controller BaseController) ResponseData(data interface{}) {
@@ -49,6 +64,24 @@ func (controller BaseController) ResponseData(data interface{}) { @@ -49,6 +64,24 @@ func (controller BaseController) ResponseData(data interface{}) {
49 controller.ServeJSON() 64 controller.ServeJSON()
50 } 65 }
51 66
  67 +func (controller BaseController) ResponsePageList(data interface{}, totalRow int, pageNumber int) {
  68 + if data == nil {
  69 + data = []interface{}{}
  70 + }
  71 + controller.Data["json"] = protocol.ResponseData{
  72 + Code: "0",
  73 + Msg: "ok",
  74 + Data: map[string]map[string]interface{}{
  75 + "gridResult": map[string]interface{}{
  76 + "lists": data,
  77 + "totalRow": totalRow,
  78 + "pageNumber": pageNumber,
  79 + },
  80 + },
  81 + }
  82 + controller.ServeJSON()
  83 +}
  84 +
52 //Finish 重写 beego.Controller 的Finish 方法 85 //Finish 重写 beego.Controller 的Finish 方法
53 func (controller *BaseController) Finish() { 86 func (controller *BaseController) Finish() {
54 strByte, _ := json.Marshal(controller.Data["json"]) 87 strByte, _ := json.Marshal(controller.Data["json"])
@@ -80,19 +113,9 @@ func (controller *BaseController) GetHeaderToken() string { @@ -80,19 +113,9 @@ func (controller *BaseController) GetHeaderToken() string {
80 return controller.Ctx.Input.Header("Authorization") 113 return controller.Ctx.Input.Header("Authorization")
81 } 114 }
82 115
83 -func (controller *BaseController) GetUserId() int {  
84 - idV := controller.Ctx.Input.GetData("admin_user_id")  
85 - uid, _ := strconv.Atoi(fmt.Sprint(idV))  
86 - return uid  
87 -}  
88 -  
89 -func (controller *BaseController) setUserId(id int) {  
90 - controller.Ctx.Input.SetData("admin_user_id", id)  
91 -}  
92 -  
93 func (controller *BaseController) ValidJWTToken() bool { 116 func (controller *BaseController) ValidJWTToken() bool {
94 headerToken := controller.GetHeaderToken() 117 headerToken := controller.GetHeaderToken()
95 - mytoken := lib.NewMyToken(0) 118 + mytoken := new(lib.MyToken)
96 err := mytoken.ValidJWTToken(headerToken) 119 err := mytoken.ValidJWTToken(headerToken)
97 if err != nil { 120 if err != nil {
98 if mytoken.IsJwtErrorExpired(err) { 121 if mytoken.IsJwtErrorExpired(err) {
@@ -117,7 +140,7 @@ func (controller *BaseController) ValidJWTToken() bool { @@ -117,7 +140,7 @@ func (controller *BaseController) ValidJWTToken() bool {
117 return true 140 return true
118 } 141 }
119 142
120 -func (controller *BaseController) ValidAdminPermission(code string, excludeURL ...string) bool { 143 +func (controller *BaseController) ValidAdminPermission(permissionCode string, excludeURL ...string) bool {
121 //排除掉的请求 144 //排除掉的请求
122 reqUrl := controller.Ctx.Input.URL() 145 reqUrl := controller.Ctx.Input.URL()
123 for i := range excludeURL { 146 for i := range excludeURL {
@@ -126,15 +149,37 @@ func (controller *BaseController) ValidAdminPermission(code string, excludeURL . @@ -126,15 +149,37 @@ func (controller *BaseController) ValidAdminPermission(code string, excludeURL .
126 } 149 }
127 } 150 }
128 //权限校验 151 //权限校验
129 - //  
130 - var err error 152 + userId := controller.GetUserId()
  153 + newAdminuserquery := adminuserquery.GetAdminUserQuery{Id: userId}
  154 + newAdminUserService := adminuserservice.NewAdminUserService(nil)
  155 + adminuser, err := newAdminUserService.GetAdminUser(&newAdminuserquery)
131 if err != nil { 156 if err != nil {
132 - controller.Data["json"] = protocol.ResponseData{  
133 - Code: "-1",  
134 - Msg: "没有操作权限",  
135 - Data: struct{}{}, 157 + logs.Error("获取用户数据失败:%s", err)
  158 + controller.ResponseError(errors.New("无操作权限"))
  159 + return false
  160 + }
  161 + if !adminuser.IsUsable {
  162 + controller.ResponseError(errors.New("用户被禁用"))
  163 + return false
  164 + }
  165 + if adminuser.IsDefault {
  166 + logs.Debug("用户是超级管理员")
  167 + return true
  168 + }
  169 + for _, v := range adminuser.Permission {
  170 + if v.Code == permissionCode {
  171 + return true
136 } 172 }
137 - controller.ServeJSON()  
138 } 173 }
139 - return true 174 + return false
  175 +}
  176 +
  177 +func (controller *BaseController) GetUserId() int64 {
  178 + idV := controller.Ctx.Input.GetData("token:admin_user_id")
  179 + uid, _ := strconv.ParseInt(fmt.Sprint(idV), 10, 64)
  180 + return uid
  181 +}
  182 +
  183 +func (controller *BaseController) setUserId(id int64) {
  184 + controller.Ctx.Input.SetData("token:admin_user_id", id)
140 } 185 }
1 -package routers  
2 -  
3 -import (  
4 - "github.com/astaxie/beego"  
5 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/beego/controllers"  
6 -)  
7 -  
8 -func init() {  
9 - adminRouter := beego.NewNamespace("/admin_user",  
10 - // 其余的路由  
11 - beego.NSRouter("/get", &controllers.AdminUserController{}, "POST:Demo"),  
12 - )  
13 - //...  
14 -  
15 - beego.AddNamespace(adminRouter)  
16 -}  
1 -package routers  
2 -  
3 -import (  
4 - "github.com/astaxie/beego"  
5 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/beego/controllers"  
6 -)  
7 -  
8 -func init() {  
9 - beego.Router("/partnerInfos/", &controllers.PartnerInfoController{}, "Post:CreatePartnerInfo")  
10 - beego.Router("/partnerInfos/:id", &controllers.PartnerInfoController{}, "Put:UpdatePartnerInfo")  
11 - beego.Router("/partnerInfos/:id", &controllers.PartnerInfoController{}, "Get:GetPartnerInfo")  
12 - beego.Router("/partnerInfos/:id", &controllers.PartnerInfoController{}, "Delete:RemovePartnerInfo")  
13 - beego.Router("/partnerInfos/", &controllers.PartnerInfoController{}, "Get:ListPartnerInfo")  
14 -}  
  1 +package routers
  2 +
  3 +import (
  4 + "github.com/astaxie/beego"
  5 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/beego/controllers"
  6 +)
  7 +
  8 +func init() {
  9 + adminRouter := beego.NewNamespace("/v1",
  10 + beego.NSNamespace("/auth",
  11 + beego.NSRouter("/login", &controllers.AdminLoginController{}, "POST:Login"),
  12 + beego.NSRouter("/captcha-init", &controllers.AdminLoginController{}, "POST:CaptchaInit"),
  13 + beego.NSRouter("/profile", &controllers.AdminLoginController{}, "POST:AdminpPofile"),
  14 + ),
  15 +
  16 + beego.NSNamespace("/admin",
  17 + beego.NSRouter("/update", &controllers.AdminUserController{}, "POST:SaveAdminUser"),
  18 + beego.NSRouter("/detail", &controllers.AdminUserController{}, "POST:GetAdminUser"),
  19 + beego.NSRouter("/list", &controllers.AdminUserController{}, "POST:ListAdminUser"),
  20 + ),
  21 + )
  22 + beego.AddNamespace(adminRouter)
  23 +}