mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-09-20 02:29:44 +08:00
issue #91 添加企业号JS_API的支持
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* Created by qianjia on 15/1/25.
|
||||
*/
|
||||
public class TestNonAtomicLongAssignment {
|
||||
|
||||
private static final long HI = 1l << 32;
|
||||
private static final long LO = 1l;
|
||||
|
||||
private static final long TEST_NUMBER = HI | LO;
|
||||
|
||||
private static long assignee = 0l;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Thread writer = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
assignee = TEST_NUMBER;
|
||||
}
|
||||
}
|
||||
});
|
||||
writer.setDaemon(true);
|
||||
|
||||
Thread reader = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
long i = 0;
|
||||
while (true) {
|
||||
i++;
|
||||
long test = assignee;
|
||||
if (test != TEST_NUMBER) {
|
||||
System.out.print(i + " times:" + toBin(test));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Thread worker = new Thread(new Runnable() {
|
||||
// @Override
|
||||
// public void run() {
|
||||
// double d = 89009808877238948224343435452333323113131313133434434341212323232424243434335354232390490189190420928348910913094983.323334401928d;
|
||||
// while(true) {
|
||||
// Math.cbrt(d);
|
||||
// d = d - 1l;
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// worker.setDaemon(true);
|
||||
// worker.start();
|
||||
|
||||
writer.start();
|
||||
reader.start();
|
||||
|
||||
}
|
||||
|
||||
public static String toBin(long n) {
|
||||
StringBuilder sb = new StringBuilder(Long.toBinaryString(n));
|
||||
int padding = 64 - sb.length();
|
||||
while (padding > 0) {
|
||||
sb.insert(0, '0');
|
||||
padding--;
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
@@ -51,6 +51,18 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
|
||||
return jsapiTicket;
|
||||
}
|
||||
|
||||
public void setJsapiTicket(String jsapiTicket) {
|
||||
this.jsapiTicket = jsapiTicket;
|
||||
}
|
||||
|
||||
public long getJsapiTicketExpiresTime() {
|
||||
return jsapiTicketExpiresTime;
|
||||
}
|
||||
|
||||
public void setJsapiTicketExpiresTime(long jsapiTicketExpiresTime) {
|
||||
this.jsapiTicketExpiresTime = jsapiTicketExpiresTime;
|
||||
}
|
||||
|
||||
public boolean isJsapiTicketExpired() {
|
||||
return System.currentTimeMillis() > this.jsapiTicketExpiresTime;
|
||||
}
|
||||
@@ -167,5 +179,4 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
|
||||
", jsapiTicketExpiresTime='" + jsapiTicketExpiresTime + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -5,9 +5,9 @@ import me.chanjar.weixin.common.session.InternalSessionManager;
|
||||
import me.chanjar.weixin.common.session.StandardSessionManager;
|
||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
import me.chanjar.weixin.common.util.LogExceptionHandler;
|
||||
import me.chanjar.weixin.common.util.WxErrorExceptionHandler;
|
||||
import me.chanjar.weixin.common.util.WxMessageDuplicateChecker;
|
||||
import me.chanjar.weixin.common.util.WxMessageInMemoryDuplicateChecker;
|
||||
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
|
||||
import me.chanjar.weixin.common.api.WxMessageDuplicateChecker;
|
||||
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker;
|
||||
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
|
||||
import org.slf4j.Logger;
|
||||
@@ -87,8 +87,8 @@ public class WxMpMessageRouter {
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 设置自定义的 {@link me.chanjar.weixin.common.util.WxMessageDuplicateChecker}
|
||||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.util.WxMessageInMemoryDuplicateChecker}
|
||||
* 设置自定义的 {@link me.chanjar.weixin.common.api.WxMessageDuplicateChecker}
|
||||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker}
|
||||
* </pre>
|
||||
* @param messageDuplicateChecker
|
||||
*/
|
||||
@@ -109,7 +109,7 @@ public class WxMpMessageRouter {
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 设置自定义的{@link me.chanjar.weixin.common.util.WxErrorExceptionHandler}
|
||||
* 设置自定义的{@link me.chanjar.weixin.common.api.WxErrorExceptionHandler}
|
||||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.util.LogExceptionHandler}
|
||||
* </pre>
|
||||
* @param exceptionHandler
|
||||
|
@@ -2,7 +2,7 @@ package me.chanjar.weixin.mp.api;
|
||||
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
import me.chanjar.weixin.common.util.WxErrorExceptionHandler;
|
||||
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
|
||||
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
|
||||
|
||||
|
@@ -1,10 +1,9 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import me.chanjar.weixin.common.bean.WxMenu;
|
||||
import me.chanjar.weixin.common.bean.WxJsapiSignature;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.session.WxSession;
|
||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.mp.bean.*;
|
||||
import me.chanjar.weixin.mp.bean.result.*;
|
||||
@@ -86,7 +85,7 @@ public interface WxMpService {
|
||||
* @param url url
|
||||
* @return
|
||||
*/
|
||||
public WxMpJsapiSignature createJsapiSignature(String url) throws WxErrorException;
|
||||
public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
@@ -7,11 +7,13 @@ import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
import me.chanjar.weixin.common.bean.WxMenu;
|
||||
import me.chanjar.weixin.common.bean.WxJsapiSignature;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.session.StandardSessionManager;
|
||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
import me.chanjar.weixin.common.util.RandomUtils;
|
||||
import me.chanjar.weixin.common.util.StringUtils;
|
||||
import me.chanjar.weixin.common.util.crypto.SHA1;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
@@ -42,15 +44,10 @@ import java.io.InputStream;
|
||||
import java.io.StringReader;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
|
||||
public class WxMpServiceImpl implements WxMpService {
|
||||
|
||||
protected final String RANDOM_STR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
|
||||
protected final Random RANDOM = new Random();
|
||||
|
||||
protected final Logger log = LoggerFactory.getLogger(WxMpServiceImpl.class);
|
||||
|
||||
/**
|
||||
@@ -148,9 +145,9 @@ public class WxMpServiceImpl implements WxMpService {
|
||||
return wxMpConfigStorage.getJsapiTicket();
|
||||
}
|
||||
|
||||
public WxMpJsapiSignature createJsapiSignature(String url) throws WxErrorException {
|
||||
public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException {
|
||||
long timestamp = System.currentTimeMillis() / 1000;
|
||||
String noncestr = getRandomStr();
|
||||
String noncestr = RandomUtils.getRandomStr();
|
||||
String jsapiTicket = getJsapiTicket(false);
|
||||
try {
|
||||
String signature = SHA1.genWithAmple(
|
||||
@@ -159,7 +156,7 @@ public class WxMpServiceImpl implements WxMpService {
|
||||
"timestamp=" + timestamp,
|
||||
"url=" + url
|
||||
);
|
||||
WxMpJsapiSignature jsapiSignature = new WxMpJsapiSignature();
|
||||
WxJsapiSignature jsapiSignature = new WxJsapiSignature();
|
||||
jsapiSignature.setTimestamp(timestamp);
|
||||
jsapiSignature.setNoncestr(noncestr);
|
||||
jsapiSignature.setUrl(url);
|
||||
@@ -170,14 +167,6 @@ public class WxMpServiceImpl implements WxMpService {
|
||||
}
|
||||
}
|
||||
|
||||
protected String getRandomStr() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < 16; i++) {
|
||||
sb.append(RANDOM_STR.charAt(RANDOM.nextInt(RANDOM_STR.length())));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void customMessageSend(WxMpCustomMessage message) throws WxErrorException {
|
||||
String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
|
||||
execute(new SimplePostRequestExecutor(), url, message.toJson());
|
||||
|
@@ -1,58 +0,0 @@
|
||||
package me.chanjar.weixin.mp.bean.result;
|
||||
|
||||
/**
|
||||
* jspai signature
|
||||
*/
|
||||
public class WxMpJsapiSignature {
|
||||
|
||||
private String noncestr;
|
||||
|
||||
private String jsapiTicket;
|
||||
|
||||
private long timestamp;
|
||||
|
||||
private String url;
|
||||
|
||||
private String signature;
|
||||
|
||||
public String getSignature() {
|
||||
return signature;
|
||||
}
|
||||
|
||||
public void setSignature(String signature) {
|
||||
this.signature = signature;
|
||||
}
|
||||
|
||||
public String getNoncestr() {
|
||||
return noncestr;
|
||||
}
|
||||
|
||||
public void setNoncestr(String noncestr) {
|
||||
this.noncestr = noncestr;
|
||||
}
|
||||
|
||||
public String getJsapiTicket() {
|
||||
return jsapiTicket;
|
||||
}
|
||||
|
||||
public void setJsapiTicket(String jsapiTicket) {
|
||||
this.jsapiTicket = jsapiTicket;
|
||||
}
|
||||
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public void setTimestamp(long timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user