mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 10:38:42 +08:00 
			
		
		
		
	issue #88 修改createJsapiSignature方法签名,便于开发人员使用
This commit is contained in:
		| @ -83,12 +83,10 @@ public interface WxMpService { | ||||
|    * | ||||
|    * 详情请见: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 | ||||
|    * </pre> | ||||
|    * @param timestamp 时间戳 | ||||
|    * @param noncestr  用户自己生成的随机字符串 | ||||
|    * @param url       url | ||||
|    * @return | ||||
|    */ | ||||
|   public String createJsapiSignature(long timestamp, String noncestr, String url) throws WxErrorException; | ||||
|   public WxMpJsapiSignature createJsapiSignature(String url) throws WxErrorException; | ||||
|  | ||||
|   /** | ||||
|    * <pre> | ||||
|  | ||||
| @ -42,10 +42,15 @@ 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); | ||||
|  | ||||
|   /** | ||||
| @ -59,7 +64,7 @@ public class WxMpServiceImpl implements WxMpService { | ||||
|   protected final Object globalJsapiTicketRefreshLock = new Object(); | ||||
|  | ||||
|   protected WxMpConfigStorage wxMpConfigStorage; | ||||
|    | ||||
|  | ||||
|   protected CloseableHttpClient httpClient; | ||||
|  | ||||
|   protected HttpHost httpProxy; | ||||
| @ -143,20 +148,36 @@ public class WxMpServiceImpl implements WxMpService { | ||||
|     return wxMpConfigStorage.getJsapiTicket(); | ||||
|   } | ||||
|  | ||||
|   public String createJsapiSignature(long timestamp, String noncestr, String url) throws WxErrorException { | ||||
|   public WxMpJsapiSignature createJsapiSignature(String url) throws WxErrorException { | ||||
|     long timestamp = System.currentTimeMillis() / 1000; | ||||
|     String noncestr = getRandomStr(); | ||||
|     String jsapiTicket = getJsapiTicket(false); | ||||
|     try { | ||||
|       return SHA1.genWithAmple( | ||||
|       String signature = SHA1.genWithAmple( | ||||
|           "jsapi_ticket=" + jsapiTicket, | ||||
|           "noncestr=" + noncestr, | ||||
|           "timestamp=" + timestamp, | ||||
|           "url=" + url | ||||
|       ); | ||||
|       WxMpJsapiSignature jsapiSignature = new WxMpJsapiSignature(); | ||||
|       jsapiSignature.setTimestamp(timestamp); | ||||
|       jsapiSignature.setNoncestr(noncestr); | ||||
|       jsapiSignature.setUrl(url); | ||||
|       jsapiSignature.setSignature(signature); | ||||
|       return jsapiSignature; | ||||
|     } catch (NoSuchAlgorithmException e) { | ||||
|       throw new RuntimeException(e); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   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()); | ||||
|  | ||||
| @ -0,0 +1,58 @@ | ||||
| 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
	 Daniel Qian
					Daniel Qian