贡献值满,升级
This commit is contained in:
@@ -308,6 +308,7 @@ public class UserDao {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加头像
|
||||
* userAvatar头像
|
||||
* @return
|
||||
*/
|
||||
@@ -331,18 +332,36 @@ public class UserDao {
|
||||
|
||||
|
||||
|
||||
public int updateUserAv(String name,String uri){
|
||||
public int updateUserAv(String name,String url){
|
||||
String label="";
|
||||
int num;
|
||||
try {
|
||||
Connection conn = JdbcUtils.getConnection();
|
||||
QueryRunner runner = new QueryRunner();
|
||||
String sql="UPDATE user SET userAvatar = ? WHERE userName= ? and isValid = 1";
|
||||
num = runner.update(conn,sql,uri,name);
|
||||
num = runner.update(conn,sql,url,name);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
public void LevelUp(String username){
|
||||
String sql = "UPDATE user SET userLevel = userLevel+1 WHERE userName = ? AND contributionValue >= 100;";
|
||||
String sql1 = "UPDATE user SET contributionValue = 0;";
|
||||
try{
|
||||
Connection conn = JdbcUtils.getConnection();
|
||||
Connection conn1 = JdbcUtils.getConnection();
|
||||
PreparedStatement prep = conn.prepareStatement(sql);
|
||||
prep.setString(1, username);
|
||||
prep.executeUpdate();
|
||||
PreparedStatement prep1 = conn1.prepareStatement(sql1);
|
||||
prep1.executeUpdate();
|
||||
JdbcUtils.closeConnection(prep,conn);
|
||||
JdbcUtils.closeConnection(prep1,conn1);
|
||||
}catch(SQLException e){
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -32,6 +32,18 @@ public class userEntity {
|
||||
|
||||
private Integer contributionValue;
|
||||
|
||||
private String userAvatar;
|
||||
|
||||
|
||||
private Integer userLevel;
|
||||
|
||||
public Integer getUserLevel() {
|
||||
return userLevel;
|
||||
}
|
||||
|
||||
public void setUserLevel(Integer userLevel) {
|
||||
this.userLevel = userLevel;
|
||||
}
|
||||
public userEntity() {
|
||||
|
||||
}
|
||||
@@ -135,7 +147,6 @@ public class userEntity {
|
||||
this.num = num;
|
||||
}
|
||||
|
||||
String userAvatar;
|
||||
|
||||
public String getUserAvatar() {
|
||||
return userAvatar;
|
||||
|
@@ -85,4 +85,6 @@ public class UserService {
|
||||
public int updateUserAv(String name,String uri){
|
||||
return userDao.updateUserAv(name,uri);
|
||||
}
|
||||
|
||||
public void LevelUp(String username){userDao.LevelUp(username);}
|
||||
}
|
||||
|
64
src/com/hellogithub/servlet/LevelUpServlet.java
Normal file
64
src/com/hellogithub/servlet/LevelUpServlet.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package com.hellogithub.servlet;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.hellogithub.entity.userEntity;
|
||||
import com.hellogithub.service.UserService;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashMap;
|
||||
|
||||
@WebServlet("/levelup")
|
||||
public class LevelUpServlet extends HttpServlet{
|
||||
private UserService userservice = new UserService();
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
doPost(req,resp);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
resp.setCharacterEncoding("UTF-8");
|
||||
resp.setContentType("text/html; charset=utf-8");
|
||||
// 设置响应头允许ajax跨域访问
|
||||
String curOrigin = req.getHeader("Origin");
|
||||
resp.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin);
|
||||
resp.setHeader("Access-Control-Allow-Credentials", "true");
|
||||
resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");
|
||||
resp.setHeader("Access-Control-Max-Age", "3600");
|
||||
resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
|
||||
resp.setContentType("application/json;charset=UTF-8");
|
||||
|
||||
PrintWriter writer = resp.getWriter();
|
||||
HttpSession session = req.getSession();
|
||||
userEntity userentity = (userEntity) session.getAttribute("user");
|
||||
if(userentity == null){
|
||||
setResultError("未获取信息", writer);
|
||||
}else{
|
||||
userservice.LevelUp(userentity.getUserName());
|
||||
setResultOK("升级成功", writer);
|
||||
}
|
||||
}
|
||||
public void setResult(Integer code, String msg, PrintWriter writer) {
|
||||
HashMap<String, Object> result = new HashMap<>();
|
||||
result.put("code", code);
|
||||
result.put("msg", msg);
|
||||
String jsonString = JSONObject.toJSONString(result);
|
||||
writer.println(jsonString);
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public void setResultError(String msg, PrintWriter writer) {
|
||||
setResult(500, msg, writer);
|
||||
}
|
||||
|
||||
public void setResultOK(String msg, PrintWriter writer) {
|
||||
setResult(200, msg, writer);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user