直接上干貨了,
特別說明:我的項(xiàng)目中的有個(gè)母板頁(yè)(main.master),這樣所有的頁(yè)面引用一下,就行了??傊@里所有的代碼都在一個(gè)公共的頁(yè)面中實(shí)現(xiàn)就行了。
首先在你的項(xiàng)目中引用微信的JS庫(kù):
main.master中的代碼:
<head runat="server">
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script type="text/javascript">
$(function(){//JQ匿名函數(shù),當(dāng)前頁(yè)面加載完成后執(zhí)行 。
wx.config({
appId: '<%=appId%>',//在CodeBehind="main.master.cs" 頁(yè)面中設(shè)置。
timestamp: <%=timestamp%>,
nonceStr: '<%=nonceStr%>',
signature: '<%=signature%>',
jsApiList: [
// 所有要調(diào)用的 API 都要加到這個(gè)列表中
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ'
//這里加上你要調(diào)用微信的功能 API ,這里只列舉幾個(gè)分享的功能
]
});
});
//啟用微信監(jiān)聽
wx.ready(function () {
//分享給朋友
wx.onMenuShareAppMessage({
title: '<%=wxTitle%>',//在CodeBehind="main.master.cs" 頁(yè)面中設(shè)置,根據(jù)不同頁(yè)面?zhèn)鬟^來的信息進(jìn)行設(shè)置。
desc: '<%=wxDesc%>',
link: '<%=wxLinkUrl%>',
imgUrl: '<%=wxImgLinkUrl.Trim()==""?wxTempImgLinkUrl:wxImgLinkUrl%>'//這里用到了三目運(yùn)算符,當(dāng)引用的頁(yè)面沒有設(shè)置分享圖片時(shí)顯示系統(tǒng)默認(rèn)圖片。
});
//分享到朋友圈
wx.onMenuShareTimeline({
title: '<%=wxTitle%>',
link: '<%=wxLinkUrl%>',
imgUrl: '<%=wxImgLinkUrl.Trim()==""?wxTempImgLinkUrl:wxImgLinkUrl%>'
});
//分享到QQ
wx.onMenuShareQQ({
title: '<%=wxTitle%>',
desc: '<%=wxDesc%>',
link: '<%=wxLinkUrl%>',
imgUrl: '<%=wxImgLinkUrl.Trim()==""?wxTempImgLinkUrl:wxImgLinkUrl%>'
});
});
</script>
</head>
CodeBehind="***.master.cs" 中的代碼
//說明:MasterBasePage只是我自己封裝的一個(gè)基類,不用理會(huì)。
public partial class main : MasterBasePage
{
protected string appId = "";
protected string nonceStr = "";
protected string timestamp = "";
protected string signature = "";
protected string wxTitle = "";//根據(jù)不同文章設(shè)置分享的標(biāo)題。
protected string wxDesc = "";//根據(jù)不同文章設(shè)置分享的描述。
protected string wxLinkUrl = "";//根據(jù)不同文章設(shè)置分享的連接。
protected string wxImgLinkUrl = "";//根據(jù)不同文章的封面設(shè)置分享的圖片。
protected string wxTempImgLinkUrl = "";//這是分享時(shí)默認(rèn)顯示的圖片路徑。標(biāo)準(zhǔn)的應(yīng)該是400*400,請(qǐng)參考微信。
protected void Page_Load(object sender, EventArgs e)
{
if (!basePage.isMobile())//這里主是判斷是不是微信內(nèi)置瀏覽器,如果不是,退出。
{
return;
}
wxTempImgLinkUrl = Request.Url.Scheme + "://" + Request.Url.Authority.ToLower() + "/temp/main/images/wxlogo.png";
wxLinkUrl = Request.Url.Scheme + "://" + Request.Url.Authority.ToLower() + Request.RawUrl;
string RawUrl = Request.RawUrl;
appId = CacheHelper.Get<string>("appId" + RawUrl);//我會(huì)將一些常用的信息放到服務(wù)的Cache中,下次使用直接用就行。微信在提供接口的幫助文檔中也是這么說的。
nonceStr = CacheHelper.Get<string>("nonceStr" + RawUrl);
timestamp = CacheHelper.Get<string>("timestamp" + RawUrl);
signature = CacheHelper.Get<string>("signature" + RawUrl);
try
{
if ((string.IsNullOrEmpty(this.timestamp) || string.IsNullOrEmpty(this.nonceStr)) || string.IsNullOrEmpty(this.signature))
{//檢查基本信息是否存在,不存在則重新獲取生成。
Model.weixin_account model = new BLL.weixin_account().GetModel(1); //獲取公眾賬戶信息,我這里設(shè)置了多個(gè)微信公眾賬號(hào)信息,所以根據(jù)ID從數(shù)據(jù)庫(kù)中讀取。當(dāng)然您可以直接寫在下面的參數(shù)中。
appId = model.appid;
string url = Request.Url.Scheme + "://" + Request.Url.Authority.ToLower() + Request.RawUrl;//這里一定要用到當(dāng)前頁(yè)面的連接。
string jmdata = "jsapi_ticket={0}&noncestr={1}×tamp={2}&url={3}";
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();
nonceStr = Utils.GetRamStr(15);
Senparc.Weixin.MP.CommonAPIs.JsApiTicketContainer.Register(model.appid, model.appsecret);
string ticket = Senparc.Weixin.MP.CommonAPIs.JsApiTicketContainer.GetTicket(model.appid);
jmdata = string.Format(jmdata, ticket, nonceStr, timestamp, url);
signature = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(jmdata, "SHA1");
CacheHelper.Insert("appId" + RawUrl, appId, 120);//將信息存在Cache中
CacheHelper.Insert("nonceStr" + RawUrl, nonceStr, 120);
CacheHelper.Insert("timestamp" + RawUrl, timestamp, 120);
CacheHelper.Insert("signature" + RawUrl, signature, 120);
}
}
catch { }
}
//這個(gè)方法是當(dāng)引用頁(yè)面調(diào)用時(shí)設(shè)置分享信息的
public void bindShare(string title = "", string description = "", string img_ur = "")
{
wxTitle = title;
wxDesc = description;
wxImgLinkUrl = img_ur;
}
使用方法:
假如有一個(gè)新聞頁(yè)面:..../news/show-1.html
在這個(gè)頁(yè)面的cs頁(yè)中
根據(jù)ID獲取文章內(nèi)容得到 model (這里只是個(gè)列子,不要在意我這里的model是什么)
((main)base.Master).bindShare(model.title,model.description,model.img_url);
完事,您可以在微信中試試分享了。