@Component("weiXinService")
@Transactional(value = "transactionManager")
public class WeiXinServiceImpl implements WeiXinService {
public static final Logger log = Logger.getLogger(WeiXinServiceImpl.class);
private static final String ACCESS_TOKEN="ACCESS_TOKEN";
@Override
public AccessToken getAccessToken() throws Exception {
Map<String, AccessToken> weixinTokenMap = AccessToken.getWeixinTokenMap();
AccessToken weixinTokenVo = null;
if(null!=weixinTokenMap){
weixinTokenVo = weixinTokenMap.get(ACCESS_TOKEN);
}else{
weixinTokenMap=new HashMap<String, AccessToken>();
}
String accessToken="";
//判斷是否在有效期內
if(null!=weixinTokenVo&&(System.currentTimeMillis()-weixinTokenVo.getTokenTime())<weixinTokenVo.getTokenExpires()*1000){
accessToken=weixinTokenVo.getAccessToken();
}
//判斷accessToken爲空 從新獲取
if(!StringUtils.checkStr(accessToken)){
if(null==weixinTokenVo){
weixinTokenVo=new AccessToken();
}
AccessToken at = CommonUtil.getAccessToken();
accessToken = at.getAccessToken();
weixinTokenVo.setAccessToken(accessToken);
weixinTokenVo.setTokenTime(System.currentTimeMillis());
weixinTokenVo.setTokenExpires(6500);
}
weixinTokenMap.put(ACCESS_TOKEN, weixinTokenVo);
AccessToken.setWeixinTokenMap(weixinTokenMap);
return weixinTokenVo;
}
/**獲取微信JS接口的臨時票據
* @return
* @throws Exception
*/
public AccessToken getJsapiTicket() throws Exception {
Map<String, AccessToken> weixinTokenMap = AccessToken.getWeixinTokenMap();
AccessToken weixinTokenVo = null;
if(null!=weixinTokenMap){
weixinTokenVo = weixinTokenMap.get(ACCESS_TOKEN);
}else{
weixinTokenMap=new HashMap<String, AccessToken>();
}
String jsapi_ticket="";
//判斷是否在有效期內
if(null!=weixinTokenVo&&(System.currentTimeMillis()-weixinTokenVo.getTicketTime())<weixinTokenVo.getTicketExpires()*1000){
jsapi_ticket=weixinTokenVo.getJsapiTicket();
}
//判斷jsapi_ticket爲空 從新獲取
if(!StringUtils.checkStr(jsapi_ticket)){
SslUtil.ignoreSsl();
weixinTokenVo=this.getAccessToken();
String ticket=this.getJSTicket(weixinTokenVo.getAccessToken());
log.info("accessToken爲:----"+weixinTokenVo.getAccessToken());
weixinTokenVo.setJsapiTicket(ticket);
weixinTokenVo.setTicketTime(System.currentTimeMillis());
weixinTokenVo.setTicketExpires(6500);
}
weixinTokenMap.put(ACCESS_TOKEN, weixinTokenVo);
AccessToken.setWeixinTokenMap(weixinTokenMap);
return weixinTokenVo;
}
private String getJSTicket(String accessToken){
String urlStr = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token="+accessToken;
JSONObject obj=CommonUtil.httpsRequest(urlStr, "GET", null);
String ticket=obj.getString("ticket");
return ticket;
}
}api
後臺調用代碼微信
WeiXinConfigVo weiXinConfigVo = new WeiXinConfigVo();
AccessToken accessTokenVo = weiXinService.getJsapiTicket();
weiXinConfigVo = WeiXinJsConfigUtil.sign(accessTokenVo.getJsapiTicket(), request);
weiXinConfigVo.setAppId(WeiXinConstant.APPID);app
頁面中寫入隱藏域ide
<input type="hidden" id="appId" value="$!weiXinConfigVo.appId">
<input type="hidden" id="timestamp" value="$!weiXinConfigVo.timestamp">
<input type="hidden" id="nonceStr" value="$!weiXinConfigVo.nonceStr">
<input type="hidden" id="signature" value="$!weiXinConfigVo.signature">this