作者 yangfu

企业平台二次验证修改

@@ -43,3 +43,5 @@ h5_host = "http://mmm-web-open-test.fjmaimaimai.com" @@ -43,3 +43,5 @@ h5_host = "http://mmm-web-open-test.fjmaimaimai.com"
43 #审核中心 43 #审核中心
44 suplus_approve_host ="http://suplus-approve-dev.fjmaimaimai.com" 44 suplus_approve_host ="http://suplus-approve-dev.fjmaimaimai.com"
45 45
  46 +#企业平台
  47 +BUSINESS_ADMIN_SERVICE_HOST ="${BUSINESS_ADMIN_SERVICE_HOST||http://suplus-business-admin-test.fjmaimaimai.com/}"
@@ -2,7 +2,7 @@ package v1 @@ -2,7 +2,7 @@ package v1
2 2
3 import ( 3 import (
4 "encoding/json" 4 "encoding/json"
5 - 5 + "github.com/astaxie/beego"
6 "opp/controllers" 6 "opp/controllers"
7 "opp/protocol" 7 "opp/protocol"
8 "opp/services/auth" 8 "opp/services/auth"
@@ -36,6 +36,10 @@ func (this *AuthController) Login() { @@ -36,6 +36,10 @@ func (this *AuthController) Login() {
36 return 36 return
37 } 37 }
38 header := controllers.GetRequestHeader(this.Ctx) 38 header := controllers.GetRequestHeader(this.Ctx)
  39 + if beego.BConfig.RunMode == "prod" || beego.BConfig.RunMode == "test" {
  40 + msg = protocol.NewReturnResponse(auth.LoginV3(header, request))
  41 + return
  42 + }
39 msg = protocol.NewReturnResponse(auth.Login(header, request)) 43 msg = protocol.NewReturnResponse(auth.Login(header, request))
40 } 44 }
41 45
@@ -2,6 +2,7 @@ package v1 @@ -2,6 +2,7 @@ package v1
2 2
3 import ( 3 import (
4 "encoding/json" 4 "encoding/json"
  5 + "github.com/astaxie/beego"
5 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 6 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
6 "opp/controllers" 7 "opp/controllers"
7 "opp/protocol" 8 "opp/protocol"
@@ -157,6 +158,10 @@ func (this *UserController) UserInfo() { @@ -157,6 +158,10 @@ func (this *UserController) UserInfo() {
157 return 158 return
158 } 159 }
159 header := controllers.GetRequestHeader(this.Ctx) 160 header := controllers.GetRequestHeader(this.Ctx)
  161 + if beego.BConfig.RunMode == "prod" || beego.BConfig.RunMode == "test" {
  162 + msg = protocol.NewReturnResponse(user.UserInfoV3(header, request))
  163 + return
  164 + }
160 msg = protocol.NewReturnResponse(user.UserInfo(header, request)) 165 msg = protocol.NewReturnResponse(user.UserInfo(header, request))
161 } 166 }
162 167
@@ -94,6 +94,12 @@ spec: @@ -94,6 +94,12 @@ spec:
94 value: "stdout" 94 value: "stdout"
95 - name: aliyun_logs_access 95 - name: aliyun_logs_access
96 value: " /opt/logs/app.log" 96 value: " /opt/logs/app.log"
  97 +
  98 + - name: BUSINESS_ADMIN_SERVICE_HOST
  99 + valueFrom:
  100 + configMapKeyRef:
  101 + name: suplus-config
  102 + key: service.businessadmin
