码迷,mamicode.com
首页 > 微信 > 详细

微信开发第五篇手机端微信公众号自定义菜单及OAuth2.0授权页面

时间:2015-08-28 15:42:44      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:微信菜单点击进去超链接并传参   自定义菜单点击进去网站并传递参数   微信菜单超链接参数   微信自定义菜单   微信oauth2.0授权   

     说到自定义菜单,首先要想到调用微信的接口,其实微信公众号开发本身就是看对不对微信公众号接口的熟悉程度,我也是在项目中才开始接触微信公众号开发的,很感谢公司能给我这个项目机会。其实对于一个程序员来说最宝贵的是他的学习能力,而不是经验,不扯没用的了。

      菜单上一篇讲到了怎么查看微信开发文档,那么很容易找到自定义菜单管理,根据里面的内容可以做一下思路

手机微信客户端与微信服务器交互,再由微信服务器与咱们自己的服务器交互,在第一次交互时先删除原始的那种可以打字的那种菜单,之后设置自己新的菜单,最后把自己的菜单加载到用户的微信客户端。

具体的代码参考

删除菜单接口调用方法:

     public string DelMenu() //删除菜单
        {
            string url_Menu_Delete = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + this.GetAccessToken();
            string result = Functions.webRequestGet(url_Menu_Delete);
            return result;
        }

设置最新菜单

    public string SetMenu() //设置最新菜单
        {
            string url_Menu_Create = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + this.GetAccessToken();
            //拼接字符串
            //MCES gtt = new MCES();
            // string postData = gtt.createMenuDate();
            //加载文价
            FileUtility fileu = new FileUtility();
            string postData = fileu.Read(Menu_Data_Path);
            string result = Functions.webRequestPost(url_Menu_Create, postData);
            return result;
        }


删除和设置菜单都会用到GetAccessToken()这个方法

这个方法其实就是获得access_token的值

需要查看开发文档找到获取access_token的接口方法

即是:

  public string GetAccessToken() //获取通行证
        {
            string url_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + devlopID + "&secret=" + devlogPsw;
            string result = Functions.webRequestGet(url_token);
            accessToken deserializedProduct = (accessToken)JsonConvert.DeserializeObject(result, typeof(accessToken));
            this.AccessToken = deserializedProduct.access_Token;
            return this.AccessToken;
        }

加载菜单到页面,其中里面我用到了读文件夹,其实也可以根据需要进行拼接菜单只要是json格式就好


       /// <summary>
        /// 菜单文件路径
        /// </summary>
        private static readonly string Menu_Data_Path = System.AppDomain.CurrentDomain.BaseDirectory + "/Data/menu.txt";


        /// <summary>
        /// 加载菜单
        /// </summary>
        /// <returns></returns>
        public string LoadMenu()
        {
            FileUtility file = new FileUtility();
            return file.Read(Menu_Data_Path);
        }

menu.txt文件里面

{
    "button": [
        {
            "type": "click",
            "name": "按钮1",
            "key": "menu_1"
        },
        {
            "name": "按钮",
            "sub_button": [
                {
                    "type": "click",
                    "name": "按钮2(1)",
                    "key": "menu_2"
                },
                {
                    "type": "click",
                    "name": "按钮2(2)",
                    "key": "menu_3"
                }
            ]
        },
        {
            "type": "view",
            "name": "菜单3",
            "url": "url"
        }
    ]
}


这样就可以实现了公众号菜单的加载

有的时间需要直接点击进入自己的页面而不是单纯的点击事件,这就是要考虑到了另外一个知识,那就是应用授权页面

说到授权,其实有两种一种是弹出提示用户授权,一种是不弹出提示默认授权

授权首先要在公众号中设置

授权域名,也就是点击后要访问那个网址

如:

技术分享


设置之后就可以调用授权接口了,我把我代码的两个授权页面贴出来

            string code = "";
            //弹出授权页面(如在不弹出授权页面基础下未获得openid,则弹出授权页面,提示用户授权)
            if (Request.QueryString["auth"] != null && Request.QueryString["auth"] != "" && Request.QueryString["auth"] == "1")
            {
                string dd = Request.QueryString["auth"];
                Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http://。。。?reurl=" + reurl + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
            }
            else
            {
                //不弹出授权页面
                Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http://。。。?reurl=" + reurl + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect");

            }

就可以通过授权获得手机的账号openid 而进去自己的页面 实现了点击菜单按钮传参。

这种菜单例如

{
    "button": [
        {
            "type": "click",
            "name": "按钮",
            "key": "menu_1"
        },
        {
            "name": "菜单2",
            "sub_button": [
                {
                    "type": "view",
                    "name": "超链接1",
                    "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=“ ”&redirect_uri=http://。。。&response_type=code&scope=snsapi_base&state=1#wechat_redirect"
                },
                {
                    "type": "view",
                    "name": "超链接2",
                    "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=“ ”&redirect_uri=http://。。。&response_type=code&scope=snsapi_base&state=1#wechat_redirect"
                }
            ]
        },
        {
            "type": "view",
            "name": "超链接",
            "url": "http://。。。。"
        }
    ]
}

我现在用的就是这种菜单,直接点击“菜单2”的超链接1和2就可以进去自己的网站。当然细作的话,有可能会有疑问欢迎提问问题。


版权声明:本文为博主原创文章,未经博主允许不得转载。

微信开发第五篇手机端微信公众号自定义菜单及OAuth2.0授权页面

标签:微信菜单点击进去超链接并传参   自定义菜单点击进去网站并传递参数   微信菜单超链接参数   微信自定义菜单   微信oauth2.0授权   

原文地址:http://blog.csdn.net/u010773333/article/details/48051239

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!