AuthController.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package com.qxgmat.controller.api;
  2. import com.nuliji.tools.MessageHelp;
  3. import com.nuliji.tools.Response;
  4. import com.nuliji.tools.ResponseHelp;
  5. import com.nuliji.tools.Transform;
  6. import com.nuliji.tools.exception.AuthException;
  7. import com.nuliji.tools.exception.ParameterException;
  8. import com.nuliji.tools.exception.SystemException;
  9. import com.qxgmat.data.dao.entity.User;
  10. import com.qxgmat.dto.request.*;
  11. import com.qxgmat.dto.response.MyDto;
  12. import com.qxgmat.help.CaptchaHelp;
  13. import com.qxgmat.help.ShiroHelp;
  14. import com.qxgmat.help.SmsHelp;
  15. import com.qxgmat.service.UsersService;
  16. import io.swagger.annotations.Api;
  17. import io.swagger.annotations.ApiOperation;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.http.MediaType;
  20. import org.springframework.validation.annotation.Validated;
  21. import org.springframework.web.bind.annotation.*;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import javax.servlet.http.HttpSession;
  25. import javax.validation.Validator;
  26. /**
  27. * Created by GaoJie on 2017/10/31.
  28. */
  29. @RestController
  30. @RequestMapping("/api/auth")
  31. @Api(tags = "用户验证", description = "登录注册找回密码", produces = MediaType.APPLICATION_JSON_VALUE)
  32. public class AuthController {
  33. @Autowired
  34. private Validator validator;
  35. @Autowired
  36. private CaptchaHelp captchaHelp;
  37. @Autowired
  38. private SmsHelp smsHelp;
  39. @Autowired
  40. private ShiroHelp shiroHelp;
  41. @Autowired
  42. private UsersService usersService;
  43. @RequestMapping(value = "/token", method = RequestMethod.POST)
  44. @ApiOperation(value = "验证token", httpMethod = "POST")
  45. public Response<MyDto> token(@RequestHeader("token") String token, HttpSession session, HttpServletRequest request) {
  46. User user;
  47. if (token == null || token.isEmpty()){
  48. user = shiroHelp.getLoginUser();
  49. if (user == null) {
  50. throw new AuthException("未登录");
  51. }
  52. }else{
  53. user = usersService.getUserByToken(token);
  54. // 用该token登录
  55. shiroHelp.getSession().login(shiroHelp.user(user.getMobile(), ""));
  56. }
  57. User entity = usersService.get(user.getId());
  58. MyDto dto = Transform.convert(entity, MyDto.class);
  59. if (!entity.getMobile().isEmpty()){
  60. dto.setBindMobile(true);
  61. }
  62. if (!entity.getWechatUnionid().isEmpty()){
  63. dto.setBindWechat(true);
  64. }
  65. if (entity.getRealStatus() > 0){
  66. dto.setBindReal(true);
  67. }
  68. if(!entity.getPrepareStatus().isEmpty()){
  69. dto.setBindPrepare(true);
  70. }
  71. return ResponseHelp.success(dto);
  72. }
  73. @RequestMapping(value = "/login", method = RequestMethod.POST)
  74. @ApiOperation(value = "登录/注册", httpMethod = "POST")
  75. public Response<MyDto> login(@RequestBody @Validated UserLoginDto userLoginDto, HttpSession session, HttpServletRequest request) {
  76. if (!smsHelp.verifyCode(userLoginDto.getMobile(), userLoginDto.getMobileVerifyCode(), session)) {
  77. throw new ParameterException("手机验证码错误!");
  78. }
  79. try {
  80. User user = usersService.register(userLoginDto.getMobile(), userLoginDto.getInviteCode(), null);
  81. }catch (ParameterException e){
  82. // 忽略已注册信息
  83. }
  84. shiroHelp.getSession().login(shiroHelp.user(userLoginDto.getMobile(), ""));
  85. User entity = shiroHelp.getLoginUser();
  86. MyDto dto = Transform.convert(entity, MyDto.class);
  87. if (!entity.getMobile().isEmpty()){
  88. dto.setBindMobile(true);
  89. }
  90. if (!entity.getWechatUnionid().isEmpty()){
  91. dto.setBindWechat(true);
  92. }
  93. if (entity.getRealStatus() > 0){
  94. dto.setBindReal(true);
  95. }
  96. if(!entity.getPrepareStatus().isEmpty()){
  97. dto.setBindPrepare(true);
  98. }
  99. return ResponseHelp.success(dto);
  100. }
  101. @RequestMapping(value = "/wechat_pc", method = RequestMethod.GET)
  102. @ApiOperation(value = "直接微信二维码登录", httpMethod = "GET")
  103. public Response<Boolean> directWechatPc(
  104. @RequestParam(required = false, defaultValue = "") String code,
  105. HttpSession session, HttpServletResponse response) {
  106. User user = (User) shiroHelp.getLoginUser();
  107. if (user!=null){
  108. // 已登录用户,绑定
  109. usersService.Oauth(user, code, "wechat_pc");
  110. }else{
  111. shiroHelp.getSession().login(shiroHelp.oauth(code, "wechat_pc"));
  112. }
  113. return MessageHelp.success(true);
  114. }
  115. @RequestMapping(value = "/wechat", method = RequestMethod.GET)
  116. @ApiOperation(value = "直接微信二维码登录", httpMethod = "GET")
  117. public Response<Boolean> directWechat(
  118. @RequestParam(required = false, defaultValue = "") String code,
  119. HttpSession session, HttpServletResponse response) {
  120. User user = (User) shiroHelp.getLoginUser();
  121. if (user!=null){
  122. // 已登录用户,绑定
  123. usersService.Oauth(user, code, "wechat_native");
  124. }else{
  125. shiroHelp.getSession().login(shiroHelp.oauth(code, "wechat_native"));
  126. }
  127. return MessageHelp.success(true);
  128. }
  129. @RequestMapping(value = "/logout", method = RequestMethod.POST)
  130. @ApiOperation(value = "登出", httpMethod = "POST")
  131. public Response<Boolean> logout(HttpSession session, HttpServletRequest request) {
  132. shiroHelp.logout();
  133. return ResponseHelp.success(true);
  134. }
  135. @RequestMapping(value = "/bind", method = RequestMethod.POST)
  136. @ApiOperation(value = "绑定手机号", notes="第三方登录后可执行", httpMethod = "POST")
  137. public Response<Boolean> bind(@RequestBody @Validated UserValidMobileDto userValidMobileDto, HttpSession session) {
  138. if (!smsHelp.verifyCode(userValidMobileDto.getMobile(), userValidMobileDto.getMobileVerifyCode(), session)) {
  139. throw new ParameterException("手机验证码错误!");
  140. }
  141. User openUser = (User) shiroHelp.getLoginUser();
  142. if(openUser == null)
  143. throw new SystemException("第三方登录错误");
  144. if(openUser.getMobile().length() > 0)
  145. throw new SystemException("手机号已绑定");
  146. try{
  147. // 创建新的账号,设定手机号,绑定第三方登录
  148. User user = usersService.register(userValidMobileDto.getMobile(), userValidMobileDto.getInviteCode(), openUser);
  149. }catch (ParameterException e){
  150. throw new ParameterException("该手机号已注册,请通过手机号进行登录!");
  151. }
  152. return ResponseHelp.success(true);
  153. }
  154. @RequestMapping(value = "/valid/invite_code", method = RequestMethod.GET)
  155. @ApiOperation(value = "验证邀请码", notes="查询邀请码对应账号", httpMethod = "GET")
  156. public Response<String> validInviteCode(
  157. @RequestParam(required = true) String inviteCode
  158. ){
  159. User user = usersService.getByInviteCode(inviteCode);
  160. if(user == null){
  161. return ResponseHelp.success(null);
  162. }else{
  163. return ResponseHelp.success(user.getNickname());
  164. }
  165. }
  166. @RequestMapping(value = "/valid/mobile", method = RequestMethod.GET)
  167. @ApiOperation(value = "验证手机号", notes="查询手机对应账号", httpMethod = "GET")
  168. public Response<Boolean> validMobile(
  169. @RequestParam(required = true) String mobile
  170. ){
  171. User user = usersService.getByMobile(mobile);
  172. if(user == null){
  173. return ResponseHelp.success(false);
  174. }else{
  175. return ResponseHelp.success(true);
  176. }
  177. }
  178. }