封装微信jssdk自定义分享代码
var protocol = window.location.protocol; //获取协议
var host = window.location.host; //获取域名
var posuDomainUrl = protocol + "//" + host; function WeChat(url, title, img, desc) {
var configData = {
title: title, // 分享标题
desc: desc, // 分享描述
link: url, // 分享链接
imgUrl: img, // 分享图标
success: function () {
// 用户确认分享后执行的回调函数
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
}; $.ajax({
url: posuDomainUrl + '/wechatShare/wechatShare.do',
type: 'POST',
data: {url:url},
success: function (resp) {
if (resp.data) {
resp.data.debug=false;
resp.data.jsApiList=['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo','onMenuShareQZone'];
wx.config(resp.data);
wx.ready(function () {
//分享到朋友圈
wx.onMenuShareTimeline({ //例如分享到朋友圈的API
title: title, // 分享标题
link: url, // 分享链接
imgUrl: img, // 分享图标
success: function () {
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
}); //分享给朋友
wx.onMenuShareAppMessage({
title: title, // 分享标题
desc: desc, // 分享描述
link: url, // 分享链接
imgUrl: img, // 分享图标
type: '', // 分享类型,music、video或link,不填默认为link
dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
success: function () {
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
}); //分享到QQ
wx.onMenuShareQQ(configData); //分享到腾讯微博
wx.onMenuShareWeibo(configData); //分享到QQ空间
wx.onMenuShareQZone(configData); wx.error(function (res) {
//alert(res.errMsg); //打印错误消息。及把 debug:false,设置为debug:ture就可以直接在网页上看到弹出的错误提示
});
});
} else {
return;
}
},error:function(e){
console.log(e);
}
});
}
package com.hlziben.web.controller.webController.wechatShare; import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import com.hlziben.web.common.request.HonlineRestTemplate;
import com.hlziben.web.common.utils.EncryptUtils;
import com.hlziben.web.common.utils.HttpClientUtil;
import com.hlziben.web.common.utils.Setting;
import com.tzbao.api.common.utils.JsonResponse;
/**
*
*<dl>
*<dt>类名:WechatShareController</dt>
*<dd>描述: </dd>
*</dl>
*/
@Controller
@RequestMapping("wechatShare")
public class WechatShareController { public static final Log log = LogFactory.getLog(WechatShareController.class);
@Resource
private HonlineRestTemplate honlineRestTemplate;
@Resource
private StringRedisTemplate numberRedisTemplate; /**
* 向微信请求accessToken和ticket
* @param role
* @param phone
* @param code
* @param password
* @param repassword
* @param request
* @param response
* @param model
* @return
*/
@RequestMapping(method = RequestMethod.POST, value = "/wechatShare.do")
@ResponseBody
public Object register(
@RequestParam("url") String url, //前台传入URL
HttpServletRequest request, HttpServletResponse response, ModelMap model) { try{
//检查Redis中现有的accessToken和ticket
Long timeStamp=System.currentTimeMillis();
String accessToken=numberRedisTemplate.opsForValue().get("member_wechat_access_token");
String appId=Setting.getSetting("weixin.app.id");
String appSecret=Setting.getSetting("weixin.app.secret");
String ticket=numberRedisTemplate.opsForValue().get("wechatTicket"); //如果redis中的accessToken为空,则重新获取
if(StringUtils.isBlank(accessToken)||StringUtils.isBlank(ticket)){
String accessTokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appSecret;
JSONObject objectAuthToken=HttpClientUtil.httpGetRequest(accessTokenUrl);
accessToken=objectAuthToken.get("access_token").toString();
numberRedisTemplate.opsForValue().set("member_wechat_access_token",accessToken,Long.valueOf(6600), TimeUnit.SECONDS);
String ticketUrl="https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token="+accessToken;
JSONObject objectTicket=HttpClientUtil.httpGetRequest(ticketUrl);
ticket=objectTicket.get("ticket").toString();
numberRedisTemplate.opsForValue().set("wechatTicket", ticket,Long.valueOf(6600), TimeUnit.SECONDS);
} //获取微信accessToken及ticket
Map<String, Object> resultMap = new HashMap<>();
String nonceString=UUID.randomUUID().toString();
String encodeStr="jsapi_ticket="+ticket+"&noncestr="+nonceString+"×tamp="+timeStamp.toString()+"&url="+url;
String signature=EncryptUtils.codesBySHA1(encodeStr);
log.info(signature);
resultMap.put("appId",appId);
resultMap.put("nonceStr",nonceString);
resultMap.put("timestamp", timeStamp.toString());
resultMap.put("url", url);
resultMap.put("signature",signature);
resultMap.put("rawString",encodeStr);
return JsonResponse.getSuccessResponse(resultMap); }catch(Exception e){
log.error(e);
return JsonResponse.getServerErrorResponse("微信共享失败");
}
} }
封装微信jssdk自定义分享代码的更多相关文章
- 配置微信jssdk自定义分享
前段时间做这个功能的时候遇到这个问题,之前的话是微信自动抓取界面第一张图,现在微信更新api,必须自行配置,接入jssdk,才能实现该功能. 详细可以查看微信的jssdk文档 微信官方开发者文档 ...
- 使用微信JSSDK自定义分享内容
微信在6.0.2.58版本以后开始使用新的api,在Android系统中不能用以前的代码来自定义分享内容了. 现在自定义内容的方法走的是公众号的一套流程 1获取access_token 2得到toke ...
- .Net微信网页开发之使用微信JS-SDK自定义微信分享内容
第一步.微信JS-SDK的使用步骤,配置信息的生成获取讲解: 关于JS-SDK的使用步骤和timestamp(时间戳),nonceStr(随机串),signature(签名),access_token ...
- c#微信开发,使用JS-SDK自定义分享功能,分享朋友圈,分享给朋友等
如果一个后端开发掌握了微信开发,甚至有微信开发上线项目,这在换工作的时候 还是有竞争力的. 微信开发的资料很多,但是用asp.net c#进行微信开发好像比较少,或者资料不够完整. 使用JS-SDK自 ...
- 微信公众号jssdk自定义分享,二次分享自定义失败解决技巧
百度上自定义微信分享标题以及描述的解决方法有很多,基本上都能实现一次分享:流程基本上是这样的 1.首先引入微信jssdk =><script src="http://res.wx ...
- 微信应用js-sdk自定义分享图文
之前写过步骤 但是代码很少 这里奉献上我自己写的代码 我是用js做的 先奉上js部分的代码 <head> <meta charset="utf-8"> &l ...
- 使用微信JSSDK自定义微信分享标题、描述、和图标
最近做一个项目的时候用到微信的分享 ,实现定义分享标题,图片,了解到微信在发布JSSDK后,把包括自定义分享在内的众多网页服务接口进行了统一.如果要想自定义分享自己的网页信息给好友或朋友圈,就最好使用 ...
- 微信公众号 分享接口 签名通过 分享无效果(JSSDK自定义分享接口的策略调整)
为规范自定义分享链接功能在网页上的使用,自2017年4月25日起,JSSDK“分享到朋友圈”及“发送给朋友”接口,自定义的分享链接,其域名或路径必须与当前页面对应的公众号JS安全域名一致,否则将调用失 ...
- 微信JS-SDK实际分享功能
为了净化网络,整顿诱导分享及诱导关注行为,微信于2014年12月30日发布了<微信公众平台关于整顿诱导分享及诱导关注行为的公告>,微信平台开发者发现,原有的微信分享功能不能用了,在ipho ...
随机推荐
- zabbix 3.0 快速安装文档
下载地址:http://www.zabbix.com/download.php 官方文档:https://www.zabbix.com/documentation/3.0/manual/install ...
- 浏览器端 禁止 html 使用后退 或者替换后退功能..
知乎大佬的代码: 作者:独夜行 链接:https://www.zhihu.com/question/40511430/answer/166467343 来源:知乎 著作权归作者所有.商业转载请联系作者 ...
- git命令行使用
* git branch : 查看本地分支 * git branch -r : 查看远程分支 * git branch -a : 查看全部分支 * git branch name : 本地新建分支 * ...
- cmd 打开mysql客户端
- [转] 利用js实现 禁用浏览器后退
[From] http://blog.csdn.net/zc474235918/article/details/53138553 现在很多的内部系统,一些界面,都是用户手动点击退出按钮的.但是为了避免 ...
- 【研究】Struts2-052漏洞
漏洞描述: CVE-2017-9805 2017年9月5日,Apache Struts官方发布最新的安全公告称,Apache Struts 2.5.x的REST插件存在远程代码执行高危漏洞,漏洞编号为 ...
- Trie - 20181113
442. Implement Trie (Prefix Tree) class TrieNode { public boolean isWord; public TrieNode[] children ...
- JENKINS安装及新建用户,权限配置
JENKINS安装及新建用户,权限配置 1. 下载安装 jenkins 官网地址https://jenkins.io/index.html 下载地址https://jenkins.io/downloa ...
- BeautifulSoup库应用实例
获取博客园本人的积分排名数据: 1. 抓包获取积分排名数据返回接口:http://www.cnblogs.com/belle-ls/mvc/blog/sidecolumn.aspx?blogApp=b ...
- 关于kernel的疑问,不解
(1)最近看的关于linux的部分主要是底层部分,linux对设备是如何访问,结果还是通过地址.机制差不错知道,下一步是阅读相关代码进一步理清内核,同时了解驱动的开发. 主要的疑问是对于高端内存映射, ...