重庆小潘seo博客

当前位置:首页 > 重庆网络营销 > 小潘杂谈 >

小潘杂谈

关于微信自定义分享功能的实现代码

时间:2020-09-07 16:00:09 作者:重庆seo小潘 来源:
本篇文章给大家分享的内容是关于微信自定义分享功能的实现代码,内容很详细,有需要的朋友可以参考一下,希望可以帮助到你们. 前端时间,开发了一个资讯类的项目,但销售部门进行微信推广时,分享的链接直接是网页链接加分享符号,即难看又不正规,于是研究了一下微

本篇文章给大家分享的内容是关于微信自定义分享功能的实现代码,内容很详细,有需要的朋友可以参考一下,希望可以帮助到你们.

前端时间,开发了一个资讯类的项目,但销售部门进行微信推广时,分享的链接直接是网页链接加分享符号,即难看又不正规,于是研究了一下微信自定义的分享功能

前期准备工作:

1.认证公众号的appId,appSecret

2.各种获取微信信息链接(此部分查找微信自定义分享API,地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115)# 获取access_token请求地址getAccessTokenUrl: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s#获取accessTokengetAccessTokenOAuthUrl: https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code# 获取用户基本信息请求地址getUserInfoUrl: https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN#获取codegetCodeUrl: https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=%s&scope=%s&state=%s#wechat_redirect#获取ticketgetTicketUrl: https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi

3.controller层/*** 微信配置信息实体*/@Autowiredprivate WeiXinProperties weiXinProperties;//微信参数private String accessToken;private String jsApiTicket;//获取参数的时刻private Long getTiketTime = 0L;private Long getTokenTime = 0L;//参数的有效时间,单位是秒(s)private Long tokenExpireTime = 0L;private Long ticketExpireTime = 0L; /*** 微信自定义分享*/@RequestMapping(value = "/getShareInfo", method = RequestMethod.POST)public Map<String, String> getShareInfo(HttpServletRequest request,HttpServletResponse response, String url) {//当前时间long now = System.currentTimeMillis();//判断accessToken是否已经存在或者token是否过期if (StringUtils.isBlank(accessToken) || (now - getTokenTime > tokenExpireTime * 1000)) {JSONObject tokenInfo = getAccessToken();if (tokenInfo != null) {accessToken = tokenInfo.getString("access_token");tokenExpireTime = tokenInfo.getLongValue("expires_in");//获取token的时间getTokenTime = System.currentTimeMillis();log.info("accessToken====>" + accessToken);log.info("tokenExpireTime====>" + tokenExpireTime + "s");log.info("getTokenTime====>" + getTokenTime + "ms");} else {log.info("====>tokenInfo is null~");log.info("====>failure of getting tokenInfo,please do some check~");}}//判断jsApiTicket是否已经存在或者是否过期if (StringUtils.isBlank(jsApiTicket) || (now - getTiketTime > ticketExpireTime * 1000)) {JSONObject ticketInfo = getJsApiTicket(accessToken);if (ticketInfo != null) {log.info("ticketInfo====>" + ticketInfo.toJSONString());jsApiTicket = ticketInfo.getString("ticket");ticketExpireTime = ticketInfo.getLongValue("expires_in");getTiketTime = System.currentTimeMillis();log.info("jsApiTicket====>" + jsApiTicket);log.info("ticketExpireTime====>" + ticketExpireTime + "s");log.info("getTiketTime====>" + getTiketTime + "ms");} else {log.info("====>ticketInfo is null~");log.info("====>failure of getting tokenInfo,please do some check~");}}//生成微信权限验证的参数Map<String, String> wechatParam = makeWXTicket(jsApiTicket, url);return wechatParam;}//获取accessTokenprivate JSONObject getAccessToken() {//String accessTokenUrl = https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET//获取微信端的accessTokenString requestUrl = String.format(weiXinProperties.getGetAccessTokenUrl(), weiXinProperties.getAppId(), weiXinProperties.getAppSecret());String result = send(requestUrl);JSONObject jsonObject = JSON.parseObject(result);return jsonObject;}//获取ticketprivate JSONObject getJsApiTicket(String access_token) {//String apiTicketUrl = https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi// 通过acessToken 获取ticketString requestUrl = String.format(weiXinProperties.getGetTicketUrl(), access_token);String result = send(requestUrl);JSONObject jsonObject = JSON.parseObject(result);return jsonObject;}//生成微信权限验证的参数public Map<String, String> makeWXTicket(String jsApiTicket, String url) {Map<String, String> ret = new HashMap<String, String>();String nonceStr = createNonceStr();String timestamp = createTimestamp();String string1;String signature = "";//注意这里参数名必须全部小写,且必须有序string1 = "jsapi_ticket=" + jsApiTicket +"&noncestr=" + nonceStr +"&timestamp=" + timestamp +"&url=" + url;log.info("String1=====>" + string1);try {MessageDigest crypt = MessageDigest.getInstance("SHA-1");crypt.reset();crypt.update(string1.getBytes("UTF-8"));signature = byteToHex(crypt.digest());log.info("signature=====>" + signature);} catch (NoSuchAlgorithmException e) {log.error("WeChatController.makeWXTicket=====Start");log.error(e.getMessage(), e);log.error("WeChatController.makeWXTicket=====End");} catch (UnsupportedEncodingException e) {log.error("WeChatController.makeWXTicket=====Start");log.error(e.getMessage(), e);log.error("WeChatController.makeWXTicket=====End");}ret.put("url", url);ret.put("jsapi_ticket", jsApiTicket);ret.put("nonceStr", nonceStr);ret.put("timestamp", timestamp);ret.put("signature", signature);ret.put("appid", weiXinProperties.getAppId());return ret;}/*** 发送请求** @param url* @return* @throws Exception*/String send(String url) {return HttpClientTools.post(url);}//字节数组转换为十六进制字符串private static String byteToHex(final byte[] hash) {Formatter formatter = new Formatter();for (byte b : hash) {formatter.format("%02x", b);}String result = formatter.toString();formatter.close();return result;}//生成随机字符串private static String createNonceStr() {return UUID.randomUUID().toString();}//生成时间戳private static String createTimestamp() {return Long.toString(System.currentTimeMillis() / 1000);}

