服务器段逻辑实现
module WeixinShare
extend ActiveSupport::Concern
App_id = "********************"
App_secret = "**********************"
def WeixinShare.weixin_signature(page_url)
share_info = $redis.hgetall(page_url)
if share_info.present?
before_time = share_info["timestamp"].to_i
current_time = Time.now.to_i
if current_time - before_time > 7000
weixin_params=WeixinShare.generate_sha1_sign(page_url)
else
weixin_params={"appid"=>App_id,"timestamp"=>share_info["timestamp"],"nonceStr"=>share_info["noncestr"],"signature"=>share_info["signature"]}
end
else
weixin_params=WeixinShare.generate_sha1_sign(page_url)
end
end
def WeixinShare.generate_sha1_sign(page_url)
noncestr = SecureRandom.hex(8)
jsapi_ticket = WeixinShare.jsapi_ticket
timestamp = Time.now.to_i.to_s
url = page_url
sign_order = "jsapi_ticket="+jsapi_ticket+"&noncestr="+noncestr+"×tamp="+timestamp+"&url="+url
require 'digest/sha1'
sha1_sign = Digest::SHA1.hexdigest(sign_order)
$redis.hmset(page_url,"appid",App_id,"timestamp",timestamp,"noncestr",noncestr,"signature",sha1_sign)
weixin_params={"timestamp"=>timestamp,"nonceStr"=>noncestr,"signature"=>sha1_sign,"appid"=>App_id}
end
def WeixinShare.jsapi_ticket
require 'net/http'
url = "https://api.weixin.qq.com/cgi-bin/token?"+
"grant_type=client_credential&appid="+App_id+"&secret="+App_secret
json= JSON.parse(Net::HTTP.get(URI(url)))
url_ticket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?"+
"access_token="+json["access_token"]+"&type=jsapi"
ticket_json = JSON.parse(Net::HTTP.get(URI(url_ticket)))
ticket_json["ticket"]
end
end
直接调用 WeixinShare.weixin_signature(page_url)就可以生成签名
详细文档请看http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
网页中代码对分享调用的代码
weixin_info= {
title:$("#share_title").val(),
link:"",
imgUrl:$("#share_image").val(),
desc:$("#share_title").val()
}
$(document).ready(function(){
$.ajax({
type:"GET",
url:"/articles/weixin_sign",
data:{url:window.location.href},
dataType:"json",
success:function(data){
console.log(data)
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: data.appid, // 必填,公众号的唯一标识
timestamp: parseInt(data.timestamp), // 必填,生成签名的时间戳
nonceStr: data.nonceStr, // 必填,生成签名的随机串
signature: data.signature,// 必填,签名,见附录1
jsApiList: ["onMenuShareTimeline",
"onMenuShareAppMessage",
"onMenuShareQQ",
"onMenuShareWeibo"
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
}
});
wx.ready(function(){
wx.onMenuShareTimeline({
title: weixin_info.title, // 分享标题
link: weixin_info.link, // 分享链接
imgUrl: weixin_info.imgUrl, // 分享图标
success: function () {
$('#inference_answer').css("display","block")
$('#answer_sharemcover').hide();
$('#sharemcover').hide();
},
cancel: function () {
// alert("onMenuShareTimeline")
// 用户取消分享后执行的回调函数
}
});
wx.onMenuShareAppMessage({
title: weixin_info.title, // 分享标题
desc: weixin_info.desc, // 分享描述
link: weixin_info.link, // 分享链接
imgUrl: weixin_info.imgUrl, // 分享图标
success: function () {
$('#inference_answer').css("display","block")
$('#answer_sharemcover').hide();
$('#sharemcover').hide();
// 用户确认分享后执行的回调函数// 用户确认分享后执行的回调函数
},
cancel: function () {
// alert("onMenuShareAppMessage")
// 用户取消分享后执行的回调函数
}
});
wx.onMenuShareQQ({
title: weixin_info.title, // 分享标题
desc: weixin_info.desc, // 分享描述
link: weixin_info.link, // 分享链接
imgUrl: weixin_info.imgUrl, // 分享图标
success: function () {
$('#inference_answer').css("display","block")
$('#answer_sharemcover').hide();
$('#sharemcover').hide();
// 用户确认分享后执行的回调函数// 用户确认分享后执行的回调函数
},
cancel: function () {
// alert("onMenuShareQQ")
// 用户取消分享后执行的回调函数
}
});
wx.onMenuShareWeibo({
title: weixin_info.title, // 分享标题
desc: weixin_info.desc, // 分享描述
link: weixin_info.link, // 分享链接
imgUrl: weixin_info.imgUrl, // 分享图标
success: function () {
$('#inference_answer').css("display","block")
$('#answer_sharemcover').hide();
$('#sharemcover').hide();
// 用户确认分享后执行的回调函数// 用户确认分享后执行的回调函数
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
});
});