mod.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package mod
  2. import (
  3. pb "git.wanbits.io/joe/franklin/protos"
  4. "git.wanbits.io/joe/kettle/rds"
  5. "git.wanbits.io/joe/kettle/utl"
  6. "github.com/go-redis/redis"
  7. "github.com/mitchellh/mapstructure"
  8. "reflect"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. var (
  14. r *redis.Client
  15. lsm *rds.LuaScriptManager
  16. )
  17. // convert ANY struct( or its pointer) to map[string]interface{}
  18. // opt: bool. if true convert keys of struct to their lowercase
  19. func StructToMap(stru interface{}, lowerCase ...bool) (map[string]interface{}, error) {
  20. conf := func(v string) string {
  21. return v
  22. }
  23. if len(lowerCase) > 0 && lowerCase[0] {
  24. conf = strings.ToLower
  25. }
  26. t := reflect.TypeOf(stru)
  27. vals := reflect.ValueOf(stru)
  28. switch t.Kind() {
  29. case reflect.Ptr:
  30. t = t.Elem()
  31. vals = vals.Elem()
  32. if t.Kind() != reflect.Struct {
  33. return nil, utl.ErrParameters
  34. }
  35. case reflect.Struct:
  36. default:
  37. return nil, utl.ErrParameters
  38. }
  39. rt := make(map[string]interface{})
  40. for i := 0; i < t.NumField(); i++ {
  41. fn := conf(t.Field(i).Name)
  42. val := vals.Field(i).Interface()
  43. rt[fn] = val
  44. }
  45. return rt, nil
  46. }
  47. func Install(client *redis.Client) {
  48. r = client
  49. lsm = rds.NewLuaScriptManager(r)
  50. }
  51. func ExistUser(userId uint64) bool {
  52. ival, err := r.Exists(kh_user(userId)).Result()
  53. return err == nil && ival != 0
  54. }
  55. func LoadUser(userId uint64) (*pb.User, error) {
  56. mp, err := r.HGetAll(kh_user(userId)).Result()
  57. if err != nil {
  58. return nil, err
  59. }
  60. //mpsi := make(map[string]interface{})
  61. //for k, v := range mp {
  62. // mpsi[k] = v
  63. //}
  64. user := &pb.User{}
  65. err = mapstructure.Decode(mp, user)
  66. return user, err
  67. }
  68. func UpdateUserAttr(userId uint64, field string, val interface{}) error {
  69. _, err := r.HSet(kh_user(userId), field, val).Result()
  70. return err
  71. }
  72. func UpdateUserAttrs(userId uint64, fields map[string]interface{}) error {
  73. _, err := r.HMSet(kh_user(userId), fields).Result()
  74. return err
  75. }
  76. func SaveKick(userId uint64, secs int) error {
  77. _, err := r.Set(ks_kick(userId), 1, time.Duration(secs)*time.Second).Result()
  78. return err
  79. }
  80. func LoadKick(userId uint64) bool {
  81. _, err := r.Get(ks_kick(userId)).Result()
  82. return err == nil
  83. }
  84. func SaveOffline(userId, logicId uint64) error {
  85. _, err := r.HSet(kh_offlines, strconv.FormatUint(userId, 10), logicId).Result()
  86. return err
  87. }
  88. func LoadOffline(userId uint64) (uint64, error) {
  89. sid, err := r.HGet(kh_offlines, strconv.FormatUint(userId, 10)).Result()
  90. if err != nil {
  91. return 0, err
  92. }
  93. return strconv.ParseUint(sid, 10, 64)
  94. }
  95. func SaveUserLogin(userId, agentId uint64) error {
  96. _, err := lsm.Exec("user_login", []string{}, userId, agentId).Result()
  97. return err
  98. }
  99. func SaveUserLogout(userId uint64) error {
  100. _, err := lsm.Exec("user_logout", []string{}, userId).Result()
  101. return err
  102. }
  103. func SaveUserChangeLogic(userId, logicId uint64) error {
  104. _, err := lsm.Exec("user_change_logic", []string{}, userId, logicId).Result()
  105. return err
  106. }
  107. func LoadUserLogic(userId uint64) (uint64, error) {
  108. sid, err := r.HGet(kh_user_logic, strconv.FormatUint(userId, 10)).Result()
  109. if err != nil {
  110. return 0, err
  111. }
  112. return strconv.ParseUint(sid, 10, 64)
  113. }