identity.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package identity
  2. import (
  3. "strconv"
  4. . "app.yhyue.com/moapp/jybase/common"
  5. "app.yhyue.com/moapp/jybase/go-xweb/httpsession"
  6. . "app.yhyue.com/moapp/jybase/mongodb"
  7. . "app.yhyue.com/moapp/jypkg/middleground"
  8. "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
  9. )
  10. type IdentityInfo struct {
  11. Name string //名称
  12. PersonId int64 //自然人id
  13. UserName string //用户昵称
  14. AccountId int64 //账户id
  15. EntAccountId int64 //企业账户id
  16. PositionId int64 //职位id
  17. PositionType int64 //职位类型
  18. EntId int64 //企业id
  19. EntUserId int64 //企业员工id
  20. EntUserName string //企业员工姓名
  21. EntRole int64 //管理员角色
  22. EntNicheDis int64 //商机分配角色
  23. }
  24. func NewIdentityInfo(i *pb.Identity) *IdentityInfo {
  25. return &IdentityInfo{
  26. Name: i.Name,
  27. PersonId: i.PersonId,
  28. UserName: i.UserName,
  29. AccountId: i.AccountId,
  30. EntAccountId: i.EntAccountId,
  31. PositionId: i.PositionId,
  32. PositionType: i.PositionType,
  33. EntId: i.EntId,
  34. EntUserId: i.EntUserId,
  35. EntUserName: i.EntUserName,
  36. EntRole: i.EntRole,
  37. EntNicheDis: i.EntNicheDis,
  38. }
  39. }
  40. // 切换身份
  41. func (i *IdentityInfo) Switch(sess *httpsession.Session, mgo *MongodbSim) bool {
  42. ok := false
  43. m := map[string]interface{}{
  44. "personId": i.PersonId,
  45. "userName": i.UserName,
  46. "accountId": i.AccountId,
  47. "positionId": i.PositionId,
  48. "positionType": i.PositionType,
  49. }
  50. mgoUserId, _ := sess.Get("mgoUserId").(string)
  51. if i.PositionType == 0 {
  52. if sess.Del("entId", "entName", "entUserId", "entUserName", "entAccountId") {
  53. m["userId"] = mgoUserId
  54. ok = true
  55. }
  56. } else {
  57. m["entId"] = i.EntId
  58. m["entUserId"] = i.EntUserId
  59. m["entName"] = i.Name
  60. m["entUserName"] = i.EntUserName
  61. m["frameworkEntId"] = i.EntId
  62. m["frameworkEntName"] = i.Name
  63. m["userId"] = strconv.FormatInt(i.PositionId, 10)
  64. m["entAccountId"] = i.EntAccountId
  65. m["entRole"] = i.EntRole
  66. m["entNicheDis"] = i.EntNicheDis
  67. ok = true
  68. }
  69. sess.SetMultiple(m)
  70. mgo.UpdateById("user", mgoUserId, map[string]interface{}{
  71. "$set": map[string]interface{}{
  72. "login_positionid": i.PositionId,
  73. },
  74. })
  75. return ok
  76. }
  77. // 切换到最优身份
  78. func SwitchToBest(userId int64, sess *httpsession.Session, mgd *Middleground, mgo *MongodbSim, isSelectLast bool) (int64, bool) {
  79. list := mgd.UserCenter.IdentityList(userId)
  80. if list == nil || len(list) == 0 {
  81. return -1, false
  82. }
  83. if isSelectLast {
  84. if mgoUserId, _ := sess.Get("mgoUserId").(string); mgoUserId != "" {
  85. user, ok := mgo.FindById("user", mgoUserId, `{"login_positionid":1}`)
  86. if ok && user != nil && len(*user) > 0 {
  87. if login_positionid := Int64All((*user)["login_positionid"]); login_positionid > 0 {
  88. for _, v := range list {
  89. if v.PositionId == login_positionid {
  90. return v.PositionId, NewIdentityInfo(v).Switch(sess, mgo)
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. reqIds := []int64{}
  98. for _, v := range list {
  99. if v.PositionType == 1 {
  100. reqIds = append(reqIds, v.EntUserId)
  101. }
  102. }
  103. if len(reqIds) > 0 {
  104. if resp := mgd.EntManageApplication.EmpowerUserIds(reqIds); resp != nil && len(resp.Ids) > 0 {
  105. for _, v := range list {
  106. if v.PositionType == 1 && v.EntUserId == resp.Ids[0] {
  107. return v.PositionId, NewIdentityInfo(v).Switch(sess, mgo)
  108. }
  109. }
  110. }
  111. }
  112. for _, v := range list {
  113. if v.PositionType == 0 {
  114. return v.PositionId, NewIdentityInfo(v).Switch(sess, mgo)
  115. }
  116. }
  117. return -1, false
  118. }