Commit dca45e7f authored by lee_mingzhu's avatar lee_mingzhu

remove phone_no column

parent 3f221ecc
...@@ -77,15 +77,15 @@ public class UserController implements IBaseController { ...@@ -77,15 +77,15 @@ public class UserController implements IBaseController {
public JsonResult login( public JsonResult login(
@RequestParam(required = false, defaultValue = "1") Long channelId, String appChannel, @RequestParam(required = false, defaultValue = "1") Long channelId, String appChannel,
@RequestParam(required = false, defaultValue = "1") Long createdFrom, @RequestParam(required = false, defaultValue = "1") Long createdFrom,
@RequestParam(required = false, defaultValue = "") String userId, String key, HttpServletRequest request) { @RequestParam(required = false, defaultValue = "") String userId, String key, HttpServletRequest request, String openId) {
Merchant merchant = merchantService.findMerchantByName(key); Merchant merchant = merchantService.findMerchantByName(key);
if (merchant == null) { if (merchant == null) {
return JsonResult.buildErrorStateResult("未知的连接", null); return JsonResult.buildErrorStateResult("未知的连接", null);
} }
if(!StringUtils.isEmpty(userId) && userId.length() > 10) { if (!StringUtils.isEmpty(userId) && userId.length() > 10) {
return loginWithUserId(channelId, appChannel, createdFrom, userId, merchant); return loginWithUserId(channelId, appChannel, createdFrom, userId, merchant);
} else { } else {
return loginWithHttpBasic(channelId, appChannel, createdFrom, merchant, request); return loginWithHttpBasic(channelId, appChannel, createdFrom, merchant, request, openId);
} }
} }
...@@ -97,9 +97,9 @@ public class UserController implements IBaseController { ...@@ -97,9 +97,9 @@ public class UserController implements IBaseController {
@RequestMapping("/login/fast") @RequestMapping("/login/fast")
public JsonResult loginFast( public JsonResult loginFast(
@RequestParam(required = false, defaultValue = "1") Long channelId, String appChannel, @RequestParam(required = false, defaultValue = "1") Long channelId, String appChannel,
@RequestParam(required = false, defaultValue = "1") Long createdFrom, String key, HttpServletRequest request){ @RequestParam(required = false, defaultValue = "1") Long createdFrom, String key, HttpServletRequest request) {
Map<String, JsonResult> validMap = getHeaderParam(request); Map<String, JsonResult> validMap = getHeaderParam(request);
if(null != validMap.get("fail")){ if (null != validMap.get("fail")) {
return validMap.get("fail"); return validMap.get("fail");
} }
Merchant merchant = merchantService.findMerchantByName(key); Merchant merchant = merchantService.findMerchantByName(key);
...@@ -109,7 +109,7 @@ public class UserController implements IBaseController { ...@@ -109,7 +109,7 @@ public class UserController implements IBaseController {
JsonResult successResult = validMap.get("success"); JsonResult successResult = validMap.get("success");
String phoneNo = successResult.getData().toString(); String phoneNo = successResult.getData().toString();
User user = userService.findByPhoneWithCache(phoneNo); User user = userService.findByPhoneWithCache(phoneNo);
if(user == null) { if (user == null) {
user = registerFastWhenLogin(phoneNo, channelId, createdFrom, appChannel); user = registerFastWhenLogin(phoneNo, channelId, createdFrom, appChannel);
if (user == null) { if (user == null) {
throw new UserNotExistException("用户未找到"); throw new UserNotExistException("用户未找到");
...@@ -121,38 +121,39 @@ public class UserController implements IBaseController { ...@@ -121,38 +121,39 @@ public class UserController implements IBaseController {
/** /**
* 快速登录验证 * 快速登录验证
*
* @param request * @param request
* @return * @return
*/ */
private Map<String, JsonResult> getHeaderParam(HttpServletRequest request){ private Map<String, JsonResult> getHeaderParam(HttpServletRequest request) {
Map<String, JsonResult> result = new HashMap<>(); Map<String, JsonResult> result = new HashMap<>();
String verificationHeader = "Verification "; String verificationHeader = "Verification ";
String credential = request.getHeader("authorization"); String credential = request.getHeader("authorization");
if(StringUtils.isBlank(credential)){ if (StringUtils.isBlank(credential)) {
result.put("fail", JsonResult.buildErrorStateResult("登录失败", null)); result.put("fail", JsonResult.buildErrorStateResult("登录失败", null));
} }
if(!credential.startsWith(verificationHeader)){ if (!credential.startsWith(verificationHeader)) {
result.put("fail", JsonResult.buildErrorStateResult("登录失败", null)); result.put("fail", JsonResult.buildErrorStateResult("登录失败", null));
} }
credential = credential.substring(verificationHeader.length(), credential.length()); credential = credential.substring(verificationHeader.length(), credential.length());
byte[] buf = Base64.decodeBase64(credential); byte[] buf = Base64.decodeBase64(credential);
try { try {
credential = new String(buf, "UTF-8"); credential = new String(buf, "UTF-8");
} catch (UnsupportedEncodingException e){ } catch (UnsupportedEncodingException e) {
LOGGER.error("不支持的编码."); LOGGER.error("不支持的编码.");
result.put("fail", JsonResult.buildErrorStateResult("登录失败", null)); result.put("fail", JsonResult.buildErrorStateResult("登录失败", null));
} }
String[] credentialArr = credential.split(":"); String[] credentialArr = credential.split(":");
if(credentialArr.length != 2){ if (credentialArr.length != 2) {
result.put("fail", JsonResult.buildErrorStateResult("登录失败", null)); result.put("fail", JsonResult.buildErrorStateResult("登录失败", null));
} }
String phoneNo = credentialArr[0]; String phoneNo = credentialArr[0];
String verificationCode = credentialArr[1]; String verificationCode = credentialArr[1];
LOGGER.info("用户快速登录,phoneNo:{} , verificationCode:{}", phoneNo, verificationCode); LOGGER.info("用户快速登录,phoneNo:{} , verificationCode:{}", phoneNo, verificationCode);
if(!ValidationUtil.validatePhoneNo(phoneNo)){ if (!ValidationUtil.validatePhoneNo(phoneNo)) {
result.put("fail", JsonResult.buildErrorStateResult("登录失败", null)); result.put("fail", JsonResult.buildErrorStateResult("登录失败", null));
} }
if(!smsService.validateFastLoginVerificationCode(phoneNo, verificationCode)){ if (!smsService.validateFastLoginVerificationCode(phoneNo, verificationCode)) {
LOGGER.info("用户快速登录,验证码校验失败,phoneNo:{} , verificationCode:{}", phoneNo, verificationCode); LOGGER.info("用户快速登录,验证码校验失败,phoneNo:{} , verificationCode:{}", phoneNo, verificationCode);
result.put("fail", JsonResult.buildErrorStateResult("验证码错误", null)); result.put("fail", JsonResult.buildErrorStateResult("验证码错误", null));
} }
...@@ -348,16 +349,16 @@ public class UserController implements IBaseController { ...@@ -348,16 +349,16 @@ public class UserController implements IBaseController {
} }
@RequestMapping("/syncUserInfo") @RequestMapping("/syncUserInfo")
public JsonResult syncUserInfo() { public JsonResult syncUserInfo() {
User user = getCurrentUser(); User user = getCurrentUser();
if (null == user) { if (null == user) {
return JsonResult.buildErrorStateResult(null, null); return JsonResult.buildErrorStateResult(null, null);
} }
UserDetail detail = userDetailService.findByUserId(user.getId()); UserDetail detail = userDetailService.findByUserId(user.getId());
//UserDetail detail = userDetailRepository.findByUserId(user.getId()); //UserDetail detail = userDetailRepository.findByUserId(user.getId());
UserModel userModel = new UserModel(user, detail); UserModel userModel = new UserModel(user, detail);
return JsonResult.buildSuccessResult("token校验成功", userModel); return JsonResult.buildSuccessResult("token校验成功", userModel);
} }
private User registerFastWhenLogin(String phoneNo, Long channelId, Long registerFrom, String appChannel) { private User registerFastWhenLogin(String phoneNo, Long channelId, Long registerFrom, String appChannel) {
...@@ -377,18 +378,22 @@ public class UserController implements IBaseController { ...@@ -377,18 +378,22 @@ public class UserController implements IBaseController {
} }
private JsonResult loginWithHttpBasic( Long channelId, String appChannel, Long createdFrom, Merchant merchant, HttpServletRequest request) { private JsonResult loginWithHttpBasic(Long channelId, String appChannel, Long createdFrom, Merchant merchant, HttpServletRequest request, String openId) {
User user = verificateUserNameAndPassword(request); User user = verificateUserNameAndPassword(request, openId);
if (user == null) { if (user == null) {
return JsonResult.buildErrorStateResult("用户名或密码不正确", null); return JsonResult.buildErrorStateResult("用户名或密码不正确", null);
} else { } else {
//id为-1,微信重复绑定的标示.
if(user.getId() == -1L) {
return JsonResult.buildErrorStateResult("该手机号已绑定了其他微信账号.", null);
}
return new JsonResult(sessionService.createSession(channelId, createdFrom, appChannel, user, merchant)); return new JsonResult(sessionService.createSession(channelId, createdFrom, appChannel, user, merchant));
} }
} }
private User verificateUserNameAndPassword(HttpServletRequest request) { private User verificateUserNameAndPassword(HttpServletRequest request, String openId) {
String credential = request.getHeader("authorization"); String credential = request.getHeader("authorization");
if(!credential.startsWith("Basic ")){ if (!credential.startsWith("Basic ")) {
return null; return null;
} }
credential = credential.substring("Basic ".length(), credential.length()); credential = credential.substring("Basic ".length(), credential.length());
...@@ -396,17 +401,31 @@ public class UserController implements IBaseController { ...@@ -396,17 +401,31 @@ public class UserController implements IBaseController {
String bufStr = ""; String bufStr = "";
try { try {
bufStr = new String(buf, "UTF-8"); bufStr = new String(buf, "UTF-8");
} catch (UnsupportedEncodingException e){ } catch (UnsupportedEncodingException e) {
LOGGER.error("不支持的编码: ", e); LOGGER.error("不支持的编码: ", e);
} }
String[] credentialArr = bufStr.split(":"); String[] credentialArr = bufStr.split(":");
if(credentialArr.length != 2){ if (credentialArr.length != 2) {
return null; return null;
} }
String userName = credentialArr[0]; String userName = credentialArr[0];
String pass = credentialArr[1]; String pass = credentialArr[1];
/*//判断是否微信登录(openId不为空,认为是微信登录)
if(StringUtils.isNotEmpty(openId)) {
WechatUserInfo wechatUserInfo = wechatService.findWechatUserInfoFromDb(openId);
//说明微信用户已经绑定过xyqb用户了
if(wechatUserInfo != null && wechatUserInfo.getUserId() != null && wechatUserInfo.getUserId() != 0L) {
//判断绑定的xyqb用户的手机号是否和本次登录的手机号一致
User xyqbUser = userService.findById(wechatUserInfo.getUserId());
if(!xyqbUser.getPhoneNo().equals(userName)) {
User user = new User();
user.setId(-1L);
return user;
}
}
}*/
User user = userService.findByPhoneWithCache(userName); User user = userService.findByPhoneWithCache(userName);
if(user == null){ if (user == null) {
return null; return null;
} }
//验证密码 //验证密码
...@@ -416,7 +435,7 @@ public class UserController implements IBaseController { ...@@ -416,7 +435,7 @@ public class UserController implements IBaseController {
return user; return user;
} }
private boolean validatePassword(String paramPass, String targetPassword){ private boolean validatePassword(String paramPass, String targetPassword) {
return StringUtils.defaultString(targetPassword, "").equals(PasswordUtil.MD5(paramPass.toLowerCase() + pwdSalt)); return StringUtils.defaultString(targetPassword, "").equals(PasswordUtil.MD5(paramPass.toLowerCase() + pwdSalt));
} }
...@@ -424,7 +443,7 @@ public class UserController implements IBaseController { ...@@ -424,7 +443,7 @@ public class UserController implements IBaseController {
//查询用户,存在则保存用户session信息,userId为uuid //查询用户,存在则保存用户session信息,userId为uuid
User user = userService.findByUuidInDb(userId); User user = userService.findByUuidInDb(userId);
//用户信息存在,更新session中的最后访问时间,重新写入缓存. //用户信息存在,更新session中的最后访问时间,重新写入缓存.
if(null != user) { if (null != user) {
return new JsonResult(sessionService.createSession(channelId, createdFrom, appChannel, user, merchant)); return new JsonResult(sessionService.createSession(channelId, createdFrom, appChannel, user, merchant));
} else { } else {
return JsonResult.buildErrorStateResult("登录失败", null); return JsonResult.buildErrorStateResult("登录失败", null);
...@@ -432,20 +451,22 @@ public class UserController implements IBaseController { ...@@ -432,20 +451,22 @@ public class UserController implements IBaseController {
} }
@RequestMapping("/associate_wechat") @RequestMapping("/associate_wechat")
public JsonResult associateWithWechat(String openId) { public JsonResult associateWithWechat(String openId, String phoneNo) {
LOGGER.info("关联用户:start"); LOGGER.info("关联用户:start");
User user = getCurrentUser(); User user = getCurrentUser();
Long userId = user.getId(); Long userId = user.getId();
LOGGER.info("关联用户:当前登录用户id:" + userId); LOGGER.info("关联用户:当前登录用户id:" + userId);
String phoneNo = user.getPhoneNo(); WechatUserInfo wechatUserInfo = wechatService.findWechatUserInfoByPhoneNo(phoneNo);
if(userService.findByPhoneInDb(phoneNo) != null) { if(wechatUserInfo != null) {
return JsonResult.buildErrorStateResult("该手机号已经关联了其他微信账号.", null); return JsonResult.buildErrorStateResult("该手机号已绑定其他微信号码.", null);
} }
WechatUserInfo userInfo = wechatService.findWechatUserInfoFromDb(openId); WechatUserInfo userInfo = wechatService.findWechatUserInfoFromDb(openId);
//限制微信用户和xyqb用户为一对一的关系
LOGGER.info("关联用户:查询微信用户信息:start"); LOGGER.info("关联用户:查询微信用户信息:start");
if (userInfo != null && userInfo.getUserId() == null) { if (userInfo != null) {
LOGGER.info("关联用户:查询到了微信用户信息."); LOGGER.info("关联用户:查询到了微信用户信息.");
userInfo.setUserId(userId); userInfo.setUserId(userId);
userInfo.setPhoneNo(phoneNo);
wechatService.saveWechatUserInfo(userInfo); wechatService.saveWechatUserInfo(userInfo);
LOGGER.info("关联用户:关联xyqb用户成功."); LOGGER.info("关联用户:关联xyqb用户成功.");
} }
......
...@@ -24,6 +24,8 @@ public class WechatUserInfo implements Serializable{ ...@@ -24,6 +24,8 @@ public class WechatUserInfo implements Serializable{
private Long userId; private Long userId;
@Column(name = "open_id") @Column(name = "open_id")
private String openId; private String openId;
@Column(name = "phone_no")
private String phoneNo;
@Column(name = "nick_name") @Column(name = "nick_name")
private String nickName; private String nickName;
@Column(name = "sex") @Column(name = "sex")
......
...@@ -9,4 +9,5 @@ import org.springframework.data.jpa.repository.JpaRepository; ...@@ -9,4 +9,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
*/ */
public interface IWeChatUserRepository extends JpaRepository<WechatUserInfo, Long> { public interface IWeChatUserRepository extends JpaRepository<WechatUserInfo, Long> {
WechatUserInfo findByOpenId(String openId); WechatUserInfo findByOpenId(String openId);
WechatUserInfo findByPhoneNo(String phoneNo);
} }
...@@ -12,6 +12,7 @@ public interface IWechatService { ...@@ -12,6 +12,7 @@ public interface IWechatService {
WechatUserInfo findWechatUserInfoFromDb(String openId); WechatUserInfo findWechatUserInfoFromDb(String openId);
WechatUserInfo findWechatUserInfoByPhoneNo(String phoneNo);
WechatUserInfo saveWechatUserInfo(WechatUserInfo userInfo); WechatUserInfo saveWechatUserInfo(WechatUserInfo userInfo);
} }
...@@ -115,6 +115,11 @@ public class WechatServiceImpl implements IWechatService { ...@@ -115,6 +115,11 @@ public class WechatServiceImpl implements IWechatService {
return weChatUserRepository.save(userInfo); return weChatUserRepository.save(userInfo);
} }
@Override
public WechatUserInfo findWechatUserInfoByPhoneNo(String phoneNo) {
return weChatUserRepository.findByPhoneNo(phoneNo);
}
private String getTokenFromWechatServer(String code) { private String getTokenFromWechatServer(String code) {
if (StringUtils.isEmpty(code)) { if (StringUtils.isEmpty(code)) {
return null; return null;
......
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