identity.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. EntDeptId int64 //部门id
  24. }
  25. func NewIdentityInfo(i *pb.Identity) *IdentityInfo {
  26. return &IdentityInfo{
  27. Name: i.Name,
  28. PersonId: i.PersonId,
  29. UserName: i.UserName,
  30. AccountId: i.AccountId,
  31. EntAccountId: i.EntAccountId,
  32. PositionId: i.PositionId,
  33. PositionType: i.PositionType,
  34. EntId: i.EntId,
  35. EntUserId: i.EntUserId,
  36. EntUserName: i.EntUserName,
  37. EntRole: i.EntRole,
  38. EntNicheDis: i.EntNicheDis,
  39. EntDeptId: i.EntDeptId,
  40. }
  41. }
  42. // 切换身份
  43. func (i *IdentityInfo) Switch(sess *httpsession.Session, mgo *MongodbSim) bool {
  44. ok := false
  45. m := map[string]interface{}{
  46. "personId": i.PersonId,
  47. "userName": i.UserName,
  48. "accountId": i.AccountId,
  49. "positionId": i.PositionId,
  50. "positionType": i.PositionType,
  51. }
  52. mgoUserId, _ := sess.Get("mgoUserId").(string)
  53. if i.PositionType == 0 {
  54. if sess.Del("entId", "entUserId", "entName", "entUserName", "frameworkEntId", "frameworkEntName", "userId", "entAccountId", "entRole", "entNicheDis", "entDeptId") {
  55. m["userId"] = mgoUserId
  56. ok = true
  57. }
  58. } else {
  59. m["entId"] = i.EntId
  60. m["entUserId"] = i.EntUserId
  61. m["entName"] = i.Name
  62. m["entUserName"] = i.EntUserName
  63. m["frameworkEntId"] = i.EntId
  64. m["frameworkEntName"] = i.Name
  65. m["userId"] = strconv.FormatInt(i.PositionId, 10)
  66. m["entAccountId"] = i.EntAccountId
  67. m["entRole"] = i.EntRole
  68. m["entNicheDis"] = i.EntNicheDis
  69. m["entDeptId"] = i.EntDeptId
  70. ok = true
  71. }
  72. sess.SetMultiple(m)
  73. mgo.UpdateById("user", mgoUserId, map[string]interface{}{
  74. "$set": map[string]interface{}{
  75. "login_positionid": i.PositionId,
  76. },
  77. })
  78. return ok
  79. }
  80. // 切换到最优身份
  81. func SwitchToBest(userId int64, sess *httpsession.Session, mgd *Middleground, mgo *MongodbSim, isSelectLast bool) (int64, bool) {
  82. list := mgd.UserCenter.IdentityList(userId)
  83. if list == nil || len(list) == 0 {
  84. return -1, false
  85. }
  86. if isSelectLast {
  87. if mgoUserId, _ := sess.Get("mgoUserId").(string); mgoUserId != "" {
  88. user, ok := mgo.FindById("user", mgoUserId, `{"login_positionid":1}`)
  89. if ok && user != nil && len(*user) > 0 {
  90. if login_positionid := Int64All((*user)["login_positionid"]); login_positionid > 0 {
  91. for _, v := range list {
  92. if v.PositionId == login_positionid {
  93. return v.PositionId, NewIdentityInfo(v).Switch(sess, mgo)
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. reqIds := []int64{}
  101. for _, v := range list {
  102. if v.PositionType == 1 {
  103. reqIds = append(reqIds, v.EntUserId)
  104. }
  105. }
  106. if len(reqIds) > 0 {
  107. if resp := mgd.EntManageApplication.EmpowerUserIds(reqIds); resp != nil && len(resp.Ids) > 0 {
  108. for _, v := range list {
  109. if v.PositionType == 1 && v.EntUserId == resp.Ids[0] {
  110. return v.PositionId, NewIdentityInfo(v).Switch(sess, mgo)
  111. }
  112. }
  113. }
  114. }
  115. for _, v := range list {
  116. if v.PositionType == 0 {
  117. return v.PositionId, NewIdentityInfo(v).Switch(sess, mgo)
  118. }
  119. }
  120. return -1, false
  121. }