local code = require("code") local tokenUtil = require("utils.tokenUtil") local serverLogUtil = require("utils.serverLogUtil") local util_user = require("utils.util_user") local util_global = require("utils.util_global") local lib_game_mysql = require("lib_game_mysql") local lib_game_redis = require("lib_game_redis") local util_3rd = require("utils.util_3rd") local accountModule = require("modules.account") 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.usr_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.usr_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.usr_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.usr_login(msg) log.info("usr_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("usr_login uid[%d] ret[%s]", tostring(uid), tostring(ret)) return code.OK, ret end -- 注册 - 手机号 function root.usr_register_by_phone(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 not is_empty(accountObj:get_data_from_db()) then -- return code.USR.ALREADY_REGISTER return root.usr_login_by_phone(msg) end -- 敏感词 if util_3rd:is_sensitive(msg.nickname) then return code.USR.INCLUDE_SENSITIVE end local uid = util_global:gen_user_id() -- 注册账号 local err = moduleData:hset(account, "account", "account", account) err = moduleData:hset(account, "account", "password", password) err = moduleData:hset(account, "account", "uid", uid) err = accountObj:backup_to_db() -- 生产推广码 msg.shareCode = util_global:gen_share_code() local values = string.format("sharecode='%s', uid=%s", tostring(msg.shareCode), tostring(uid)) local sql = string.format("insert into mdl_sharecode set %s;", tostring(values)) lib_game_mysql:query(sql) -- 正常注册 userData:user_init_register_info(uid, msg) -- 绑定主播邀请码 if not is_empty(msg.sharecode) and util_global:is_sharecode_active(msg.sharecode) then userData:band_share_code(uid, msg.sharecode) end -- 注册埋点 serverLogUtil.logRegister(uid, msg.channel, msg.version, msg.sysVer or "", msg.uuid, msg.udid, msg.ip) return root.usr_login_by_phone(msg) end -- 登录 - 手机号 function root.usr_login_by_phone(msg) log.info("usr_login_by_phone msg[%s]", tostring(msg)) local account, password = msg.phone, msg.password if not account or not password then return code.PARAMTER_ERROR end local accountObj = accountModule.new(account) if is_empty(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("usr_login_by_phone uid[%d] ret[%s]", tostring(uid), tostring(ret)) return code.OK, ret end return root