- 前端
var vxurl = location.href.split('#')[0];
$.ajax({
type: "POST",
url: path + "/m/product/*",
dataType: "json",
data: JSON.stringify(encodeURIComponent(vxurl)),
async: false,
success: function (res) {
if(res.success){
var config = res.data.data;
console.log(config)
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: config.appId, // 必填,公众号的唯一标识
timestamp : config.timestamp, // 必填,生成签名的时间戳
nonceStr: config.nonceStr, // 必填,生成签名的随机串
signature: config.signature,// 必填,签名,见附录1
jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage']// 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready (function () {
// 微信分享的数据
var shareData = {
"imgUrl" : 'http://**/images/single-title.jpg',
"link" : config.link,
"desc" : shareDesc, // 分享描述
"title" : shareTitle, // 分享标题
success : function () {
}
}
wx.onMenuShareTimeline (shareData);
wx.onMenuShareAppMessage (shareData);
});
}
},
error: function(a, b) {
alert("error")
}
});
2 后端
@RequestMapping(value = "/set", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
@ResponseBody
public void set(HttpServletRequest request,HttpServletResponse response,@RequestBody String requestBody) {
try {
ResponseVo responseVo = new ResponseVo();
String searchtext=URLDecoder.decode(requestBody,"UTF-8");
String url = searchtext.substring(1, searchtext.length()-2);
String appId = "wx0a5665a6*******";
String AppSecret = "4fac1f122123a00ba39d0715391*****";
TokenResponse tokenVo = null;
JSTicketResponse ticketVo = null;
String key = "WX_JSAPITICKET*";
String jsapiTicket = cacheService.getStrValue(key);
LOGGER.info("1.获取缓存:{}", jsapiTicket);
String signature = null;
if(jsapiTicket == null){
try {
String tokenURL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+AppSecret;
String jsonObject = HttpClientUtil.get(tokenURL, 60000);
LOGGER.info("2.获取token,url:{},result:{}", tokenURL,jsonObject);
tokenVo = JsonFormatter.toObject(jsonObject.toString(), TokenResponse.class);
if(null != tokenVo && !tokenVo.getAccess_token().isEmpty()){
String ticketURL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+tokenVo.getAccess_token()+"&type=jsapi";
String res = HttpClientUtil.get(ticketURL, 60000);
ticketVo = JsonFormatter.toObject(res.toString(), JSTicketResponse.class);
LOGGER.info("3.获取ticket,url:{},result:{}",ticketURL, res);
if(null != ticketVo ){
cacheService.setJsonVlaueForRedis(key, ticketVo.getTicket(), ticketVo.getExpires_in());
jsapiTicket = ticketVo.getTicket();
}
}else{
LOGGER.error("获取token为空");
}
} catch (Exception e) {
LOGGER.error("获取异常", e);
}
}
WXconfig config = new WXconfig();
String nonce_str = create_nonce_str();
String timestamp = create_timestamp();
String string1 = "jsapi_ticket=" + jsapiTicket +
"&noncestr=" + nonce_str +
"×tamp=" + timestamp +
"&url=" + url;
LOGGER.info("4.验签string1:{}", string1);
signature = DigestUtils.shaHex(string1);
config.setLink(url);
config.setAppId(appId);
config.setTimestamp(timestamp);
config.setNonceStr(nonce_str);
config.setSignature(signature);
responseVo.setData(config);
TNCUtils.setResponseVo(response, responseVo, null);
} catch (Exception e) {
LOGGER.error("cleancache Exception", e);
}
3.项目要能直接访问该文件:MP_verify_XheMvg3q1lQU1Epf.txt ,文件内容XheMvg3q1lQU1Epf
http://**/inm/MP_verify_XheMvg3q1lQU1Epf.txt