UserAuthRestController.java 6.3 KB

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