Commit 138beb1d authored by 技术部-任文超's avatar 技术部-任文超

测试微信绑定功能新接口,20180330-wechat-binding

parent 3b5e384f
...@@ -1127,15 +1127,16 @@ public class InnerController implements IBaseController { ...@@ -1127,15 +1127,16 @@ public class InnerController implements IBaseController {
} }
String str = AESUtil.decryptAfterBase64Decode(content, Constants.AES_KEY); String str = AESUtil.decryptAfterBase64Decode(content, Constants.AES_KEY);
if (null == str || !str.equals(phoneNo)) { if (null == str || !str.equals(phoneNo)) {
LOGGER.info("[forbiddenUserOrNot][禁用或激活用户]:解密有误, phoneNo:{},aes:{}", phoneNo, AESUtil.encryptAndBase64Encode(phoneNo, Constants.AES_KEY));
return JsonResult.buildErrorStateResult("解密有误", null); return JsonResult.buildErrorStateResult("解密有误", null);
} }
if (!ValidationUtil.validatePhoneNo(phoneNo)) { if (!ValidationUtil.validatePhoneNo(phoneNo)) {
LOGGER.info("用户手机号错误, phoneNo:{}", phoneNo); LOGGER.info("[forbiddenUserOrNot][禁用或激活用户]:用户手机号错误, phoneNo:{}", phoneNo);
return JsonResult.buildErrorStateResult("用户手机号错误", null); return JsonResult.buildErrorStateResult("用户手机号错误", null);
} }
int affectedRows = userService.forbiddenUser(enable, phoneNo); int affectedRows = userService.forbiddenUser(enable, phoneNo);
LOGGER.info("禁用或者重启用户的理由,reason:{},手机号phoneNo:{},受影响的行数affectedRows:{}", reason, phoneNo, affectedRows); LOGGER.info("[forbiddenUserOrNot][禁用或激活用户]:理由:reason:{},手机号phoneNo:{},受影响的行数affectedRows:{}", reason, phoneNo, affectedRows);
return JsonResult.buildSuccessResult("用户禁用或重启成功成功", affectedRows); return JsonResult.buildSuccessResult("用户禁用或重启成功成功", affectedRows);
} }
......
...@@ -510,11 +510,13 @@ public class UserController implements IBaseController { ...@@ -510,11 +510,13 @@ public class UserController implements IBaseController {
/* /*
* 未绑定信用钱包用户 * 未绑定信用钱包用户
*/ */
userInfo.setUserId(userId);
userInfo.setPhoneNo(user.getPhoneNo());
try { try {
userInfo = wechatService.saveWechatUserInfo(userInfo); int rows = wechatService.bindingUser(userId, user.getPhoneNo(), openId);
LOGGER.info("微信关联成功:user:[{}],openId:[{}],wechatUserInfo:[{}]",user,openId,userInfo); if(rows > 0){
LOGGER.info("微信关联成功:rows:{}, user:[{}],openId:[{}],wechatUserInfo:[{}]",rows,user,openId,userInfo);
}else {
LOGGER.warn("微信关联失败,保存遇到问题:rows:{}, user:[{}],openId:[{}],wechatUserInfo:[{}]",rows,user,openId,userInfo);
}
} catch (Exception ex) { } catch (Exception ex) {
// 不做绑定 // 不做绑定
LOGGER.warn("微信关联失败,保存遇到问题:user:[{}],openId:[{}],wechatUserInfo:[{}]",user,openId,userInfo, ex); LOGGER.warn("微信关联失败,保存遇到问题:user:[{}],openId:[{}],wechatUserInfo:[{}]",user,openId,userInfo, ex);
......
...@@ -4,6 +4,7 @@ package cn.quantgroup.xyqb.repository; ...@@ -4,6 +4,7 @@ package cn.quantgroup.xyqb.repository;
import cn.quantgroup.xyqb.entity.WechatUserInfo; import cn.quantgroup.xyqb.entity.WechatUserInfo;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
/** /**
...@@ -21,4 +22,9 @@ public interface IWeChatUserRepository extends JpaRepository<WechatUserInfo, Lon ...@@ -21,4 +22,9 @@ public interface IWeChatUserRepository extends JpaRepository<WechatUserInfo, Lon
@Transactional @Transactional
@Modifying @Modifying
int removeByUserId(Long userId); int removeByUserId(Long userId);
@Transactional
@Modifying
@Query(value = "update wechat_userinfo set user_id=?1,phone_no=?2 where open_id=?3 and user_id is null", nativeQuery = true)
int bindingUser(Long userId, String phoneNo, String openId);
} }
...@@ -17,6 +17,8 @@ public interface IWechatService { ...@@ -17,6 +17,8 @@ public interface IWechatService {
WechatUserInfo saveWechatUserInfo(WechatUserInfo userInfo); WechatUserInfo saveWechatUserInfo(WechatUserInfo userInfo);
int bindingUser(Long userId, String phoneNo, String openId);
WechatUserInfo queryByUserId(Long userId); WechatUserInfo queryByUserId(Long userId);
int forbiddenUserWeChat(Long userId); int forbiddenUserWeChat(Long userId);
......
...@@ -21,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional; ...@@ -21,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
...@@ -123,7 +124,7 @@ public class WechatServiceImpl implements IWechatService { ...@@ -123,7 +124,7 @@ public class WechatServiceImpl implements IWechatService {
} }
long count = weChatUserRepository.countByOpenId(userInfo.getOpenId()); long count = weChatUserRepository.countByOpenId(userInfo.getOpenId());
if(count > 0){ if(count > 0){
//注意,这里会抛异常(5000/total),Controller中已捕获处理 //注意,这里会抛异常(5000/total),WeChatController中已捕获处理
return weChatUserRepository.findByOpenId(userInfo.getOpenId()); return weChatUserRepository.findByOpenId(userInfo.getOpenId());
} }
if (null == userInfo.getPhoneNo()) { if (null == userInfo.getPhoneNo()) {
...@@ -132,6 +133,16 @@ public class WechatServiceImpl implements IWechatService { ...@@ -132,6 +133,16 @@ public class WechatServiceImpl implements IWechatService {
return weChatUserRepository.save(userInfo); return weChatUserRepository.save(userInfo);
} }
@Override
@CacheEvict(value = "WechatUserInfo", key = "'openId:' + #openId", cacheManager = "cacheManager")
@Transactional(rollbackFor = Exception.class)
public int bindingUser(Long userId, String phoneNo, String openId) {
if(Objects.isNull(userId) || Objects.isNull(openId)){
return 0;
}
return weChatUserRepository.bindingUser(userId, Optional.ofNullable(phoneNo).orElse(""), openId);
}
@Override @Override
public WechatUserInfo findWechatUserInfoByPhoneNo(String phoneNo) { public WechatUserInfo findWechatUserInfoByPhoneNo(String phoneNo) {
return weChatUserRepository.findByPhoneNo(phoneNo); return weChatUserRepository.findByPhoneNo(phoneNo);
......
package cn.quantgroup.xyqb.util; package cn.quantgroup.xyqb.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
...@@ -10,13 +13,13 @@ import javax.crypto.spec.SecretKeySpec; ...@@ -10,13 +13,13 @@ import javax.crypto.spec.SecretKeySpec;
* 使用AES加密解密 AES-128-ECB加密 * 使用AES加密解密 AES-128-ECB加密
* @version 1.0 * @version 1.0
*/ */
@Slf4j
public class AESUtil { public class AESUtil {
/** /**
* KEY算法 * KEY算法
*/ */
private static final String KEY_ALGORITHM = "AES"; private static final String KEY_ALGORITHM = "AES";
/** /**
* 加密算法 * 加密算法
* "/算法/模式/补码方式" * "/算法/模式/补码方式"
...@@ -41,7 +44,7 @@ public class AESUtil { ...@@ -41,7 +44,7 @@ public class AESUtil {
return new String(bytes, DATA_ENCODING); return new String(bytes, DATA_ENCODING);
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error("AES-解密出错:data:{},key:{}", data, key, e);
} }
return null; return null;
} }
...@@ -52,7 +55,7 @@ public class AESUtil { ...@@ -52,7 +55,7 @@ public class AESUtil {
* @param key * @param key
* @return * @return
*/ */
public static byte[] decryptAfterBase64DecodeToByte(String data, String key) { private static byte[] decryptAfterBase64DecodeToByte(String data, String key) {
try { try {
if (key == null || "".equals(key.trim().length())) { if (key == null || "".equals(key.trim().length())) {
return null; return null;
...@@ -65,9 +68,60 @@ public class AESUtil { ...@@ -65,9 +68,60 @@ public class AESUtil {
byte[] original = cipher.doFinal(encrypted1); byte[] original = cipher.doFinal(encrypted1);
return original; return original;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error("AES-解密出错:data:{},key:{}", data, key, e);
}
return null;
}
/**
* 数据加密
*
* @param data 待加密的数据
* @param key 加密使用的KEY
* @return 加密之后的数据
*/
public static String encryptAndBase64Encode(String data, String key) {
try {
return encryptAndBase64Encode(data.getBytes(DATA_ENCODING), key);
} catch (Exception e) {
log.error("AES-加密出错:data:{},key:{}", data, key, e);
return null; return null;
} }
} }
/**
* 对字节数组加密
* @param data
* @param key
* @return
*/
private static String encryptAndBase64Encode(byte[] data, String key) {
try {
if (StringUtils.isBlank(key)) {
log.error("AES-加密出错:AES Key为空:data:{},key:{}", data, key);
return null;
}
return doEncryptAndBase64Encode(data, key);
} catch (Exception ex) {
log.error("AES-加密出错:data:{},key:{}", data, key);
return null;
}
}
/**
* 执行加密和base64编码
* @param data
* @param key
* @return
* @throws Exception
*/
private static String doEncryptAndBase64Encode(byte[] data, String key) throws Exception {
byte[] raw = key.getBytes(DATA_ENCODING);
SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(data);
return new Base64().encodeToString(encrypted);
}
} }
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