第7-8-9章课程源码参考
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.3 KiB

4 years ago
package com.fr.plugin.http.ip;
import com.fr.decision.fun.impl.BaseHttpHandler;
import com.fr.json.JSONObject;
import com.fr.plugin.IPTokenPluginDecisionDBAccessProvider;
import com.fr.plugin.dao.UserIpRelationDao;
import com.fr.plugin.entity.UserIpEntity;
import com.fr.stable.db.accessor.DBAccessor;
import com.fr.stable.query.QueryFactory;
import com.fr.stable.query.restriction.RestrictionFactory;
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
import com.fr.web.utils.WebUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ModApi extends BaseHttpHandler {
@Override
public RequestMethod getMethod() {
return null;
}
@Override
public String getPath() {
return "/ip/mod";
}
@Override
public boolean isPublic() {
return true;
}
@Override
public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
DBAccessor dbAccessor = IPTokenPluginDecisionDBAccessProvider.getDbAccessor();
//通过这两个函数获取的参数可以避免中文乱码和空指针
String userName = WebUtils.getHTTPRequestParameter(httpServletRequest, "userName");
String ip = WebUtils.getHTTPRequestParameter(httpServletRequest, "Ip");
//先查找要更新的用户
UserIpEntity entity = dbAccessor.runQueryAction(daoContext -> daoContext.getDAO(UserIpRelationDao.class)
.findOne(QueryFactory.create().addRestriction(RestrictionFactory.eq("userName", userName)))
);
if (entity != null) {
entity.setIp(ip);
dbAccessor.runDMLAction(daoContext -> {
//执行更新操作
daoContext.getDAO(UserIpRelationDao.class).update(entity);
return null;
});
JSONObject jsonObject = new JSONObject();
jsonObject.put("msg", "success");
jsonObject.put("code", 200);
WebUtils.printAsJSON(httpServletResponse, jsonObject);
} else {
JSONObject jsonObject = new JSONObject();
jsonObject.put("msg", "user not find");
jsonObject.put("code", -1);
WebUtils.printAsJSON(httpServletResponse, jsonObject);
}
}
}