您现在的位置是:网站首页> 编程开发> java 编程开发
java在H5中微信公众号分享
2021-06-07【java】 1573人已围观
简介今天研究了一下关于微信公众号里的分享,本来是想要做成下图一样的分享方式。但后来发现H5里根本不支持这种方式。微信对H5的支持还是挺不友好的,上图中我发现是小程序或APP内可以做到以上的方式。公众号里虽然有对外的分享接口,但只能做到回调的判断。我贴一下自己研究的代码,以下代码是H5里做的一些代码的总结。 前端代码:<input type="hidden"
java在H5中微信公众号分享
最后更新:2021-06-07 11:32:49
推荐指数:
今天研究了一下关于微信公众号里的分享,本来是想要做成下图一样的分享方式。但后来发现H5里根本不支持这种方式。
微信对H5的支持还是挺不友好的,上图中我发现是小程序或APP内可以做到以上的方式。
公众号里虽然有对外的分享接口,但只能做到回调的判断。
我贴一下自己研究的代码,以下代码是H5里做的一些代码的总结。
前端代码:
<input type="hidden" id="title" value="title"> <input type="hidden" id="desc" value="desc"> <input type="hidden" id="imgUrl" value="imgUrl"> <input type="hidden" id="path" value="http://www.shengsenorg.com"> <!-- --> <script type="text/JavaScript" src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> <script> function share() { var title = $("#title").val(); var desc = $("#desc").val(); // var name = $("#program_name").val(); var imgUrl = $("#imgUrl").val(); var path = $("#path").val(); var url = location.href.split('#')[0]; // var url = "http://www.shengsenorg.com"; var signature, timestamp, noncestr, appid; /**/ $.ajax({ url: "/ajax/weixinshare/getShare", type: 'GET', data: { url: url }, success: function (d) { signature = d.data.wxSignature; timestamp = d.data.wxTimestamp; noncestr = d.data.wxNoncestr; appid = d.data.appId; wx.config({ debug: false, // ,apialertpclogpcappId: appid, // timestamp: timestamp, // nonceStr: noncestr, // signature: signature,// jsApiList: ['checkJsApi', 'onMenuShareAppMessage','onMenuShareTimeline'] // JS// jsApiList: ['checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'updateTimelineShareData', 'updateAppMessageShareData', 'getLocation', 'scanQRCode', 'uploadImage', 'downloadImage', 'showAllNonBaseMenuItem', 'hideAllNonBaseMenuItem'] // JS}); wx.checkJsApi({ jsApiList: ['checkJsApi', 'onMenuShareAppMessage', 'onMenuShareTimeline' ], success: function (res) { console.log(res) // apitruefalse // {"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"} } }); wx.error(function (res) { alert("error00:" + JSON.stringify(res)); }); wx.ready(function () { //wx.onMenuShareAppMessage({ title: title, // desc: desc, // link: path, // JSimgUrl: imgUrl, // s type: 'link', // ,musicvideolinklink dataUrl: '', // typemusicvideosuccess: function () { // alert("OK"); }, cancel: function () { // alert("fail"); } }); //wx.onMenuShareTimeline({ title: title, // link: path, // JSimgUrl: imgUrl, // success: function () { // }, }); }); }, error: function (error) { console.log(error); } }); } </script>
后端代码:
@Autowired RedisUtils redisUtils; WeixinShareResponse shareSignature(String appid, String secret, String shareUrl) { String key = ; String access_token = ; Object o = .redisUtils.get(key); String s = ; (o != ) { s = Convert.toStr(o); (s.contains()) { s = .getToken(appid, secret); } } { s = .getToken(appid, secret); .redisUtils.set(key, s, , TimeUnit.HOURS); } JSONObject jsonObjectToken = ; { jsonObjectToken = JSONUtil.parseObj(s); } (Exception var21) { var21.printStackTrace(); .redisUtils.deleteKey(key); s = .getToken(appid, secret); jsonObjectToken = JSONUtil.parseObj(s); } (jsonObjectToken != ) { access_token = Convert.toStr(jsonObjectToken.get()); } String requestUrl = ; requestUrl = requestUrl.replace(, access_token); String s1 = HttpUtil.get(requestUrl); JSONObject jsonObject = JSONUtil.parseObj(s1); (jsonObject != ) { { String ticket = jsonObject.getStr(); String noncestr = create_nonce_str(); String timestamp = create_timestamp(); String param = + ticket + + noncestr + + timestamp + + shareUrl; String signature = ; { MessageDigest crypt = MessageDigest.getInstance(); crypt.reset(); crypt.update(param.getBytes()); signature = byteToHex(crypt.digest()); } (NoSuchAlgorithmException var19) { var19.printStackTrace(); } (UnsupportedEncodingException var20) { var20.printStackTrace(); } WeixinShareResponse weixinShareResponse = WeixinShareResponse(); weixinShareResponse.setWxNoncestr(noncestr); weixinShareResponse.setWxTimestamp(timestamp); weixinShareResponse.setWxSignature(signature); weixinShareResponse.setAppId(appid); weixinShareResponse.setWxUrl(shareUrl); String logData = JSONUtil.toJsonStr(weixinShareResponse); System.out.print(+ logData + + ticket); weixinShareResponse; } (Exception var22) { .redisUtils.deleteKey(key); var22.printStackTrace(); } } ; } String getToken(String appid, String secret) { String requestTokenUrl = ; requestTokenUrl = requestTokenUrl.replace(, appid); requestTokenUrl = requestTokenUrl.replace(, secret); String s = HttpUtil.get(requestTokenUrl); s; } String byteToHex([] hash) { Formatter formatter = Formatter(); [] var2 = hash; var3 = hash.length; (var4 = ; var4 < var3; ++var4) { b = var2[var4]; formatter.format(, b); } String result = formatter.toString(); formatter.close(); result; } String create_nonce_str() { UUID.randomUUID().toString(); } String create_timestamp() { Long.toString(System.currentTimeMillis() / ); } WeixinShareUtil(RedisUtils redisUtils) { .redisUtils = redisUtils; }
下期说我到底是怎么解决H5里的分享问题。
很赞哦! (0)
文章评论
验证码: