usr.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 util_global = require("utils.util_global")
  7. local lib_game_mysql = require("lib_game_mysql")
  8. local lib_game_redis = require("lib_game_redis")
  9. local moduleData = require("data.module")
  10. local userData = require("data.user")
  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:msg(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. -- 快速注册
  50. function root.register(msg)
  51. local errCode, ret = l_uuid_register(msg)
  52. if errCode then
  53. return errCode, ret
  54. end
  55. -- 正常注册
  56. local uid = util_global:gen_user_id()
  57. userData:user_init_register_info(uid, msg)
  58. -- 注册埋点
  59. serverLogUtil.logRegister(uid, msg.channel, msg.version, msg.sysVer or "", msg.uuid, msg.udid, msg.ip)
  60. msg.uid = uid
  61. msg.password = moduleData:hget_int(uid, "user", "password")
  62. errCode, ret = root.login(msg)
  63. if code.is_ok(errCode) then
  64. ret.uid = msg.uid
  65. ret.password = msg.password
  66. else
  67. errCode = errCode or code.UNKNOWN
  68. ret = nil
  69. end
  70. return errCode, ret
  71. end
  72. -- 快速登陆
  73. function root.login(msg)
  74. log.info("login msg[%s]", tostring(msg))
  75. local uid = msg.uid
  76. if is_empty(uid) or is_empty(msg.password) then
  77. return code.PARAMTER_ERROR
  78. end
  79. -- 密码检验
  80. if not userData:user_is_match_password(msg.password) then
  81. return code.USR.LOGIN_PASSWORD_ERROR
  82. end
  83. -- 状态
  84. local status = moduleData:hget_int(uid, "user", "status")
  85. if status > 0 then
  86. return code.USR.FORCE_OUT
  87. end
  88. -- 分配网关服务器
  89. local nodeInfo = util_user:user_dispatch_gate_node(uid)
  90. if is_empty(nodeInfo) or is_empty(nodeInfo.ip) or is_empty(nodeInfo.wsPort) then
  91. return code.NOT_FOUND_SERVER
  92. end
  93. -- 暂存登陆数据
  94. -- 登陆埋点
  95. serverLogUtil.logLogin(
  96. uid,
  97. msg.channel,
  98. msg.version,
  99. msg.sysVer or "",
  100. msg.operator,
  101. msg.network,
  102. msg.uuid,
  103. msg.udid,
  104. msg.ip
  105. )
  106. local uuid = moduleData:hget_int(uid, "user", "uuid")
  107. local registerTime = moduleData:hget_int(uid, "user", "registerTime")
  108. local password = moduleData:hget_int(uid, "user", "password")
  109. local token = tokenUtil.create(uid, password)
  110. local ret = {
  111. sysTime = skynet_time(),
  112. ip = nodeInfo.ip,
  113. wsPort = nodeInfo.wsPort,
  114. token = token,
  115. registerTime = registerTime,
  116. isGuest = is_empty(uuid),
  117. uuid = uuid,
  118. uid = uid,
  119. password = password
  120. }
  121. log.info("login uid[%d] ret[%s]", tostring(uid), tostring(ret))
  122. return code.OK, ret
  123. end
  124. -- 注册 - 手机号
  125. function root.registerByPhone(msg)
  126. local errCode, ret = l_uuid_register(msg)
  127. if errCode then
  128. return errCode, ret
  129. end
  130. local account = msg.phone
  131. local password = msg.password
  132. -- 账号信息
  133. local accountObj = accountModule.new(account)
  134. -- 已注册
  135. if accountObj:get_data_from_db() then
  136. -- return code.USR.ALREADY_REGISTER
  137. return root.loginByPhone(msg)
  138. end
  139. local uid = util_global:gen_user_id()
  140. -- 注册账号
  141. moduleData:hset(account, "account", "account", account)
  142. moduleData:hset(account, "account", "password", password)
  143. moduleData:hset(account, "account", "uid", uid)
  144. accountObj:backup_to_db()
  145. -- 正常注册
  146. userData:user_init_register_info(uid, msg)
  147. -- 注册埋点
  148. serverLogUtil.logRegister(uid, msg.channel, msg.version, msg.sysVer or "", msg.uuid, msg.udid, msg.ip)
  149. return root.loginByPhone(msg)
  150. end
  151. -- 登录 - 手机号
  152. function root.loginByPhone(msg)
  153. log.info("loginByPhone msg[%s]", tostring(msg))
  154. local account, password = msg.account, msg.accountPassword
  155. if not account or not password then
  156. return code.PARAMTER_ERROR
  157. end
  158. local accountObj = accountModule.new(account)
  159. if not accountObj:get_data_from_db() then
  160. return code.USR.NOT_EXIST_USER
  161. end
  162. local psw = moduleData:hget(account, "account", "password")
  163. if psw ~= password then
  164. return code.USR.LOGIN_PASSWORD_ERROR
  165. end
  166. local uid = moduleData:hget(account, "account", "uid")
  167. -- 状态
  168. local status = moduleData:hget_int(uid, "user", "status")
  169. if status > 0 then
  170. return code.USR.FORCE_OUT
  171. end
  172. -- 分配网关服务器
  173. local nodeInfo = util_user:user_dispatch_gate_node(uid)
  174. if is_empty(nodeInfo) or is_empty(nodeInfo.ip) or is_empty(nodeInfo.wsPort) then
  175. return code.NOT_FOUND_SERVER
  176. end
  177. -- 暂存登陆数据
  178. -- 登陆埋点
  179. serverLogUtil.logLogin(
  180. uid,
  181. msg.channel or "",
  182. msg.version or "",
  183. msg.sysVer or "",
  184. msg.operator or "",
  185. msg.network or "",
  186. msg.uuid or "",
  187. msg.udid or "",
  188. msg.ip
  189. )
  190. local registerTime = moduleData:hget_int(uid, "user", "registerTime")
  191. local password = moduleData:hget_int(uid, "user", "password")
  192. local token = tokenUtil.create(uid, password)
  193. local ret = {
  194. sysTime = skynet_time(),
  195. ip = nodeInfo.ip,
  196. port = nodeInfo.port,
  197. wsPort = nodeInfo.wsPort,
  198. token = token,
  199. uid = uid,
  200. registerTime = registerTime
  201. }
  202. log.info("loginByPhone uid[%d] ret[%s]", tostring(uid), tostring(ret))
  203. return code.OK, ret
  204. end
  205. return root