Package com.ourlinc.helloworld.controller

Source Code of com.ourlinc.helloworld.controller.UserController

package com.ourlinc.helloworld.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ourlinc.helloworld.MyCookie;
import com.ourlinc.helloworld.model.Activity;
import com.ourlinc.helloworld.model.User;
import com.ourlinc.helloworld.service.ActivityService;
import com.ourlinc.helloworld.service.UserService;
import com.ourlinc.helloworld.util.WebUtils;
import com.ourlinc.swift.util.Misc;
import com.ourlinc.swift.util.ResultPage;

@Controller
public class UserController {
  @Resource(name = "userService")
  private UserService m_UserService;
  @Resource(name = "activityService")
  private ActivityService m_ActivityService;

  /**
   * 用户注册
   *
   * @param request
   * @return
   * @throws IOException
   */
  @RequestMapping
  String register(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    String email = request.getParameter("email");
    String password = request.getParameter("password");
    String repassword = request.getParameter("repassword");
    String username = request.getParameter("username");
    String department = request.getParameter("department");
    String birthday = request.getParameter("birthday");
    String entryDate = request.getParameter("entryDate");
    if (null == email || null == password || null == repassword
        || null == username || null == department || null == birthday
        || null == entryDate) {
      return "user/register";
    }
    // 检查邮箱是否重复的功能
    User u = m_UserService.getUserByEmail(email);
    if (null != u) {
      request.setAttribute("errorMsg", "邮箱已经被注册");
      request.setAttribute("password", password);
      request.setAttribute("repassword", repassword);
      request.setAttribute("username", username);
      request.setAttribute("department", department);
      request.setAttribute("birthday", birthday);
      request.setAttribute("entryDate", entryDate);
      return "user/register";
    }
    Date dBirthday = WebUtils.strFormatDate(birthday);
    Date dEntryDate = WebUtils.strFormatDate(entryDate);
    User user = m_UserService.registerUser(username.trim(), Misc
        .md5Hash(password.trim()), department.trim(), email.trim()
        .toLowerCase(), dBirthday, dEntryDate);
    user.flush();
    request.getSession().setAttribute("userLogin", user);
    response.sendRedirect("/activity/index.jspx");
    return null;

  }

  /**
   * 用户退出
   *
   * @param request
   * @return
   * @throws IOException
   */
  @RequestMapping
  String userlogout(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    request.getSession().setAttribute("userLogin", null);
    MyCookie.clearCookie(response);
    response.sendRedirect("/activity/index.jspx");
    return null;
  }

