微信公眾平台開發學習系列(四):微信分享內容修改與分享之后才能訪問某頁面


本來是想實現點擊一個按鈕就彈出微信的分享窗口,但是並沒有發現微信提供這樣的功能。可總是有收獲的,比如,如何修改微信的分享內容。這些功能是由微信JS-SDK提供。

官方文檔地址:http://mp.weixin.qq.com/wiki/11/74ad127cc054f6b80759c40f77ec03db.html

根據步驟,綁定域名-引入js-編寫js代碼

前台代碼:

 <script>
      wx.config({
          debug: false, // 開啟調試模式,調用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數,可以在pc端打開,參數信息會通過log打出,僅在pc端時才會打印。
          appId: '@ViewData["AppId"]', // 必填,公眾號的唯一標識
          timestamp: '@ViewData["Timestamp"]', // 必填,生成簽名的時間戳
          nonceStr: '@ViewData["NonceStr"]', // 必填,生成簽名的隨機串
          signature: '@ViewData["Signature"]', // 必填,簽名
          jsApiList: [
              'checkJsApi',
              'openLocation',
              'getLocation',
              'onMenuShareTimeline',
              'onMenuShareAppMessage',
              'scanQRCode'
          ] // 必填,需要使用的JS接口列表,所有JS接口列表見附錄2。詳見:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
      });

      wx.error(function (res) {
          console.log(res);
          alert('驗證失敗');
      });

      wx.ready(function () {
          wx.onMenuShareTimeline({
              title: '自定義的分享標題', // 分享標題
              link: 'http://www.baidu.com', // 分享鏈接
              imgUrl: 'https://images0.cnblogs.com/i/340216/201404/301756448922305.jpg', // 分享圖標
              success: function () {
              $.post("/Home/GetCookie");
                  alert('轉發成功!');
              },
              cancel: function () {
                  alert("轉發失敗");
              },
              fail: function (res) {
                  alert(JSON.stringify(res));
              }
          });
      });
  </script>

獲取timestamp等信息的后台代碼如下:

        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();
        }

 

以下內容與微信開發並沒什么關系,只是記錄而已。

根據需求,需要分享之后才能訪問某頁面,我的想法是再分享success是post一個請求,返回cookie,cookie記錄一個特殊值,同樣,session中也記錄同樣的值,在頁面上邏輯判斷。代碼為:

        public ActionResult ProtectView()
        {
            if (Request.Cookies["protect"] != null && Session["protect"] != null)
            {
                if (Session["protect"].ToString() == Request.Cookies["protect"].Value)
                {
                    return Content("你有權訪問此頁面");
                }
                return Content("你無權訪問此頁面,需要先分享圖文鏈接");
            }
            return Content("你無權訪問此頁面,需要先分享圖文鏈接");
        }

 

 post請求的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);
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM