user.go 5.6 KB

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