usr.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. local code = require "code"
  2. local tokenUtil = require "utils.tokenUtil"
  3. local nodeUtil = require("utils.nodeUtil")
  4. local lib_game_mysql = require("lib_game_mysql")
  5. local lib_game_redis = require("lib_game_redis")
  6. local moduleData = require("data.module")
  7. local userData = require("data.user")
  8. local lib_logger = require("log.lib_logger")
  9. local logRegister = lib_logger:new("register")
  10. local logLogin = lib_logger:new("login")
  11. local root = {}
  12. -- uuid注册
  13. local function l_uuid_register(msg)
  14. -- 是否第三方平台
  15. local uuid = msg.uuid
  16. if is_empty(uuid) then
  17. return
  18. end
  19. -- 是否已绑定uid
  20. local sql = string.format("select max(uid) as uid from mdl_user where uuid = '%s';", uuid)
  21. local result = lib_game_mysql:query(sql)
  22. log.info("l_uuid_register uuid[%s] result[%s]", tostring(uuid), tostring(result))
  23. if is_empty(result) then
  24. return
  25. end
  26. local uid = result[1]["uid"]
  27. if is_nil(uid) then
  28. return
  29. end
  30. uid = tonumber(uid)
  31. -- 账号状态
  32. local status = moduleData:hget_int(uid, "user", "status")
  33. if status > 0 then
  34. return
  35. end
  36. -- 直接进行登陆
  37. msg.uid = uid
  38. msg.password = moduleData:hget(uid, "password")
  39. local errCode, ret = root.login(msg)
  40. if code.is_ok(errCode) then
  41. ret.uid = msg.uid
  42. ret.password = msg.password
  43. else
  44. errCode = errCode or code.UNKNOWN
  45. ret = nil
  46. end
  47. return errCode, ret
  48. end
  49. -- 获取新uid
  50. local function l_get_register_uid()
  51. local uid = lib_game_redis:incrby("user:max:id", 1)
  52. if uid < 1000000 then
  53. uid = uid + 1000000
  54. lib_game_redis:set("user:max:id", uid)
  55. end
  56. return uid
  57. end
  58. -- 快速注册
  59. function root.register(msg)
  60. local errCode, ret = l_uuid_register(msg)
  61. if errCode then
  62. return errCode, ret
  63. end
  64. -- 正常注册
  65. local uid = l_get_register_uid()
  66. userData:user_init_register_info(uid, msg)
  67. -- 注册埋点
  68. local cntDatas = {
  69. uid,
  70. msg.channel,
  71. msg.version,
  72. msg.sysVer or "",
  73. msg.uuid,
  74. msg.udid,
  75. msg.ip
  76. }
  77. logRegister:raw(table.concat(cntDatas, ";"))
  78. msg.uid = uid
  79. msg.password = moduleData:hget_int(uid, "user", "password")
  80. errCode, ret = root.login(msg)
  81. if code.is_ok(errCode) then
  82. ret.uid = msg.uid
  83. ret.password = msg.password
  84. else
  85. errCode = errCode or code.UNKNOWN
  86. ret = nil
  87. end
  88. return errCode, ret
  89. end
  90. -- 快速登陆
  91. function root.login(msg)
  92. log.info("login msg[%s]", tostring(msg))
  93. local uid = msg.uid
  94. if is_empty(uid) or is_empty(msg.password) then
  95. return code.PARAMTER_ERROR
  96. end
  97. -- 密码检验
  98. if not userData:user_is_match_password(msg.password) then
  99. return code.USR.LOGIN_PASSWORD_ERROR
  100. end
  101. -- 状态
  102. local status = moduleData:hget_int(uid, "user", "status")
  103. if status > 0 then
  104. return code.USR.FORCE_OUT
  105. end
  106. -- 分配网关服务器
  107. local nodeInfo = nodeUtil:user_dispatch_gate_node_and_agent(uid)
  108. if is_empty(nodeInfo) or is_empty(nodeInfo.ip) or is_empty(nodeInfo.wsPort) then
  109. return code.NOT_FOUND_SERVER
  110. end
  111. -- 暂存登陆数据
  112. -- 登陆埋点
  113. local cntDatas = {
  114. uid,
  115. msg.channel,
  116. msg.version,
  117. msg.sysVer or "",
  118. msg.operator,
  119. msg.network,
  120. msg.uuid,
  121. msg.udid,
  122. msg.ip
  123. }
  124. logLogin:raw(table.concat(cntDatas, ";"))
  125. local uuid = moduleData:hget_int(uid, "user", "uuid")
  126. local registerTime = moduleData:hget_int(uid, "user", "registerTime")
  127. local password = moduleData:hget_int(uid, "user", "password")
  128. local token = tokenUtil.create(uid, password)
  129. local ret = {
  130. sysTime = skynet_time(),
  131. ip = nodeInfo.ip,
  132. wsPort = nodeInfo.wsPort,
  133. token = token,
  134. registerTime = registerTime,
  135. isGuest = is_empty(uuid),
  136. uuid = uuid,
  137. uid = uid,
  138. password = password
  139. }
  140. log.info("login uid[%d] ret[%s]", tostring(uid), tostring(ret))
  141. return code.OK, ret
  142. end
  143. return root