Commit d3956a85 authored by minminyan's avatar minminyan

禁用用户的同时删除相应的缓存

parent 8aa4d2be
......@@ -4,6 +4,7 @@ import cn.quantgroup.xyqb.entity.*;
import cn.quantgroup.xyqb.entity.enumerate.*;
import cn.quantgroup.xyqb.model.*;
import cn.quantgroup.xyqb.service.auth.IIdCardService;
import cn.quantgroup.xyqb.service.session.ISessionService;
import cn.quantgroup.xyqb.service.user.*;
import cn.quantgroup.xyqb.service.wechat.IWechatService;
import com.alibaba.fastjson.JSONObject;
......@@ -44,7 +45,8 @@ public class InnerController {
private IAddressService addressService;
@Autowired
private IWechatService wechatService;
@Autowired
private ISessionService sessionService;
@RequestMapping("/user/search/phoneNo")
public JsonResult findByPhoneNo(String phoneNo) {
......@@ -71,19 +73,19 @@ public class InnerController {
String phoneNo, Long registeredFrom, Long createdAt, Long updatedAt,
String password, String uuid) {
//参数验证
if(StringUtils.isBlank(phoneNo)){
if (StringUtils.isBlank(phoneNo)) {
return JsonResult.buildErrorStateResult("用户手机号不能为空.", null);
}
if(registeredFrom == null){
if (registeredFrom == null) {
registeredFrom = 0L;
}
if(StringUtils.isBlank(password)){
if (StringUtils.isBlank(password)) {
password = "";
}
if(StringUtils.isBlank(uuid)){
if (StringUtils.isBlank(uuid)) {
return JsonResult.buildErrorStateResult("用户uuid为空.", null);
}
if(createdAt == 0L || updatedAt == 0L){
if (createdAt == 0L || updatedAt == 0L) {
createdAt = System.currentTimeMillis();
updatedAt = System.currentTimeMillis();
}
......@@ -100,7 +102,7 @@ public class InnerController {
user.setPassword(password);
user = userService.saveUser(user);
UserRet userRet = null;
if(user != null){
if (user != null) {
userRet = UserRet.getUserRet(user);
}
return JsonResult.buildSuccessResult(null, userRet);
......@@ -108,6 +110,7 @@ public class InnerController {
/**
* 保存用户详细信息
*
* @param userId
* @param phoneNo
* @param name
......@@ -117,22 +120,22 @@ public class InnerController {
*/
@RequestMapping("/user_detail/save")
public JsonResult saveUserDetail(Long userId, String phoneNo, String name, String idNo,
String email, Long id){
String email, Long id) {
//参数验证
if(userId == null || userId == 0L){
if (userId == null || userId == 0L) {
return JsonResult.buildErrorStateResult("用户id为空.", null);
}
if(StringUtils.isBlank(phoneNo)){
if (StringUtils.isBlank(phoneNo)) {
return JsonResult.buildErrorStateResult("用户手机号为空.", null);
}
if(StringUtils.isBlank(name)){
if (StringUtils.isBlank(name)) {
return JsonResult.buildErrorStateResult("用户名为空.", null);
}
if(StringUtils.isBlank(idNo)){
if (StringUtils.isBlank(idNo)) {
return JsonResult.buildErrorStateResult("用户身份证为空.", null);
}
UserDetail userDetail = new UserDetail();
if(id != null && id > 0){
if (id != null && id > 0) {
userDetail.setId(id);
}
userDetail.setUserId(userId);
......@@ -151,47 +154,48 @@ public class InnerController {
}
userDetail.setEmail(email);
userDetail = userDetailService.saveUserDetail(userDetail);
if(userDetail != null){
if (userDetail != null) {
return JsonResult.buildSuccessResult(null, UserDetailRet.getUserDetail(userDetail));
}
return JsonResult.buildErrorStateResult("",null);
return JsonResult.buildErrorStateResult("", null);
}
/**
* 根据用户id查询用户的详细信息
*
* @param userId
* @return
*/
@RequestMapping("/user_detail/search/userId")
public JsonResult findUserDetailByUserId(Long userId){
public JsonResult findUserDetailByUserId(Long userId) {
UserDetail userDetail = userDetailService.findByUserId(userId);
if(userDetail != null){
if (userDetail != null) {
return JsonResult.buildSuccessResult(null, UserDetailRet.getUserDetail(userDetail));
}
return JsonResult.buildErrorStateResult("", null);
}
@RequestMapping("/user/search/userId")
public JsonResult findUserByUserId(Long userId){
public JsonResult findUserByUserId(Long userId) {
User user = userService.findById(userId);
if(user != null){
if (user != null) {
return JsonResult.buildSuccessResult(null, UserRet.getUserRet(user));
}
return JsonResult.buildErrorStateResult("", null);
}
@RequestMapping("/user_detail/search/phone")
public JsonResult findUserDetailByPhone(String phoneNo){
public JsonResult findUserDetailByPhone(String phoneNo) {
UserDetail userDetail = userDetailService.findByPhoneNo(phoneNo);
if(userDetail != null){
if (userDetail != null) {
return JsonResult.buildSuccessResult(null, UserDetailRet.getUserDetail(userDetail));
}
return JsonResult.buildErrorStateResult("", null);
}
@RequestMapping("/user_detail/update/qq")
public JsonResult updateUserQQ(String qq, Long userId){
if(StringUtils.isEmpty(qq) || userId == null || userId == 0L){
public JsonResult updateUserQQ(String qq, Long userId) {
if (StringUtils.isEmpty(qq) || userId == null || userId == 0L) {
return JsonResult.buildErrorStateResult("参数校验失败,qq或用户id为空", null);
}
userDetailService.updateUserQQ(userId, qq);
......@@ -204,7 +208,7 @@ public class InnerController {
return JsonResult.buildErrorStateResult(null, null);
}
List<Contact> contacts = contactService.findByUserId(userId);
if(null == contacts || contacts.size() == 0) {
if (null == contacts || contacts.size() == 0) {
return JsonResult.buildErrorStateResult(null, Collections.emptyList());
}
return JsonResult.buildSuccessResult(null, ContactRet.contacts2ContactRets(contacts));
......@@ -220,10 +224,11 @@ public class InnerController {
return JsonResult.buildErrorStateResult(null, null);
}
List<Contact> userContact = contactService.findByUserId(userId);
if(userContact != null && userContact.size() >= 2) {
if (userContact != null && userContact.size() >= 2) {
return JsonResult.buildSuccessResult(null, ContactRet.contacts2ContactRets(userContact));
}
List<Contact> contacts = JSONObject.parseObject(contactsStr, new TypeReference<List<Contact>>() {});
List<Contact> contacts = JSONObject.parseObject(contactsStr, new TypeReference<List<Contact>>() {
});
if (CollectionUtils.isEmpty(contacts)) {
return JsonResult.buildErrorStateResult(null, null);
}
......@@ -342,11 +347,11 @@ public class InnerController {
@RequestMapping("/user/query/openId")
public JsonResult queryOpenIdByUserId(Long userId) {
if(userId == null) {
if (userId == null) {
return JsonResult.buildErrorStateResult("userId不能为空", null);
}
WechatUserInfo wechatUserInfo = wechatService.queryOpenIdByUserId(userId);
if(wechatUserInfo == null) {
if (wechatUserInfo == null) {
return JsonResult.buildErrorStateResult(null, null);
}
return JsonResult.buildSuccessResult("success", wechatUserInfo.getOpenId());
......@@ -354,15 +359,20 @@ public class InnerController {
@RequestMapping("/user/disable")
public JsonResult disableUser(Long userId) {
if(null == userId || 0L == userId) {
if (null == userId || 0L == userId) {
return JsonResult.buildErrorStateResult("userId不能为空", null);
}
User user = userService.findById(userId);
if(null == user) {
if (null == user) {
return JsonResult.buildErrorStateResult("未查询到该用户,用户id:" + userId, null);
}
user.setEnable(false);
user.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
LOGGER.info("注销用户的信息,userId={}", userId);
user = userService.saveUser(user);
if (!user.getEnable()) {
sessionService.deleteByUserId(userId);
}
return JsonResult.buildSuccessResult("用户已禁用.", user.getEnable() == false);
}
......
......@@ -106,6 +106,9 @@ public class UserController implements IBaseController {
JsonResult successResult = validMap.get("success");
String phoneNo = successResult.getData().toString();
User user = userService.findByPhoneWithCache(phoneNo);
if (user != null && !user.getEnable()) {
return JsonResult.buildErrorStateResult("登录失败", null);
}
if (user == null) {
user = registerFastWhenLogin(phoneNo, channelId, createdFrom, appChannel);
if (user == null) {
......@@ -403,7 +406,7 @@ public class UserController implements IBaseController {
String userName = credentialArr[0];
String pass = credentialArr[1];
User user = userService.findByPhoneWithCache(userName);
if (user == null) {
if (user == null || !user.getEnable()) {
return null;
}
//验证密码
......@@ -421,7 +424,7 @@ public class UserController implements IBaseController {
//查询用户,存在则保存用户session信息,userId为uuid
User user = userService.findByUuidInDb(userId);
//用户信息存在,更新session中的最后访问时间,重新写入缓存.
if (null != user) {
if (null != user || !user.getEnable()) {
return new JsonResult(sessionService.createSession(channelId, createdFrom, appChannel, user, merchant));
} else {
return JsonResult.buildErrorStateResult("登录失败", null);
......
......@@ -2,7 +2,6 @@ package cn.quantgroup.xyqb.service.session;
import cn.quantgroup.xyqb.entity.Merchant;
import cn.quantgroup.xyqb.entity.User;
import cn.quantgroup.xyqb.function.OneArgFunction;
import cn.quantgroup.xyqb.model.AuthBean;
import cn.quantgroup.xyqb.model.LoginProperties;
import cn.quantgroup.xyqb.model.session.SessionStruct;
......@@ -21,8 +20,12 @@ public interface ISessionService {
String findSessionIdByUserIdLoginProperties(Long userId, LoginProperties properties);
String findSessionValueBySessionId(String sessionId);
SessionStruct newSession(User user, LoginProperties properties);
void persistSession(String token, SessionValue sessionValue);
SessionStruct findSessionBySessionId(String sessionId);
void deleteByUserId(long userId);
}
......@@ -3,34 +3,34 @@ package cn.quantgroup.xyqb.service.session.impl;
import cn.quantgroup.xyqb.Constants;
import cn.quantgroup.xyqb.entity.Merchant;
import cn.quantgroup.xyqb.entity.User;
import cn.quantgroup.xyqb.function.OneArgFunction;
import cn.quantgroup.xyqb.model.AuthBean;
import cn.quantgroup.xyqb.model.JsonResult;
import cn.quantgroup.xyqb.model.LoginProperties;
import cn.quantgroup.xyqb.model.session.SessionStruct;
import cn.quantgroup.xyqb.model.session.SessionValue;
import cn.quantgroup.xyqb.service.session.ISessionService;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Joiner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
*
* Created by 11 on 2016/12/28.
*/
@Slf4j
@Service
public class SessionServiceImpl implements ISessionService{
public class SessionServiceImpl implements ISessionService {
@Autowired
@Qualifier("stringRedisTemplate")
......@@ -47,7 +47,7 @@ public class SessionServiceImpl implements ISessionService{
//找到用户
String sessionId = findSessionIdByUserIdLoginProperties(user.getId(), properties);
// String sessionId = sessionService.findSessionIdByUserIdAndMerchant(user.getId(), merchant);
if(org.apache.commons.lang.StringUtils.isNotEmpty(sessionId)) {
if (org.apache.commons.lang.StringUtils.isNotEmpty(sessionId)) {
SessionStruct sessionStruct = findSessionBySessionId(sessionId);
sessionStruct.setAttribute("channelId", String.valueOf(channelId));
sessionStruct.setAttribute("createdFrom", String.valueOf(createdFrom));
......@@ -99,13 +99,13 @@ public class SessionServiceImpl implements ISessionService{
}
@Override
public String findSessionValueBySessionId(String sessionId){
public String findSessionValueBySessionId(String sessionId) {
String result = stringRedisTemplate.opsForValue().get(Constants.Session.USER_SESSION_CACHE + sessionId);
return StringUtils.defaultString(result, "");
}
@Override
public SessionStruct newSession(User user, LoginProperties loginProperties){
public SessionStruct newSession(User user, LoginProperties loginProperties) {
Timestamp now = new Timestamp(System.currentTimeMillis());
SessionStruct sessionStruct = new SessionStruct();
SessionValue sessionValue = new SessionValue();
......@@ -134,7 +134,7 @@ public class SessionServiceImpl implements ISessionService{
@Override
public SessionStruct findSessionBySessionId(String sessionId) {
String sessionValue = findSessionValueBySessionId(sessionId);
if(StringUtils.isEmpty(sessionValue)) {
if (StringUtils.isEmpty(sessionValue)) {
return null;
}
try {
......@@ -152,5 +152,15 @@ public class SessionServiceImpl implements ISessionService{
}
@Override
public void deleteByUserId(long userId) {
String pattern = Constants.Session.USER_SESSION_ID_CACHE + ":" + String.valueOf(userId) + ":*";
Set<String> keys = stringRedisTemplate.keys(pattern);
if (!CollectionUtils.isEmpty(keys)) {
log.info("删除用户userId={}的缓存信息,个数:{},keys={}", userId,
keys.size(),
Joiner.on(",").join(keys));
}
stringRedisTemplate.delete(keys);
}
}
......@@ -47,6 +47,7 @@ public class UserServiceImpl implements IUserService {
}
@Override
@CacheEvict(value = "usercache", key = "'xyqbuser' + #user.phoneNo", cacheManager = "cacheManager")
public User saveUser(User user) {
return userRepository.save(user);
}
......@@ -101,7 +102,7 @@ public class UserServiceImpl implements IUserService {
user.setUuid(uuid);
user.setPassword(PasswordUtil.MD5(password.toLowerCase() + Constants.PASSWORD_SALT));
//解决线上白条registerFrom为1的问题
if(channelId == 222L) {
if (channelId == 222L) {
user.setRegisteredFrom(channelId);
} else {
user.setRegisteredFrom(registerFrom);
......@@ -120,10 +121,11 @@ public class UserServiceImpl implements IUserService {
/**
* 修改用户密码
* @date 2017-02-15 修改用户修改密码时,更新updatedAt时间
*
* @param phoneNo
* @param password
* @return
* @date 2017-02-15 修改用户修改密码时,更新updatedAt时间
*/
@Override
@CacheEvict(value = "usercache", key = "'xyqbuser' + #phone", cacheManager = "cacheManager")
......
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