Browse Source

手机号绑定,需合并时,返回手机号

tall3
zhizhi wu 4 years ago
parent
commit
7c82e7b975
  1. 59
      signin/src/main/java/com/ccsens/signin/api/UserController.java
  2. 4
      signin/src/main/java/com/ccsens/signin/service/IUserService.java
  3. 32
      signin/src/main/java/com/ccsens/signin/service/UserService.java

59
signin/src/main/java/com/ccsens/signin/api/UserController.java

@ -4,16 +4,16 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil; import cn.hutool.extra.servlet.ServletUtil;
import com.alibaba.fastjson.JSONObject;
import com.ccsens.signin.bean.dto.UserDto; import com.ccsens.signin.bean.dto.UserDto;
import com.ccsens.signin.bean.po.SysUser; import com.ccsens.signin.bean.po.SysUser;
import com.ccsens.signin.bean.vo.UserVo; import com.ccsens.signin.bean.vo.UserVo;
import com.ccsens.signin.exception.UserLoginException; import com.ccsens.signin.exception.UserLoginException;
import com.ccsens.signin.service.IUserService; import com.ccsens.signin.service.IUserService;
import com.ccsens.util.CodeEnum; import com.ccsens.util.*;
import com.ccsens.util.JsonResponse;
import com.ccsens.util.JwtUtil;
import com.ccsens.util.WebConstant;
import com.ccsens.util.exception.BaseException; import com.ccsens.util.exception.BaseException;
import com.ccsens.wechatutil.bean.po.WxPhoneDecryptInfo;
import com.ccsens.wechatutil.wxmini.MiniEncryptionAndDecryptionUtil;
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.SignatureException; import io.jsonwebtoken.SignatureException;
@ -23,6 +23,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.util.Map; import java.util.Map;
@ -32,8 +33,10 @@ import java.util.Map;
@RestController @RestController
@RequestMapping("/users") @RequestMapping("/users")
public class UserController { public class UserController {
@Autowired @Resource
private IUserService userService; private IUserService userService;
@Resource
private RedisUtil redisUtil;
@ApiOperation(value = "/用户登录", notes = "") @ApiOperation(value = "/用户登录", notes = "")
@ -224,20 +227,52 @@ public class UserController {
public JsonResponse<UserVo.TokenBean> bindingPhoneNoCode(HttpServletRequest request, public JsonResponse<UserVo.TokenBean> bindingPhoneNoCode(HttpServletRequest request,
@ApiParam @RequestBody UserDto.WxBindingPhone2 wxPhone) throws Exception { @ApiParam @RequestBody UserDto.WxBindingPhone2 wxPhone) throws Exception {
Long currentUserId = Long.valueOf(((Claims) request.getAttribute(WebConstant.REQUEST_KEY_CLAIMS)).getSubject()); Long currentUserId = Long.valueOf(((Claims) request.getAttribute(WebConstant.REQUEST_KEY_CLAIMS)).getSubject());
// 解密
Object sessionKey = redisUtil.get(StrUtil.format(WebConstant.Wx.SESSION_KEY, currentUserId, wxPhone.getMiniType()));
if (ObjectUtil.isNull(sessionKey)) {
return JsonResponse.newInstance().ok(CodeEnum.PARAM_ERROR);
}
String decryption = MiniEncryptionAndDecryptionUtil.decryption(wxPhone.getEncryptedData(), (String) sessionKey, wxPhone.getIv());
if (StrUtil.isEmpty(decryption)) {
return JsonResponse.newInstance().ok(CodeEnum.DATA_DECRYPTION);
}
WxPhoneDecryptInfo wxPhoneDecryptInfo = JSONObject.parseObject(decryption, WxPhoneDecryptInfo.class);
String phone = wxPhoneDecryptInfo.getPhoneNumber();
if (StrUtil.isEmpty(phone)) {
return JsonResponse.newInstance().ok(CodeEnum.WBS_NOT_PHONE);
}
UserVo.UserSign userSignVo = null;
try {
userSignVo = userService.bindingPhoneNoCode(currentUserId,phone);
if (ObjectUtil.isNull(userSignVo)) {
UserVo.UserSign userSignVo = userService.bindingPhoneNoCode(currentUserId,wxPhone); return JsonResponse.newInstance().ok();
}
UserVo.TokenBean tokenBean = null;
if (ObjectUtil.isNotNull(userSignVo)) {
WebConstant.CLIENT_TYPE clientType = WebConstant.CLIENT_TYPE.valueOf(1); WebConstant.CLIENT_TYPE clientType = WebConstant.CLIENT_TYPE.valueOf(1);
WebConstant.IDENTIFY_TYPE identifyType = WebConstant.IDENTIFY_TYPE.valueOf(3); WebConstant.IDENTIFY_TYPE identifyType = WebConstant.IDENTIFY_TYPE.valueOf(3);
Map<String, Object> theMap = CollectionUtil.newHashMap(); Map<String, Object> theMap = CollectionUtil.newHashMap();
theMap.put("authId", String.valueOf(userSignVo.getAuthId())); theMap.put("authId", String.valueOf(userSignVo.getAuthId()));
tokenBean = userService.getUserInfoAndToken(clientType,identifyType, userSignVo, theMap); UserVo.TokenBean tokenBean = userService.getUserInfoAndToken(clientType,identifyType, userSignVo, theMap);
}
return JsonResponse.newInstance().ok(tokenBean); return JsonResponse.newInstance().ok(tokenBean);
} catch (BaseException e) {
if (e.getCode().intValue() == CodeEnum.MERGE_WX_PHONE.getCode().intValue()) {
log.info("绑定手机号,需要合并:{},{}", phone, currentUserId);
JsonResponse ok = JsonResponse.newInstance();
ok.setMsg(e.getMessage());
UserVo.TokenBean tokenBean = new UserVo.TokenBean();
tokenBean.setPhone(phone);
ok.setData(tokenBean);
return ok;
}
log.info("条件不符合要求,code:{}, message:{}", e.getCode(), e.getMessage());
return JsonResponse.newInstance().ok();
} catch (Exception e) {
log.error("微信绑定手机号发生异常:", e);
return JsonResponse.newInstance().ok();
}
} }

4
signin/src/main/java/com/ccsens/signin/service/IUserService.java

@ -105,8 +105,8 @@ public interface IUserService {
/** /**
* 绑定手机号不用验证码 * 绑定手机号不用验证码
* @param currentUserId userId * @param currentUserId userId
* @param wxPhone 加密数据 * @param phone 手机号
* @return * @return
*/ */
UserVo.UserSign bindingPhoneNoCode(Long currentUserId, UserDto.WxBindingPhone2 wxPhone); UserVo.UserSign bindingPhoneNoCode(Long currentUserId, String phone);
} }

32
signin/src/main/java/com/ccsens/signin/service/UserService.java

@ -377,11 +377,11 @@ public class UserService implements IUserService {
if (ObjectUtil.isNull(openId)) { if (ObjectUtil.isNull(openId)) {
return null; return null;
} }
String key = openId + TallConstant.LOGIN + identifyType; // String key = openId + TallConstant.LOGIN + identifyType;
Object o = redisUtil.get(key); // Object o = redisUtil.get(key);
if (ObjectUtil.isNotNull(o)) { // if (ObjectUtil.isNotNull(o)) {
return (UserVo.UserSign) o; // return (UserVo.UserSign) o;
} // }
SysAuthExample authExample = new SysAuthExample(); SysAuthExample authExample = new SysAuthExample();
authExample.createCriteria().andIdentifyTypeEqualTo(identifyType).andIdentifierEqualTo(openId); authExample.createCriteria().andIdentifyTypeEqualTo(identifyType).andIdentifierEqualTo(openId);
@ -448,7 +448,7 @@ public class UserService implements IUserService {
userSignVo.setAuthId(theAuth.getId()); userSignVo.setAuthId(theAuth.getId());
//存入redis //存入redis
redisUtil.set(key, userSignVo, TallConstant.LOGIN_TIME); // redisUtil.set(key, userSignVo, TallConstant.LOGIN_TIME);
long end2 = System.currentTimeMillis(); long end2 = System.currentTimeMillis();
log.info("方法结束总耗时:{}", end2 - end1); log.info("方法结束总耗时:{}", end2 - end1);
return userSignVo; return userSignVo;
@ -826,11 +826,11 @@ public class UserService implements IUserService {
* 绑定手机号不用验证码 * 绑定手机号不用验证码
* *
* @param currentUserId userId * @param currentUserId userId
* @param wxPhone 手机号加密 * @param phone 手机号
* @return 用户id和认证类型 * @return 用户id和认证类型
*/ */
@Override @Override
public UserVo.UserSign bindingPhoneNoCode(Long currentUserId, UserDto.WxBindingPhone2 wxPhone) { public UserVo.UserSign bindingPhoneNoCode(Long currentUserId, String phone) {
UserVo.UserSign userSignVo; UserVo.UserSign userSignVo;
//查找该用户以前绑定的手机 //查找该用户以前绑定的手机
SysAuthExample authExample = new SysAuthExample(); SysAuthExample authExample = new SysAuthExample();
@ -840,20 +840,7 @@ public class UserService implements IUserService {
if (CollectionUtil.isNotEmpty(authList)) { if (CollectionUtil.isNotEmpty(authList)) {
throw new BaseException(CodeEnum.ALREADY_BINDING_PHONE); throw new BaseException(CodeEnum.ALREADY_BINDING_PHONE);
} else { } else {
// 解密
Object sessionKey = redisUtil.get(StrUtil.format(WebConstant.Wx.SESSION_KEY, currentUserId, wxPhone.getMiniType()));
if (ObjectUtil.isNull(sessionKey)) {
throw new BaseException(CodeEnum.PARAM_ERROR);
}
String decryption = MiniEncryptionAndDecryptionUtil.decryption(wxPhone.getEncryptedData(), (String) sessionKey, wxPhone.getIv());
if (StrUtil.isEmpty(decryption)) {
throw new BaseException(CodeEnum.DATA_DECRYPTION);
}
WxPhoneDecryptInfo wxPhoneDecryptInfo = JSONObject.parseObject(decryption, WxPhoneDecryptInfo.class);
String phone = wxPhoneDecryptInfo.getPhoneNumber();
if (StrUtil.isEmpty(phone)) {
throw new BaseException(CodeEnum.WBS_NOT_PHONE);
}
//改手机对应账户,如果有,提示 //改手机对应账户,如果有,提示
List<SysAuth> phoneList; List<SysAuth> phoneList;
SysAuthExample phoneExample = new SysAuthExample(); SysAuthExample phoneExample = new SysAuthExample();
@ -1127,6 +1114,7 @@ public class UserService implements IUserService {
} }
//将其他登录方式的userId替换为新userId //将其他登录方式的userId替换为新userId
userDao.replaceAuth(currentUserId, userId); userDao.replaceAuth(currentUserId, userId);
//将用户项目关联表里的userId替换为新的 //将用户项目关联表里的userId替换为新的
userDao.replaceUserProject(currentUserId, userId); userDao.replaceUserProject(currentUserId, userId);
//TODO 调用业务系统替换所有的userId //TODO 调用业务系统替换所有的userId

Loading…
Cancel
Save