Skip to content

Commit 15aff7f

Browse files
committed
issue #88 修改createJsapiSignature方法签名,便于开发人员使用
1 parent 1819b0e commit 15aff7f

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,10 @@ public interface WxMpService {
8383
*
8484
* 详情请见: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
8585
* </pre>
86-
* @param timestamp 时间戳
87-
* @param noncestr 用户自己生成的随机字符串
8886
* @param url url
8987
* @return
9088
*/
91-
public String createJsapiSignature(long timestamp, String noncestr, String url) throws WxErrorException;
89+
public WxMpJsapiSignature createJsapiSignature(String url) throws WxErrorException;
9290

9391
/**
9492
* <pre>

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,15 @@
4242
import java.io.StringReader;
4343
import java.security.NoSuchAlgorithmException;
4444
import java.util.List;
45+
import java.util.Random;
4546
import java.util.UUID;
4647

4748
public class WxMpServiceImpl implements WxMpService {
4849

50+
protected final String RANDOM_STR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
51+
52+
protected final Random RANDOM = new Random();
53+
4954
protected final Logger log = LoggerFactory.getLogger(WxMpServiceImpl.class);
5055

5156
/**
@@ -59,7 +64,7 @@ public class WxMpServiceImpl implements WxMpService {
5964
protected final Object globalJsapiTicketRefreshLock = new Object();
6065

6166
protected WxMpConfigStorage wxMpConfigStorage;
62-
67+
6368
protected CloseableHttpClient httpClient;
6469

6570
protected HttpHost httpProxy;
@@ -143,20 +148,36 @@ public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
143148
return wxMpConfigStorage.getJsapiTicket();
144149
}
145150

146-
public String createJsapiSignature(long timestamp, String noncestr, String url) throws WxErrorException {
151+
public WxMpJsapiSignature createJsapiSignature(String url) throws WxErrorException {
152+
long timestamp = System.currentTimeMillis() / 1000;
153+
String noncestr = getRandomStr();
147154
String jsapiTicket = getJsapiTicket(false);
148155
try {
149-
return SHA1.genWithAmple(
156+
String signature = SHA1.genWithAmple(
150157
"jsapi_ticket=" + jsapiTicket,
151158
"noncestr=" + noncestr,
152159
"timestamp=" + timestamp,
153160
"url=" + url
154161
);
162+
WxMpJsapiSignature jsapiSignature = new WxMpJsapiSignature();
163+
jsapiSignature.setTimestamp(timestamp);
164+
jsapiSignature.setNoncestr(noncestr);
165+
jsapiSignature.setUrl(url);
166+
jsapiSignature.setSignature(signature);
167+
return jsapiSignature;
155168
} catch (NoSuchAlgorithmException e) {
156169
throw new RuntimeException(e);
157170
}
158171
}
159172

173+
protected String getRandomStr() {
174+
StringBuilder sb = new StringBuilder();
175+
for (int i = 0; i < 16; i++) {
176+
sb.append(RANDOM_STR.charAt(RANDOM.nextInt(RANDOM_STR.length())));
177+
}
178+
return sb.toString();
179+
}
180+
160181
public void customMessageSend(WxMpCustomMessage message) throws WxErrorException {
161182
String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
162183
execute(new SimplePostRequestExecutor(), url, message.toJson());
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package me.chanjar.weixin.mp.bean.result;
2+
3+
/**
4+
* jspai signature
5+
*/
6+
public class WxMpJsapiSignature {
7+
8+
private String noncestr;
9+
10+
private String jsapiTicket;
11+
12+
private long timestamp;
13+
14+
private String url;
15+
16+
private String signature;
17+
18+
public String getSignature() {
19+
return signature;
20+
}
21+
22+
public void setSignature(String signature) {
23+
this.signature = signature;
24+
}
25+
26+
public String getNoncestr() {
27+
return noncestr;
28+
}
29+
30+
public void setNoncestr(String noncestr) {
31+
this.noncestr = noncestr;
32+
}
33+
34+
public String getJsapiTicket() {
35+
return jsapiTicket;
36+
}
37+
38+
public void setJsapiTicket(String jsapiTicket) {
39+
this.jsapiTicket = jsapiTicket;
40+
}
41+
42+
public long getTimestamp() {
43+
return timestamp;
44+
}
45+
46+
public void setTimestamp(long timestamp) {
47+
this.timestamp = timestamp;
48+
}
49+
50+
public String getUrl() {
51+
return url;
52+
}
53+
54+
public void setUrl(String url) {
55+
this.url = url;
56+
}
57+
58+
}

0 commit comments

Comments
 (0)