4.引入share.js.要分享的页面

$(function(){var url = location.href.split('#').toString();//url不能写死$.ajax({type : "post",url : "/user/login/getShareInfo",dataType : "json",async : false,data:{url:url},success : function(data) {wx.config({debug: false,////生产环境需要关闭debug模式appId: data.appid,//appId通过微信服务号后台查看timestamp: data.timestamp,//生成签名的时间戳nonceStr: data.nonceStr,//生成签名的随机字符串signature: data.signature,//签名jsApiList: [//需要调用的JS接口列表'checkJsApi',//判断当前客户端版本是否支持指定JS接口'onMenuShareTimeline',//分享给好友'onMenuShareAppMessage'//分享到朋友圈]});},error: function(xhr, status, error) {//alert(status);//alert(xhr.responseText);}})});

5.在要分享的页面中引入,微信分享的核心js和share.js

<script type="text/javascript" alt="关于微信自定义分享功能的实现代码" src="/resources/js/jweixin-1.2.0.js"></script><script type="text/javascript" alt="关于微信自定义分享功能的实现代码" src="/resources/js/share.js"></script>6.在当前页面<script>中,此部分可以直接写到share.js中/*分享代码*/wx.ready(function() {// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。console.log('weixin 验证成功');// 分享到朋友圈wx.onMenuShareTimeline({title: detail_title, // 分享标题link: link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致imgUrl: 'http://develop.fangxinyuesao.com/resources/images/logo.png', // 分享图标success: function() {// 用户确认分享后执行的回调函数},cancel: function() {// 用户取消分享后执行的回调函数}});// 分享给朋友wx.onMenuShareAppMessage({title: detail_title, // 分享标题desc: '来自妇幼头条的分享', // 分享描述link: link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致imgUrl: 'http://develop.fangxinyuesao.com/resources/images/logo.png', // 分享图标type: '', // 分享类型,music、video或link,不填默认为linkdataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空success: function() {// 用户确认分享后执行的回调函数},cancel: function() {// 用户取消分享后执行的回调函数}});});wx.error(function(res) {// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。console.log('weixin 验证失败');console.log(res);});注意事项:分享要设置网站白名单,电脑端调试时,推荐使用微信WEB开发工具

相关推荐:

微信自定义菜单的处理开发示例,微信自定义示例以上就是关于微信自定义分享功能的实现代码的详细内容,更多请关注小潘博客其它相关文章!