package com.nova.sankuai.controller.v2; import com.alibaba.fastjson.JSONObject; import com.nova.sankuai.domain.dto.LiandengPageJumpAutoDownloadDTO; import com.nova.sankuai.domain.dto.LiandengPageJumpAutoLoginDTO; import com.nova.sankuai.domain.dto.LoginUserDto; import com.nova.sankuai.domain.enums.SourceTypeEnum; import com.nova.sankuai.domain.vo.common.MessageCode; import com.nova.sankuai.infra.config.CommonException; import com.nova.sankuai.infra.constants.ResultCode; import com.nova.sankuai.infra.utils.IpUtil; import com.nova.sankuai.infra.utils.MessageCodeUtil; import com.nova.sankuai.infra.utils.ResultJson; import com.nova.sankuai.infra.utils.UserUtil; import com.nova.sankuai.service.IChannelInfoService; import com.nova.sankuai.service.ICustomerUserService; import com.nova.sankuai.service.ILiandengPageJumpService; import com.nova.sankuai.service.ISysUserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.net.URLDecoder; import java.util.Map; /** * @author weikangdi */ @Api(tags = "登录授权V2") @Slf4j @RestController @RequestMapping(value = "/api/v2/auth") @AllArgsConstructor public class AuthV2Controller { private final ISysUserService sysUserService; private final IChannelInfoService channelInfoService; private final ICustomerUserService customerUserService; private final ILiandengPageJumpService liandengPageJumpService; @PostMapping(value = "/smsLogin") @ApiOperation(value = "手机验证码登录") public ResultJson smsLogin(@RequestBody LoginUserDto userDto) { if (!SourceTypeEnum.ADMIN.getCode().equals(userDto.getSource())) { ResultJson checkResult = channelInfoService.checkChannel(userDto.getAppId(), null); if (checkResult != null) { return checkResult; } } JSONObject token = sysUserService.smsLogin(userDto); return ResultJson.ok(token); } @PostMapping(value = "/otherSmsLogin") @ApiOperation(value = "小凌手机验证码登录") public ResultJson otherSmsLogin(@RequestBody LoginUserDto userDto) { if (!SourceTypeEnum.ADMIN.getCode().equals(userDto.getSource())) { ResultJson checkResult = channelInfoService.checkChannel(userDto.getAppId(), null); if (checkResult != null) { return checkResult; } } JSONObject token = sysUserService.otherSmsLogin(userDto); return ResultJson.ok(token); } @GetMapping("/generate-verify-code") @ApiOperation("生成图形验证码") public ResponseEntity>> generateVerifyCode(String phone) throws IOException { if (!MessageCodeUtil.verifyPhone(phone)) { throw new CommonException("手机号不符合规范"); } return MessageCodeUtil.getGenerateImgCode(phone); } @GetMapping("/code") @ApiOperation(value = "获取手机验证码") public ResultJson getCode(@RequestParam(value = "phone", required = false) String phone, @RequestParam(value = "source", required = false) Integer source, HttpServletRequest request, @RequestHeader(value = "verify-code", required = false) String verifyCode, @RequestParam(value = "inputVerifyCode", required = false) String inputVerifyCode) { if (StringUtils.isBlank(phone)) return ResultJson.failure(ResultCode.BAD_REQUEST, "phone 不能为空"); if (StringUtils.isBlank(verifyCode)) return ResultJson.failure(ResultCode.BAD_REQUEST, "请求头校验失败"); if (inputVerifyCode == null) return ResultJson.failure(ResultCode.BAD_REQUEST, "请求头校验失败"); if (source == null) return ResultJson.failure(ResultCode.BAD_REQUEST, "source 不能为空"); if (!MessageCodeUtil.verifyPhone(phone)) { // throw new CommonException("手机号不符合规范"); return ResultJson.failure(ResultCode.BAD_REQUEST, "手机号不符合规范"); } MessageCode code = MessageCodeUtil.getCode(phone); if (code != null) { boolean verifyImage = MessageCodeUtil.verifyImageCode(phone, verifyCode, inputVerifyCode); if (!verifyImage) { return ResultJson.failure(ResultCode.BAD_REQUEST, "图片验证码错误"); } } String ip = IpUtil.getIpAddr(request); sysUserService.getCode(phone, source, ip); return ResultJson.success(); } @GetMapping(value = "/logout") @ApiOperation(value = "退出登录") @PreAuthorize("hasAuthority('auth.logout')") public ResultJson logout(HttpServletRequest request) { String token = request.getHeader("Authorization"); UserUtil.removeUserByToken(token); return ResultJson.success(); } @GetMapping(value = "/autoLogin") @ApiOperation(value = "联登接口") public ResultJson autoLogin(@RequestParam("autologin") String autologin, @RequestParam("appId") String appId) { try { // 20220727 新增联登跳转校验 LiandengPageJumpAutoLoginDTO param = new LiandengPageJumpAutoLoginDTO(); param.setAppId(appId); param.setAutologin(autologin.replaceAll(" ", "+")); try { String targetURL = liandengPageJumpService.checkAutoLogin(param); JSONObject json = new JSONObject(); json.put("url", targetURL); return ResultJson.ok(json); } catch (Exception e) { log.debug("联登跳转异常,走常规流程: error={}", e.getMessage()); } JSONObject success = customerUserService.autoLogin(autologin); return ResultJson.ok(success); } catch (Exception e) { e.printStackTrace(); return ResultJson.failure(ResultCode.RSA_DECRYPTION_FAILED, e.getMessage()); } } @GetMapping(value = "/logoutUser") @ApiOperation(value = "用户注销") public ResultJson logoutUser(HttpServletRequest request) { String result = sysUserService.logoutUser(request); return ResultJson.ok(result); } @GetMapping(value = "/autodownload") @ApiOperation(value = "联登下载接口") public ResultJson autodownload(@RequestParam("autodownload") String autodownload, @RequestParam("appId") String appId) { try { LiandengPageJumpAutoDownloadDTO param = new LiandengPageJumpAutoDownloadDTO(); param.setAppId(appId); param.setAutodownload(URLDecoder.decode(autodownload, "UTF-8").replaceAll(" ", "+")); try { if (liandengPageJumpService.checkAutoDownload(param)) { return ResultJson.ok(null); } } catch (Exception e) { log.debug("联登跳转异常,走常规流程: error={}", e.getMessage()); } JSONObject success = customerUserService.autodownload(autodownload); return ResultJson.ok(success); } catch (Exception e) { e.printStackTrace(); return ResultJson.failure(ResultCode.RSA_DECRYPTION_FAILED, e.getMessage()); } } }