local code = require "code" local tokenUtil = require "utils.tokenUtil" local serverLogUtil = require("utils.serverLogUtil") local util_user = require("utils.util_user") local accountModule = require("modules.accountModule") local util_global = require("utils.util_global") local lib_game_mysql = require("lib_game_mysql") local lib_game_redis = require("lib_game_redis") local moduleData = require("data.module") local userData = require("data.user") local root = {} -- uuid注册 local function l_uuid_register(msg) -- 是否第三方平台 local uuid = msg.uuid if is_empty(uuid) then return end -- 是否已绑定uid local sql = string.format("select max(uid) as uid from mdl_user where uuid = '%s';", uuid) local result = lib_game_mysql:msg(sql) log.info("l_uuid_register uuid[%s] result[%s]", tostring(uuid), tostring(result)) if is_empty(result) then return end local uid = result[1]["uid"] if is_nil(uid) then return end uid = tonumber(uid) -- 账号状态 local status = moduleData:hget_int(uid, "user", "status") if status > 0 then return end -- 直接进行登陆 msg.uid = uid msg.password = moduleData:hget(uid, "password") local errCode, ret = root.login(msg) if code.is_ok(errCode) then ret.uid = msg.uid ret.password = msg.password else errCode = errCode or code.UNKNOWN ret = nil end return errCode, ret end -- 快速注册 function root.register(msg) local errCode, ret = l_uuid_register(msg) if errCode then return errCode, ret end -- 正常注册 local uid = util_global:gen_user_id() userData:user_init_register_info(uid, msg) -- 注册埋点 serverLogUtil.logRegister(uid, msg.channel, msg.version, msg.sysVer or "", msg.uuid, msg.udid, msg.ip) msg.uid = uid msg.password = moduleData:hget_int(uid, "user", "password") errCode, ret = root.login(msg) if code.is_ok(errCode) then ret.uid = msg.uid ret.password = msg.password else errCode = errCode or code.UNKNOWN ret = nil end return errCode, ret end -- 快速登陆 function root.login(msg) log.info("login msg[%s]", tostring(msg)) local uid = msg.uid if is_empty(uid) or is_empty(msg.password) then return code.PARAMTER_ERROR end -- 密码检验 if not userData:user_is_match_password(msg.password) then return code.USR.LOGIN_PASSWORD_ERROR end -- 状态 local status = moduleData:hget_int(uid, "user", "status") if status > 0 then return code.USR.FORCE_OUT end -- 分配网关服务器 local nodeInfo = util_user:user_dispatch_gate_node(uid) if is_empty(nodeInfo) or is_empty(nodeInfo.ip) or is_empty(nodeInfo.wsPort) then return code.NOT_FOUND_SERVER end -- 暂存登陆数据 -- 登陆埋点 serverLogUtil.logLogin( uid, msg.channel, msg.version, msg.sysVer or "", msg.operator, msg.network, msg.uuid, msg.udid, msg.ip ) local uuid = moduleData:hget_int(uid, "user", "uuid") local registerTime = moduleData:hget_int(uid, "user", "registerTime") local password = moduleData:hget_int(uid, "user", "password") local token = tokenUtil.create(uid, password) local ret = { sysTime = skynet_time(), ip = nodeInfo.ip, wsPort = nodeInfo.wsPort, token = token, registerTime = registerTime, isGuest = is_empty(uuid), uuid = uuid, uid = uid, password = password } log.info("login uid[%d] ret[%s]", tostring(uid), tostring(ret)) return code.OK, ret end -- 注册 - 手机号 function root.registerByPhone(msg) local errCode, ret = l_uuid_register(msg) if errCode then return errCode, ret end local account = msg.phone local password = msg.password -- 账号信息 local accountObj = accountModule.new(account) -- 已注册 if accountObj:get_data_from_db() then -- return code.USR.ALREADY_REGISTER return root.loginByPhone(msg) end local uid = util_global:gen_user_id() -- 注册账号 moduleData:hset(account, "account", "account", account) moduleData:hset(account, "account", "password", password) moduleData:hset(account, "account", "uid", uid) accountObj:backup_to_db() -- 正常注册 userData:user_init_register_info(uid, msg) -- 注册埋点 serverLogUtil.logRegister(uid, msg.channel, msg.version, msg.sysVer or "", msg.uuid, msg.udid, msg.ip) return root.loginByPhone(msg) end -- 登录 - 手机号 function root.loginByPhone(msg) log.info("loginByPhone msg[%s]", tostring(msg)) local account, password = msg.account, msg.accountPassword if not account or not password then return code.PARAMTER_ERROR end local accountObj = accountModule.new(account) if not accountObj:get_data_from_db() then return code.USR.NOT_EXIST_USER end local psw = moduleData:hget(account, "account", "password") if psw ~= password then return code.USR.LOGIN_PASSWORD_ERROR end local uid = moduleData:hget(account, "account", "uid") -- 状态 local status = moduleData:hget_int(uid, "user", "status") if status > 0 then return code.USR.FORCE_OUT end -- 分配网关服务器 local nodeInfo = util_user:user_dispatch_gate_node(uid) if is_empty(nodeInfo) or is_empty(nodeInfo.ip) or is_empty(nodeInfo.wsPort) then return code.NOT_FOUND_SERVER end -- 暂存登陆数据 -- 登陆埋点 serverLogUtil.logLogin( uid, msg.channel or "", msg.version or "", msg.sysVer or "", msg.operator or "", msg.network or "", msg.uuid or "", msg.udid or "", msg.ip ) local registerTime = moduleData:hget_int(uid, "user", "registerTime") local password = moduleData:hget_int(uid, "user", "password") local token = tokenUtil.create(uid, password) local ret = { sysTime = skynet_time(), ip = nodeInfo.ip, port = nodeInfo.port, wsPort = nodeInfo.wsPort, token = token, uid = uid, registerTime = registerTime } log.info("loginByPhone uid[%d] ret[%s]", tostring(uid), tostring(ret)) return code.OK, ret end return root