97 volumes: 103 volumes:
98 - name: accesslogs 104 - name: accesslogs
99 emptyDir: {} 105 emptyDir: {}
@@ -129,3 +129,80 @@ func CheckUcenterResponse(message *protocol.Message) (err error) { @@ -129,3 +129,80 @@ func CheckUcenterResponse(message *protocol.Message) (err error) {
129 } 129 }
130 return 130 return
131 } 131 }
  132 +
  133 +/*******************企业平台****************/
  134 +var MethodUserAuth = "auth/get-user-auth"
  135 +
  136 +//验证用户模块权限
  137 +func CheckUserModuleAuth(uid int64) (auth bool) {
  138 + var req = NewRequest(fmt.Sprintf("%v%v", beego.AppConfig.String("BUSINESS_ADMIN_SERVICE_HOST"), MethodUserAuth), http.MethodPost)
  139 + var message = &protocol.Message{}
  140 + var request = struct {
  141 + UserId string `json:"userId"`
  142 + PlatformId string `json:"platformId"`
  143 + }{UserId: fmt.Sprintf("%v", uid), PlatformId: "3"} //平台id:1素+;2问题;3机会;18价值
  144 + var response = struct {
  145 + UserAuth bool `json:"userAuth"`
  146 + }{}
  147 + if data, err := req.ActionDefault(request, &message); err != nil {
  148 + log.Error(err)
  149 + return false
  150 + } else {
  151 + log.Debug(req.Url, string(data))
  152 + }
  153 + if message.Errno != 0 {
  154 + log.Debug("CheckUserModuleAuth :", message.Errno, message.Errmsg)
  155 + return false
  156 + }
  157 + if err := json.Unmarshal(message.Data, &response); err != nil {
  158 + log.Error(err, message, string(message.Data))
  159 + return
  160 + }
  161 + return response.UserAuth
  162 +}
  163 +
  164 +type Request struct {
  165 + HttpReq *httplib.BeegoHTTPRequest
  166 + Url string
  167 +}
  168 +
  169 +func NewRequest(url, httpMethod string) *Request {
  170 + var (
  171 + httpReq *httplib.BeegoHTTPRequest
  172 + )
  173 + if httpMethod == http.MethodGet {
  174 + httpReq = httplib.Get(url)
  175 + } else if httpMethod == http.MethodPost {
  176 + httpReq = httplib.Post(url)
  177 + } else if httpMethod == http.MethodPut {
  178 + httpReq = httplib.Put(url)
  179 + }
  180 + ret := &Request{
  181 + HttpReq: httpReq,
  182 + Url: url,
  183 + }
  184 + return ret
  185 +}
  186 +
  187 +func (req *Request) ActionDefault(request interface{}, message interface{}) (data []byte, err error) {
  188 + var (
  189 + httpRsp *http.Response
  190 + )
  191 + req.HttpReq.JSONBody(request)
  192 + req.HttpReq.Header("Content-Type", "application/json")
  193 + if httpRsp, err = req.HttpReq.DoRequest(); err != nil {
  194 + log.Error(err)
  195 + return
  196 + }
  197 + data, err = ioutil.ReadAll(httpRsp.Body)
  198 + defer httpRsp.Body.Close()
  199 + if err != nil {
  200 + log.Error(err)
  201 + return
  202 + }
  203 + if err = json.Unmarshal(data, message); err != nil {
  204 + log.Error(err)
  205 + return
  206 + }
  207 + return
  208 +}
@@ -15,3 +15,9 @@ func Test_UcenterIsUserExists(t *testing.T) { @@ -15,3 +15,9 @@ func Test_UcenterIsUserExists(t *testing.T) {
15 log.Fatal(err) 15 log.Fatal(err)
16 } 16 }
17 } 17 }
  18 +
  19 +func Test_CheckUserModuleAuth(t *testing.T) {
  20 + if ok := CheckUserModuleAuth(3649639319273472); !ok {
  21 + t.Fatal("CheckUserModuleAuth error")
  22 + }
  23 +}
