Skip to content

Commit cef8ac0

Browse files
committed
git commit -m issue
1 parent f00c549 commit cef8ac0

File tree

5 files changed

+133
-14
lines changed

5 files changed

+133
-14
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/SHA1.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,49 @@
1010
public class SHA1 {
1111

1212
/**
13-
* 生成SHA1签名
13+
* 串接arr参数,生成sha1 digest
14+
*
1415
* @param arr
1516
* @return
1617
*/
1718
public static String gen(String... arr) throws NoSuchAlgorithmException {
1819
Arrays.sort(arr);
1920
StringBuilder sb = new StringBuilder();
20-
for(String a : arr) {
21+
for (String a : arr) {
2122
sb.append(a);
2223
}
24+
return genStr(sb.toString());
25+
}
26+
27+
/**
28+
* 用&串接arr参数,生成sha1 digest
29+
*
30+
* @param arr
31+
* @return
32+
*/
33+
public static String genWithAmple(String... arr) throws NoSuchAlgorithmException {
34+
Arrays.sort(arr);
35+
StringBuilder sb = new StringBuilder();
36+
for (int i = 0; i < arr.length; i++) {
37+
String a = arr[i];
38+
sb.append(a);
39+
if (i != arr.length - 1) {
40+
sb.append('&');
41+
}
42+
}
43+
return genStr(sb.toString());
44+
}
2345

46+
public static String genStr(String str) throws NoSuchAlgorithmException {
2447
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
25-
sha1.update(sb.toString().getBytes());
48+
sha1.update(str.getBytes());
2649
byte[] output = sha1.digest();
2750
return bytesToHex(output);
2851
}
2952

30-
3153
protected static String bytesToHex(byte[] b) {
32-
char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
33-
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
54+
char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
55+
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
3456
StringBuffer buf = new StringBuffer();
3557
for (int j = 0; j < b.length; j++) {
3658
buf.append(hexDigit[(b[j] >> 4) & 0x0f]);

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpConfigStorage.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@
99
*/
1010
public interface WxMpConfigStorage {
1111

12+
/**
13+
* 应该是线程安全的
14+
* @param accessToken
15+
*/
1216
public void updateAccessToken(WxAccessToken accessToken);
13-
17+
18+
/**
19+
* 应该是线程安全的
20+
* @param accessToken
21+
* @param expiresIn
22+
*/
1423
public void updateAccessToken(String accessToken, int expiresIn);
15-
24+
1625
public String getAccessToken();
1726

1827
public String getAppId();
@@ -35,4 +44,15 @@ public interface WxMpConfigStorage {
3544

3645
public String getHttp_proxy_password();
3746

47+
48+
public String getJsapiTicket();
49+
50+
public boolean isJsapiTokenExpired();
51+
52+
/**
53+
* 应该是线程安全的
54+
* @param jsapiTicket
55+
*/
56+
public void updateJsapiTicket(String jsapiTicket, int expiresInSeconds);
57+
3858
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpInMemoryConfigStorage.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
1111

12+
private static final long l = 7000 * 1000l;
13+
1214
protected String appId;
1315
protected String secret;
1416
protected String token;
@@ -23,11 +25,14 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
2325
protected String http_proxy_username;
2426
protected String http_proxy_password;
2527

26-
public void updateAccessToken(WxAccessToken accessToken) {
28+
protected String jsapiTicket;
29+
protected long jsapiTicketExpiresTime;
30+
31+
public synchronized void updateAccessToken(WxAccessToken accessToken) {
2732
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
2833
}
2934

30-
public void updateAccessToken(String accessToken, int expiresIn) {
35+
public synchronized void updateAccessToken(String accessToken, int expiresIn) {
3136
this.accessToken = accessToken;
3237
this.expiresIn = expiresIn;
3338
}
@@ -121,6 +126,21 @@ public void setHttp_proxy_password(String http_proxy_password) {
121126
this.http_proxy_password = http_proxy_password;
122127
}
123128

129+
130+
public String getJsapiTicket() {
131+
return jsapiTicket;
132+
}
133+
134+
public boolean isJsapiTokenExpired() {
135+
return System.currentTimeMillis() > this.jsapiTicketExpiresTime;
136+
}
137+
138+
public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) {
139+
this.jsapiTicket = jsapiTicket;
140+
// 预留200秒的时间
141+
this.jsapiTicketExpiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l;
142+
}
143+
124144
@Override
125145
public String toString() {
126146
return "WxMpInMemoryConfigStorage{" +

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,42 @@ public interface WxMpService {
4343
* @throws me.chanjar.weixin.common.exception.WxErrorException
4444
*/
4545
public void accessTokenRefresh() throws WxErrorException;
46-
46+
47+
/**
48+
* <pre>
49+
* 获得jsapi_ticket
50+
* 获得时会检查jsapiToken是否过期,如果过期了,那么就刷新一下,否则就什么都不干
51+
*
52+
* 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD.951-JS-SDK.E4.BD.BF.E7.94.A8.E6.9D.83.E9.99.90.E7.AD.BE.E5.90.8D.E7.AE.97.E6.B3.95
53+
* </pre>
54+
* @return
55+
* @throws WxErrorException
56+
*/
57+
public String getJsapiTicket() throws WxErrorException;
58+
59+
/**
60+
* <pre>
61+
* 创建调用jsapi时所需要的签名
62+
*
63+
* 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD.951-JS-SDK.E4.BD.BF.E7.94.A8.E6.9D.83.E9.99.90.E7.AD.BE.E5.90.8D.E7.AE.97.E6.B3.95
64+
* </pre>
65+
* @param timestamp 时间戳
66+
* @param noncestr 用户自己生成的随机字符串
67+
* @param url url
68+
* @return
69+
*/
70+
public String createJsapiSignature(String timestamp, String noncestr, String url) throws WxErrorException;
71+
4772
/**
4873
* <pre>
4974
* 上传多媒体文件
50-
*
75+
*
5176
* 上传的多媒体文件有格式和大小限制,如下:
5277
* 图片(image): 1M,支持JPG格式
5378
* 语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式
5479
* 视频(video):10MB,支持MP4格式
5580
* 缩略图(thumb):64KB,支持JPG格式
56-
*
81+
*
5782
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件
5883
* </pre>
5984
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.io.IOException;
3737
import java.io.InputStream;
3838
import java.io.StringReader;
39+
import java.security.NoSuchAlgorithmException;
3940
import java.util.List;
4041
import java.util.UUID;
4142
import java.util.concurrent.atomic.AtomicBoolean;
@@ -106,7 +107,38 @@ public void accessTokenRefresh() throws WxErrorException {
106107
// 刷新完毕了,就没他什么事儿了
107108
}
108109
}
109-
110+
111+
public String getJsapiTicket() throws WxErrorException {
112+
if (wxMpConfigStorage.isJsapiTokenExpired()) {
113+
synchronized (wxMpConfigStorage) {
114+
if (wxMpConfigStorage.isJsapiTokenExpired()) {
115+
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";
116+
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
117+
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
118+
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
119+
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
120+
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
121+
wxMpConfigStorage.updateJsapiTicket(jsapiTicket, expiresInSeconds);
122+
}
123+
}
124+
}
125+
return wxMpConfigStorage.getJsapiTicket();
126+
}
127+
128+
public String createJsapiSignature(String timestamp, String noncestr, String url) throws WxErrorException {
129+
String jsapiTicket = getJsapiTicket();
130+
try {
131+
return SHA1.genWithAmple(
132+
"jsapi_ticket=" + jsapiTicket,
133+
"timestamp=" + timestamp,
134+
"noncestr=" + noncestr,
135+
"url=" + url
136+
);
137+
} catch (NoSuchAlgorithmException e) {
138+
throw new RuntimeException(e);
139+
}
140+
}
141+
110142
public void customMessageSend(WxMpCustomMessage message) throws WxErrorException {
111143
String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
112144
execute(new SimplePostRequestExecutor(), url, message.toJson());

0 commit comments

Comments
 (0)