UserAuthRestController.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.BaseInfoService;
  13. import cn.com.qmth.examcloud.app.service.UserAuthService;
  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. * @version v1.0
  26. * @author: fengdesheng
  27. * @since: 2018/7/16
  28. */
  29. @RestController
  30. @RequestMapping("${$rmp}/v2")
  31. @Api(tags = "认证中心相关接口")
  32. public class UserAuthRestController {
  33. private final static Logger log = LoggerFactory.getLogger(UserAuthRestController.class);
  34. @Autowired
  35. private UserAuthService userAuthService;
  36. @Autowired
  37. private BaseInfoService baseInfoService;
  38. @ApiOperation(value = "登录接口", notes = "参数accountType值说明:学生身份证号类型=STUDENT_IDENTITY_NUMBER,学生学号类型=STUDENT_CODE,学生手机号类型=STUDENT_PHONE")
  39. @RequestMapping(value = "/user/login", method = {RequestMethod.GET, RequestMethod.POST})
  40. public Result<UserInfo> login(@RequestParam String account, @RequestParam String password, @RequestParam String accountType, @RequestParam(required = false) Long rootOrgId,
  41. @RequestParam(required = false) String domain, @RequestHeader String deviceId) throws Exception {
  42. LoginInfo loginInfo = new LoginInfo(account, password, accountType, rootOrgId, domain, deviceId, null);
  43. Result<UserInfo> result = userAuthService.login(loginInfo);
  44. if (result.isSuccess() && result.getData() != null) {
  45. //登录成功后缓存Token信息
  46. UserInfo userInfo = result.getData();
  47. loginInfo.setUserName(userInfo.getDisplayName());
  48. loginInfo.setKey(userInfo.getKey());
  49. loginInfo.setToken(userInfo.getToken());
  50. loginInfo.setAppToken(userInfo.getToken());
  51. userAuthService.cacheLoginInfo(loginInfo, userInfo.getKey());
  52. log.info(String.format("key:%s token:%s", userInfo.getKey(), userInfo.getToken()));
  53. }
  54. return result;
  55. }
  56. @ApiOperation(value = "验证码接口", notes = "参数accountType值说明:学生身份证号类型=STUDENT_IDENTITY_NUMBER,学生学号类型=STUDENT_CODE,学生手机号类型=STUDENT_PHONE")
  57. @RequestMapping(value = "/user/verify", method = {RequestMethod.GET, RequestMethod.POST})
  58. public Result<UserInfo> verifyLogin(@RequestParam String account, @RequestParam String smsCode, @RequestParam(required = false) Long rootOrgId,
  59. @RequestParam(required = false) String domain, @RequestHeader String deviceId) throws Exception {
  60. LoginInfo loginInfo = new LoginInfo(account, null, LoginType.STUDENT_PHONE.name(), rootOrgId, domain, deviceId, smsCode);
  61. Result<UserInfo> result = userAuthService.login(loginInfo);
  62. if (result.isSuccess() && result.getData() != null) {
  63. //登录成功后缓存Token信息
  64. UserInfo userInfo = result.getData();
  65. loginInfo.setUserName(userInfo.getDisplayName());
  66. loginInfo.setToken(userInfo.getToken());
  67. loginInfo.setKey(userInfo.getKey());
  68. loginInfo.setAppToken(userInfo.getToken());
  69. userAuthService.cacheLoginInfo(loginInfo, userInfo.getKey());
  70. log.info(String.format("key:%s token:%s", userInfo.getKey(), userInfo.getToken()));
  71. }
  72. return result;
  73. }
  74. @ApiOperation(value = "登出接口")
  75. @RequestMapping(value = "/user/logout", method = {RequestMethod.GET, RequestMethod.POST})
  76. public Result logout(@RequestHeader(name = PARAM_APP_KEY) String key, @RequestHeader(name = PARAM_APP_TOKEN) String token) throws Exception {
  77. return userAuthService.logout(key, token);
  78. }
  79. @ApiOperation(value = "获取用户信息接口")
  80. @RequestMapping(value = "/user/info", method = {RequestMethod.GET, RequestMethod.POST})
  81. public Result getUserInfo(@RequestHeader(name = PARAM_APP_KEY) String key, @RequestHeader(name = PARAM_APP_TOKEN) String token) throws Exception {
  82. //return userAuthService.getUserInfo(key, token);
  83. return baseInfoService.getStudentInfo(key, token);
  84. }
  85. @ApiOperation(value = "修改密码接口")
  86. @RequestMapping(value = "/user/update/password", method = {RequestMethod.GET, RequestMethod.POST})
  87. public Result updatePassword(@RequestHeader(name = PARAM_APP_KEY) String key, @RequestHeader(name = PARAM_APP_TOKEN) String token, @RequestParam Long studentId, @RequestParam String password,
  88. @RequestParam String newPassword) throws Exception {
  89. return userAuthService.updateStudentPassword(key, token, studentId, password, newPassword);
  90. }
  91. @ApiOperation(value = "重置密码接口")
  92. @RequestMapping(value = "/user/reset/password", method = {RequestMethod.GET, RequestMethod.POST})
  93. public Result updateNewPassword(@RequestHeader(name = PARAM_APP_KEY) String key, @RequestHeader(name = PARAM_APP_TOKEN) String token, @RequestParam String newPassword) throws Exception {
  94. return userAuthService.resetStudentPassword(key, token, newPassword);
  95. }
  96. @ApiOperation(value = "保存用户绑定的手机号接口")
  97. @RequestMapping(value = "/user/binding/phone", method = {RequestMethod.GET, RequestMethod.POST})
  98. 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 {
  99. return userAuthService.userBindingPhone(key, token, phone, code);
  100. }
  101. }