identity.go 3.2 KB

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