您的位置:首页 > 移动开发

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

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

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

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

具体的代码参考

删除菜单接口调用方法:

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就可以进去自己的网站。当然细作的话,有可能会有疑问欢迎提问问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: