diff --git a/src/main/java/top/ezzd/top/controller/UserController.java b/src/main/java/top/ezzd/top/controller/UserController.java new file mode 100644 index 0000000..3c78ed8 --- /dev/null +++ b/src/main/java/top/ezzd/top/controller/UserController.java @@ -0,0 +1,373 @@ +package top.ezzd.controller; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.UUID; + +import javax.annotation.Resource; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +import top.ezzd.pojo.Apply; +import top.ezzd.pojo.Ip; +import top.ezzd.pojo.Photo; +import top.ezzd.pojo.User; +import top.ezzd.service.ApplyService; +import top.ezzd.service.BanIpService; +import top.ezzd.service.IpService; +import top.ezzd.service.UserPhotoService; +import top.ezzd.service.UserService; +import top.ezzd.tools.GetIp; +import top.ezzd.tools.PhotoFileUtils; + +/** + * �����û��˻�����Ŀ������� + * + * @author SmallHappy + * + */ +@Controller +public class UserController { + @Resource + private UserService userServiceImpl; + @Resource + private UserPhotoService userPhotoServiceImpl; + @Resource + private ApplyService applyServiceImpl; + @Resource + private IpService ipServiceImpl; + @Resource + BanIpService banIpServiceImpl; + + + + private GetIp getIp =new GetIp(); + + private Ip ip = new Ip(); + // @Resource + // private UserForgetService userForgetServiceImpl; + + /** + * ��¼ + * + * @param u + * @param session + * @return + */ + @RequestMapping(value={"/login"},method={RequestMethod.POST}) + @ResponseBody + public String userLogin(User u, HttpSession session, HttpServletRequest req, HttpServletResponse resp) { + try { + // ����ȡ����user��Ϣ���ݵ�service���ѯ + User user = userServiceImpl.selectUser(u); + // ���user��Ϊ�����ʾ��¼�ɹ� + if (user != null) { + // ��ȡǰ��ҳ�湴ѡ�����Ϣ + String remember = req.getParameter("remember"); + // ��user��Ϣд��session�� + session.setAttribute("user", user); + // ��applyд��session�� + Apply apply = applyServiceImpl.selectApplyByUname(user.getUname()); + session.setAttribute("apply", apply); + // �����ȡ����ǰ��ҳ�浥ѡ�����ϢΪtrue + if (remember.equals("true")) { + // ʹ��cookie + Cookie cookie = new Cookie("USERJSESSIONID", user.getCookieuuid()); + // ��ֹʹ��cookie�ƹ���¼ + cookie.setHttpOnly(true); + // ����cookie����ʱ�� + cookie.setMaxAge(3600 * 24 * 30); + // ����cookie��·�� + cookie.setPath(req.getContextPath()+"/"); + // ��cookieд����Ӧ�� + resp.addCookie(cookie); + } + + return "1"; + } else { + return "2"; + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return "3"; + } + + } + + /** + * �û�ע�� + * + * @param u + * @param req + * @param session + * @return + */ + @RequestMapping("insertUser") + @ResponseBody + public String insertUser(User u, HttpServletRequest req, HttpSession session) { + // ����UUID ����CookieUUID + String cookieuuid = UUID.randomUUID().toString(); + // �����ɵ�UUID����User�� + u.setCookieuuid(cookieuuid); + // ��ȡ�û��������֤�� + String uyzm = req.getParameter("uyzm"); + // String root = req.getParameter("root"); + // if (root.equals("SMDH")) { + // return "4"; + // } else { + // ��ȡsession�д������֤�� + String yzm = (String) session.getAttribute("registCode"); + // �ж���֤���Ƿ�һ�� + + if (yzm==null||!yzm.equals(uyzm)) { + return "0"; + } + // ��֤��ʹ�ú����� �����ظ�ʹ�� + session.removeAttribute("registCode"); + try { + // ���ò����û�ҵ��� + int index = userServiceImpl.insertUser(u); + if (index == 1) { + // ����ȡ����uname����session�� + session.setAttribute("uname", u.getUname()); + return "1"; + } else { + return "2"; + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return "3"; + // } + } + } + + /** + * �޸� + * + * @param req + * @param session + * @return + */ + @RequestMapping("updateUser") + @ResponseBody + public String updateUser(HttpServletRequest req, HttpSession session) { + // ��ȡ�û���д�������Ϣ + String nickname = req.getParameter("nickname"); + String uname = req.getParameter("uname"); + String newpwd = req.getParameter("newpwd"); + String oldpwd = req.getParameter("oldpwd"); + String uyzm = req.getParameter("uyzm"); + // ��ȡsession�������֤�� + String yzm = (String) session.getAttribute("updateCode"); + + // �ж���֤���Ƿ�һ�� + if (yzm == null || !yzm.equals(uyzm)) { + return "0"; + } else { + try { + // ��֤��ʹ�ú� ���� �����ظ�ʹ�� + session.removeAttribute("updateCode"); + // ��ȡҵ��� ���û���д�������Ϣ���� + int index = userServiceImpl.updateUser(nickname, uname, newpwd, oldpwd); + + if (index == 1) { + // �ж��û��Ƿ�Ϊ��¼״̬����Ϊ��¼״̬���Ƴ���¼״̬ + if (session.getAttribute("user") != null) { + session.removeAttribute("user"); + } + return "1"; + + } else { + return "2"; + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return "3"; + } + + } + + } + + /** + * ɾ���û� + * + * @param u + * @param req + * @param session + * @return + */ + @RequestMapping("deleteUser") + @ResponseBody + public String deleteUser(User u, HttpServletRequest req, HttpSession session) { + // ��ȡ�û���д����֤�� + String uyzm = req.getParameter("uyzm"); + // ��ȡsession�д������֤�� + String yzm = (String) session.getAttribute("deleteCode"); + // �ж���֤���Ƿ�һ�� + if (yzm==null||!yzm.equals(uyzm)) { + + return "0"; + + } else { + try { + // ��֤��ʹ�ú� ���� �����ظ�ʹ�� + session.removeAttribute("deleteCode"); + // �ж��û��Ƿ���� + User user = userServiceImpl.selectUser(u); + if (user != null) { + // ����ҵ��㣬����ѯ��������Ƭ����list������ + List photo = userPhotoServiceImpl.selectPhotoById(user.getUid()); + // ���� + for (Photo photo2 : photo) { + // ���� + String path = "/usr/local/tomcat/webapps/ROOT/UpPhoto/" + photo2.getAddress(); + // �����ļ��� + File file = new File(path); + // ɾ�� + PhotoFileUtils.deleteFile(file); + } + } else { + return "2"; + } + // �����û�ɾ��ҵ��� + int index = userServiceImpl.deleteUser(u); + if (index == 1) { + // ����ҵ��㣬�����û�IDɾ����Ƭ + userPhotoServiceImpl.deletePhotoByUid(user.getUid()); + + //�������session(ʹ��ǰ�ỰsessionʧЧ) + session.invalidate(); + return "1"; + } else { + return "2"; + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return "3"; + } + + } + } + + /** + * �û��dz� + * + * @param session + * @return + */ + @RequestMapping("logout") + public String userLogOut(HttpSession session, HttpServletRequest req, HttpServletResponse resp) { + + + //�������session(ʹ��ǰ�ỰsessionʧЧ) + session.invalidate(); + + // ��ȡǰ̨cookie���� + if (req.getCookies() != null) { + // ��cookie������Ϊ�� + Cookie cookie = new Cookie("USERJSESSIONID", null); + // ������������Ϊ 0 + cookie.setMaxAge(0); + cookie.setPath("/"); + // ����cookie + resp.addCookie(cookie); + } + String loginUrl = resp.encodeRedirectURL("/dispatcher?page=login"); + return "redirect:"+loginUrl; + } + + /** + * ���� (����֮�󿪷�) + * + * + * + * + * @param req + * @param session + */ + @RequestMapping("userSuggest") + public String userSuggest(HttpServletRequest req, HttpSession session,HttpServletResponse resp) { + session.getAttribute("user"); + // System.out.println( + // u.getNickname() + "�������\n����Ϊ:" + req.getParameter("title") + "\n����Ϊ:" + // + req.getParameter("message")); + String indexUrl = resp.encodeURL("/dispatcher?page=index"); + return indexUrl; + } + + /* + * Cookie��¼ + * + * @param req �������� + * + * @param session + * + * @return + */ + @RequestMapping("cookieLogin") + public String cookieLogin(HttpServletRequest req, HttpSession session, HttpServletResponse resp) { + // ip��ַ��ȡ + String ipAddress = req.getRemoteAddr(); + if(banIpServiceImpl.selectBanIpByIp(ipAddress)!=null){ + String Url_503 = resp.encodeRedirectURL("/dispatcher?page=503"); + return "redirect:"+Url_503; + } + + try { + String region = getIp.IpRegion(ipAddress); + + ip.setIpaddress(ipAddress); + ip.setRegion(region); + ipServiceImpl.insertIp(ip); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + String indexUrl = resp.encodeRedirectURL("/dispatcher?page=index"); + if (session.getAttribute("user") == null) { + Cookie[] cookies = req.getCookies(); + if (cookies != null) { + for (Cookie c : cookies) { + if (c.getName().equalsIgnoreCase("USERJSESSIONID")) { + if (c.getValue() != null) { + String cookieuuid = c.getValue(); + User user = userServiceImpl.selectUserByUuid(cookieuuid); + if (user != null) { + // ��applyд��session�� + Apply apply = applyServiceImpl.selectApplyByUname(user.getUname()); + session.setAttribute("apply", apply); + // ��userд��session + session.setAttribute("user", user); + return "redirect:"+indexUrl; + } else { + return "redirect:"+indexUrl; + } + } else { + return "redirect:"+indexUrl; + } + } else { + return "redirect:"+indexUrl; + } + } + } else { + return "redirect:"+indexUrl; + } + } else { + return "redirect:"+indexUrl; + } + return "redirect:"+indexUrl; + } +}