usr.lua 7.1 KB

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