user.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package utility
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/date"
  5. "app.yhyue.com/moapp/jybase/mongodb"
  6. "app.yhyue.com/moapp/jybase/usercenter"
  7. "context"
  8. "fmt"
  9. "github.com/gogf/gf/v2/frame/g"
  10. "github.com/gogf/gf/v2/util/gconv"
  11. "net/http"
  12. "time"
  13. )
  14. var (
  15. ctx = context.Background()
  16. )
  17. // GetCreateUserData 查询用户 不存在注册用户 isEntSign 是否需要注册企业信息(避免校验调用) 如entId不为0代办已注册企业信息
  18. func GetCreateUserData(phone, companyName, orderCode string, entId int, isEntSign bool) map[string]interface{} {
  19. data := make(map[string]interface{})
  20. userData, ok := MG.DB().FindOne("user", map[string]interface{}{
  21. "$or": []map[string]interface{}{
  22. {"s_phone": phone},
  23. {"s_m_phone": phone},
  24. },
  25. })
  26. if ok && userData != nil && len(*userData) > 0 {
  27. data = *userData
  28. } else {
  29. id := MG.DB().Save("user", map[string]interface{}{
  30. "i_appid": 2,
  31. "s_phone": phone,
  32. "s_password": "",
  33. "l_registedate": time.Now().Unix(),
  34. "i_ts_guide": 2,
  35. "o_jy": map[string]interface{}{
  36. "i_wxpush": 1,
  37. "i_apppush": 1,
  38. "i_ratemode": 2,
  39. "l_modifydate": time.Now().Unix(),
  40. },
  41. "s_regsource": "qmx_admin",
  42. })
  43. if id != "" {
  44. formdata := map[string]interface{}{
  45. "appid": 10000,
  46. "phone": phone,
  47. "password": "",
  48. }
  49. ck := &http.Cookie{}
  50. usercenter.AddBaseUser(*MG.DB(), g.Cfg().MustGet(ctx, "userCenterUrl").String(), id, formdata, ck)
  51. SaveUserLog(id, phone)
  52. uData, _ := MG.DB().FindById("user", id, "")
  53. data = *uData
  54. }
  55. }
  56. var userPositionId string
  57. //查找baseUserId
  58. baseUserId := common.Int64All(data["base_user_id"])
  59. positionData, _ := g.DB().Ctx(ctx).GetOne(ctx, fmt.Sprintf(`SELECT s_name FROM base_position WHERE type=1 and user_id='%d' and ent_id=%d`, baseUserId, entId))
  60. if !positionData.IsEmpty() {
  61. userPositionId = gconv.String(positionData.Map()["id"])
  62. }
  63. //校验企业用户是否创建
  64. if entId == 0 && isEntSign {
  65. entId, _, userPositionId = AutomaticallyCreatingEnt(companyName, phone, orderCode, common.InterfaceToStr(data["_id"]))
  66. }
  67. data["entId"] = entId
  68. data["userPositionId"] = userPositionId
  69. return data
  70. }
  71. // 用户注册日志保存
  72. func SaveUserLog(userid, phone string) {
  73. data := map[string]interface{}{
  74. "userid": userid,
  75. "phone": phone,
  76. "way": "phone",
  77. "system": "pc",
  78. "source": "background",
  79. "create_time": date.NowFormat(date.Date_Full_Layout),
  80. }
  81. MG.DB().Save("register_log", data)
  82. }
  83. var getPositionIdByMongoUserId = func(mgoUserId string) int64 {
  84. userData, ok := MG.DB().FindById("user", mgoUserId, `"base_user_id":1`)
  85. if userData != nil && len(*userData) > 0 && ok {
  86. baseUserId := common.Int64All((*userData)["base_user_id"])
  87. positionData, _ := g.DB("base").GetOne(ctx, "SELECT id FROM base_position WHERE type=0 and ent_id=0 and user_id=?", baseUserId)
  88. if !positionData.IsEmpty() {
  89. return gconv.Int64(positionData.Map()["id"])
  90. }
  91. }
  92. return -1
  93. }
  94. func ClearBigVipUserPower(ctx context.Context, userId string) {
  95. var (
  96. powerChacheKey = g.Cfg().MustGet(ctx, "bigmemberKey", "bigmember_power_3_").String()
  97. IsGetUserBaseInfoRedisKey = "baseinfo_%s"
  98. RedisMenuKeyPC = "jy_workdesktopmenu_10000_PC_menu1_%s" //剑鱼appid:10000
  99. RedisMenuKeyWX = "jy_workdesktopmenu_10000_WX_menu1_%s" //剑鱼appid:10000
  100. RedisMenuKeyAPP = "jy_workdesktopmenu_10000_APP_menu1_%s" //剑鱼appid:10000
  101. UserPowerRedisKey = "jy_userpowerredis_10000_%d_%s" //工作桌面 用户功能缓存(类似bigmember_power_3_%s)
  102. )
  103. if mongodb.IsObjectIdHex(userId) {
  104. user_id := userId
  105. //大会员主账号清理
  106. userDatas, ok := MG.DB().Find("user", map[string]interface{}{"s_member_mainid": user_id, "i_member_sub_status": 1}, nil, nil, false, -1, -1)
  107. if ok && userDatas != nil && len(*userDatas) > 0 {
  108. for _, v := range *userDatas {
  109. if pId := getPositionIdByMongoUserId(mongodb.BsonIdToSId(v["_id"])); pId > 0 {
  110. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf("%s%d", powerChacheKey, pId))
  111. }
  112. }
  113. }
  114. //通过mongo查找职位标识
  115. if pId := getPositionIdByMongoUserId(userId); pId > 0 {
  116. userId = fmt.Sprint(pId)
  117. }
  118. }
  119. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf(powerChacheKey, userId))
  120. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf("jy_workdesktopmenu_10000_menu1_%s", userId))
  121. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf("jy_userpowerredis_10000_17_%s", userId))
  122. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf("jy_workdesktopmenu_10000_menu2_%s", userId))
  123. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf("jy_workdesktopmenu_10000_PC_menu2_%s", userId))
  124. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf("jy_workdesktopmenu_10000_WX_menu2_%s", userId))
  125. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf("jy_workdesktopmenu_10000_APP_menu2_%s", userId))
  126. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf("user_power_info_%s", userId))
  127. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf(IsGetUserBaseInfoRedisKey, userId))
  128. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf(RedisMenuKeyPC, userId))
  129. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf(RedisMenuKeyWX, userId))
  130. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf(RedisMenuKeyAPP, userId))
  131. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf(UserPowerRedisKey, time.Now().Day(), userId))
  132. _, _ = g.Redis("newother").Del(ctx, "pl_indexMessage_"+userId)
  133. }
  134. func PayUserIdentitySwitchRedis(mgoUserId string, data map[string]interface{}) {
  135. key := fmt.Sprintf("jy_identitySwitch_%s", mgoUserId)
  136. _ = g.Redis("newother").SetEX(ctx, key, data, -1)
  137. }