package com.jmfy.handler; import com.alibaba.fastjson.JSONObject; import com.jmfy.WebSecurityConfig; import com.jmfy.dao.GSUserDao; import com.jmfy.dao.ServerInfoDao; import com.jmfy.dao.UserBannedDao; import com.jmfy.dao.UserInfoDao; import com.jmfy.model.CUserInfo; import com.jmfy.model.GSUser; import com.jmfy.model.ServerInfo; import com.jmfy.model.UserBanned; import com.jmfy.redisProperties.RedisUserKey; import com.jmfy.thrift.idl.Result; import com.jmfy.utils.DateUtil; import com.jmfy.utils.RPCClient; import com.jmfy.utils.RedisUtil; import com.jmfy.utils.SeqUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.annotation.Resource; import javax.servlet.http.HttpSession; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @Component public class GMHandler extends BaseHandler { private static final Logger LOGGER = LoggerFactory.getLogger(GMHandler.class); @Resource private ServerInfoDao serverListDao; @Resource private UserInfoDao userInfoDao; @Resource private UserBannedDao userBannedDao; @Resource private GSUserDao gsUserDao; @Resource private SeqUtils seqUtils; /** * 禁止发言 */ public static final int BANNED_TO_POST = 1; /** * 取消禁止发言 */ public static final int CANCEL_BANNED_TO_POST = 2; /** * 踢下线 */ public static final int KILL_USER = 3; /** * 禁止登陆 */ public static final int BANNED_LOGIN = 4; /** * 取消禁止登陆 */ public static final int CANCEL_BANNED_LOGIN = 5; /** * 修改用户名 */ public static final int UPDATE_USERNAME = 6; /** * 天眼禁言封禁 */ public static final int SKYEYE_BAN = 11; /** * 重置公会名称 */ public static final int RESET_GUILD_NAME = 9; /** * 重置公会公告 */ public static final int RESET_GUILD_ANNOUNCEMENT = 10; @Override public int execute(JSONObject jsonObject,HttpSession session) throws Exception { LOGGER.info("gm封禁,传参:{}",jsonObject.toJSONString()); String openIds = jsonObject.getString("uid"); // 根据openId查询玩家注册的全部账号 List coreUserInfoList = userInfoDao.findUserInfo(openIds); if (coreUserInfoList == null || coreUserInfoList.isEmpty()) { throw new Exception("帐号不存在"); } // 遍历全部账号,执行封禁操作 for (CUserInfo coreUserInfo : coreUserInfoList) { oneUserBanner(coreUserInfo, jsonObject.getIntValue("dsid"), jsonObject, jsonObject.getIntValue("treat_type"), session); } return 1; } @Override public boolean skyEyeExecute(JSONObject json, HttpSession session, Map map) throws Exception { // 暂时不用 // String nickname = json.getString("nickname"); // String playerId = json.getString("playerId"); // String serverId = json.getString("serverId"); // String sign = json.getString("sign"); // json参数获取 JSONObject jsonBan = new JSONObject(); jsonBan.put("uid",json.getString("userId")); jsonBan.put("banTime",json.getString("time")); jsonBan.put("dsid",json.getString("zoneId")); int skyEyeType = getSkyEyeType(json.getIntValue("channel")); jsonBan.put("treat_type",skyEyeType); HashMap reasonMap = new HashMap<>(); reasonMap.put("source",json.getString("source")); reasonMap.put("content",json.getString("content")); jsonBan.put("banReason",reasonMap.toString()); jsonBan.put("channel",json.getString("channel")); jsonBan.put("url",json.getString("extra[url]")); jsonBan.put("guildId",json.getString("extra[guildId]")); // 日志 LOGGER.info("天眼封禁,传参:{}",jsonBan.toJSONString()); CUserInfo coreUserInfo = userInfoDao.findUserInfoByUserId(jsonBan.getIntValue("uid")); if (coreUserInfo == null) { map.put("message","帐号不存在"+jsonBan.getIntValue("uid")); return false; } boolean userBanner = oneUserBanner(coreUserInfo, jsonBan.getIntValue("dsid"), jsonBan, skyEyeType, session); if (!userBanner){ map.put("message","操作异常,请查看日志"+jsonBan.getIntValue("uid")); } return userBanner; } /** * 单个用户封禁 * @param coreUserInfo * @param serverId * @param json * @param type * @param session * @return * @throws Exception */ private boolean oneUserBanner(CUserInfo coreUserInfo, int serverId, JSONObject json, int type, HttpSession session) throws Exception { int hefuServerId = HeFuManager.getHefuServerId(serverId); // 获取拼接参数 String cmd = getCmd(json, coreUserInfo, type); if ("".equals(cmd) || cmd == null){ LOGGER.error("参数拼接失败,json:{},uid:{},type:{}", json.toJSONString(),coreUserInfo.getUserId(),type); return false; } String rpcString = RedisUtil.getInstence().getObject(RedisUserKey.LOGIC_SERVER_INFO, String.valueOf(hefuServerId), String.class, -1); if (null == rpcString) { LOGGER.error("获取rpc参数报错:serverId:{}", coreUserInfo.getServerid()); return false; } String thriftIp = rpcString.split(":")[0]; String thriftPort = rpcString.split(":")[1]; if (thriftIp == null || thriftIp.isEmpty() || null == thriftPort || thriftPort.isEmpty()) { LOGGER.error("rpc参数存在空值:thriftIp:{},thriftPort:{}", thriftIp,thriftPort); return false; } Result result = RPCClient.gmSend(cmd, thriftIp, thriftPort); // 异常或者错误 if (result.getResultCode() != 0) { LOGGER.error("rpc返回值错误:code:{},msg:{}", result.getResultCode(),result.getResultMsg()); return false; } // 封禁入库 updateUserBanner(coreUserInfo,session,json); return true; } /** * 获取天眼封禁类型 * 天眼类型目前只有9、10、11 * @param channel * @return */ private int getSkyEyeType(int channel){ int result; switch (channel){ case GMHandler.RESET_GUILD_NAME: case GMHandler.RESET_GUILD_ANNOUNCEMENT: result = channel; break; default: result = GMHandler.SKYEYE_BAN; break; } return result; } /** * 获取cmd参数 * @param jsonObject * @param coreUserInfo * @param type * @return * @throws Exception */ private String getCmd(JSONObject jsonObject,CUserInfo coreUserInfo,int type){ String cmd = ""; switch (type) { case BANNED_TO_POST: cmd = getCmdContent("silence",coreUserInfo.getUserId(),1); break; case CANCEL_BANNED_TO_POST: cmd = getCmdContent("silence",coreUserInfo.getUserId(),0); break; case KILL_USER: cmd = getCmdContent("kickuser",coreUserInfo.getUserId()); break; case BANNED_LOGIN: cmd = getCmdContent("addblack",coreUserInfo.getUserId()); break; case CANCEL_BANNED_LOGIN: cmd = getCmdContent("reblack",coreUserInfo.getUserId()); break; case UPDATE_USERNAME: if (jsonObject.containsKey("actor_id")) { if (jsonObject.containsKey("actor_name")) { cmd = getCmdContent("changename",coreUserInfo.getUserId(),jsonObject.getString("actor_name")); } else { cmd = getCmdContent("changename",coreUserInfo.getUserId()); } } break; case SKYEYE_BAN: // 天眼用户禁言 String banTime = Optional.ofNullable(jsonObject.getString("banTime")).orElse("0"); cmd = getCmdContent("skyeye",coreUserInfo.getUserId(),SKYEYE_BAN,banTime); break; case RESET_GUILD_NAME: // 天眼,公会名重置 String banTime1 = jsonObject.getString("banTime"); String guildId = jsonObject.getString("guildId"); cmd = getCmdContent("skyeye",coreUserInfo.getUserId(),RESET_GUILD_NAME,banTime1,guildId); break; case RESET_GUILD_ANNOUNCEMENT: // 天眼 公会宣言重置 String banTime2 = jsonObject.getString("banTime"); String guildId2 = jsonObject.getString("guildId"); cmd = getCmdContent("skyeye",coreUserInfo.getUserId(),RESET_GUILD_ANNOUNCEMENT,banTime2,guildId2); break; default: break; } LOGGER.info("gm参数:【{}】",cmd); return cmd; } /** * 获取cmd内容,免去拼写麻烦,用空格衔接 * @param value * @return */ public static String getCmdContent(Object... value){ StringBuilder builder = new StringBuilder(); for (Object s : value) { builder.append(s).append(" "); } if (builder.length() <= 0){ return builder.toString(); }else { return builder.substring(0,builder.length()-1); } } /** * 修改添加封禁用户 * @param cUser * @param session * @param jsonObject * @throws Exception */ private void updateUserBanner(CUserInfo cUser,HttpSession session,JSONObject jsonObject) throws Exception { int type = jsonObject.getIntValue("treat_type"); // 封禁用户 UserBanned banned = userBannedDao.getUserBannedByBannedType(cUser.getUserId(),type); switch (type) { case GMHandler.BANNED_TO_POST: case GMHandler.BANNED_LOGIN: // 封禁操作 if (session == null){ throw new Exception("请求超时,请刷新页面后重新操作!"); } insertOrUpdateBannedUser(cUser, session, jsonObject, banned); break; case GMHandler.CANCEL_BANNED_TO_POST: case GMHandler.CANCEL_BANNED_LOGIN: // 解封操作 userBannedDao.deleteBannedUser(banned); break; case GMHandler.SKYEYE_BAN: // 天眼操作 int banTime = jsonObject.getIntValue("banTime"); // 封禁时间小于0就是解封 if (banTime <= 0){ userBannedDao.deleteBannedUser(banned); }else { insertOrUpdateBannedUser(cUser, session, jsonObject, banned); } break; default: break; } } /** * 创建封禁用户 * @param cUser * @param session * @param jsonObject * @return * @throws Exception */ private void insertOrUpdateBannedUser(CUserInfo cUser, HttpSession session,JSONObject jsonObject,UserBanned banned) throws Exception { UserBanned userBanned; String notTime = DateUtil.nowString(); if (banned == null){ // 初始化信息 userBanned = new UserBanned(); userBanned.setId(seqUtils.getSequence("banned_user_id")); userBanned.setServerId(cUser.getServerid()); userBanned.setUserId(cUser.getUserId()); // 用户名称 GSUser gsUser = gsUserDao.findUserInfoQuick(cUser.getServerid(), cUser.getUserId()); if (gsUser != null){ userBanned.setUserName(gsUser.getPlayerManager().getNickName()); } // 账号id userBanned.setAccountId(cUser.getOpenId()); // 封禁类型 userBanned.setBannedType(jsonObject.getIntValue("treat_type")); // 解封类型 int relieve = relieve(jsonObject.getIntValue("treat_type")); userBanned.setRelieveType(relieve); // 创建时间 userBanned.setCreateTime(notTime); }else { userBanned = banned; } // 操作人,gm账号 String rootName = (String) session.getAttribute(WebSecurityConfig.SESSION_KEY); if (rootName != null){ userBanned.setAdminAccount(rootName); } // 封禁说明 userBanned.setBannedExplain(jsonObject.getString("banReason")); // 封禁时间,秒 int time = jsonObject.getIntValue("banTime"); userBanned.setBannedTime(time); // 修改时间 userBanned.setUpdateTime(notTime); // 封禁结束时间 String last = DateUtil.stampToTime(time*1000 + DateUtil.now()); userBanned.setBannedLastTime(last); if (banned == null){ userBannedDao.insertBannedUser(userBanned); }else { userBannedDao.updateBannedUser(userBanned); } } /** * 获取解封类型 * @param type * @return */ private int relieve(int type){ int resultType = 0; switch (type){ case GMHandler.BANNED_TO_POST: resultType = GMHandler.CANCEL_BANNED_TO_POST; break; case GMHandler.BANNED_LOGIN: resultType = GMHandler.CANCEL_BANNED_LOGIN; break; default:break; } return resultType; } public static String sendGm(String serverId, String cmd){ String rpcString = RedisUtil.getInstence().getObject(RedisUserKey.LOGIC_SERVER_INFO, serverId, String.class, -1); if (null == rpcString) { return "获取玩家服务器端口失败,服务器id错误"; } String thriftIp = rpcString.split(":")[0]; String thriftPort = rpcString.split(":")[1]; if (thriftIp == null || thriftIp.isEmpty() || null == thriftPort || thriftPort.isEmpty()) { return "获取玩家服务器端口失败,服务器id错误"; } Result result = RPCClient.gmSend(cmd, thriftIp, thriftPort); if (result.getResultCode() != 0) { LOGGER.error("游戏服返回信息错误,cmd:{},code:{},result:{}",cmd,result.resultCode,result.getResultMsg()); return "游戏服返回信息错误"; } return result.getResultMsg() == null ? "":result.getResultMsg(); } @Override public String getRequestURI() { return "gm"; } @Override public int execute(JSONObject json) throws Exception { return execute(json,null); } }