usr.lua 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 util_3rd = require("utils.util_3rd")
  8. local accountModule = require("modules.account")
  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 not is_empty(accountObj:get_data_from_db()) then
  136. -- return code.USR.ALREADY_REGISTER
  137. return root.usr_login_by_phone(msg)
  138. end
  139. -- 敏感词
  140. if util_3rd:is_sensitive(msg.nickname) then
  141. return code.USR.INCLUDE_SENSITIVE
  142. end
  143. local uid = util_global:gen_user_id()
  144. -- 注册账号
  145. local err = moduleData:hset(account, "account", "account", account)
  146. err = moduleData:hset(account, "account", "password", password)
  147. err = moduleData:hset(account, "account", "uid", uid)
  148. err = accountObj:backup_to_db()
  149. -- 生产推广码
  150. msg.shareCode = util_global:gen_share_code()
  151. local values = string.format("sharecode='%s', uid=%s", tostring(msg.shareCode), tostring(uid))
  152. local sql = string.format("insert into mdl_sharecode set %s;", tostring(values))
  153. lib_game_mysql:query(sql)
  154. -- 正常注册
  155. userData:user_init_register_info(uid, msg)
  156. -- 绑定主播邀请码
  157. if not is_empty(msg.sharecode) and util_global:is_sharecode_active(msg.sharecode) then
  158. userData:band_share_code(uid, msg.sharecode)
  159. end
  160. -- 注册埋点
  161. serverLogUtil.logRegister(
  162. uid,
  163. msg.sharecode,
  164. msg.channel,
  165. msg.version,
  166. msg.sysVer or "",
  167. msg.uuid,
  168. msg.udid,
  169. msg.ip
  170. )
  171. return root.usr_login_by_phone(msg)
  172. end
  173. -- 登录 - 手机号
  174. function root.usr_login_by_phone(msg)
  175. log.info("usr_login_by_phone msg[%s]", tostring(msg))
  176. local account, password = msg.phone, msg.password
  177. if not account or not password then
  178. return code.PARAMTER_ERROR
  179. end
  180. local accountObj = accountModule.new(account)
  181. if is_empty(accountObj:get_data_from_db()) then
  182. return code.USR.NOT_EXIST_USER
  183. end
  184. local psw = moduleData:hget(account, "account", "password")
  185. if psw ~= password then
  186. return code.USR.LOGIN_PASSWORD_ERROR
  187. end
  188. local uid = moduleData:hget(account, "account", "uid")
  189. -- 状态
  190. local status = moduleData:hget_int(uid, "user", "status")
  191. if status > 0 then
  192. return code.USR.FORCE_OUT
  193. end
  194. -- 分配网关服务器
  195. local nodeInfo = util_user:user_dispatch_gate_node(uid)
  196. if is_empty(nodeInfo) or is_empty(nodeInfo.ip) or is_empty(nodeInfo.wsPort) then
  197. return code.NOT_FOUND_SERVER
  198. end
  199. local bandShareCode = moduleData:hget(uid, "user", "bandShareCode")
  200. -- 登陆埋点
  201. serverLogUtil.logLogin(
  202. uid,
  203. bandShareCode or "",
  204. msg.channel or "",
  205. msg.version or "",
  206. msg.sysVer or "",
  207. msg.operator or "",
  208. msg.network or "",
  209. msg.uuid or "",
  210. msg.udid or "",
  211. msg.ip
  212. )
  213. local registerTime = moduleData:hget_int(uid, "user", "registerTime")
  214. local password = moduleData:hget_int(uid, "user", "password")
  215. local token = tokenUtil.create(uid, password)
  216. local ret = {
  217. sysTime = skynet_time(),
  218. ip = nodeInfo.ip,
  219. port = nodeInfo.port,
  220. wsPort = nodeInfo.wsPort,
  221. token = token,
  222. uid = uid,
  223. registerTime = registerTime
  224. }
  225. log.info("usr_login_by_phone uid[%d] ret[%s]", tostring(uid), tostring(ret))
  226. return code.OK, ret
  227. end
  228. return root