赞
踩
/** * 用户记录步数 controller */ @RestController @RequestMapping("/account_step") public class AccountStepController { @Autowired private AccountStepService accountStepService; /** * 上报微信步数 * @param jsonObject 加密参数 * @return 上报微信步数结果 */ @PostMapping("/add_account_steps") public ResultData<Object> addAccountSteps(@RequestBody JSONObject jsonObject){ String encryptedData = jsonObject.getString("encryptedData"); String iv = jsonObject.getString("iv"); return ResultData.success(accountStepService.addAccountSteps(encryptedData,iv)); } }
/**
* 用户步数记录 service
*/
public interface AccountStepService {
/**
* 上报微信步数
* @param encryptedData 用户信息的加密数据
* @param iv 加密算法的初始向量
* @return 上报微信步数结果
*/
Integer addAccountSteps(String encryptedData, String iv);
}
/** * 用户步数记录 impl */ @Slf4j @Service public class AccountStepServiceImpl implements AccountStepService { @Override public Integer addAccountSteps(String encryptedData, String iv) { //获取当前登录用户 WxydAccountVO currentUserInfo = ThreadLocalUserInfo.getCurrentUserInfo(); //获取微信运动步数 String result = WxUtils.getEncryptedData(encryptedData, iv, currentUserInfo.getSessionKey()); WxRunData wxRunData = JSON.parseObject(result, WxRunData.class); if (ObjectUtil.isEmpty(wxRunData.stepInfoList)) { throw new ServiceException(ResultCodeEnum.BUSINESS_ERROR.getCode(), "获取微信步数失败,请确认是否开启微信运动功能"); } wxRunData.stepInfoList.forEach(stepInfo -> { Date date = new Date(stepInfo.timestamp * 1000L); AccountStepVO accountStep = accountStepMapper.getAccountStep(currentUserInfo.getId(), date); byte type = CommonUtils.judgeStepAbnormal(date, stepInfo.step); if (ObjectUtil.isEmpty(accountStep)) { //不存在此记录则添加 accountStepMapper.addAccountStep(currentUserInfo.getId(), stepInfo.step, date, type); } else { //存在此记录则更新 accountStepMapper.updateAccountStep(accountStep.getAccountId(), stepInfo.step, date); } }); return 1; } }
/** * 微信工具类 */ @Component @Slf4j public class WxUtils { /** * 用户数据的签名验证和加解密 * * @param encryptedData 用户信息的加密数据 * @param iv 加密算法的初始向量 * @param sessionKey sessionKey * @return 解密结果 */ public static String getEncryptedData(String encryptedData, String iv, String sessionKey) { try { String result = ""; // 被加密的数据 byte[] dataByte = Base64.decodeBase64(encryptedData); // 加密秘钥 byte[] keyByte = Base64.decodeBase64(sessionKey); // 偏移量 byte[] ivByte = Base64.decodeBase64(iv); // 如果密钥不足16位,那么就补足. 这个if 中的内容很重要 int base = 16; if (keyByte.length % base != 0) { int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0); byte[] temp = new byte[groups * base]; Arrays.fill(temp, (byte) 0); System.arraycopy(keyByte, 0, temp, 0, keyByte.length); keyByte = temp; } // 初始化 Security.addProvider(new BouncyCastleProvider()); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); SecretKeySpec spec = new SecretKeySpec(keyByte, "AES"); AlgorithmParameters parameters = AlgorithmParameters .getInstance("AES"); parameters.init(new IvParameterSpec(ivByte)); // 初始化 cipher.init(Cipher.DECRYPT_MODE, spec, parameters); byte[] resultByte = cipher.doFinal(dataByte); if (null != resultByte && resultByte.length > 0) { result = new String(resultByte, "UTF-8"); } return result; } catch (Exception e) { log.error("用户数据的签名验证和加解密出现异常", e); throw new ServiceException(ResultCodeEnum.BUSINESS_ERROR.getCode(), "获取用户步数异常"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。