Commit 45122387 authored by liqin's avatar liqin 💬

bug fixed

parent 12e752ce
...@@ -25,6 +25,7 @@ import org.springframework.web.bind.annotation.*; ...@@ -25,6 +25,7 @@ import org.springframework.web.bind.annotation.*;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
...@@ -32,7 +33,6 @@ import java.util.HashMap; ...@@ -32,7 +33,6 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
*
* Created by m1991 on 2021/3/2 13:35 * Created by m1991 on 2021/3/2 13:35
*/ */
@Api(tags = "登录/注册") @Api(tags = "登录/注册")
...@@ -42,7 +42,6 @@ import java.util.Map; ...@@ -42,7 +42,6 @@ import java.util.Map;
public class LoginController { public class LoginController {
@Autowired @Autowired
private RedisUtils redisUtils; private RedisUtils redisUtils;
...@@ -54,6 +53,7 @@ public class LoginController { ...@@ -54,6 +53,7 @@ public class LoginController {
/** /**
* 手机登录接口 * 手机登录接口
*
* @param userId * @param userId
* @param sms * @param sms
* @return * @return
...@@ -65,92 +65,98 @@ public class LoginController { ...@@ -65,92 +65,98 @@ public class LoginController {
@ApiImplicitParam(name = "sms", value = "验证码", dataType = "String"), @ApiImplicitParam(name = "sms", value = "验证码", dataType = "String"),
@ApiImplicitParam(name = "userId", value = "用户手机号", required = true, dataType = "String")}) @ApiImplicitParam(name = "userId", value = "用户手机号", required = true, dataType = "String")})
@RequestMapping("/login") @RequestMapping("/login")
public Map loginBySms(@RequestParam String userId, @RequestParam String sms)throws Exception{ public Map loginBySms(@RequestParam String userId, @RequestParam String sms) throws Exception {
User users=null; User users = null;
Map map=new HashMap(); Map map = new HashMap();
String key= StringUtil.formatKeyWithPrefix(Constants.RedisKey.PROJECT_PRIFIX,Constants.RedisKey.SMS_PRIFIX,userId,Constants.Sms.CodeType.LOGIN_OR_REGISTER+""); String key = StringUtil.formatKeyWithPrefix(Constants.RedisKey.PROJECT_PRIFIX, Constants.RedisKey.SMS_PRIFIX, userId, Constants.Sms.CodeType.LOGIN_OR_REGISTER + "");
String redisCode=redisUtils.getValue(key); String redisCode = redisUtils.getValue(key);
if(StringUtil.isBlank(redisCode) || !sms.equals(redisCode)){ if (StringUtil.isBlank(redisCode) || !sms.equals(redisCode)) {
map.put("code","1003"); map.put("code", "1003");
map.put("msg","验证码错误"); map.put("msg", "验证码错误");
// throw new BaseException(ResultEnum.FAIL_VERIFY); // throw new BaseException(ResultEnum.FAIL_VERIFY);
return map; return map;
} }
redisUtils.delete(key); redisUtils.delete(key);
//根据手机号判断用户是否存在 //根据手机号判断用户是否存在
//不存在则保存用户信息--修改为提示用户注册 //不存在则保存用户信息--修改为提示用户注册
users=usersService.queryUsersByPhone(userId); users = usersService.queryUsersByPhone(userId);
if(null==users){ if (null == users) {
//throw new BaseException(ResultEnum.FAIL_ACCOUNT_NOT_EXIST); //throw new BaseException(ResultEnum.FAIL_ACCOUNT_NOT_EXIST);
map.put("code","1005"); map.put("code", "1005");
map.put("msg","账号不存在,请注册"); map.put("msg", "账号不存在,请注册");
return map; return map;
} }
String token= null; String token = null;
token = createToken(users); token = createToken(users);
if(!StringUtil.isBlank(token)){ if (!StringUtil.isBlank(token)) {
String wyz=users.getInviteCode(); String wyz = users.getInviteCode();
Map<String, Object> map1=StringUtil.createSimpleMap("token",token); Map<String, Object> map1 = StringUtil.createSimpleMap("token", token);
map1.put("wyz",wyz); map1.put("wyz", wyz);
map1.put("code",0); map1.put("code", 0);
map1.put("msg","成功!"); map1.put("msg", "成功!");
return map1; return map1;
} }
return (Map) ResultUtils.returnFail(); return (Map) ResultUtils.returnFail();
} }
public String createToken(User users)throws Exception{ public String createToken(User users) throws Exception {
String token=StringUtil.createToken(); String token = StringUtil.createToken();
//保存token //保存token
String tokenKey=StringUtil.formatKeyWithPrefix(Constants.RedisKey.PROJECT_PRIFIX,Constants.RedisKey.TOKEN_PRIFIX,token); String tokenKey = StringUtil.formatKeyWithPrefix(Constants.RedisKey.PROJECT_PRIFIX, Constants.RedisKey.TOKEN_PRIFIX, token);
UsersDto usersDto=new UsersDto(); UsersDto usersDto = new UsersDto();
BeanUtils.copyProperties(users,usersDto); BeanUtils.copyProperties(users, usersDto);
redisUtils.set(tokenKey, JSONObject.toJSONString(usersDto),Constants.Duration.HALF_HOUR_INT); redisUtils.set(tokenKey, JSONObject.toJSONString(usersDto), Constants.Duration.HALF_HOUR_INT);
return token; return token;
} }
@ApiOperation(value = "获取用户登录token信息", notes = "获取用户登录token信息", httpMethod = "POST", produces = "application/json; charset=UTF-8") @ApiOperation(value = "获取用户登录token信息", notes = "获取用户登录token信息", httpMethod = "POST", produces = "application/json; charset=UTF-8")
@RequestMapping("/info") @RequestMapping("/info")
public Map info(HttpServletRequest request)throws Exception{ public Map info(HttpServletRequest request) throws Exception {
String token=request.getHeader("token"); String token = request.getHeader("token");
String tokenKey=StringUtil.formatKeyWithPrefix(Constants.RedisKey.PROJECT_PRIFIX,Constants.RedisKey.TOKEN_PRIFIX,token); String tokenKey = StringUtil.formatKeyWithPrefix(Constants.RedisKey.PROJECT_PRIFIX, Constants.RedisKey.TOKEN_PRIFIX, token);
String userDtoJson=redisUtils.getValue(tokenKey); String userDtoJson = redisUtils.getValue(tokenKey);
if(StringUtil.isBlank(userDtoJson)){ if (StringUtil.isBlank(userDtoJson)) {
Map map=new HashMap(); Map map = new HashMap();
map.put("code","2001"); map.put("code", "2001");
map.put("msg","未登录"); map.put("msg", "未登录");
return map; return map;
} }
UsersDto usersDto=JSONObject.parseObject(userDtoJson,UsersDto.class); UsersDto usersDto = JSONObject.parseObject(userDtoJson, UsersDto.class);
usersDto.setPassword(null); usersDto.setPassword(null);
return (Map) ResultUtils.returnDataSuccess(userDtoJson); return (Map) ResultUtils.returnDataSuccess(userDtoJson);
} }
@ApiOperation(value = "二维码邀请注册", notes = "二维码邀请注册", httpMethod = "GET") @ApiOperation(value = "二维码邀请注册", notes = "二维码邀请注册", httpMethod = "GET")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "用户主键ID", required = true, dataType = "String") @ApiImplicitParam(name = "userId", value = "用户主键ID", required = true, dataType = "Long")
}) })
@GetMapping(value = "/registerByQrCode") @GetMapping(value = "/registerByQrCode")
public ResponseEntity<byte[]> registerByQrCode(@RequestParam("userId") Long userId, HttpServletRequest request) throws IOException { public ResponseEntity<byte[]> registerByQrCode(@RequestParam("userId") Long userId,
@RequestParam(value = "width", required = false, defaultValue = "120") int width,
@RequestParam(value = "height", required = false, defaultValue = "120") int height,
HttpServletRequest request) throws IOException {
User user = this.usersService.getUserById(userId); User user = this.usersService.getUserById(userId);
if (user != null) { if (user != null) {
String regFullUrl = request.getScheme() + "://" + request.getServerName() + ":"+request.getServerPort() String regFullUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ "/front/register?beInvitedCode=" + user.getInviteCode(); + "/front/register?beInvitedCode=" + user.getInviteCode();
final InputStream is = new ClassPathResource("templates/background.jpg").getInputStream(); final InputStream is = new ClassPathResource("templates/background.jpg").getInputStream();
BufferedImage srcImage = ImageIO.read(is); BufferedImage srcImage = ImageIO.read(is);
QrConfig config = new QrConfig(130, 130); QrConfig config = new QrConfig(width, height);
config.setCharset(StandardCharsets.UTF_8); config.setCharset(StandardCharsets.UTF_8);
config.setMargin(0); config.setMargin(0);
BufferedImage waterImage = QrCodeUtil.generate(regFullUrl,config); BufferedImage waterImage = QrCodeUtil.generate(regFullUrl, config);
byte[] bytes = ImageUtil.watermarkImageSimple1(srcImage, waterImage); byte[] bytes = ImageUtil.watermarkImageSimple1(srcImage, waterImage);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(waterImage, "jpg", out);
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG); headers.setContentType(MediaType.IMAGE_JPEG);
headers.setContentLength(bytes.length); headers.setContentLength(bytes.length);
return new ResponseEntity<>(bytes, headers, HttpStatus.OK); return new ResponseEntity<>(out.toByteArray(), headers, HttpStatus.OK);
} }
return null; return null;
} }
...@@ -159,45 +165,46 @@ public class LoginController { ...@@ -159,45 +165,46 @@ public class LoginController {
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "用户手机号", required = true, dataType = "String"), @ApiImplicitParam(name = "userId", value = "用户手机号", required = true, dataType = "String"),
@ApiImplicitParam(name = "beInvitedCode", value = "推荐人邀请码", required = false, dataType = "String"), @ApiImplicitParam(name = "beInvitedCode", value = "推荐人邀请码", required = false, dataType = "String"),
@ApiImplicitParam(name = "sms", value = "验证码",required = true, dataType = "String") @ApiImplicitParam(name = "sms", value = "验证码", required = true, dataType = "String")
}) })
@RequestMapping("/register") @RequestMapping("/register")
public Map register(@RequestParam String userId,@RequestParam String beInvitedCode, @RequestParam String sms)throws Exception { public Map register(@RequestParam String userId, @RequestParam String beInvitedCode, @RequestParam String sms) throws Exception {
User users=null; User users = null;
String key= StringUtil.formatKeyWithPrefix(Constants.RedisKey.PROJECT_PRIFIX,Constants.RedisKey.SMS_PRIFIX,userId,Constants.Sms.CodeType.LOGIN_OR_REGISTER+""); String key = StringUtil.formatKeyWithPrefix(Constants.RedisKey.PROJECT_PRIFIX, Constants.RedisKey.SMS_PRIFIX, userId, Constants.Sms.CodeType.LOGIN_OR_REGISTER + "");
String redisCode=redisUtils.getValue(key); String redisCode = redisUtils.getValue(key);
if(StringUtil.isBlank(redisCode) || !sms.equals(redisCode)){ if (StringUtil.isBlank(redisCode) || !sms.equals(redisCode)) {
Map map=new HashMap(); Map map = new HashMap();
map.put("code","1003"); map.put("code", "1003");
map.put("msg","验证码错误"); map.put("msg", "验证码错误");
return map; return map;
} }
redisUtils.delete(key); redisUtils.delete(key);
//判断phone是否符合输入类型 //判断phone是否符合输入类型
if(!userId.matches(Constants.RegConstant.PHONE_REGSTR)){ if (!userId.matches(Constants.RegConstant.PHONE_REGSTR)) {
Map map=new HashMap(); Map map = new HashMap();
map.put("code","1008"); map.put("code", "1008");
map.put("msg","手机号码格式不正确"); map.put("msg", "手机号码格式不正确");
return map; return map;
} }
return usersService.userByZx(userId,beInvitedCode); return usersService.userByZx(userId, beInvitedCode);
} }
/** /**
* 退出登录 * 退出登录
*
* @param request * @param request
* @return * @return
*/ */
@ApiOperation(value = "退出登录", produces = "application/json", notes = "退出登录") @ApiOperation(value = "退出登录", produces = "application/json", notes = "退出登录")
@ApiImplicitParam(paramType = "header",name = "token", value = "用户token", required = true, dataType = "String") @ApiImplicitParam(paramType = "header", name = "token", value = "用户token", required = true, dataType = "String")
@PostMapping("/logout") @PostMapping("/logout")
public Result logout(HttpServletRequest request) { public Result logout(HttpServletRequest request) {
log.info("退出登录"); log.info("退出登录");
Result result = ResultUtils.returnFail(); Result result = ResultUtils.returnFail();
String token = request.getHeader("token"); String token = request.getHeader("token");
String key = RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.PREFIX_TOKEN, token); String key = RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.PREFIX_TOKEN, token);
if(redisUtils.getValue(key) == null){ if (redisUtils.getValue(key) == null) {
log.info("要退出登录的用户未登录"); log.info("要退出登录的用户未登录");
return ResultUtils.returnResult(ResultEnum.FILE_NOT_LOGIN); return ResultUtils.returnResult(ResultEnum.FILE_NOT_LOGIN);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment