user.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package jyutil
  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. "github.com/pkg/errors"
  12. "jyOrderManager/internal/consts"
  13. "jyOrderManager/internal/model"
  14. "net/http"
  15. "time"
  16. )
  17. // GetCreateUserData 查询用户 不存在注册用户 isEntSign 是否需要注册企业信息(避免校验调用) 如entId不为0代办已注册企业信息
  18. func GetCreateUserData(phone, companyName string, isEntSign bool) (data map[string]interface{}, entId, userPositionId int64, err error) {
  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. return nil, 0, 0, fmt.Errorf("创建用户失败")
  45. }
  46. ck := &http.Cookie{}
  47. usercenter.AddBaseUser(*MG.DB(), g.Cfg().MustGet(ctx, "userCenterUrl").String(), id, map[string]interface{}{
  48. "appid": 10000,
  49. "phone": phone,
  50. "password": "",
  51. }, ck)
  52. SaveUserLog(id, phone)
  53. uData, _ := MG.DB().FindById("user", id, "")
  54. data = *uData
  55. }
  56. data["userId"] = common.InterfaceToStr(data["_id"])
  57. delete(data, "_id")
  58. //企业查询企业信息是否注册
  59. if isEntSign {
  60. entUser, err := g.DB().Ctx(ctx).GetOne(ctx, fmt.Sprintf(`SELECT a.ent_id FROM entniche_user a
  61. LEFT JOIN entniche_info b on b.name='%s' and a.ent_id = b.id
  62. WHERE phone = '%s' limit 1`, companyName, phone))
  63. if err != nil {
  64. return nil, 0, 0, errors.Wrapf(err, "查询企业是否存在异常 %s", companyName)
  65. }
  66. entId = gconv.Int64(entUser.Map()["ent_id"])
  67. if entId == 0 {
  68. entId, userPositionId, err = AutomaticallyCreatingEnt(companyName, phone, "", common.InterfaceToStr(data["_id"]))
  69. if err != nil {
  70. return nil, 0, 0, errors.Wrapf(err, "创建企业异常 %s", companyName)
  71. }
  72. } else {
  73. //查找baseUserId
  74. baseUserId := common.Int64All(data["base_user_id"])
  75. 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))
  76. if !positionData.IsEmpty() {
  77. userPositionId = gconv.Int64(positionData.Map()["id"])
  78. }
  79. }
  80. }
  81. return data, entId, userPositionId, nil
  82. }
  83. // 用户注册日志保存
  84. func SaveUserLog(userid, phone string) {
  85. data := map[string]interface{}{
  86. "userid": userid,
  87. "phone": phone,
  88. "way": "phone",
  89. "system": "pc",
  90. "source": "background",
  91. "create_time": date.NowFormat(date.Date_Full_Layout),
  92. }
  93. MG.DB().Save("register_log", data)
  94. }
  95. func GetUserFullId(phone, companyName string) (mongoUserId string, baseUserId, entId, entUserId int64) {
  96. userData, ok := MG.DB().FindOne("user", map[string]interface{}{
  97. "$or": []map[string]interface{}{
  98. {"s_phone": phone},
  99. {"s_m_phone": phone},
  100. },
  101. })
  102. if ok && userData != nil && len(*userData) > 0 {
  103. mongoUserId = mongodb.BsonIdToSId((*userData)["_id"])
  104. baseUserId = gconv.Int64((*userData)["base_user_id"])
  105. }
  106. if companyName == "" {
  107. entUser, err := g.DB().Ctx(ctx).GetOne(ctx, fmt.Sprintf(`SELECT a.ent_id,a.id FROM entniche_user a
  108. LEFT JOIN entniche_info b on b.name='%s' and a.ent_id = b.id
  109. WHERE phone = '%s' limit 1`, companyName, phone))
  110. if err == nil && !entUser.IsEmpty() {
  111. entId = gconv.Int64(entUser.Map()["ent_id"])
  112. entUserId = gconv.Int64(entUser.Map()["id"])
  113. }
  114. }
  115. return
  116. }
  117. var getPositionIdByMongoUserId = func(mgoUserId string) int64 {
  118. userData, ok := MG.DB().FindById("user", mgoUserId, `"base_user_id":1`)
  119. if userData != nil && len(*userData) > 0 && ok {
  120. baseUserId := common.Int64All((*userData)["base_user_id"])
  121. positionData, _ := g.DB("base").GetOne(ctx, "SELECT id FROM base_position WHERE type=0 and ent_id=0 and user_id=?", baseUserId)
  122. if !positionData.IsEmpty() {
  123. return gconv.Int64(positionData.Map()["id"])
  124. }
  125. }
  126. return -1
  127. }
  128. func ClearBigVipUserPower(ctx context.Context, userId string) {
  129. var (
  130. powerChacheKey = g.Cfg().MustGet(ctx, "bigmemberKey", "bigmember_power_3_").String()
  131. IsGetUserBaseInfoRedisKey = "baseinfo_%s"
  132. RedisMenuKeyPC = "jy_workdesktopmenu_10000_PC_menu1_%s" //剑鱼appid:10000
  133. RedisMenuKeyWX = "jy_workdesktopmenu_10000_WX_menu1_%s" //剑鱼appid:10000
  134. RedisMenuKeyAPP = "jy_workdesktopmenu_10000_APP_menu1_%s" //剑鱼appid:10000
  135. UserPowerRedisKey = "jy_userpowerredis_10000_%d_%s" //工作桌面 用户功能缓存(类似bigmember_power_3_%s)
  136. )
  137. if mongodb.IsObjectIdHex(userId) {
  138. user_id := userId
  139. //大会员主账号清理
  140. userDatas, ok := MG.DB().Find("user", map[string]interface{}{"s_member_mainid": user_id, "i_member_sub_status": 1}, nil, nil, false, -1, -1)
  141. if ok && userDatas != nil && len(*userDatas) > 0 {
  142. for _, v := range *userDatas {
  143. if pId := getPositionIdByMongoUserId(mongodb.BsonIdToSId(v["_id"])); pId > 0 {
  144. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf("%s%d", powerChacheKey, pId))
  145. }
  146. }
  147. }
  148. //通过mongo查找职位标识
  149. if pId := getPositionIdByMongoUserId(userId); pId > 0 {
  150. userId = fmt.Sprint(pId)
  151. }
  152. }
  153. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf(powerChacheKey, userId))
  154. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf("jy_workdesktopmenu_10000_menu1_%s", userId))
  155. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf("jy_userpowerredis_10000_17_%s", userId))
  156. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf("jy_workdesktopmenu_10000_menu2_%s", userId))
  157. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf("jy_workdesktopmenu_10000_PC_menu2_%s", userId))
  158. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf("jy_workdesktopmenu_10000_WX_menu2_%s", userId))
  159. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf("jy_workdesktopmenu_10000_APP_menu2_%s", userId))
  160. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf("user_power_info_%s", userId))
  161. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf(IsGetUserBaseInfoRedisKey, userId))
  162. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf(RedisMenuKeyPC, userId))
  163. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf(RedisMenuKeyWX, userId))
  164. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf(RedisMenuKeyAPP, userId))
  165. _, _ = g.Redis("newother").Del(ctx, fmt.Sprintf(UserPowerRedisKey, time.Now().Day(), userId))
  166. _, _ = g.Redis("newother").Del(ctx, "pl_indexMessage_"+userId)
  167. }
  168. func PayUserIdentitySwitchRedis(mgoUserId string, data map[string]interface{}) {
  169. key := fmt.Sprintf("jy_identitySwitch_%s", mgoUserId)
  170. _ = g.Redis("newother").SetEX(ctx, key, data, -1)
  171. }
  172. func GetUserMsgFromCtx(ctx context.Context) *model.User {
  173. if value := ctx.Value(consts.ContextKey); value != nil {
  174. if c, ok := value.(*model.User); ok {
  175. return c
  176. }
  177. }
  178. return nil
  179. }
  180. func GetUserDeptIdFromCtx(ctx context.Context) int64 {
  181. if u := GetUserMsgFromCtx(ctx); u != nil {
  182. return u.EntDeptId
  183. }
  184. return 0
  185. }