participate.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package util
  2. import (
  3. "app.yhyue.com/moapp/jybase/redis"
  4. "encoding/json"
  5. "fmt"
  6. IC "jyBXCore/rpc/init"
  7. "sync"
  8. )
  9. var (
  10. PLock *ParticipateLock
  11. )
  12. type ParticipateLock struct {
  13. sync.Mutex
  14. UserLock map[string]*sync.Mutex
  15. }
  16. func NewParticipateLock() *ParticipateLock {
  17. return &ParticipateLock{
  18. UserLock: make(map[string]*sync.Mutex),
  19. }
  20. }
  21. func GetParticipateLock(str string) *sync.Mutex {
  22. PLock.Lock()
  23. if PLock.UserLock[str] == nil {
  24. PLock.UserLock[str] = &sync.Mutex{}
  25. }
  26. PLock.Unlock()
  27. return PLock.UserLock[str]
  28. }
  29. // 参标权限判断 权益请求check userId 应传 mongo userId
  30. func IsAllowedParticipate(appId, userId string, newUserId, accountId, entAccountId, entId, entUserId, positionId, positionType int64) (b bool, role int64) {
  31. powerCheck := IC.Middleground.PowerCheckCenter.Check(appId, userId, newUserId, accountId, entId, positionType, positionId)
  32. //不是超级订阅 也不是大会员
  33. if powerCheck.Vip.Status <= 0 && powerCheck.Member.Status <= 0 {
  34. return
  35. }
  36. role = powerCheck.Ent.EntRoleId
  37. resource := IC.Middleground.ResourceCenter.Haspowers(accountId, entAccountId, entId, entUserId)
  38. if len(resource.Powers) != 0 {
  39. for _, r := range resource.Powers {
  40. //资源中台 cb_zy_code
  41. if r == IC.C.ResourceCode {
  42. b = true
  43. break
  44. }
  45. }
  46. }
  47. return
  48. }
  49. // 3秒内防止重复提交
  50. func IsAllowedAccess(key string) string {
  51. redisKey := fmt.Sprintf("participate_isAllowed_%s", key)
  52. if b, err := redis.Exists("other", redisKey); err != nil && b {
  53. return "访问频次过快,请稍后再试"
  54. }
  55. redis.Put("other", redisKey, key, 3)
  56. return ""
  57. }
  58. // 判断字符串是否为合法的 JSON 字符串,如果是,则转换为 map;如果不是,则直接输出原始字符串
  59. // bool :true则是map 否则是string
  60. func ConvertJSONString(s string) (interface{}, bool) {
  61. m, err := JSONStringToMap(s)
  62. if err != nil {
  63. return s, false
  64. }
  65. return m, true
  66. }
  67. // 判断字符串是否为合法的 JSON 字符串,并将其转换为 map
  68. func JSONStringToMap(s string) (map[string]interface{}, error) {
  69. var m map[string]interface{}
  70. err := json.Unmarshal([]byte(s), &m)
  71. if err != nil {
  72. return nil, err
  73. }
  74. return m, nil
  75. }