白羽
2018-09-11
来源 :网络
阅读 1384
评论 0
摘要:本文将带你了解微信应用开发之微信开发第五篇手机端微信公众号自定义菜单及OAuth2.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格式就好
///
private static readonly string Menu_Data_Path = System.AppDomain.CurrentDomain.BaseDirectory + "/Data/menu.txt";
///
///
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=//。。。?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=//。。。?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=//。。。&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=//。。。&response_type=code&scope=snsapi_base&state=1#wechat_redirect"
}
]
},
{
"type": "view",
"name": "超链接",
"url": "//。。。。"
}
]
}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之微信频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号