@RestController
@RequestMapping("/account_step")
public class AccountStepController {
@Autowired
private AccountStepService accountStepService;
@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));
}
}
public interface AccountStepService {
Integer addAccountSteps(String encryptedData, String iv);
}
@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 {
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);
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(), "获取用户步数异常");
}
}