#835 小程序模块增加微信运动数据解密方法

This commit is contained in:
Binary Wang 2018-11-04 15:20:00 +08:00
parent 4b7cce8706
commit e204b0e2e4
6 changed files with 163 additions and 6 deletions

View File

@ -0,0 +1,24 @@
package cn.binarywang.wx.miniapp.api;
import java.util.List;
import cn.binarywang.wx.miniapp.bean.WxMaRunStepInfo;
/**
* 微信运动相关操作接口.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public interface WxMaRunService {
/**
* 解密分享敏感数据.
* 文档地址https://developers.weixin.qq.com/miniprogram/dev/api/open-api/werun/wx.getWeRunData.html
*
* @param sessionKey 会话密钥
* @param encryptedData 消息密文
* @param ivStr 加密算法的初始向量
*/
List<WxMaRunStepInfo> getRunStepInfo(String sessionKey, String encryptedData, String ivStr);
}

View File

@ -152,39 +152,45 @@ public interface WxMaService {
WxMaTemplateService getTemplateService();
/**
* 数据分析相关查询服务
* 数据分析相关查询服务.
*
* @return WxMaAnalysisService
*/
WxMaAnalysisService getAnalysisService();
/**
* 返回代码操作相关的 API
* 返回代码操作相关的 API.
*
* @return WxMaCodeService
*/
WxMaCodeService getCodeService();
/**
* 返回jsapi操作相关的 API服务类对象
* 返回jsapi操作相关的 API服务类对象.
*
* @return WxMaJsapiService
*/
WxMaJsapiService getJsapiService();
/**
* 小程序修改服务器地址成员管理 API
* 小程序修改服务器地址成员管理 API.
*
* @return WxMaSettingService
*/
WxMaSettingService getSettingService();
/**
* 返回分享相关查询服务
* 返回分享相关查询服务.
* @return WxMaShareService
*/
WxMaShareService getShareService();
/**
* 返回维新运动相关接口服务对象.
* @return WxMaShareService
*/
WxMaRunService getRunService();
/**
* 初始化http请求对象.
*/

View File

@ -0,0 +1,29 @@
package cn.binarywang.wx.miniapp.api.impl;
import java.util.List;
import cn.binarywang.wx.miniapp.api.WxMaRunService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaRunStepInfo;
import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils;
/**
* <pre>
*
* Created by Binary Wang on 2018/11/4.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public class WxMaRunServiceImpl implements WxMaRunService {
private WxMaService service;
public WxMaRunServiceImpl(WxMaService service) {
this.service = service;
}
@Override
public List<WxMaRunStepInfo> getRunStepInfo(String sessionKey, String encryptedData, String ivStr) {
return WxMaRunStepInfo.fromJson(WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr));
}
}

View File

@ -6,7 +6,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import cn.binarywang.wx.miniapp.api.*;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
@ -16,6 +15,18 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.binarywang.wx.miniapp.api.WxMaAnalysisService;
import cn.binarywang.wx.miniapp.api.WxMaCodeService;
import cn.binarywang.wx.miniapp.api.WxMaJsapiService;
import cn.binarywang.wx.miniapp.api.WxMaMediaService;
import cn.binarywang.wx.miniapp.api.WxMaMsgService;
import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaRunService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaSettingService;
import cn.binarywang.wx.miniapp.api.WxMaShareService;
import cn.binarywang.wx.miniapp.api.WxMaTemplateService;
import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import com.google.common.base.Joiner;
@ -57,6 +68,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
private WxMaSettingService settingService = new WxMaSettingServiceImpl(this);
private WxMaJsapiService jsapiService = new WxMaJsapiServiceImpl(this);
private WxMaShareService shareService = new WxMaShareServiceImpl(this);
private WxMaRunService runService = new WxMaRunServiceImpl(this);
private int retrySleepMillis = 1000;
private int maxRetryTimes = 5;
@ -332,4 +344,9 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
public WxMaShareService getShareService() {
return this.shareService;
}
@Override
public WxMaRunService getRunService() {
return this.runService;
}
}

View File

@ -0,0 +1,38 @@
package cn.binarywang.wx.miniapp.bean;
import java.io.Serializable;
import java.util.List;
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import lombok.Data;
/**
* 微信运动步数信息.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Data
public class WxMaRunStepInfo implements Serializable {
private static final JsonParser JSON_PARSER = new JsonParser();
private static final long serialVersionUID = -7496372171398607044L;
/**
* 时间戳表示数据对应的时间.
*/
private Long timestamp;
/**
* 微信运动步数.
*/
private Integer step;
public static List<WxMaRunStepInfo> fromJson(String json) {
JsonObject jsonObject = JSON_PARSER.parse(json).getAsJsonObject();
return WxMaGsonBuilder.create().fromJson(jsonObject.get("stepInfoList").toString(),
new TypeToken<List<WxMaRunStepInfo>>() {
}.getType());
}
}

View File

@ -0,0 +1,43 @@
package cn.binarywang.wx.miniapp.bean;
import java.util.List;
import org.testng.annotations.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.*;
/**
* <pre>
*
* Created by Binary Wang on 2018/11/4.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public class WxMaRunStepInfoTest {
@Test
public void testFromJson() {
// 数据来源https://developers.weixin.qq.com/miniprogram/dev/api/open-api/werun/wx.getWeRunData.html
String json = "{\n" +
" \"stepInfoList\": [\n" +
" {\n" +
" \"timestamp\": 1445866601,\n" +
" \"step\": 100\n" +
" },\n" +
" {\n" +
" \"timestamp\": 1445876601,\n" +
" \"step\": 120\n" +
" }\n" +
" ]\n" +
"}";
final List<WxMaRunStepInfo> stepInfoList = WxMaRunStepInfo.fromJson(json);
assertThat(stepInfoList).isNotEmpty();
assertThat(stepInfoList.get(0).getStep()).isEqualTo(100);
assertThat(stepInfoList.get(0).getTimestamp()).isEqualTo(1445866601);
assertThat(stepInfoList.get(1).getStep()).isEqualTo(120);
assertThat(stepInfoList.get(1).getTimestamp()).isEqualTo(1445876601);
}
}