PageRenderTime 258ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/b13/nooote/controllers/UserController.java

https://github.com/Eric-Sun/nooote
Java | 77 lines | 42 code | 12 blank | 23 comment | 2 complexity | c76a9a67f5212041596606f498a27306 MD5 | raw file
  1. package com.b13.nooote.controllers;
  2. import java.io.IOException;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import com.alibaba.fastjson.JSON;
  9. import com.b13.nooote.core.ResultDTO;
  10. import com.b13.nooote.user.services.UserService;
  11. import com.b13.nooote.utils.ResponseWritter;
  12. import com.b13.nooote.utils.session.SessionUtil;
  13. /**
  14. * 用户相关的http接口
  15. * @author Eric
  16. *
  17. */
  18. @Controller
  19. @RequestMapping("/user")
  20. public class UserController {
  21. @Autowired
  22. UserService userServ;
  23. /**
  24. * 登录方法 <p>
  25. * 传入的参数为:
  26. * <pre>
  27. * userMail 用户的邮箱 *
  28. * userPwd 用户账号的 *
  29. * </pre>
  30. * 返回值
  31. * <pre>
  32. * {
  33. * "userId":1 用户的ID
  34. * }
  35. * </pre>
  36. * @return
  37. * @throws Exception
  38. */
  39. @RequestMapping("/login")
  40. public String login(HttpServletRequest req, HttpServletResponse resp) throws Exception{
  41. String userMail = req.getParameter("userMail");
  42. String userPwd = req.getParameter("userPwd");
  43. long userId = userServ.login(userMail, userPwd);
  44. class UserLoginDTO{
  45. private long userId;
  46. public long getUserId() {
  47. return userId;
  48. }
  49. public void setUserId(long userId) {
  50. this.userId = userId;
  51. }
  52. }
  53. UserLoginDTO d = new UserLoginDTO();
  54. d.userId = userId;
  55. ResultDTO dto = new ResultDTO(d);
  56. if( d.userId == -1)
  57. dto.setResult(1);
  58. else
  59. // set session
  60. new SessionUtil(req).set("userId", userId);
  61. // req.getSession().setAttribute("userId", userId);
  62. new ResponseWritter(resp).write(JSON.toJSONString(dto)).end();
  63. return null;
  64. }
  65. }