/* * ************************************************* * Copyright (c) 2018 QMTH. All Rights Reserved. * Created by Deason on 2018-07-16 17:50:31. * ************************************************* */ package cn.com.qmth.examcloud.app.controller; import static cn.com.qmth.examcloud.app.model.Constants.PARAM_APP_KEY; import static cn.com.qmth.examcloud.app.model.Constants.PARAM_APP_TOKEN; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import cn.com.qmth.examcloud.app.model.LoginInfo; import cn.com.qmth.examcloud.app.model.LoginType; import cn.com.qmth.examcloud.app.model.Result; import cn.com.qmth.examcloud.app.model.UserInfo; import cn.com.qmth.examcloud.app.service.CoreAuthService; import cn.com.qmth.examcloud.app.service.CoreBasicService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; /** * 认证中心相关接口 * * @author: QMTH * @since: 2018/7/16 */ @RestController @RequestMapping("${$rmp}/v2") @Api(tags = "认证中心相关接口") public class UserAuthRestController { private static final Logger LOG = LoggerFactory.getLogger(UserAuthRestController.class); @Autowired private CoreAuthService authService; @Autowired private CoreBasicService basicService; @ApiOperation(value = "登录接口", notes = "参数accountType值:STUDENT_IDENTITY_NUMBER、STUDENT_CODE、STUDENT_PHONE") @RequestMapping(value = "/user/login", method = {RequestMethod.POST}) public Result login(@RequestParam String account, @RequestParam String password, // @RequestParam String accountType, @RequestParam(required = false) Long rootOrgId, @RequestParam(required = false) String domain, @RequestHeader String deviceId) throws Exception { // 改为只允许手机号登录 LoginInfo loginInfo = new LoginInfo(account, password, LoginType.STUDENT_PHONE.name(), rootOrgId, domain, deviceId, null); Result result = authService.login(loginInfo); if (result.isSuccess() && result.getData() != null) { //登录成功后缓存Token信息 UserInfo userInfo = result.getData(); loginInfo.setUserId(userInfo.getUserId()); loginInfo.setRootOrgId(userInfo.getRootOrgId()); loginInfo.setUserName(userInfo.getDisplayName()); loginInfo.setKey(userInfo.getKey()); loginInfo.setToken(userInfo.getToken()); loginInfo.setAppToken(userInfo.getToken()); authService.cacheLoginInfo(loginInfo, userInfo.getKey()); LOG.info(String.format("key:%s token:%s", userInfo.getKey(), userInfo.getToken())); } return result; } @ApiOperation(value = "验证码登录接口") @RequestMapping(value = "/user/login/verify", method = {RequestMethod.POST}) public Result verifyLogin(@RequestParam String account, @RequestParam String smsCode, @RequestParam(required = false) Long rootOrgId, @RequestParam(required = false) String domain, @RequestHeader String deviceId) throws Exception { LoginInfo loginInfo = new LoginInfo(account, null, LoginType.STUDENT_PHONE.name(), rootOrgId, domain, deviceId, smsCode); Result result = authService.login(loginInfo); if (result.isSuccess() && result.getData() != null) { //登录成功后缓存Token信息 UserInfo userInfo = result.getData(); loginInfo.setUserId(userInfo.getUserId()); loginInfo.setRootOrgId(userInfo.getRootOrgId()); loginInfo.setUserName(userInfo.getDisplayName()); loginInfo.setToken(userInfo.getToken()); loginInfo.setKey(userInfo.getKey()); loginInfo.setAppToken(userInfo.getToken()); authService.cacheLoginInfo(loginInfo, userInfo.getKey()); LOG.info(String.format("key:%s token:%s", userInfo.getKey(), userInfo.getToken())); } return result; } @ApiOperation(value = "登出接口") @RequestMapping(value = "/user/logout", method = {RequestMethod.POST}) public Result logout(@RequestHeader(name = PARAM_APP_KEY) String key, @RequestHeader(name = PARAM_APP_TOKEN) String token) throws Exception { return authService.logout(key, token); } @ApiOperation(value = "获取用户信息接口") @RequestMapping(value = "/user/info", method = {RequestMethod.POST}) public Result getUserInfo(@RequestHeader(name = PARAM_APP_KEY) String key, @RequestHeader(name = PARAM_APP_TOKEN) String token) throws Exception { //return authService.getUserInfo(key, token); return basicService.getStudentInfo(key, token); } @ApiOperation(value = "修改密码接口") @RequestMapping(value = "/user/update/password", method = {RequestMethod.POST}) public Result updatePassword(@RequestHeader(name = PARAM_APP_KEY) String key, @RequestHeader(name = PARAM_APP_TOKEN) String token, @RequestParam String password, @RequestParam String newPassword) throws Exception { return authService.updateStudentPassword(key, token,password, newPassword); } @ApiOperation(value = "重置密码接口") @RequestMapping(value = "/user/reset/password", method = {RequestMethod.POST}) public Result updateNewPassword(@RequestHeader(name = PARAM_APP_KEY) String key, @RequestHeader(name = PARAM_APP_TOKEN) String token, @RequestParam String newPassword) throws Exception { return authService.resetStudentPassword(key, token, newPassword); } @ApiOperation(value = "保存用户绑定的手机号接口") @RequestMapping(value = "/user/binding/phone", method = {RequestMethod.POST}) 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 { return authService.userBindingPhone(key, token, phone, code); } @ApiOperation(value = "极验-验证初始化接口") @PostMapping(value = "/verifyCode/register") public Result register(@RequestParam String user_id,@RequestParam String client_type,@RequestParam(required = false) String ip_address) throws Exception{ return authService.register(user_id, client_type, ip_address) ; } @ApiOperation(value = "极验-验证码登录接口") @PostMapping(value = "/verifyCode/gt/login") public Result geetestLogin( @RequestParam String seccode,@RequestParam String validate,@RequestParam String challenge, @RequestParam String user_id,@RequestParam String client_type,@RequestParam(required = false) String ip_address, @RequestParam String accountValue,@RequestParam String password,@RequestParam String accountType, @RequestParam Long rootOrgId) throws Exception{ return authService.geetestLogin(seccode, validate, challenge, user_id, client_type, ip_address, accountValue, password, accountType, rootOrgId); } }