本來(lái)是想實(shí)現(xiàn)點(diǎn)擊一個(gè)按鈕就彈出微信的分享窗口,但是并沒(méi)有發(fā)現(xiàn)微信提供這樣的功能。可總是有收獲的,比如,如何修改微信的分享內(nèi)容。這些功能是由微信JS-SDK提供。
官方文檔地址:http://mp.weixin.QQ.com/wiki/11/74ad127cc054f6b80759c40f77ec03db.html
根據(jù)步驟,綁定域名-引入js-編寫(xiě)js代碼
前臺(tái)代碼:
<script> wx.config({ debug: false, // 開(kāi)啟調(diào)試模式,調(diào)用的所有api的返回值會(huì)在客戶(hù)端alert出來(lái),若要查看傳入的參數(shù),可以在pc端打開(kāi),參數(shù)信息會(huì)通過(guò)log打出,僅在pc端時(shí)才會(huì)打印。 appId: '@ViewData["AppId"]', // 必填,公眾號(hào)的唯一標(biāo)識(shí) timestamp: '@ViewData["Timestamp"]', // 必填,生成簽名的時(shí)間戳 nonceStr: '@ViewData["NonceStr"]', // 必填,生成簽名的隨機(jī)串 signature: '@ViewData["Signature"]', // 必填,簽名 jsApiList: [ 'checkJsApi', 'openLocation', 'getLocation', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'scanQRCode' ] // 必填,需要使用的JS接口列表,所有JS接口列表見(jiàn)附錄2。詳見(jiàn):http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html }); wx.error(function (res) { console.log(res); alert('驗(yàn)證失敗'); }); wx.ready(function () { wx.onMenuShareTimeline({ title: '自定義的分享標(biāo)題', // 分享標(biāo)題 link: 'http://www.baidu.com', // 分享鏈接 imgUrl: 'http://images.cnitblog.com/i/340216/201404/301756448922305.jpg', // 分享圖標(biāo) success: function () { $.post("/Home/GetCookie"); alert('轉(zhuǎn)發(fā)成功!'); }, cancel: function () { alert("轉(zhuǎn)發(fā)失敗"); }, fail: function (res) { alert(JSON.stringify(res)); } }); }); </script>
獲取timestamp等信息的后臺(tái)代碼如下:
public ActionResult Index() { var timestamp = JSSDKHelper.GetTimestamp(); var nonceStr = JSSDKHelper.GetNoncestr(); string ticket = accessTokenContainer.TryGetJsApiTicket(appId, secret); var signature = JSSDKHelper.GetSignature(ticket, nonceStr, timestamp, Request.Url.AbsoluteUri); ViewData["AppId"] = appId; ViewData["Timestamp"] = timestamp; ViewData["NonceStr"] = nonceStr; ViewData["Signature"] = signature; return View(); }
以下內(nèi)容與微信開(kāi)發(fā)并沒(méi)什么關(guān)系,只是記錄而已。
根據(jù)需求,需要分享之后才能訪(fǎng)問(wèn)某頁(yè)面,我的想法是再分享success是post一個(gè)請(qǐng)求,返回cookie,cookie記錄一個(gè)特殊值,同樣,session中也記錄同樣的值,在頁(yè)面上邏輯判斷。代碼為:
public ActionResult PRotectView() { if (Request.Cookies["protect"] != null && Session["protect"] != null) { if (Session["protect"].ToString() == Request.Cookies["protect"].Value) { return Content("你有權(quán)訪(fǎng)問(wèn)此頁(yè)面"); } return Content("你無(wú)權(quán)訪(fǎng)問(wèn)此頁(yè)面,需要先分享圖文鏈接"); } return Content("你無(wú)權(quán)訪(fǎng)問(wèn)此頁(yè)面,需要先分享圖文鏈接"); }
post請(qǐng)求的action為:
[HttpPost] public void GetCookie() { Guid guid = Guid.NewGuid(); HttpCookie cookie = Request.Cookies["protect"]; cookie = new HttpCookie("protect",guid.ToString()); Session["protect"] = guid.ToString(); cookie.Expires = DateTime.Now.AddMinutes(10); Response.Cookies.Add(cookie); }
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注