WeChat 공개 플랫폼 서비스 계정에서 사용자 정의 메뉴 API를 오픈했습니다. 이 사례에서는 WeChat 공개 계정의 사용자 정의 메뉴 기능을 개발하기 위해 C#을 도입했습니다. 이를 바탕으로 보다 완벽한 맞춤형 메뉴 관리 기능을 개발할 수 있습니다
코드 복사 코드는 다음과 같습니다.
nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <title>test</title>
코드 복사 코드는 다음과 같습니다.
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; namespace guotaotao_weixin { public partial class cm : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { weixin wx = new weixin(); //lt_msg.Text = wx.GetAccessToken(); lt_msg.Text = wx.DelMenu(); lt_msg.Text += wx.SetMenu(); } } } }
코드 복사코드는 다음과 같습니다.
namespace guotaotao_weixin { public partial class cm { /// <summary> /// form1 控件。 /// </summary> /// <remarks> /// 自动生成的字段。 /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。 /// </remarks> protected global::System.Web.UI.HtmlControls.HtmlForm form1; /// <summary> /// lt_msg 控件。 /// </summary> /// <remarks> /// 自动生成的字段。 /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。 /// </remarks> protected global::System.Web.UI.WebControls.Literal lt_msg; } }
코드 복사코드는 다음과 같습니다.
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using guotaotao_weixin.database; using guotaotao.database; namespace guotaotao_weixin { public class guotaotao { private string _content = ""; private string _FromUserName = ""; public int msgType = 0; public int isFirst = 0; public int pid = 0; #region "构造函数" public guotaotao() { } public guotaotao(string content) { this._content = content; } public guotaotao(string content,string FromUserName) { this._content = content; this._FromUserName = FromUserName; } #endregion public string createMenuDate() { string postData = "{" + "\r\n"; postData += "\"button\":[ " + "\r\n"; postData += "{ " + "\r\n"; postData += "\"name\":\"产品\"," + "\r\n"; postData += "\"sub_button\":[" + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"click\"," + "\r\n"; postData += " \"name\":\"无糖系列\", " + "\r\n"; postData += " \"key\":\"gtt_menu_001001\"" + "\r\n"; postData += "}," + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"click\"," + "\r\n"; postData += " \"name\":\"干果系列\", " + "\r\n"; postData += " \"key\":\"gtt_menu_001002\"" + "\r\n"; postData += "}," + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"click\"," + "\r\n"; postData += " \"name\":\"休闲系列\", " + "\r\n"; postData += " \"key\":\"gtt_menu_001003\"" + "\r\n"; postData += "}," + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"click\"," + "\r\n"; postData += " \"name\":\"儿童系列\", " + "\r\n"; postData += " \"key\":\"gtt_menu_001004\"" + "\r\n"; postData += " }]" + "\r\n"; postData += "}," + "\r\n"; postData += "{" + "\r\n"; postData += "\"name\":\"活动\", " + "\r\n"; postData += "\"sub_button\":[" + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"click\"," + "\r\n"; postData += " \"name\":\"抽奖\", " + "\r\n"; postData += " \"key\":\"gtt_menu_002001\"" + "\r\n"; postData += "}," + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"view\"," + "\r\n"; postData += " \"name\":\"获奖名单\", " + "\r\n"; postData += " \"url\":\"http://www.aaa.com/apps/honor_list.aspx\"" + "\r\n"; postData += "}," + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"click\"," + "\r\n"; postData += " \"name\":\"优惠券\", " + "\r\n"; postData += " \"key\":\"gtt_menu_002003\"" + "\r\n"; postData += " }]" + "\r\n"; postData += "}," + "\r\n"; postData += "{" + "\r\n"; postData += "\"name\":\"帮助\"," + "\r\n"; postData += "\"sub_button\":[" + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"view\"," + "\r\n"; postData += " \"name\":\"关于我们\", " + "\r\n"; postData += " \"url\":\"http://www.aaa.com/apps/aboutus.aspx\"" + "\r\n"; postData += "}," + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"view\"," + "\r\n"; postData += " \"name\":\"联系我们\", " + "\r\n"; postData += " \"url\":\"http://www.aaa.com/apps/contactus.aspx\"" + "\r\n"; postData += "}," + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"view\"," + "\r\n"; postData += " \"name\":\"查询订单\", " + "\r\n"; postData += " \"url\":\"http://www.aaa.com/apps/orders_chaxun.aspx\"" + "\r\n"; postData += "}," + "\r\n"; postData += "{ " + "\r\n"; postData += " \"type\":\"view\"," + "\r\n"; postData += " \"name\":\"留言反馈\", " + "\r\n"; postData += " \"url\":\"http://www.aaa.com/apps/feedback.aspx\"" + "\r\n"; postData += " }]" + "\r\n"; postData += "}]" + "\r\n"; postData += "}" + "\r\n"; return postData; } } }
메뉴의 URL 주소 자신의
코드 복사 코드는 다음과 같습니다.
using System; using System.Web.Security; using System.Xml; using aaa_weixin.database; using Newtonsoft.Json; namespace aaa_weixin { public class weixin { private string Token = ""; //微信里面开发者模式Token private string devlopID = "";//微信里面开发者模式:开发者ID private string devlogPsw = "";//微信里面开发者模式: 开发者密码 public string AccessToken = ""; //获取的通行证 public void Auth() { string echoStr = System.Web.HttpContext.Current.Request.QueryString["echoStr"]; if (CheckSignature()) { if (!string.IsNullOrEmpty(echoStr)) { System.Web.HttpContext.Current.Response.Write(echoStr); System.Web.HttpContext.Current.Response.End(); } } } public string GetAccessToken() //获取通行证 { string url_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + devlopID + "&secret=" + devlogPsw; string result = func.webRequestGet(url_token); accessToken deserializedProduct = (accessToken)JsonConvert.DeserializeObject(result, typeof(accessToken)); this.AccessToken = deserializedProduct.access_Token; return this.AccessToken; } public string GetMenu() //获取当前菜单情况 { string url_Menu_Get = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + this.AccessToken; string output = func.webRequestGet(url_Menu_Get); //wxErr deserializedProduct = (wxErr)JsonConvert.DeserializeObject(output, typeof(wxErr)); //return deserializedProduct.errmsg; return output; } public string SetMenu() //设置最新菜单 { string url_Menu_Create = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + this.GetAccessToken(); aaa gtt = new aaa(); string postData = gtt.createMenuDate(); string result = func.webRequestPost(url_Menu_Create,postData); return result; } public string DelMenu() //删除菜单 { string url_Menu_Delete = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + this.GetAccessToken(); string result = func.webRequestGet(url_Menu_Delete); return result; } /// <summary> /// 验证微信签名 /// </summary> /// * 将token、timestamp、nonce三个参数进行字典序排序 /// * 将三个参数字符串拼接成一个字符串进行sha1加密 /// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。 /// <returns></returns> private bool CheckSignature() { string signature = System.Web.HttpContext.Current.Request.QueryString["signature"]; string timestamp = System.Web.HttpContext.Current.Request.QueryString["timestamp"]; string nonce = System.Web.HttpContext.Current.Request.QueryString["nonce"]; string[] ArrTmp = { Token, timestamp, nonce }; Array.Sort(ArrTmp); //字典排序 string tmpStr = string.Join("", ArrTmp); tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1"); tmpStr = tmpStr.ToLower(); if (tmpStr == signature) { return true; } else { return false; } } } public class wxErr { private int Errcode; public int errcode { get { return Errcode; } set { Errcode = value; } } private string Errmsg; public string errmsg { get { return Errmsg; } set { Errmsg = value; } } } public class accessToken { private string access_token; public string access_Token { get { return access_token; } set { access_token = value; } } private int expires_in; public int expires_In { get { return expires_in; } set { expires_in = value; } } } }
위 내용은 C#을 사용하여 공개 플랫폼 사용자 정의 메뉴 기능 예제 코드 개발의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

SecList
SecLists는 최고의 보안 테스터의 동반자입니다. 보안 평가 시 자주 사용되는 다양한 유형의 목록을 한 곳에 모아 놓은 것입니다. SecLists는 보안 테스터에게 필요할 수 있는 모든 목록을 편리하게 제공하여 보안 테스트를 더욱 효율적이고 생산적으로 만드는 데 도움이 됩니다. 목록 유형에는 사용자 이름, 비밀번호, URL, 퍼징 페이로드, 민감한 데이터 패턴, 웹 셸 등이 포함됩니다. 테스터는 이 저장소를 새로운 테스트 시스템으로 간단히 가져올 수 있으며 필요한 모든 유형의 목록에 액세스할 수 있습니다.

SublimeText3 영어 버전
권장 사항: Win 버전, 코드 프롬프트 지원!

Atom Editor Mac 버전 다운로드
가장 인기 있는 오픈 소스 편집기

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경