@@ -31,6 +31,142 @@ var ( @@ -31,6 +31,142 @@ var (
31 ) 31 )
32 32
33 //登录 33 //登录
  34 +func LoginV3(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) {
  35 + var (
  36 + user *models.User
  37 + userAuth *models.UserAuth
  38 + id = request.Uid
  39 + getUserRequest *protocol.UCenterServerLoginRequest = &protocol.UCenterServerLoginRequest{
  40 + Uid: id,
  41 + Token: request.Token,
  42 + Type: 2,
  43 + }
  44 + getUserResponse *protocol.UCenterGetUserResponse
  45 + message *protocol.Message
  46 + company *models.Company
  47 + companys []*models.Company
  48 + )
  49 + user, err = models.GetUserByUcenterId(id)
  50 + if err != nil {
  51 + log.Error(err)
  52 + err = protocol.NewErrWithMessage(2002, err) //账号不存在
  53 + return
  54 + }
  55 + if companys, err = models.GetCompanyByPermission(user.Id); err != nil {
  56 + log.Error(err)
  57 + err = protocol.NewErrWithMessage(2002, err) //账号不存在
  58 + return
  59 + }
  60 + if len(companys) == 0 {
  61 + err = protocol.NewErrWithMessage(2002, err) //账号不存在
  62 + return
  63 + }
  64 + if !utils.ValidVersion(header.Version, protocol.RequireVersion) {
  65 + log.Warn(fmt.Sprintf("版本不足 当前手机版本:%v 需要版本大于:%v", header.Version, protocol.RequireVersion))
  66 + err = protocol.NewCustomMessage(2002, "版本不足,请升级app") //账号不存在
  67 + return
  68 + }
  69 + //获取最后一次公司编号给统一用户中心
  70 + if u, e := models.GetUserAuthByUserId(user.Id, protocol.DeviceType); e == nil && user.UserCenterId == id {
  71 + if company, e = models.GetCompanyById(u.CurrentCompanyId); e == nil {
  72 + getUserRequest.CompanyId = company.UserCenterId
  73 + }
  74 + }
  75 + //验证 当前登录的公司是否有模块权限
  76 + /************后期移除************/
  77 + if ucIds, e := models.GetUserAllCompany(user.Id); e != nil {
  78 + log.Error(e)
  79 + err = protocol.NewErrWithMessage(2002, err) //账号不存在
  80 + return
  81 + } else {
  82 + var hasAuth bool = false
  83 + for i := 0; i < len(ucIds); i++ {
  84 + hasAuth = agg.CheckUserModuleAuth(ucIds[i].Id)
  85 + if hasAuth {
  86 + break
  87 + }
  88 + }
  89 + if !hasAuth {
  90 + err = protocol.NewErrWithMessage(2002, err) //账号不存在
  91 + return
  92 + }
  93 + return
  94 + }
  95 + /************后期移除************/
  96 +
  97 + //从用户中心获取用户信息
  98 + if _, err = agg.RequestUserCenter(protocol.MethodServerLogin, http.MethodPost, getUserRequest, &message); err != nil {
  99 + log.Error(err)
  100 + return
  101 + }
  102 + log.Debug(fmt.Sprintf("ucenter_id:%v getuser response:", request.Uid), message.Errno, message.Errmsg)
  103 + if message.Errno == 0 && message.Errmsg == "ok" {
  104 + if err = message.Unmarshal(&getUserResponse); err != nil {
  105 + log.Error(err)
  106 + return
  107 + }
  108 + }
  109 + switch message.Errno {
  110 + case -1:
  111 + err = protocol.NewErrWithMessage(2002, err) //账号不存在
  112 + return
  113 + case 0:
  114 + goto Success
  115 + case 2002:
  116 + err = protocol.NewErrWithMessage(2002, err) //账号不存在
  117 + return
  118 + case 10001:
  119 + err = protocol.NewErrWithMessage(2002, err) //账号不存在
  120 + return
  121 + case 10003:
  122 + err = protocol.NewErrWithMessage(4140, err) //账号不存在
  123 + return
  124 + default:
  125 + log.Error("error_no:%v msg:%v", message.Errno, message.Errmsg)
  126 + err = protocol.NewErrWithMessage(4140, err)
  127 + return
  128 + }
  129 +
  130 +Success:
  131 + {
  132 + userAuth, err = models.GetUserAuthByUserId(user.Id, 1)
  133 + if err != nil {
  134 + if err == orm.ErrNoRows {
  135 + err = nil
  136 + userAuth = &models.UserAuth{
  137 + UserId: user.Id,
  138 + DeviceType: 1, //int8(header.DeviceType),
  139 + }
  140 + models.AddUserAuth(userAuth)
  141 + } else {
  142 + log.Error(err)
  143 + return
  144 + }
  145 + }
  146 + userAuth.AuthCode = uid.NewV1().StringNoDash()
  147 +
  148 + /*更新用户信息*/
  149 + user.CsAccount = getUserResponse.CustomerAccount
  150 + user.ImToken = getUserResponse.ImToken
  151 + user.Icon = getUserResponse.Avatar
  152 + user.NickName = getUserResponse.NickName
  153 + user.Accid = getUserResponse.Accid
  154 + user.UserCenterId = getUserResponse.Id
  155 + if err = models.UpdateUsersById(user); err != nil {
  156 + log.Error(err)
  157 + return
  158 + }
  159 + userAuth.AuthCodeExp = time.Now().Add(time.Second * protocol.TokenExpire)
  160 + if err = models.UpdateUserAuthById(userAuth); err != nil {
  161 + return
  162 + }
  163 + rsp = &protocol.LoginResponse{AuthCode: userAuth.AuthCode}
  164 + }
  165 + err = protocol.NewSuccessWithMessage("登录成功")
  166 + return
  167 +}
  168 +
  169 +//登录