  /**
   * 用户登陆
   *
   * @param request
   * @return
   * @throws IOException
   */
  @RequestMapping
  String userlogin(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    String email = request.getParameter("email");
    String password = request.getParameter("password");
    String autoLogin = request.getParameter("autoLogin");
    if (null == email || null == password) {
      return "user/userlogin";
    }
    User user = m_UserService.login(email.trim().toLowerCase(), Misc
        .md5Hash(password.trim()));
    if (null == user) {
      request.setAttribute("errorMsg", "你的email和密码不符,请再试一次");
      request.setAttribute("email", email);
      request.setAttribute("password", password);
      return "user/userlogin";
    }
    // 设置cookie
    if ("true".equals(autoLogin)) {
      try {
        MyCookie.keepUserLogin(email.trim().toLowerCase(), Misc
            .md5Hash(password.trim()), response);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    request.getSession().setAttribute("userLogin", user);
    response.sendRedirect("/activity/index.jspx");
    return null;
  }

  /**
   * 参加活动
   *
   * @param request
   * @return
   * @throws IOException
   */
  @RequestMapping
  String join(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    User user = (User) request.getSession().getAttribute("userLogin");
    String actId = request.getParameter("actId");
    Activity act = m_ActivityService.getActivity(actId);
    if (null == act) {
      request.setAttribute("errorMsg", "找不到id=" + actId + "的活动");
      return "error";
    }
    // 检查这个活动能不能参加
    if (act.canEditByUser()) {
      // 用户能不能参加这个活动
      if (user.joinActivity(act)) {
        user.flush();
        response.sendRedirect("/user/myact.jspx");
        return null;
      }
    }
    request.setAttribute("errorMsg", "你不能参加这个活动,原因可能是你离职了或者已经参加了");
    return "error";
  }

  /**
   * 取消参加活动
   *
   * @param request
   * @return
   * @throws IOException
   */
  @RequestMapping
  String canceljoin(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    User user = (User) request.getSession().getAttribute("userLogin");
    String actId = request.getParameter("actId");
    Activity act = m_ActivityService.getActivity(actId);
    if (null == act) {
      request.setAttribute("errorMsg", "找不到id=" + actId + "的活动");
      return "error";
    }
    user.cancelActivity(act);
    user.flush();
    response.sendRedirect("/activity/index.jspx");
    return null;
  }

  /**
   * 管理员登陆
   *
   * @param request
   * @return
   * @throws IOException
   */
  @RequestMapping
  String adminlogin(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    String email = request.getParameter("email");
    String password = request.getParameter("password");
    if (null == email || null == password) {
      return "user/adminlogin";
    }
    User admin = m_UserService.login(email.trim().toLowerCase(), Misc
        .md5Hash(password.trim()));
    if (null == admin || User.ADMIN != admin.getAuthority()) {
      request.setAttribute("errorMsg", "你的email和密码不符,请再试一次");
      request.setAttribute("email", email);
      request.setAttribute("password", password);
      return "user/adminlogin";
    }
    request.getSession().setAttribute("userLogin", admin);
    response.sendRedirect("/activity/index.jspx");
    return null;
  }

  /**
   * 修改用户信息
   *
   * @param request
   * @return
   * @throws IOException
   */
  @RequestMapping
  String userinfo(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    User user = (User) request.getSession().getAttribute("userLogin");
    String email = request.getParameter("email");
    String password = request.getParameter("password");
    String repassword = request.getParameter("repassword");
    String username = request.getParameter("username");
    String department = request.getParameter("department");
    String birthday = request.getParameter("birthday");
    String entryDate = request.getParameter("entryDate");
    String leaveDate = request.getParameter("leaveDate");
    String editUserId = request.getParameter("editUserId");
    // 返回页面展示
    if (null == email || null == username || null == department
        || null == birthday || null == entryDate) {
      String id = request.getParameter("id");
      User u = m_UserService.getUser(id);
      if (null == u) {
        return "error";
      }
      List<Activity> list = u.getActivities();
      request.setAttribute("actList", list);
      request.setAttribute("editUser", u);
      return "user/userinfo";
    }
    // 真正修改

    User editUser = m_UserService.getUser(editUserId);
    List<Activity> list0 = editUser.getActivities();
    if ("".equals(email) || "".equals(username) || "".equals(department)
        || "".equals(birthday) || "".equals(entryDate)) {
      request.setAttribute("editUser", editUser);
      request.setAttribute("actList", list0);
      request.setAttribute("errorMsg", "请填写必填的信息!");
      return "user/userinfo";
    }
    // 管理员不能修改管理员
    if (editUser.isAdmin() && editUser.getId() != user.getId()) {
      request.setAttribute("errorMsg", "你不能修改其他管理员的信息");
      request.setAttribute("editUser", editUser);
      request.setAttribute("actList", list0);
      return "user/userinfo";
    }

    // 检查邮箱相同
    if (!email.equals(editUser.getEmail())) {
      User u = m_UserService.getUserByEmail(email);
      if (null != u) {
        request.setAttribute("errorMsg", "邮箱已经被注册");
        request.setAttribute("editUser", editUser);
        request.setAttribute("actList", list0);
        return "user/userinfo";
      }
    }

    if (!password.equals(repassword)) {
      request.setAttribute("editUser", editUser);
      request.setAttribute("actList", list0);
      request.setAttribute("errorMsg", "两次密码不一样");
      return "user/userinfo";
    }

    Date dBirthday = WebUtils.strFormatDate(birthday);
    Date dEntryDate = WebUtils.strFormatDate(entryDate);
    Date dLeaveDate = WebUtils.strFormatDate(leaveDate);

    editUser.edit(username.trim(), department.trim(), email.toLowerCase()
        .trim(), dBirthday, dEntryDate, dLeaveDate, ""
        .equals(leaveDate) ? 1 : 0);

    if (!"".equals(password)) {
      editUser.editPass(Misc.md5Hash(password.trim()));
    }

    editUser.flush();
    response.sendRedirect("/user/manager.jspx");
    return null;

  }

  /**
   * 修改个人信息
   *
   * @param request
   * @return
   * @throws IOException
   */
  @RequestMapping
  String editaccount(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    User user = (User) request.getSession().getAttribute("userLogin");
    String email = request.getParameter("email");
    String password = request.getParameter("password");
    String repassword = request.getParameter("repassword");
    String department = request.getParameter("department");
    String birthday = request.getParameter("birthday");
    // 返回页面进行展示
    if (null == email || null == password || null == repassword
        || null == department || null == birthday) {
      request.setAttribute("userLogin", user);
      return "user/editaccount";
    }
    // 修改个人信息
    Date dBirthday = WebUtils.strFormatDate(birthday);
    if (!email.equals(user.getEmail())) {
      // 检查邮件是否重复
      User u = m_UserService.getUserByEmail(email);
      if (null != u) {
        request.setAttribute("userLogin", user);
        request.setAttribute("errorMsg", "邮箱已经被注册");
        return "user/editaccount";
      }
    }
    user.edit(department.trim(), email.trim().toLowerCase(), dBirthday);
    if (!"".equals(password)) {
      user.editPass(Misc.md5Hash(password.trim()));
    }
    user.flush();
    response.sendRedirect("/activity/index.jspx");
    return null;
  }

  /**
   * 用户管理
   *
   * @param request
   * @return
   */
  @RequestMapping
  String manager(HttpServletRequest request) {
    ResultPage<User> rp = m_UserService.listUser();
    rp.setPageSize(20);
    String p = request.getParameter("p");
    int intp = WebUtils.toInt(p);
    rp.gotoPage(intp);
    request.setAttribute("list", rp);
    return "user/manager";
  }

  /**
   * 查看我已报名的活动
   *
   * @param request
   * @return
   * @throws IOException
   */
  @RequestMapping
  String myact(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    User user = (User) request.getSession().getAttribute("userLogin");
    ResultPage<Activity> rp = user.listMyAct();
    rp.setPageSize(20);
    String p = request.getParameter("p");
    int intp = WebUtils.toInt(p);
    rp.gotoPage(intp);
    request.setAttribute("list", rp);
    request.setAttribute("act", "listNow");
    return "user/myact";
  }

  /**
   * 查看我的已参加活动
   *
   * @param request
   * @return
   * @throws IOException
   */
  @RequestMapping
  String myactover(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    User user = (User) request.getSession().getAttribute("userLogin");
    ResultPage<Activity> rp = user.listMyActOver();
    rp.setPageSize(20);
    String p = request.getParameter("p");
    int intp = WebUtils.toInt(p);
    rp.gotoPage(intp);
    request.setAttribute("list", rp);
    request.setAttribute("act", "listOver");
    return "user/myact";
  }

  /**
   * 查看我的已参加但被取消的活动
   *
   * @param request
   * @return
   * @throws IOException
   */
  @RequestMapping
  String myactcancel(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    User user = (User) request.getSession().getAttribute("userLogin");
    ResultPage<Activity> rp = user.listMyActCancel();
    rp.setPageSize(20);
    String p = request.getParameter("p");
    int intp = WebUtils.toInt(p);
    rp.gotoPage(intp);
    request.setAttribute("list", rp);
    request.setAttribute("act", "listCancel");
    return "user/myact";
  }

  /**
   * 测试ajax验证用户名有没有重复
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping
  String checkemail_aj(HttpServletRequest request,
      HttpServletResponse response) {
    String email = WebUtils.toString(request.getParameter("email")).trim()
        .toLowerCase();
    if (email.length() > 0) {
      User user = m_UserService.getUserByEmail(email);
      response.setContentType("text/plain;charset=UTF-8");
      response.setHeader("Cache-Control", "no-cache");
      String htmlDoc = (null != user) ? "邮箱已经存在!" : "用户名可以使用!";
      try {
        response.getWriter().write(htmlDoc);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return null;
  }
}
TOP

Related Classes of com.ourlinc.helloworld.controller.UserController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.