作者 唐旭辉

日常提交保存

  1 +package command
  2 +
  3 +import "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
  4 +
  5 +type LoginBySecretKeyCommand struct {
  6 + Secret string `json:"secret"`
  7 +}
  8 +
  9 +func (login LoginBySecretKeyCommand) ValidateCommand() error {
  10 + if len(login.Secret) == 0 {
  11 + return lib.ThrowError(lib.ARG_ERROR, "登录参数错误")
  12 + }
  13 + return nil
  14 +}
@@ -257,13 +257,13 @@ func (adminUserSrv AdminUserService) UpdateAdminIsUsable(uid int64, isUsable boo @@ -257,13 +257,13 @@ func (adminUserSrv AdminUserService) UpdateAdminIsUsable(uid int64, isUsable boo
257 adminuserDao = v 257 adminuserDao = v
258 } 258 }
259 if ok, err := adminuserDao.AdminUserIsDefault(uid); err != nil { 259 if ok, err := adminuserDao.AdminUserIsDefault(uid); err != nil {
260 - return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) 260 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
261 } else if ok { 261 } else if ok {
262 return lib.ThrowError(lib.BUSINESS_ERROR, "请勿禁用超级管理员") 262 return lib.ThrowError(lib.BUSINESS_ERROR, "请勿禁用超级管理员")
263 } 263 }
264 err = adminuserDao.UpdateIsUsable(uid, isUsable) 264 err = adminuserDao.UpdateIsUsable(uid, isUsable)
265 if err != nil { 265 if err != nil {
266 - return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) 266 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
267 } 267 }
268 transactionContext.CommitTransaction() 268 transactionContext.CommitTransaction()
269 return nil 269 return nil
@@ -362,7 +362,6 @@ func (service SyncEmployeeService) ChangeSuperAdmin(cmd command.ChanceSuperAdmin @@ -362,7 +362,6 @@ func (service SyncEmployeeService) ChangeSuperAdmin(cmd command.ChanceSuperAdmin
362 if err != nil { 362 if err != nil {
363 return lib.ThrowError(lib.BUSINESS_ERROR, err.Error()) 363 return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
364 } 364 }
365 - //提取到domain???  
366 err = newSuperUser.Update(map[string]interface{}{ 365 err = newSuperUser.Update(map[string]interface{}{
367 "AdminType": domain.UserIsAdmin, 366 "AdminType": domain.UserIsAdmin,
368 }) 367 })
  1 +package command
  2 +
  3 +import "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
  4 +
  5 +type LoginBySecretKeyCommand struct {
  6 + Secret string `json:"secret"`
  7 +}
  8 +
  9 +func (login LoginBySecretKeyCommand) ValidateCommand() error {
  10 + if len(login.Secret) == 0 {
  11 + return lib.ThrowError(lib.ARG_ERROR, "登录参数错误")
  12 + }
  13 + return nil
  14 +}
  1 +package service
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
  7 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/command"
  8 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
  9 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/serviceGateway"
  10 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
  11 +)
  12 +
  13 +type UsersService struct {
  14 +}
  15 +
  16 +func NewUsersService(option map[string]interface{}) *UsersService {
  17 + newUsersService := new(UsersService)
  18 + return newUsersService
  19 +}
  20 +
  21 +func (service UsersService) UserLoginBySecretKey(cmd command.LoginBySecretKeyCommand) (interface{}, error) {
  22 + var err error
  23 + if err = cmd.ValidateCommand(); err != nil {
  24 + return nil, err
  25 + }
  26 + //向统一用户中心确认密钥信息并获取用户数据
  27 + ucenterService := serviceGateway.NewMmmUserCenterServiceGateway()
  28 + loginResp, err := ucenterService.RequestUCenterLoginBySecret(cmd.Secret)
  29 + if err != nil {
  30 + e := fmt.Sprintf("通过密钥(secret=%s)从统一用户中心获取数据失败:%s", cmd.Secret, err.Error())
  31 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
  32 + }
  33 + var (
  34 + transactionContext, _ = factory.CreateTransactionContext(nil)
  35 + )
  36 + if err = transactionContext.StartTransaction(); err != nil {
  37 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  38 + }
  39 + defer func() {
  40 + transactionContext.RollbackTransaction()
  41 + }()
  42 + var (
  43 + companyRespository domain.CompanyRepository
  44 + userRespository domain.UsersRepository
  45 + companyData domain.Company
  46 + usersData domain.Users
  47 + )
  48 + if companyRespository, err = factory.CreateCompanyRepository(map[string]interface{}{
  49 + "transactionContext": transactionContext,
  50 + }); err != nil {
  51 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  52 + }
  53 + if userRespository, err = factory.CreateUsersRepository(map[string]interface{}{
  54 + "transactionContext": transactionContext,
  55 + }); err != nil {
  56 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  57 + }
  58 + //检索本系统的公司数据判断公司权限
  59 + companyData, err = companyRespository.FindOne(map[string]interface{}{
  60 + "Id": loginResp.Data.Muid,
  61 + })
  62 + if err != nil {
  63 + e := fmt.Sprintf("获取公司(id=%d)数据失败:%s", loginResp.Data.Muid, err.Error())
  64 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
  65 + }
  66 + if !companyData.EnableIsOk() {
  67 + return nil, lib.ThrowError(lib.BUSINESS_ERROR, "该公司没有操作权限")
  68 + }
  69 + //检索本系统的用户数据
  70 + usersData, err = userRespository.FindOne(map[string]interface{}{
  71 + "OpenId": loginResp.Data.Id,
  72 + "CompanyId": companyData.Id,
  73 + })
  74 + if err != nil {
  75 + e := fmt.Sprintf("获取用户(OpenId=%d;CompanyId=%d)数据失败:%s",
  76 + loginResp.Data.Id, companyData.Id, err.Error())
  77 + return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
  78 + }
  79 + //确认用户权限
  80 + if !usersData.IsUsable() {
  81 + return nil, lib.ThrowError(lib.BUSINESS_ERROR, "用户被禁用")
  82 + }
  83 + err = transactionContext.CommitTransaction()
  84 + //生成token
  85 +
  86 + return nil, nil
  87 +}
  88 +
  89 +//GetAdminpPofile 登录后获取用户的权限配置数据
  90 +func (service UsersService) GetAdminpPofile() (interface{}, error) {
  91 + return nil, nil
  92 +}
  93 +
  94 +//ValidateAdminpPermission 校验用户的操作权限
  95 +func (service UsersService) ValidateAdminpPermission() (interface{}, error) {
  96 + return nil, nil
  97 +}
@@ -125,9 +125,15 @@ func (reponsitory UsersRepository) FindOne(queryOptions map[string]interface{}) @@ -125,9 +125,15 @@ func (reponsitory UsersRepository) FindOne(queryOptions map[string]interface{})
125 if v, ok := queryOptions["Id"]; ok { 125 if v, ok := queryOptions["Id"]; ok {
126 query = query.Where("id=?", v) 126 query = query.Where("id=?", v)
127 } 127 }
128 - if v, ok := queryOptions["phone"]; ok { 128 + if v, ok := queryOptions["Phone"]; ok {
129 query = query.Where("phone=?", v) 129 query = query.Where("phone=?", v)
130 } 130 }
  131 + if v, ok := queryOptions["CompanyId"]; ok {
  132 + query = query.Where("company_id=?", v)
  133 + }
  134 + if v, ok := queryOptions["OpenId"]; ok {
  135 + query = query.Where("open_id=?", v)
  136 + }
131 err = query.First() 137 err = query.First()
132 if err != nil { 138 if err != nil {
133 return domain.Users{}, err 139 return domain.Users{}, err
1 -package service_gateway 1 +package serviceGateway
2 2
3 import ( 3 import (
4 "bytes" 4 "bytes"
@@ -79,8 +79,8 @@ func (gateway MmmUserCenterServiceGateway) httpDo(reqURL string, mathod string, @@ -79,8 +79,8 @@ func (gateway MmmUserCenterServiceGateway) httpDo(reqURL string, mathod string,
79 type ResponseLogin struct { 79 type ResponseLogin struct {
80 UCenterCommonMsg 80 UCenterCommonMsg
81 Data struct { 81 Data struct {
82 - Id int64 `json:"id"` //统一用户中心的id,对应本系统中users表的open_id  
83 - Phone string `json:"phone"` 82 + Id int64 `json:"id"` //统一用户中心的id,对应本系统中users表的open_id
  83 + Phone string `json:"phone"` //手机号 ,账号
84 NickName string `json:"nickname"` //昵称 84 NickName string `json:"nickname"` //昵称
85 Avatar string `json:"avatar"` //头像 85 Avatar string `json:"avatar"` //头像
86 Imtoken string `json:"imtoken"` //网易云imtoken 86 Imtoken string `json:"imtoken"` //网易云imtoken
1 -package service_gateway  
2 -  
3 -import (  
4 - "time"  
5 -)  
6 -  
7 -type httplibBaseServiceGateway struct {  
8 - baseURL string  
9 - connectTimeout time.Duration  
10 - readWriteTimeout time.Duration  
11 -}  
@@ -6,8 +6,6 @@ import ( @@ -6,8 +6,6 @@ import (
6 "fmt" 6 "fmt"
7 "time" 7 "time"
8 8
9 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"  
10 -  
11 "github.com/GeeTeam/gt3-golang-sdk/geetest" 9 "github.com/GeeTeam/gt3-golang-sdk/geetest"
12 "github.com/astaxie/beego/logs" 10 "github.com/astaxie/beego/logs"
13 adminPermissionquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/query" 11 adminPermissionquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/query"
@@ -15,6 +13,7 @@ import ( @@ -15,6 +13,7 @@ import (
15 adminuserCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/command" 13 adminuserCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/command"
16 adminuserquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query" 14 adminuserquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query"
17 adminuserservice "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/service" 15 adminuserservice "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/service"
  16 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
18 ) 17 )
19 18
20 type AdminLoginController struct { 19 type AdminLoginController struct {
@@ -40,6 +39,52 @@ func (c *AdminLoginController) Prepare() { @@ -40,6 +39,52 @@ func (c *AdminLoginController) Prepare() {
40 } 39 }
41 40
42 //Login 用户登录 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 +// if !adminuser.IsUsable {
  68 +// c.ResponseError(errors.New("用户被禁用"))
  69 +// }
  70 +// //TODO
  71 +// newJwt := lib.NewMyToken(adminuser.Id, 0)
  72 +// newToken, err := newJwt.CreateJWTToken()
  73 +// if err != nil {
  74 +// logs.Error("生成jwt数据失败:%s", err)
  75 +// c.ResponseError(errors.New("服务异常"))
  76 +// return
  77 +// }
  78 +// rspdata := map[string]interface{}{
  79 +// "access": map[string]interface{}{
  80 +// "accessToken": newToken,
  81 +// "expiresIn": lib.JWtExpiresSecond,
  82 +// },
  83 +// }
  84 +// c.ResponseData(rspdata)
  85 +// return
  86 +// }
  87 +
43 func (c *AdminLoginController) Login() { 88 func (c *AdminLoginController) Login() {
44 type Paramter struct { 89 type Paramter struct {
45 Username string `json:"username"` 90 Username string `json:"username"`
@@ -55,21 +100,9 @@ func (c *AdminLoginController) Login() { @@ -55,21 +100,9 @@ func (c *AdminLoginController) Login() {
55 } 100 }
56 newAdminuserquery := adminuserquery.GetAdminUserQuery{AdminAccount: param.Username} 101 newAdminuserquery := adminuserquery.GetAdminUserQuery{AdminAccount: param.Username}
57 newAdminUserService := adminuserservice.NewAdminUserService(nil) 102 newAdminUserService := adminuserservice.NewAdminUserService(nil)
58 - adminuser, err := newAdminUserService.GetAdminUser(&newAdminuserquery)  
59 - if err != nil {  
60 - logs.Error("获取用户数据失败:%s", err)  
61 - c.ResponseError(errors.New("用户不存在"))  
62 - return  
63 - }  
64 - if adminuser.Password != param.Password {  
65 - c.ResponseError(errors.New("账号或密码错误"))  
66 - return  
67 - }  
68 - if !adminuser.IsUsable {  
69 - c.ResponseError(errors.New("用户被禁用"))  
70 - }  
71 - //TODO  
72 - newJwt := lib.NewMyToken(adminuser.Id, 0) 103 + _ = newAdminuserquery
  104 + _ = newAdminUserService
  105 + newJwt := lib.NewMyToken(0, 0)
73 newToken, err := newJwt.CreateJWTToken() 106 newToken, err := newJwt.CreateJWTToken()
74 if err != nil { 107 if err != nil {
75 logs.Error("生成jwt数据失败:%s", err) 108 logs.Error("生成jwt数据失败:%s", err)
@@ -144,6 +144,7 @@ func (controller *BaseController) ValidJWTToken() bool { @@ -144,6 +144,7 @@ func (controller *BaseController) ValidJWTToken() bool {
144 return false 144 return false
145 } 145 }
146 controller.setUserId(tokenData.UID) 146 controller.setUserId(tokenData.UID)
  147 + controller.setUserCompanyId(tokenData.CompanyId)
147 return true 148 return true
148 } 149 }
149 150
@@ -196,3 +197,14 @@ func (controller *BaseController) setUserId(id int64) { @@ -196,3 +197,14 @@ func (controller *BaseController) setUserId(id int64) {
196 logs.Info("token:admin_user_id = ", id) 197 logs.Info("token:admin_user_id = ", id)
197 controller.Ctx.Input.SetData("token:admin_user_id", id) 198 controller.Ctx.Input.SetData("token:admin_user_id", id)
198 } 199 }
  200 +
  201 +func (controller *BaseController) setUserCompanyId(id int64) {
  202 + logs.Info("token:company_id = ", id)
  203 + controller.Ctx.Input.SetData("token:company_id", id)
  204 +}
  205 +
  206 +func (controller *BaseController) GetUserCompany() int64 {
  207 + idV := controller.Ctx.Input.GetData("token:company_id")
  208 + uid, _ := strconv.ParseInt(fmt.Sprint(idV), 10, 64)
  209 + return uid
  210 +}
@@ -11,7 +11,7 @@ func init() { @@ -11,7 +11,7 @@ func init() {
11 beego.NSRouter("/login", &controllers.AdminLoginController{}, "POST:Login"), 11 beego.NSRouter("/login", &controllers.AdminLoginController{}, "POST:Login"),
12 beego.NSRouter("/captcha-init", &controllers.AdminLoginController{}, "POST:CaptchaInit"), 12 beego.NSRouter("/captcha-init", &controllers.AdminLoginController{}, "POST:CaptchaInit"),
13 beego.NSRouter("/profile", &controllers.AdminLoginController{}, "POST:AdminpPofile"), 13 beego.NSRouter("/profile", &controllers.AdminLoginController{}, "POST:AdminpPofile"),
14 - beego.NSRouter("/pwd-update", &controllers.AdminLoginController{}, "POST:PwdUpdate"), 14 + // beego.NSRouter("/pwd-update", &controllers.AdminLoginController{}, "POST:PwdUpdate"),
15 ), 15 ),
16 beego.NSNamespace("/admin", 16 beego.NSNamespace("/admin",
17 beego.NSRouter("/update", &controllers.AdminUserController{}, "POST:SaveAdminUser"), 17 beego.NSRouter("/update", &controllers.AdminUserController{}, "POST:SaveAdminUser"),