34 func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) { 170 func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) {
35 var ( 171 var (
36 user *models.User 172 user *models.User
@@ -248,6 +248,10 @@ func SwitchCompany(header *protocol.RequestHeader, request *protocol.SwitchCompa @@ -248,6 +248,10 @@ func SwitchCompany(header *protocol.RequestHeader, request *protocol.SwitchCompa
248 err = protocol.NewErrWithMessage(4201) //找不到这家公司 248 err = protocol.NewErrWithMessage(4201) //找不到这家公司
249 return 249 return
250 } 250 }
  251 + if hasAuth := agg.CheckUserModuleAuth(userCompany.Id); !hasAuth {
  252 + err = protocol.NewErrWithMessage(2002) //找不到这家公司
  253 + return
  254 + }
251 if auth, err = models.GetUserAuthByUserId(header.Uid, protocol.DeviceType); err != nil { 255 if auth, err = models.GetUserAuthByUserId(header.Uid, protocol.DeviceType); err != nil {
252 log.Error(err) 256 log.Error(err)
253 return 257 return
@@ -372,6 +376,130 @@ func UserInfo(header *protocol.RequestHeader, request *protocol.UserInfoRequest) @@ -372,6 +376,130 @@ func UserInfo(header *protocol.RequestHeader, request *protocol.UserInfoRequest)
372 return 376 return
373 } 377 }
374 378
  379 +//用户信息
  380 +func UserInfoV3(header *protocol.RequestHeader, request *protocol.UserInfoRequest) (rsp *protocol.UserInfoResponse, err error) {
  381 + var (
  382 + companyId int64
  383 + userCompany *models.UserCompany
  384 + userAuth *models.UserAuth
  385 + userBaseAgg *protocol.UserBaseInfoAggregation
  386 + companys []*models.Company
  387 + userCompanys []*models.UserCompany
  388 + tmpCompanys []*models.Company = make([]*models.Company, 0)
  389 + defaultUserId int64
  390 + )
  391 + if companys, err = models.GetCompanyByPermission(header.Uid); err != nil {
  392 + log.Error(err)
  393 + return
  394 + }
  395 + if userCompanys, err = models.GetUserAllCompany(header.Uid); err != nil {
  396 + log.Error(err)
  397 + return
  398 + }
  399 + for i := range userCompanys {
  400 + if hasAuth := agg.CheckUserModuleAuth(userCompanys[i].Id); !hasAuth {
  401 + log.Debug("检查公司权限:", userCompanys[i].Id, userCompanys[i].CompanyId, "无模块权限")
  402 + continue
  403 + }
  404 + if companyId == 0 { //取默认一个有权限的公司
  405 + companyId = userCompanys[i].CompanyId
  406 + defaultUserId = userCompanys[i].Id
  407 + }
  408 + for j := 0; j < len(companys); j++ {
  409 + if userCompanys[i].CompanyId == companys[j].Id {
  410 + tmpCompanys = append(tmpCompanys, companys[j])
  411 + break
  412 + }
  413 + }
  414 + if userCompanys[i].Id == header.UserId && userCompanys[i].CompanyId == header.CompanyId {
  415 + companyId = header.CompanyId
  416 + break
  417 + }
  418 + }
  419 + //公司列表是所有有权限的公司,企业平台
  420 + companys = tmpCompanys
  421 + if len(companys) == 0 {
  422 + err = protocol.NewErrWithMessage(2002, err) //账号不存在
  423 + return
  424 + }
  425 + if companyId == 0 {
  426 + if userCompany, err = models.GetUserCompanysFirst(header.Uid); err != nil {
  427 + log.Error(err)
  428 + return
  429 + }
  430 + if userAuth, err = models.GetUserAuthByUserId(header.Uid, protocol.DeviceType); err != nil {
  431 + log.Error(err)
  432 + return
  433 + }
  434 + if err = utils.UpdateTableByMap(&models.UserAuth{Id: userAuth.Id}, map[string]interface{}{
  435 + "CurrentCompanyId": userCompany.CompanyId, "CurrentUserCompanyId": userCompany.Id}); err != nil {
  436 + log.Error(err)
  437 + return
  438 + }
  439 + companyId = int64(userCompany.CompanyId)
  440 + header.UserId = userCompany.Id
  441 + }
  442 + if header.UserId == 0 {
  443 + header.UserId = defaultUserId
  444 + }
  445 + if userBaseAgg, err = agg.GetUserBaseInfoAggregation(header.UserId, companyId); err != nil {
  446 + return
  447 + }
  448 + rsp = &protocol.UserInfoResponse{
  449 + User: protocol.User{
  450 + UserId: header.UserId,
  451 + Name: userBaseAgg.User.NickName,
  452 + Phone: userBaseAgg.User.Phone,
  453 + Image: protocol.Picture{
  454 + Path: userBaseAgg.User.Icon,
  455 + H: 0,
  456 + W: 0,
  457 + },
  458 + ImToken: userBaseAgg.User.ImToken,
  459 + CompanyId: int(companyId),
  460 + Company: protocol.Company{
  461 + Id: userBaseAgg.Company.Id,
  462 + Name: userBaseAgg.Company.Name,
  463 + CId: userBaseAgg.Company.UserCenterId,
  464 + },
  465 + Departments: make([]protocol.Dep, 0),
  466 + Positions: make([]protocol.Job, 0),
  467 + },
  468 + }
  469 + for i := range companys {
  470 + rsp.User.Companys = append(rsp.User.Companys, protocol.Company{
  471 + Id: companys[i].Id,
  472 + Name: companys[i].Name,
  473 + CId: companys[i].UserCenterId,
  474 + })
  475 + }
  476 + newDep := func(item *protocol.Department) protocol.Dep {
  477 + return protocol.Dep{
  478 + Id: item.DepartmentId,
  479 + Name: item.Name,
  480 + }
  481 + }
  482 + newPos := func(item *protocol.Position) protocol.Job {
  483 + return protocol.Job{
  484 + Id: item.PositionId,
  485 + Name: item.Name,
  486 + }
  487 + }
  488 + for i := range userBaseAgg.Departments {
  489 + rsp.User.Departments = append(rsp.User.Departments, newDep(userBaseAgg.Departments[i]))
  490 + }
  491 + for i := range userBaseAgg.Positions {
  492 + rsp.User.Positions = append(rsp.User.Positions, newPos(userBaseAgg.Positions[i]))
  493 + }
  494 + if topDep := agg.GetTopDepartment(userBaseAgg.Departments); topDep.DepartmentId != 0 {
  495 + rsp.User.Department = newDep(topDep)
  496 + }
  497 + if topPos := agg.GetTopPosition(userBaseAgg.Positions); topPos.PositionId != 0 {
  498 + rsp.User.Position = newPos(topPos)
  499 + }
  500 + return
  501 +}
  502 +
375 //用户中心-统计信息 503 //用户中心-统计信息
376 func UserStatistics(header *protocol.RequestHeader, request *protocol.UserStatisticsRequest) (rsp *protocol.UserStatisticsResponse, err error) { 504 func UserStatistics(header *protocol.RequestHeader, request *protocol.UserStatisticsRequest) (rsp *protocol.UserStatisticsResponse, err error) {
377 var ( 505 var (