zoukankan      html  css  js  c++  java
  • 微信公众平台开发学习系列(四):微信分享内容修改与分享之后才能访问某页面

    本来是想实现点击一个按钮就弹出微信的分享窗口,但是并没有发现微信提供这样的功能。可总是有收获的,比如,如何修改微信的分享内容。这些功能是由微信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: '//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);
            }
  • 相关阅读:
    hdu 3342 Legal or Not 拓排序
    hdu 1596 find the safest road Dijkstra
    hdu 1874 畅通工程续 Dijkstra
    poj 2676 sudoku dfs
    poj 2251 BFS
    poj Prime Path BFS
    poj 3278 BFS
    poj 2387 Dijkstra 模板
    poj 3083 DFS 和BFS
    poj 1062 昂贵的聘礼 dijkstra
  • 原文地址:https://www.cnblogs.com/dashuailuoli/p/5125524.html
Copyright © 2011-2022 走看看