checkPower.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package rpc
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "app.yhyue.com/moapp/jybase/redis"
  6. "bp.jydev.jianyu360.cn/BaseService/gateway/common/db"
  7. . "bp.jydev.jianyu360.cn/BaseService/gateway/common/gatecode"
  8. "github.com/gogf/gf/v2/util/gconv"
  9. )
  10. var PowerCacheDb = "other"
  11. var PowerCacheKey = "bigmember_power_3_%s"
  12. //大会员状态redis缓存
  13. type BigVipBaseMsg struct {
  14. Status int `json:"status"` //大会员状态
  15. VipStatus int `json:"vip_status"` //超级订阅状态
  16. EntnicheStatus int `json:"entniche_status"` //商机管理状态
  17. }
  18. /// CheckUserPower 校验账户是否有对应权益
  19. // userId 老用户id (mongodb里的objectid)
  20. // power_type 权益判断
  21. func CheckUserPower(userid, phone string, power_type int) error {
  22. cacheKey := fmt.Sprintf(PowerCacheKey, userid)
  23. userPower := BigVipBaseMsg{}
  24. //判断权益
  25. if bytes, err := redis.GetBytes(PowerCacheDb, cacheKey); err == nil && bytes != nil {
  26. if err := json.Unmarshal(*bytes, &userPower); err != nil {
  27. return NewErrorWithCode(GLOBAL_ERR_RESOURCE_PORWE_FAIL, fmt.Sprintf("校验异常"))
  28. }
  29. } else {
  30. //未找到缓存
  31. data, ok := db.MgoJy.FindById("user", userid, `{"i_member_status":1,"i_vip_status":1}`)
  32. if ok && *data != nil && len(*data) > 0 {
  33. userPower.Status = gconv.Int((*data)["i_member_status"])
  34. userPower.VipStatus = gconv.Int((*data)["i_vip_status"])
  35. }
  36. }
  37. userPower.EntnicheStatus = 0
  38. if count := db.JyMysql.CountBySql(`SELECT count(1) FROM entniche_user u LEFT JOIN entniche_info i
  39. ON u.ent_id=i.id
  40. WHERE u.phone=? and u.power=1 and i.status=1`, phone); count > 0 {
  41. userPower.EntnicheStatus = 1
  42. }
  43. /*
  44. 权益判断方式、0走资源中台
  45. 1前置代理判断,是否是付费用户
  46. 2前置代理判断,是否是超级订阅
  47. 3前置代理判断,是否是大会员
  48. 4前置代理判断,是否是商机管理
  49. */
  50. switch power_type {
  51. case 1:
  52. if userPower.VipStatus <= 0 && userPower.Status <= 0 && userPower.EntnicheStatus <= 0 {
  53. return NewErrorWithCode(GLOBAL_ERR_NOPOWER, fmt.Sprintf("该用户非付费用户"))
  54. }
  55. case 2:
  56. if userPower.VipStatus <= 0 {
  57. return NewErrorWithCode(GLOBAL_ERR_NOPOWER, fmt.Sprintf("该用户非超级订阅用户"))
  58. }
  59. case 3:
  60. if userPower.Status <= 0 {
  61. return NewErrorWithCode(GLOBAL_ERR_NOPOWER, fmt.Sprintf("该用户非大会员用户"))
  62. }
  63. case 4:
  64. if userPower.EntnicheStatus <= 0 {
  65. return NewErrorWithCode(GLOBAL_ERR_NOPOWER, fmt.Sprintf("该用户非商机管理用户"))
  66. }
  67. case 22: //临时加大会员专家版、商机版判断
  68. if !(userPower.Status == 1 || userPower.Status == 2 || userPower.Status == 6 || userPower.Status == 7) {
  69. return NewErrorWithCode(GLOBAL_ERR_NOPOWER, fmt.Sprintf("该用户非大会员专家版、商机版用户"))
  70. }
  71. }
  72. return nil
  73. }