AdminController.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package cn.efunbox.audio.controller;
  2. import cn.efunbox.audio.consts.Consts;
  3. import cn.efunbox.audio.entity.Admin;
  4. import cn.efunbox.audio.entity.Channel;
  5. import cn.efunbox.audio.service.AdminService;
  6. import cn.efunbox.audio.utils.ApiCode;
  7. import cn.efunbox.audio.utils.Common;
  8. import cn.efunbox.audio.utils.HttpUtil;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.data.redis.core.StringRedisTemplate;
  13. import org.springframework.data.redis.core.ValueOperations;
  14. import org.springframework.web.bind.annotation.RequestBody;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RequestMethod;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20. import java.util.Date;
  21. import java.util.List;
  22. import java.util.UUID;
  23. import java.util.concurrent.TimeUnit;
  24. /**
  25. * Created by yao on 17-9-26.
  26. */
  27. @RestController
  28. @Slf4j
  29. @RequestMapping(value = "/admin")
  30. public class AdminController {
  31. @Autowired
  32. AdminService adminService;
  33. @Autowired
  34. StringRedisTemplate stringRedisTemplate;
  35. @Value("${admin.token.expire}")
  36. int tokenExpire = 24*30;
  37. @RequestMapping(value = "/login" ,method = RequestMethod.POST)
  38. public void Login(HttpServletRequest request, HttpServletResponse response){
  39. String name = request.getParameter("name");
  40. String pwd = request.getParameter("pwd");
  41. if(name==null && pwd==null){
  42. HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
  43. return;
  44. }
  45. List<Admin> list = adminService.SearchByName(name);
  46. if(list==null || list.size()<1){
  47. HttpUtil.responseApiCode(request, response, ApiCode.NOT_FOUND);
  48. return;
  49. }
  50. Admin admin = list.get(0);
  51. String pwdMD5 = Common.getMD5(Common.getMD5(pwd)+admin.getSalt());
  52. // System.out.println(pwdMD5);
  53. // System.out.println(admin.getPwd());
  54. if(pwdMD5.equalsIgnoreCase(admin.getPwd())){
  55. if(admin.getStatus()<0){
  56. HttpUtil.responseApiCode(request, response, ApiCode.ACCESS_DENIED);
  57. return;
  58. }
  59. String token = UUID.randomUUID().toString().replaceAll("-", "");
  60. admin.setToken(token);
  61. adminService.Update(admin);
  62. admin.setPwd("");
  63. admin.setSalt("");
  64. SaveRedis(admin);
  65. HttpUtil.responseOkData(request, response, admin);
  66. }else
  67. HttpUtil.responseApiCode(request, response, ApiCode.INVALID_TOKEN);
  68. return;
  69. }
  70. // @RequestMapping(value = "/register", method = RequestMethod.POST)
  71. // public void Insert(HttpServletRequest request, HttpServletResponse response){
  72. // String name = request.getParameter("name");
  73. // String pwd = request.getParameter("pwd");
  74. // String idChannel = request.getParameter("idChannel");
  75. // if(name==null || name.length()<4 || pwd==null || pwd.length()<4){
  76. // HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
  77. // return;
  78. // }
  79. // if(idChannel==null || idChannel.length()==0)
  80. // idChannel = "0";
  81. // String salt = UUID.randomUUID().toString().replaceAll("-","");
  82. // String token = UUID.randomUUID().toString().replaceAll("-", "");
  83. // String pwdMD5 = Common.getMD5(Common.getMD5(pwd) + salt);
  84. //
  85. // List<Admin> list = adminService.SearchByName(name);
  86. // if(list!=null && list.size()>0){
  87. // HttpUtil.responseApiCode(request, response, ApiCode.RECORD_EXIST);
  88. // return;
  89. // }
  90. // Admin admin = new Admin();
  91. // admin.setName(name);
  92. // admin.setIdChannel(Long.valueOf(idChannel));
  93. // admin.setPwd(pwdMD5);
  94. // admin.setSalt(salt);
  95. // admin.setToken(token);
  96. // admin.setCreated(new Date());
  97. // admin = adminService.Insert(admin);
  98. // SaveRedis(admin);
  99. //
  100. // HttpUtil.responseOutWithJson(request, response, admin);
  101. // return;
  102. // }
  103. @RequestMapping(value = "/search" ,method = RequestMethod.POST)
  104. public void Search(HttpServletRequest request, HttpServletResponse response){
  105. List<Admin> adminList = adminService.SearchAll();
  106. HttpUtil.responseOkData(request, response, adminList);
  107. return;
  108. }
  109. @RequestMapping(value = "/register", method = RequestMethod.POST)
  110. public void Register(HttpServletRequest request, HttpServletResponse response, @RequestBody Admin admin){
  111. InsertUpdate(request, response, admin);
  112. }
  113. @RequestMapping(value = "/update", method = RequestMethod.POST)
  114. public void Update(HttpServletRequest request, HttpServletResponse response, @RequestBody Admin admin){
  115. InsertUpdate(request, response, admin);
  116. }
  117. public void InsertUpdate(HttpServletRequest request, HttpServletResponse response, Admin admin){
  118. if(admin.getId()==null){
  119. String name = admin.getName();
  120. String pwd = admin.getPwd();
  121. if(name==null || name.length()<4 || pwd==null || pwd.length()<4){
  122. HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
  123. return;
  124. }
  125. List<Admin> list = adminService.SearchByName(name);
  126. if(list!=null && list.size()>0){
  127. HttpUtil.responseApiCode(request, response, ApiCode.RECORD_EXIST);
  128. return;
  129. }
  130. admin.setCreated(new Date());
  131. }
  132. if(admin.getPwd()!=null){
  133. String pwd = admin.getPwd();
  134. String salt = UUID.randomUUID().toString().replaceAll("-","");
  135. String token = UUID.randomUUID().toString().replaceAll("-", "");
  136. String pwdMD5 = Common.getMD5(Common.getMD5(pwd) + salt);
  137. admin.setPwd(pwdMD5);
  138. admin.setSalt(salt);
  139. admin.setToken(token);
  140. }
  141. admin = adminService.Insert(admin);
  142. SaveRedis(admin);
  143. HttpUtil.responseOutWithJson(request, response, admin);
  144. return;
  145. }
  146. /**
  147. * 将token放入redis
  148. * @param admin
  149. */
  150. public void SaveRedis(Admin admin){
  151. ValueOperations valueOperations = stringRedisTemplate.opsForValue();
  152. valueOperations.set(Consts.REDIS_ADMIN+admin.getId(), admin.getToken(), tokenExpire*3600, TimeUnit.SECONDS);
  153. }
  154. }