package com.qiwu.inference.aop; import com.qiwu.inference.common.utils.CacheUtil; import com.qiwu.inference.common.utils.YamlUtil; import com.qiwu.inference.dto.request.OperationRequest; import com.qiwu.inference.dto.response.CommonResponse; import com.qiwu.inference.dto.response.ResultVarsResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import org.yaml.snakeyaml.Yaml; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class MyAop implements InvocationHandler{ Logger log = LoggerFactory.getLogger(MyAop.class); private String offlineTime = YamlUtil.getValueByKey("offlineTime"); private Object object; //构造代理对象 public Object newProxy(Object object) { this.object = object; return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), this); } @Override public Object invoke(Object o, Method method, Object[] objects) throws Throwable { OperationRequest request = (OperationRequest) objects[0]; //该玩家以唯一标识加入全局缓存,用于玩家离线判断 String uniqueUid = request.getUid() + "%#%online%#%"; CacheUtil.put(uniqueUid, "", Long.parseLong(offlineTime)); //通过反射获取方法返参 Object var = method.invoke(this.object, objects); //检索到有新消息 CommonResponse commonResponse = (CommonResponse) var; ResultVarsResponse data = commonResponse.getData(); if (!StringUtils.isEmpty(data)) { //所有“后台附加返回”为null值的都给转成“无效返回” if (StringUtils.isEmpty(data.getVars().get后台附加返回())) { data.getVars().set后台附加返回("无效返回"); } //所有“后台附加返回”为null值的都给转成“无效返回” if (StringUtils.isEmpty(data.getVars().get后台附加返回2())) { data.getVars().set后台附加返回2("无效返回"); } String feedContent = "后台返回参数 = [" + "code:" + commonResponse.getCode() + ", msg:" + commonResponse.getMsg() + ", 反馈ID:" + data.getVars().get反馈ID() + ", 反馈文本:" + data.getVars().get反馈文本() + ", 后台附加返回:" + data.getVars().get后台附加返回() + ", 后台附加返回2:" + data.getVars().get后台附加返回2() + "]"; //玩家有新消息提示 log.info(feedContent); } return var; } }