Skip to content

Commit e204b0e

Browse files
committed
binarywang#835 小程序模块增加微信运动数据解密方法
1 parent 4b7cce8 commit e204b0e

File tree

6 files changed

+163
-6
lines changed

6 files changed

+163
-6
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cn.binarywang.wx.miniapp.api;
2+
3+
import java.util.List;
4+
5+
import cn.binarywang.wx.miniapp.bean.WxMaRunStepInfo;
6+
7+
/**
8+
* 微信运动相关操作接口.
9+
*
10+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
11+
*/
12+
public interface WxMaRunService {
13+
14+
/**
15+
* 解密分享敏感数据.
16+
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/werun/wx.getWeRunData.html
17+
*
18+
* @param sessionKey 会话密钥
19+
* @param encryptedData 消息密文
20+
* @param ivStr 加密算法的初始向量
21+
*/
22+
List<WxMaRunStepInfo> getRunStepInfo(String sessionKey, String encryptedData, String ivStr);
23+
24+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,39 +152,45 @@ public interface WxMaService {
152152
WxMaTemplateService getTemplateService();
153153

154154
/**
155-
* 数据分析相关查询服务
155+
* 数据分析相关查询服务.
156156
*
157157
* @return WxMaAnalysisService
158158
*/
159159
WxMaAnalysisService getAnalysisService();
160160

161161
/**
162-
* 返回代码操作相关的 API
162+
* 返回代码操作相关的 API.
163163
*
164164
* @return WxMaCodeService
165165
*/
166166
WxMaCodeService getCodeService();
167167

168168
/**
169-
* 返回jsapi操作相关的 API服务类对象
169+
* 返回jsapi操作相关的 API服务类对象.
170170
*
171171
* @return WxMaJsapiService
172172
*/
173173
WxMaJsapiService getJsapiService();
174174

175175
/**
176-
* 小程序修改服务器地址、成员管理 API
176+
* 小程序修改服务器地址、成员管理 API.
177177
*
178178
* @return WxMaSettingService
179179
*/
180180
WxMaSettingService getSettingService();
181181

182182
/**
183-
* 返回分享相关查询服务
183+
* 返回分享相关查询服务.
184184
* @return WxMaShareService
185185
*/
186186
WxMaShareService getShareService();
187187

188+
/**
189+
* 返回维新运动相关接口服务对象.
190+
* @return WxMaShareService
191+
*/
192+
WxMaRunService getRunService();
193+
188194
/**
189195
* 初始化http请求对象.
190196
*/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.binarywang.wx.miniapp.api.impl;
2+
3+
import java.util.List;
4+
5+
import cn.binarywang.wx.miniapp.api.WxMaRunService;
6+
import cn.binarywang.wx.miniapp.api.WxMaService;
7+
import cn.binarywang.wx.miniapp.bean.WxMaRunStepInfo;
8+
import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils;
9+
10+
/**
11+
* <pre>
12+
*
13+
* Created by Binary Wang on 2018/11/4.
14+
* </pre>
15+
*
16+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
17+
*/
18+
public class WxMaRunServiceImpl implements WxMaRunService {
19+
private WxMaService service;
20+
21+
public WxMaRunServiceImpl(WxMaService service) {
22+
this.service = service;
23+
}
24+
25+
@Override
26+
public List<WxMaRunStepInfo> getRunStepInfo(String sessionKey, String encryptedData, String ivStr) {
27+
return WxMaRunStepInfo.fromJson(WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr));
28+
}
29+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImpl.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.Map;
77
import java.util.concurrent.locks.Lock;
88

9-
import cn.binarywang.wx.miniapp.api.*;
109
import org.apache.http.HttpHost;
1110
import org.apache.http.client.config.RequestConfig;
1211
import org.apache.http.client.methods.CloseableHttpResponse;
@@ -16,6 +15,18 @@
1615
import org.slf4j.Logger;
1716
import org.slf4j.LoggerFactory;
1817

18+
import cn.binarywang.wx.miniapp.api.WxMaAnalysisService;
19+
import cn.binarywang.wx.miniapp.api.WxMaCodeService;
20+
import cn.binarywang.wx.miniapp.api.WxMaJsapiService;
21+
import cn.binarywang.wx.miniapp.api.WxMaMediaService;
22+
import cn.binarywang.wx.miniapp.api.WxMaMsgService;
23+
import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
24+
import cn.binarywang.wx.miniapp.api.WxMaRunService;
25+
import cn.binarywang.wx.miniapp.api.WxMaService;
26+
import cn.binarywang.wx.miniapp.api.WxMaSettingService;
27+
import cn.binarywang.wx.miniapp.api.WxMaShareService;
28+
import cn.binarywang.wx.miniapp.api.WxMaTemplateService;
29+
import cn.binarywang.wx.miniapp.api.WxMaUserService;
1930
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
2031
import cn.binarywang.wx.miniapp.config.WxMaConfig;
2132
import com.google.common.base.Joiner;
@@ -57,6 +68,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
5768
private WxMaSettingService settingService = new WxMaSettingServiceImpl(this);
5869
private WxMaJsapiService jsapiService = new WxMaJsapiServiceImpl(this);
5970
private WxMaShareService shareService = new WxMaShareServiceImpl(this);
71+
private WxMaRunService runService = new WxMaRunServiceImpl(this);
6072

6173
private int retrySleepMillis = 1000;
6274
private int maxRetryTimes = 5;
@@ -332,4 +344,9 @@ public WxMaSettingService getSettingService() {
332344
public WxMaShareService getShareService() {
333345
return this.shareService;
334346
}
347+
348+
@Override
349+
public WxMaRunService getRunService() {
350+
return this.runService;
351+
}
335352
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cn.binarywang.wx.miniapp.bean;
2+
3+
import java.io.Serializable;
4+
import java.util.List;
5+
6+
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
7+
import com.google.gson.JsonObject;
8+
import com.google.gson.JsonParser;
9+
import com.google.gson.reflect.TypeToken;
10+
import lombok.Data;
11+
12+
/**
13+
* 微信运动步数信息.
14+
*
15+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
16+
*/
17+
@Data
18+
public class WxMaRunStepInfo implements Serializable {
19+
private static final JsonParser JSON_PARSER = new JsonParser();
20+
private static final long serialVersionUID = -7496372171398607044L;
21+
22+
/**
23+
* 时间戳,表示数据对应的时间.
24+
*/
25+
private Long timestamp;
26+
27+
/**
28+
* 微信运动步数.
29+
*/
30+
private Integer step;
31+
32+
public static List<WxMaRunStepInfo> fromJson(String json) {
33+
JsonObject jsonObject = JSON_PARSER.parse(json).getAsJsonObject();
34+
return WxMaGsonBuilder.create().fromJson(jsonObject.get("stepInfoList").toString(),
35+
new TypeToken<List<WxMaRunStepInfo>>() {
36+
}.getType());
37+
}
38+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cn.binarywang.wx.miniapp.bean;
2+
3+
import java.util.List;
4+
5+
import org.testng.annotations.*;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
import static org.testng.Assert.*;
9+
10+
/**
11+
* <pre>
12+
*
13+
* Created by Binary Wang on 2018/11/4.
14+
* </pre>
15+
*
16+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
17+
*/
18+
public class WxMaRunStepInfoTest {
19+
20+
@Test
21+
public void testFromJson() {
22+
// 数据来源:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/werun/wx.getWeRunData.html
23+
String json = "{\n" +
24+
" \"stepInfoList\": [\n" +
25+
" {\n" +
26+
" \"timestamp\": 1445866601,\n" +
27+
" \"step\": 100\n" +
28+
" },\n" +
29+
" {\n" +
30+
" \"timestamp\": 1445876601,\n" +
31+
" \"step\": 120\n" +
32+
" }\n" +
33+
" ]\n" +
34+
"}";
35+
36+
final List<WxMaRunStepInfo> stepInfoList = WxMaRunStepInfo.fromJson(json);
37+
assertThat(stepInfoList).isNotEmpty();
38+
assertThat(stepInfoList.get(0).getStep()).isEqualTo(100);
39+
assertThat(stepInfoList.get(0).getTimestamp()).isEqualTo(1445866601);
40+
assertThat(stepInfoList.get(1).getStep()).isEqualTo(120);
41+
assertThat(stepInfoList.get(1).getTimestamp()).isEqualTo(1445876601);
42+
}
43+
}

0 commit comments

Comments
 (0)