usr.lua 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.account")
  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.usr_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.usr_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.usr_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.usr_login(msg)
  74. log.info("usr_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("usr_login uid[%d] ret[%s]", tostring(uid), tostring(ret))
  122. return code.OK, ret
  123. end
  124. -- 注册 - 手机号
  125. function root.usr_register_by_phone(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.usr_login_by_phone(msg)
  138. end
  139. local uid = util_global:gen_user_id()
  140. log.info("usr_register_by_phone account[%s] uid[%s]", tostring(account), tostring(uid))
  141. -- 注册账号
  142. local err = moduleData:hset(account, "account", "account", account)
  143. log.info("usr_register_by_phone account[%s] err[%s]", tostring(account), tostring(err))
  144. err = moduleData:hset(account, "account", "password", password)
  145. log.info("usr_register_by_phone password[%s] err[%s]", tostring(password), tostring(err))
  146. err = moduleData:hset(account, "account", "uid", uid)
  147. log.info("usr_register_by_phone uid[%s] err[%s]", tostring(uid), tostring(err))
  148. err = accountObj:backup_to_db()
  149. log.info("usr_register_by_phone backup_to_db err[%s]", tostring(err))
  150. -- 正常注册
  151. userData:user_init_register_info(uid, msg)
  152. -- 注册埋点
  153. serverLogUtil.logRegister(uid, msg.channel, msg.version, msg.sysVer or "", msg.uuid, msg.udid, msg.ip)
  154. return root.usr_login_by_phone(msg)
  155. end
  156. -- 登录 - 手机号
  157. function root.usr_login_by_phone(msg)
  158. log.info("usr_login_by_phone msg[%s]", tostring(msg))
  159. local account, password = msg.phone, msg.password
  160. if not account or not password then
  161. return code.PARAMTER_ERROR
  162. end
  163. local accountObj = accountModule.new(account)
  164. if not accountObj:get_data_from_db() then
  165. return code.USR.NOT_EXIST_USER
  166. end
  167. local psw = moduleData:hget(account, "account", "password")
  168. if psw ~= password then
  169. return code.USR.LOGIN_PASSWORD_ERROR
  170. end
  171. local uid = moduleData:hget(account, "account", "uid")
  172. -- 状态
  173. local status = moduleData:hget_int(uid, "user", "status")
  174. if status > 0 then
  175. return code.USR.FORCE_OUT
  176. end
  177. -- 分配网关服务器
  178. local nodeInfo = util_user:user_dispatch_gate_node(uid)
  179. if is_empty(nodeInfo) or is_empty(nodeInfo.ip) or is_empty(nodeInfo.wsPort) then
  180. return code.NOT_FOUND_SERVER
  181. end
  182. -- 暂存登陆数据
  183. -- 登陆埋点
  184. serverLogUtil.logLogin(
  185. uid,
  186. msg.channel or "",
  187. msg.version or "",
  188. msg.sysVer or "",
  189. msg.operator or "",
  190. msg.network or "",
  191. msg.uuid or "",
  192. msg.udid or "",
  193. msg.ip
  194. )
  195. local registerTime = moduleData:hget_int(uid, "user", "registerTime")
  196. local password = moduleData:hget_int(uid, "user", "password")
  197. local token = tokenUtil.create(uid, password)
  198. local ret = {
  199. sysTime = skynet_time(),
  200. ip = nodeInfo.ip,
  201. port = nodeInfo.port,
  202. wsPort = nodeInfo.wsPort,
  203. token = token,
  204. uid = uid,
  205. registerTime = registerTime
  206. }
  207. log.info("usr_login_by_phone uid[%d] ret[%s]", tostring(uid), tostring(ret))
  208. return code.OK, ret
  209. end
  210. return root