usr.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. local code = require "code"
  2. local tokenUtil = require "utils.tokenUtil"
  3. local serverLogUtil = require("utils.serverLogUtil")
  4. local util_user = require("utils.util_user")
  5. local accountModule = require("modules.accountModule")
  6. local lib_game_mysql = require("lib_game_mysql")
  7. local lib_game_redis = require("lib_game_redis")
  8. local moduleData = require("data.module")
  9. local userData = require("data.user")
  10. local root = {}
  11. -- uuid注册
  12. local function l_uuid_register(msg)
  13. -- 是否第三方平台
  14. local uuid = msg.uuid
  15. if is_empty(uuid) then
  16. return
  17. end
  18. -- 是否已绑定uid
  19. local sql = string.format("select max(uid) as uid from mdl_user where uuid = '%s';", uuid)
  20. local result = lib_game_mysql:msg(sql)
  21. log.info("l_uuid_register uuid[%s] result[%s]", tostring(uuid), tostring(result))
  22. if is_empty(result) then
  23. return
  24. end
  25. local uid = result[1]["uid"]
  26. if is_nil(uid) then
  27. return
  28. end
  29. uid = tonumber(uid)
  30. -- 账号状态
  31. local status = moduleData:hget_int(uid, "user", "status")
  32. if status > 0 then
  33. return
  34. end
  35. -- 直接进行登陆
  36. msg.uid = uid
  37. msg.password = moduleData:hget(uid, "password")
  38. local errCode, ret = root.login(msg)
  39. if code.is_ok(errCode) then
  40. ret.uid = msg.uid
  41. ret.password = msg.password
  42. else
  43. errCode = errCode or code.UNKNOWN
  44. ret = nil
  45. end
  46. return errCode, ret
  47. end
  48. -- 获取新uid
  49. local function l_get_register_uid()
  50. local uid = lib_game_redis:incrby("user:max:id", 1)
  51. if uid < 1000000 then
  52. uid = uid + 1000000
  53. lib_game_redis:set("user:max:id", uid)
  54. end
  55. return uid
  56. end
  57. -- 快速注册
  58. function root.register(msg)
  59. local errCode, ret = l_uuid_register(msg)
  60. if errCode then
  61. return errCode, ret
  62. end
  63. -- 正常注册
  64. local uid = l_get_register_uid()
  65. userData:user_init_register_info(uid, msg)
  66. -- 注册埋点
  67. serverLogUtil.logRegister(uid, msg.channel, msg.version, msg.sysVer or "", msg.uuid, msg.udid, msg.ip)
  68. msg.uid = uid
  69. msg.password = moduleData:hget_int(uid, "user", "password")
  70. errCode, ret = root.login(msg)
  71. if code.is_ok(errCode) then
  72. ret.uid = msg.uid
  73. ret.password = msg.password
  74. else
  75. errCode = errCode or code.UNKNOWN
  76. ret = nil
  77. end
  78. return errCode, ret
  79. end
  80. -- 快速登陆
  81. function root.login(msg)
  82. log.info("login msg[%s]", tostring(msg))
  83. local uid = msg.uid
  84. if is_empty(uid) or is_empty(msg.password) then
  85. return code.PARAMTER_ERROR
  86. end
  87. -- 密码检验
  88. if not userData:user_is_match_password(msg.password) then
  89. return code.USR.LOGIN_PASSWORD_ERROR
  90. end
  91. -- 状态
  92. local status = moduleData:hget_int(uid, "user", "status")
  93. if status > 0 then
  94. return code.USR.FORCE_OUT
  95. end
  96. -- 分配网关服务器
  97. local nodeInfo = util_user:user_dispatch_gate_node(uid)
  98. if is_empty(nodeInfo) or is_empty(nodeInfo.ip) or is_empty(nodeInfo.wsPort) then
  99. return code.NOT_FOUND_SERVER
  100. end
  101. -- 暂存登陆数据
  102. -- 登陆埋点
  103. serverLogUtil.logLogin(
  104. uid,
  105. msg.channel,
  106. msg.version,
  107. msg.sysVer or "",
  108. msg.operator,
  109. msg.network,
  110. msg.uuid,
  111. msg.udid,
  112. msg.ip
  113. )
  114. local uuid = moduleData:hget_int(uid, "user", "uuid")
  115. local registerTime = moduleData:hget_int(uid, "user", "registerTime")
  116. local password = moduleData:hget_int(uid, "user", "password")
  117. local token = tokenUtil.create(uid, password)
  118. local ret = {
  119. sysTime = skynet_time(),
  120. ip = nodeInfo.ip,
  121. wsPort = nodeInfo.wsPort,
  122. token = token,
  123. registerTime = registerTime,
  124. isGuest = is_empty(uuid),
  125. uuid = uuid,
  126. uid = uid,
  127. password = password
  128. }
  129. log.info("login uid[%d] ret[%s]", tostring(uid), tostring(ret))
  130. return code.OK, ret
  131. end
  132. -- 注册 - 手机号
  133. function root.registerByPhone(msg)
  134. local errCode, ret = l_uuid_register(msg)
  135. if errCode then
  136. return errCode, ret
  137. end
  138. local account = msg.phone
  139. local password = msg.password
  140. -- 账号信息
  141. local accountObj = accountModule.new(account)
  142. -- 已注册
  143. if accountObj:get_data_from_db() then
  144. -- return code.USR.ALREADY_REGISTER
  145. return root.loginByPhone(msg)
  146. end
  147. local uid = l_get_register_uid()
  148. -- 注册账号
  149. moduleData:hset(account, "account", "account", account)
  150. moduleData:hset(account, "account", "password", password)
  151. moduleData:hset(account, "account", "uid", uid)
  152. accountObj:backup_to_db()
  153. -- 正常注册
  154. userData:user_init_register_info(uid, msg)
  155. -- 注册埋点
  156. serverLogUtil.logRegister(uid, msg.channel, msg.version, msg.sysVer or "", msg.uuid, msg.udid, msg.ip)
  157. return root.loginByPhone(msg)
  158. end
  159. -- 登录 - 手机号
  160. function root.loginByPhone(msg)
  161. log.info("loginByPhone msg[%s]", tostring(msg))
  162. local account, password = msg.account, msg.accountPassword
  163. if not account or not password then
  164. return code.PARAMTER_ERROR
  165. end
  166. local accountObj = accountModule.new(account)
  167. if not accountObj:get_data_from_db() then
  168. return code.USR.NOT_EXIST_USER
  169. end
  170. local psw = moduleData:hget(account, "account", "password")
  171. if psw ~= password then
  172. return code.USR.LOGIN_PASSWORD_ERROR
  173. end
  174. local uid = moduleData:hget(account, "account", "uid")
  175. -- 状态
  176. local status = moduleData:hget_int(uid, "user", "status")
  177. if status > 0 then
  178. return code.USR.FORCE_OUT
  179. end
  180. -- 分配网关服务器
  181. local nodeInfo = util_user:user_dispatch_gate_node(uid)
  182. if is_empty(nodeInfo) or is_empty(nodeInfo.ip) or is_empty(nodeInfo.wsPort) then
  183. return code.NOT_FOUND_SERVER
  184. end
  185. -- 暂存登陆数据
  186. -- 登陆埋点
  187. serverLogUtil.logLogin(
  188. uid,
  189. msg.channel or "",
  190. msg.version or "",
  191. msg.sysVer or "",
  192. msg.operator or "",
  193. msg.network or "",
  194. msg.uuid or "",
  195. msg.udid or "",
  196. msg.ip
  197. )
  198. local registerTime = moduleData:hget_int(uid, "user", "registerTime")
  199. local password = moduleData:hget_int(uid, "user", "password")
  200. local token = tokenUtil.create(uid, password)
  201. local ret = {
  202. sysTime = skynet_time(),
  203. ip = nodeInfo.ip,
  204. port = nodeInfo.port,
  205. wsPort = nodeInfo.wsPort,
  206. token = token,
  207. uid = uid,
  208. registerTime = registerTime
  209. }
  210. log.info("loginByPhone uid[%d] ret[%s]", tostring(uid), tostring(ret))
  211. return code.OK, ret
  212. end
  213. return root