UserAuthRestController.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * *************************************************
  3. * Copyright (c) 2018 QMTH. All Rights Reserved.
  4. * Created by Deason on 2018-07-16 17:50:31.
  5. * *************************************************
  6. */
  7. package cn.com.qmth.examcloud.app.controller;
  8. import static cn.com.qmth.examcloud.app.model.Constants.PARAM_APP_KEY;
  9. import static cn.com.qmth.examcloud.app.model.Constants.PARAM_APP_TOKEN;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.web.bind.annotation.PostMapping;
  14. import org.springframework.web.bind.annotation.RequestHeader;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RequestMethod;
  17. import org.springframework.web.bind.annotation.RequestParam;
  18. import org.springframework.web.bind.annotation.RestController;
  19. import cn.com.qmth.examcloud.app.model.LoginInfo;
  20. import cn.com.qmth.examcloud.app.model.LoginType;
  21. import cn.com.qmth.examcloud.app.model.Result;
  22. import cn.com.qmth.examcloud.app.model.UserInfo;
  23. import cn.com.qmth.examcloud.app.service.CoreAuthService;
  24. import cn.com.qmth.examcloud.app.service.CoreBasicService;
  25. import io.swagger.annotations.Api;
  26. import io.swagger.annotations.ApiOperation;
  27. /**
  28. * 认证中心相关接口
  29. *
  30. * @author: QMTH
  31. * @since: 2018/7/16
  32. */
  33. @RestController
  34. @RequestMapping("${$rmp}/v2")
  35. @Api(tags = "认证中心相关接口")
  36. public class UserAuthRestController {
  37. private static final Logger LOG = LoggerFactory.getLogger(UserAuthRestController.class);
  38. @Autowired
  39. private CoreAuthService authService;
  40. @Autowired
  41. private CoreBasicService basicService;
  42. @ApiOperation(value = "登录接口", notes = "参数accountType值:STUDENT_IDENTITY_NUMBER、STUDENT_CODE、STUDENT_PHONE")
  43. @RequestMapping(value = "/user/login", method = {RequestMethod.POST})
  44. public Result<UserInfo> login(@RequestParam String account,
  45. @RequestParam String password,
  46. // @RequestParam String accountType,
  47. @RequestParam(required = false) Long rootOrgId,
  48. @RequestParam(required = false) String domain,
  49. @RequestHeader String deviceId) throws Exception {
  50. // 改为只允许手机号登录
  51. LoginInfo loginInfo = new LoginInfo(account, password, LoginType.STUDENT_PHONE.name(), rootOrgId, domain, deviceId, null);
  52. Result<UserInfo> result = authService.login(loginInfo);
  53. if (result.isSuccess() && result.getData() != null) {
  54. //登录成功后缓存Token信息
  55. UserInfo userInfo = result.getData();
  56. loginInfo.setUserId(userInfo.getUserId());
  57. loginInfo.setRootOrgId(userInfo.getRootOrgId());
  58. loginInfo.setUserName(userInfo.getDisplayName());
  59. loginInfo.setKey(userInfo.getKey());
  60. loginInfo.setToken(userInfo.getToken());
  61. loginInfo.setAppToken(userInfo.getToken());
  62. authService.cacheLoginInfo(loginInfo, userInfo.getKey());
  63. LOG.info(String.format("key:%s token:%s", userInfo.getKey(), userInfo.getToken()));
  64. }
  65. return result;
  66. }
  67. @ApiOperation(value = "验证码登录接口")
  68. @RequestMapping(value = "/user/login/verify", method = {RequestMethod.POST})
  69. public Result<UserInfo> verifyLogin(@RequestParam String account,
  70. @RequestParam String smsCode,
  71. @RequestParam(required = false) Long rootOrgId,
  72. @RequestParam(required = false) String domain,
  73. @RequestHeader String deviceId) throws Exception {
  74. LoginInfo loginInfo = new LoginInfo(account, null, LoginType.STUDENT_PHONE.name(), rootOrgId, domain, deviceId, smsCode);
  75. Result<UserInfo> result = authService.login(loginInfo);
  76. if (result.isSuccess() && result.getData() != null) {
  77. //登录成功后缓存Token信息
  78. UserInfo userInfo = result.getData();
  79. loginInfo.setUserId(userInfo.getUserId());
  80. loginInfo.setRootOrgId(userInfo.getRootOrgId());
  81. loginInfo.setUserName(userInfo.getDisplayName());
  82. loginInfo.setToken(userInfo.getToken());
  83. loginInfo.setKey(userInfo.getKey());
  84. loginInfo.setAppToken(userInfo.getToken());
  85. authService.cacheLoginInfo(loginInfo, userInfo.getKey());
  86. LOG.info(String.format("key:%s token:%s", userInfo.getKey(), userInfo.getToken()));
  87. }
  88. return result;
  89. }
  90. @ApiOperation(value = "登出接口")
  91. @RequestMapping(value = "/user/logout", method = {RequestMethod.POST})
  92. public Result logout(@RequestHeader(name = PARAM_APP_KEY) String key, @RequestHeader(name = PARAM_APP_TOKEN) String token) throws Exception {
  93. return authService.logout(key, token);
  94. }
  95. @ApiOperation(value = "获取用户信息接口")
  96. @RequestMapping(value = "/user/info", method = {RequestMethod.POST})
  97. public Result getUserInfo(@RequestHeader(name = PARAM_APP_KEY) String key, @RequestHeader(name = PARAM_APP_TOKEN) String token) throws Exception {
  98. //return authService.getUserInfo(key, token);
  99. return basicService.getStudentInfo(key, token);
  100. }
  101. @ApiOperation(value = "修改密码接口")
  102. @RequestMapping(value = "/user/update/password", method = {RequestMethod.POST})
  103. public Result updatePassword(@RequestHeader(name = PARAM_APP_KEY) String key, @RequestHeader(name = PARAM_APP_TOKEN) String token, @RequestParam String password,
  104. @RequestParam String newPassword) throws Exception {
  105. return authService.updateStudentPassword(key, token,password, newPassword);
  106. }
  107. @ApiOperation(value = "重置密码接口")
  108. @RequestMapping(value = "/user/reset/password", method = {RequestMethod.POST})
  109. public Result updateNewPassword(@RequestHeader(name = PARAM_APP_KEY) String key, @RequestHeader(name = PARAM_APP_TOKEN) String token, @RequestParam String newPassword) throws Exception {
  110. return authService.resetStudentPassword(key, token, newPassword);
  111. }
  112. @ApiOperation(value = "保存用户绑定的手机号接口")
  113. @RequestMapping(value = "/user/binding/phone", method = {RequestMethod.POST})
  114. public Result userBindingPhone(@RequestHeader(name = PARAM_APP_KEY) String key, @RequestHeader(name = PARAM_APP_TOKEN) String token, @RequestParam String phone, @RequestParam String code) throws Exception {
  115. return authService.userBindingPhone(key, token, phone, code);
  116. }
  117. @ApiOperation(value = "极验-验证初始化接口")
  118. @PostMapping(value = "/verifyCode/register")
  119. public Result register(@RequestParam String user_id,@RequestParam String client_type,@RequestParam(required = false) String ip_address) throws Exception{
  120. return authService.register(user_id, client_type, ip_address) ;
  121. }
  122. @ApiOperation(value = "极验-验证码登录接口")
  123. @PostMapping(value = "/verifyCode/gt/login")
  124. public Result geetestLogin(
  125. @RequestParam String seccode,@RequestParam String validate,@RequestParam String challenge,
  126. @RequestParam String user_id,@RequestParam String client_type,@RequestParam(required = false) String ip_address,
  127. @RequestParam String accountValue,@RequestParam String password,@RequestParam String accountType,
  128. @RequestParam Long rootOrgId) throws Exception{
  129. return authService.geetestLogin(seccode, validate, challenge, user_id, client_type, ip_address, accountValue, password, accountType, rootOrgId);
  130. }
  131. }