Commit 5473549d authored by m1991's avatar m1991

登录——新加退出功能

parent 5ba2c6f8
......@@ -85,4 +85,24 @@ public class Constants {
public static String TOKEN_PRIFIX="token";
public static String BANK_PRIFIX="bank";
}
/**
* redis常量
*/
public static class Redis{
/**
* 项目公共 前缀
*/
public final static String PREFIX = "xts";
/**
* 短信相关
*/
public final static String PREFIX_SMS = "sms:";
/**
* token相关
*/
public final static String PREFIX_TOKEN = "token:";
}
}
package cn.wisenergy.common.utils;
/**
* redis key工具类
* m1991
*/
public class RedisKeyUtils {
/**
* 根据出入的参数创建一个Redis key
* @return 如果参数为空,那么返回null
*/
public static String formatKeys(String ... args){
if (args != null && args.length > 0){
StringBuilder key = new StringBuilder();
for (String s: args){
key.append(s).append(Constants.Connnector.UNDERLINE);
}
return key.toString();
}
return null;
}
/**
* 根据出入的参数创建一个Redis key,自动拼接前缀
* @return 如果参数为空,那么返回null
*/
public static String formatKeyWithPrefix(String ... args){
if (args != null && args.length > 0){
StringBuilder key = new StringBuilder(Constants.Redis.PREFIX).append(Constants.Connnector.UNDERLINE);
for (String s: args){
key.append(s).append(Constants.Connnector.UNDERLINE);
}
return key.toString();
}
return null;
}
}
\ No newline at end of file
This diff is collapsed.
......@@ -47,6 +47,12 @@ public interface UserService {
*/
Map userByZx(String userId, String beInvitedCode);
/**
* 用户登出
* @param token
* @return
*/
int logout(String token);
Integer getUserIdById(String userId);
......
package cn.wisenergy.service.app.impl;
import cn.wisenergy.common.utils.R;
import cn.wisenergy.common.utils.RedisUtils;
import cn.wisenergy.common.utils.ResultUtils;
import cn.wisenergy.common.utils.ShareCodeUtil;
import cn.wisenergy.common.utils.*;
import cn.wisenergy.mapper.RecommendUserMapper;
import cn.wisenergy.mapper.TeamUserInfoMapper;
import cn.wisenergy.mapper.UsersMapper;
......@@ -42,6 +39,9 @@ public class UserServiceImpl extends ServiceImpl<UsersMapper, User> implements U
@Autowired
private TeamUserInfoMapper teamUserInfoMapper;
@Autowired
private RedisUtils redisUtils;
@Override
public User getById(String userId) {
return usersMapper.getByUserId(userId);
......@@ -284,4 +284,19 @@ public class UserServiceImpl extends ServiceImpl<UsersMapper, User> implements U
teamgg(beInvitedCode);
return R.ok("团队表普通用户数量+1成功!", 0);
}
/**
* 用户登出
* @param token
* @return
*/
@Override
public int logout(String token) {
int succ = 0;
String key = RedisKeyUtils.formatKeyWithPrefix(token);
redisUtils.delete(key);
if(redisUtils.getValue(key) == null){
succ = 1;
}
return succ;
}
}
......@@ -2,6 +2,7 @@ package cn.wisenergy.web.admin.controller.app;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import cn.wisenergy.common.enums.ResultEnum;
import cn.wisenergy.common.utils.*;
import cn.wisenergy.model.app.User;
import cn.wisenergy.model.app.UsersDto;
......@@ -43,6 +44,9 @@ import java.util.Map;
@RequestMapping("api/user")
@RestController
public class LoginController {
@Autowired
private RedisUtils redisUtils;
......@@ -183,4 +187,29 @@ public class LoginController {
return usersService.userByZx(userId,beInvitedCode);
}
/**
* 退出登录
* @param request
* @return
*/
@ApiOperation(value = "退出登录", produces = "application/json", notes = "退出登录")
@ApiImplicitParam(paramType = "header",name = "token", value = "用户token", required = true, dataType = "String")
@PostMapping("/logout")
public Result logout(HttpServletRequest request) {
log.info("退出登录");
Result result = ResultUtils.returnFail();
String token = request.getHeader("token");
String key = RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.PREFIX_TOKEN, token);
if(redisUtils.getValue(key) == null){
log.info("要退出登录的用户未登录");
return ResultUtils.returnResult(ResultEnum.FILE_NOT_LOGIN);
}
int succ = usersService.logout(token);
if (succ > 0) {
result = ResultUtils.returnSuccess();
}
return result;
}
}
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