usr.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. msg.idfa
  77. }
  78. logRegister:raw(table.concat(cntDatas, ";"))
  79. msg.uid = uid
  80. msg.password = moduleData:hget_int(uid, "user", "password")
  81. errCode, ret = root.login(msg)
  82. if code.is_ok(errCode) then
  83. ret.uid = msg.uid
  84. ret.password = msg.password
  85. else
  86. errCode = errCode or code.UNKNOWN
  87. ret = nil
  88. end
  89. return errCode, ret
  90. end
  91. -- 快速登陆
  92. function root.login(msg)
  93. log.info("login msg[%s]", tostring(msg))
  94. local uid = msg.uid
  95. if is_empty(uid) or is_empty(msg.password) then
  96. return code.PARAMTER_ERROR
  97. end
  98. -- 密码检验
  99. if not userData:user_is_match_password(msg.password) then
  100. return code.USR.LOGIN_PASSWORD_ERROR
  101. end
  102. -- 状态
  103. local status = moduleData:hget_int(uid, "user", "status")
  104. if status > 0 then
  105. return code.USR.FORCE_OUT
  106. end
  107. -- 分配网关服务器
  108. local nodeInfo = nodeUtil:user_dispatch_gate_node_and_agent(uid)
  109. if is_empty(nodeInfo) or is_empty(nodeInfo.ip) or is_empty(nodeInfo.wsPort) then
  110. return code.NOT_FOUND_SERVER
  111. end
  112. -- 暂存登陆数据
  113. -- 登陆埋点
  114. local cntDatas = {
  115. uid,
  116. msg.channel,
  117. msg.version,
  118. msg.sysVer or "",
  119. msg.operator,
  120. msg.network,
  121. msg.uuid,
  122. msg.udid,
  123. msg.ip
  124. }
  125. logLogin:raw(table.concat(cntDatas, ";"))
  126. local uuid = moduleData:hget_int(uid, "user", "uuid")
  127. local registerTime = moduleData:hget_int(uid, "user", "registerTime")
  128. local password = moduleData:hget_int(uid, "user", "password")
  129. local token = tokenUtil.create(uid, password)
  130. local ret = {
  131. sysTime = skynet_time(),
  132. ip = nodeInfo.ip,
  133. wsPort = nodeInfo.wsPort,
  134. token = token,
  135. registerTime = registerTime,
  136. isGuest = is_empty(uuid),
  137. uuid = uuid,
  138. uid = uid,
  139. password = password
  140. }
  141. log.info("login uid[%d] ret[%s]", tostring(uid), tostring(ret))
  142. return code.OK, ret
  143. end
  144. -- 账号是否已注册
  145. local function l_get_account_uid(account)
  146. if is_empty(account) then
  147. return
  148. end
  149. local sql = string.format("select max(uid) as uid from mdl_account where account = '%s';", account)
  150. local result = lib_game_mysql:query(sql)
  151. log.info("l_is_account_registed account[%s] result[%s]", tostring(account), tostring(result))
  152. if is_empty(result) then
  153. return
  154. end
  155. local uid = result[1]["uid"]
  156. if is_nil(uid) then
  157. return
  158. end
  159. return tonumber(uid)
  160. end
  161. -- 账号注册
  162. function root.account_register(msg)
  163. local account, password = msg.account, msg.password
  164. if is_empty(account) or is_empty(password) then
  165. return code.PARAMTER_ERROR
  166. end
  167. msg.password = nil
  168. -- 重置uuid
  169. local errCode, ret = root.register(msg)
  170. if not code.is_ok(errCode) then
  171. return errCode
  172. end
  173. -- 账号信息
  174. local uid = l_get_account_uid(account)
  175. if uid then
  176. return code.USR.ALREADY_REGISTER
  177. end
  178. -- 新增账号
  179. local sql =
  180. string.format(
  181. "replace into `mdl_account` (`uid`, `account`, `password`) VALUE (%s, '%s', '%s');",
  182. tostring(uid),
  183. account,
  184. password
  185. )
  186. lib_game_mysql:query(sql)
  187. return code.OK, {uid = ret.uid, password = ret.password}
  188. end
  189. -- 账号登录
  190. function root.account_login(msg)
  191. local account, password = msg.account, msg.password
  192. if is_empty(account) or is_empty(password) then
  193. return code.PARAMTER_ERROR
  194. end
  195. local uid = l_get_account_uid(account)
  196. if is_empty(uid) then
  197. return code.USR.NOT_EXIST_USER
  198. end
  199. -- 密码检验
  200. if not userData:user_is_match_password(password) then
  201. return code.USR.LOGIN_PASSWORD_ERROR
  202. end
  203. msg.password = password
  204. return root.login(msg)
  205. end
  206. return root