分享内容到朋友圈微博等

喜欢的朋友可以关注下,粉丝也缺。


下面为大家介绍如何分享内容到朋友圈

//分享初始化
function initShare(){
//获取当前页面URL
var geturl=window.location.href.split('#')[0];
var getencodeurl=encodeURIComponent(geturl);
$.ajax({
type : 'POST',
url : "/GetSignature?url="+getencodeurl,
contentType:"application/x-www-form-urlencoded", 
success : function(response) {
var params = eval("(" + response + ")");
wx.config({
debug: false,
appId : params.appid, // 必填,公众号的唯一标识
timestamp : params.timestamp, // 必填,生成签名的时间戳
nonceStr : params.nonceStr,  // 必填,生成签名的随机串
signature : params.signature,// 必填,签名,见附录1
jsApiList : [ 'onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareQZone','onMenuShareWeibo','hideOptionMenu']
});
wx.ready(function() {
wx.hideOptionMenu();
});
}
})
}

function fenshare(){
//这里给你分享的内容设置 标题 地址 图片 内容(这里的地址跟图片的路径都必须是绝对路径)

ShareTimeline(标题,地址,图片,内容);
ShareAppMessage(标题,地址,图片,内容);
ShareQQ(标题,地址,图片,内容);
ShareWeibo(标题,地址,图片,内容);
ShareQZone(标题,地址,图片,内容);


}

function ShareTimeline(shareTitle,lineLink,imgUrl,descContent){
        //分享到朋友圈
wx.onMenuShareTimeline({
title: shareTitle, // 分享标题
link: lineLink, // 分享链接
desc: descContent, // 分享描述
img_width: "40",
                img_height: "40",
imgUrl: imgUrl, // 分享图标
success: function () { 
window.location.reload(); 
// 用户确认分享后执行的回调函数
},
cancel: function () { 
window.location.reload(); 
// 用户取消分享后执行的回调函数
},
fail: function (res) {
window.location.reload(); 
}
});
$("#mcover").css("display","block");  // 分享给好友圈按钮触动函数
}
function ShareAppMessage(shareTitle,lineLink,imgUrl,descContent){
//分享到朋友
wx.onMenuShareAppMessage({
   title: shareTitle, // 分享标题
   desc: descContent, // 分享描述
   link: lineLink, // 分享链接
   imgUrl: imgUrl, // 分享图标
//    type: link, // 分享类型,music、video或link,不填默认为link
//    dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
   success: function () { 
    window.location.reload(); 
       // 用户确认分享后执行的回调函数
   },
   cancel: function () { 
    window.location.reload(); 
       // 用户取消分享后执行的回调函数
   }
});
$("#mcover").css("display","block");  // 分享给好友圈按钮触动函数
}
function ShareQQ(shareTitle,lineLink,imgUrl,descContent){
//分享到QQ
wx.onMenuShareAppMessage({
   title: shareTitle, // 分享标题
   desc: descContent, // 分享描述
   link: lineLink, // 分享链接
   imgUrl: imgUrl, // 分享图标
//    type: link, // 分享类型,music、video或link,不填默认为link
//    dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
   success: function () { 
    window.location.reload(); 
       // 用户确认分享后执行的回调函数
   },
   cancel: function () { 
    window.location.reload(); 
       // 用户取消分享后执行的回调函数
   }
});
$("#mcover").css("display","block");  // 分享给好友圈按钮触动函数
}
function ShareQZone(shareTitle,lineLink,imgUrl,descContent){
//分享到QQ空间
wx.onMenuShareAppMessage({
   title: shareTitle, // 分享标题
   desc: descContent, // 分享描述
   link: lineLink, // 分享链接
   imgUrl: imgUrl, // 分享图标
//    type: link, // 分享类型,music、video或link,不填默认为link
//    dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
   success: function () { 
    window.location.reload(); 
       // 用户确认分享后执行的回调函数
   },
   cancel: function () { 
    window.location.reload(); 
       // 用户取消分享后执行的回调函数
   }
});
$("#mcover").css("display","block");  // 分享给好友圈按钮触动函数
}
function ShareWeibo(shareTitle,lineLink,imgUrl,descContent){
//分享到腾讯微博
wx.onMenuShareAppMessage({
   title: shareTitle, // 分享标题
   desc: descContent, // 分享描述
   link: lineLink, // 分享链接
   imgUrl: imgUrl, // 分享图标
//    type: link, // 分享类型,music、video或link,不填默认为link
//    dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
   success: function () { 
    window.location.reload(); 
       // 用户确认分享后执行的回调函数
   },
   cancel: function () { 
    window.location.reload(); 
       // 用户取消分享后执行的回调函数
   }
});
$("#mcover").css("display","block");  // 分享给好友圈按钮触动函数
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
//你的支付目录的Url,不需要转义,不包含#及其后面部分
String url = request.getParameter("url");
ServletContext application = this.getServletContext();
String jsapi_ticket=String.valueOf(application.getAttribute("jsapi_ticket"));
JSONObject obj = WeixinPay.getSignature(url, jsapi_ticket);
// Map<String, String> sign = Sign.sign(jsapi_ticket,url);
out.print(obj.toString());
out.flush();
out.close();
}catch (Exception e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}

}

//下面介绍的是获取ticket的方法

public RefToken(ServletContext application){
this.application = application;
}

public void run() {
String token = getAccessToken(appId,appSecret);
if(!"".equalsIgnoreCase(token)){
this.application.setAttribute("token", token);
System.out.println("token---init:"+token);
String ticket = getTicket();
this.application.setAttribute("jsapi_ticket", ticket);
System.out.println("ticket--init:"+ticket);
}
}

private static DefaultHttpClient httpclient;
static {
httpclient = new DefaultHttpClient();
httpclient = (DefaultHttpClient) HttpClientConnectionManager
.getSSLInstance(httpclient);
}

private static String getAccessToken(String appid, String secret) {
HttpGet get = HttpClientConnectionManager
.getGetMethod("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
+ appid + "&secret=" + secret);
HttpResponse response;
try {
response = httpclient.execute(get);
String jsonStr = EntityUtils
.toString(response.getEntity(), "utf-8");
JSONObject demoJson = JSONObject.fromObject(jsonStr);
return demoJson.getString("access_token");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return "";
}

private String getTicket(){
String token = (String) this.application.getAttribute("token");
if("".equalsIgnoreCase(token) || null ==  token ){
return "";
}
HttpGet get = HttpClientConnectionManager
.getGetMethod("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+token+"&type=jsapi");

HttpResponse response;
try {
response = httpclient.execute(get);
String jsonStr = EntityUtils
.toString(response.getEntity(), "utf-8");
JSONObject demoJson = JSONObject.fromObject(jsonStr);
return demoJson.getString("ticket");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return "";

}

如遇到问题欢迎进群308742428

喜欢的朋友可以关注下,粉丝也缺。


猜你喜欢

转载自blog.csdn.net/dsn727455218/article/details/65630959