-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
236 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package weixin.popular.api; | ||
|
||
import org.apache.http.client.methods.HttpUriRequest; | ||
import org.apache.http.client.methods.RequestBuilder; | ||
|
||
import weixin.popular.bean.Ticket; | ||
import weixin.popular.client.LocalHttpClient; | ||
|
||
/** | ||
* JSAPI ticket | ||
* @author LiYi | ||
* | ||
*/ | ||
public class TicketAPI extends BaseAPI{ | ||
|
||
/** | ||
* 获取 jsapi_ticket | ||
* @param access_token | ||
* @return | ||
*/ | ||
public static Ticket ticketGetticket(String access_token){ | ||
HttpUriRequest httpUriRequest = RequestBuilder.post() | ||
.setUri(BASE_URI + "/cgi-bin/ticket/getticket") | ||
.addParameter("access_token",access_token) | ||
.addParameter("type", "jsapi") | ||
.build(); | ||
return LocalHttpClient.executeJsonResult(httpUriRequest,Ticket.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package weixin.popular.bean; | ||
|
||
public class Ticket extends BaseResult{ | ||
|
||
private String ticket; | ||
|
||
private Integer expires_in; | ||
|
||
public String getTicket() { | ||
return ticket; | ||
} | ||
|
||
public void setTicket(String ticket) { | ||
this.ticket = ticket; | ||
} | ||
|
||
public Integer getExpires_in() { | ||
return expires_in; | ||
} | ||
|
||
public void setExpires_in(Integer expires_in) { | ||
this.expires_in = expires_in; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package weixin.popular.support; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
|
||
import weixin.popular.api.TicketAPI; | ||
import weixin.popular.bean.Ticket; | ||
|
||
/** | ||
* TicketManager ticket 自动刷新 | ||
* @author LiYi | ||
* | ||
*/ | ||
public class TicketManager { | ||
|
||
private static Map<String,String> ticketMap = new LinkedHashMap<String,String>(); | ||
|
||
private static Map<String,Timer> timerMap = new HashMap<String, Timer>(); | ||
|
||
|
||
/** | ||
* 初始化ticket 刷新,每119分钟刷新一次。 | ||
* 依赖TokenManager | ||
* @param appid | ||
*/ | ||
public static void init(final String appid){ | ||
if(!timerMap.containsKey(appid)){ | ||
Timer timer = new Timer(true); | ||
timer.schedule(new TimerTask() { | ||
@Override | ||
public void run() { | ||
String access_token = TokenManager.getToken(appid); | ||
Ticket ticket = TicketAPI.ticketGetticket(access_token); | ||
ticketMap.put(appid,ticket.getTicket()); | ||
} | ||
},0,1000*60*119); | ||
timerMap.put(appid,timer); | ||
} | ||
} | ||
|
||
/** | ||
* 获取 jsapi ticket | ||
* @param appid | ||
* @return | ||
*/ | ||
public static String getTicket(final String appid){ | ||
return ticketMap.get(appid); | ||
} | ||
|
||
/** | ||
* 获取第一个appid 的 jsapi ticket | ||
* 适用于单一微信号 | ||
* @param appid | ||
* @return | ||
*/ | ||
public static String getDefaultTicket(){ | ||
Object[] objs = ticketMap.values().toArray(); | ||
return objs.length>0?objs[0].toString():null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package weixin.popular.util; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import weixin.popular.bean.pay.PayPackage; | ||
|
||
public class JsUtil { | ||
|
||
/** | ||
* 生成 config接口 signature | ||
* @param noncestr | ||
* @param jsapi_ticket | ||
* @param timestamp | ||
* @param url | ||
* @return | ||
*/ | ||
public static String generateConfigSignature(String noncestr,String jsapi_ticket,String timestamp,String url){ | ||
Map<String, String> map = new HashMap<String, String>(); | ||
map.put("noncestr", noncestr); | ||
map.put("jsapi_ticket", jsapi_ticket); | ||
map.put("timestamp", timestamp); | ||
map.put("url", url); | ||
return SignatureUtil.generatePaySign(map, null); | ||
} | ||
|
||
/** | ||
* 生成 config接口注入权限验证 JSON | ||
* @param jsapi_ticket | ||
* @param debug | ||
* @param appId | ||
* @param url | ||
* @param jsApiList | ||
* onMenuShareTimeline | ||
onMenuShareAppMessage | ||
onMenuShareQQ | ||
onMenuShareWeibo | ||
startRecord | ||
stopRecord | ||
onVoiceRecordEnd | ||
playVoice | ||
pauseVoice | ||
stopVoice | ||
onVoicePlayEnd | ||
uploadVoice | ||
downloadVoice | ||
chooseImage | ||
previewImage | ||
uploadImage | ||
downloadImage | ||
translateVoice | ||
getNetworkType | ||
openLocation | ||
getLocation | ||
hideOptionMenu | ||
showOptionMenu | ||
hideMenuItems | ||
showMenuItems | ||
hideAllNonBaseMenuItem | ||
showAllNonBaseMenuItem | ||
closeWindow | ||
scanQRCode | ||
chooseWXPay | ||
openProductSpecificView | ||
addCard | ||
chooseCard | ||
openCard | ||
* @return | ||
*/ | ||
public static String generateConfigJson(String jsapi_ticket,boolean debug,String appId,String url,String... jsApiList){ | ||
long timestamp = System.currentTimeMillis()/1000; | ||
String nonceStr = UUID.randomUUID().toString(); | ||
String signature = generateConfigSignature(nonceStr, jsapi_ticket,timestamp+"",url); | ||
return new StringBuilder() | ||
.append("{") | ||
.append("debug:").append(debug).append(",") | ||
.append("appId:").append("'").append(appId).append("'").append(",") | ||
.append("timestamp:").append(timestamp).append(",") | ||
.append("nonceStr:").append("'").append(nonceStr).append("'").append(",") | ||
.append("signature:").append("'").append(signature).append("'").append(",") | ||
.append("jsApiList:").append(JsonUtil.toJSONString(jsApiList)) | ||
.append("}").toString(); | ||
} | ||
|
||
/** | ||
* 生成微信支付JSON | ||
* @param payPackage | ||
* @param appid | ||
* @param paternerKey | ||
* @param paySignkey | ||
* @return | ||
*/ | ||
public static String generateChooseWXPayJson(PayPackage payPackage, | ||
String appid, | ||
String paternerKey, | ||
String paySignkey){ | ||
Map<String, String> mapP = MapUtil.objectToMap(payPackage); | ||
String package_ = SignatureUtil.generatePackage(mapP, paternerKey); | ||
Map<String, String> map = new LinkedHashMap<String, String>(); | ||
long timestamp = System.currentTimeMillis()/1000; | ||
String noncestr = UUID.randomUUID().toString(); | ||
map.put("timestamp", timestamp+""); | ||
map.put("noncestr",noncestr); | ||
map.put("package",package_); | ||
map.put("appid",appid); | ||
String paySign = SignatureUtil.generatePaySign(map,paySignkey); | ||
map.put("paySign", paySign); | ||
map.remove("appid"); | ||
return JsonUtil.toJSONString(map); | ||
} | ||
|
||
} |