


C# development of WeChat portals and applications (15) - WeChat menu adds functions of scanning, sending pictures, and sending geographical location
Feb 18, 2017 am 09:14 AMI have introduced many articles about using C# to develop WeChat portals and applications. They basically encapsulated almost all the interfaces that WeChat could do at that time. The WeChat framework has also accumulated a lot of modules and users. Recently, it has been discovered that the WeChat public platform has increased A lot of content has been added, especially the functions of scanning, sending pictures, and sending geographical locations have been added to the custom menu. These functional modules are very important. Think about it before, I wanted to add a QR code scanning function to the WeChat official account. None of the functions are available, but now it is possible, and functions such as taking photos and uploading are also available. This article mainly introduces a series of articles based on my previous framework and further introduces how to integrate and use these new functions.
1. Official introduction to several functions of WeChat
1). Scan the QR code to push events
After the user clicks the button, the WeChat client will activate the scan tool. After completing the scan operation, the scan result will be displayed (if it is a URL, the URL will be entered), and the result of the scan will be sent to the developer. The developer Messages can be sent.
#2). Scan the QR code to push the event, and the "Message Receiving" prompt box will pop up
The user clicks the button After that, the WeChat client will launch the scan tool. After completing the scan operation, the result of the scan will be sent to the developer. At the same time, the scan tool will be put away, and the "Message Receiving" prompt box will pop up, and then the message may appear. Receive a message from the developer.
3). Pop up the system to take pictures and post pictures
After the user clicks the button, the WeChat client will call up the system After completing the photo-taking operation, the camera will send the captured photos to the developer and push the event to the developer. At the same time, the system camera will be put away. You may then receive a message from the developer.
4). Pop-up to take pictures or post pictures to album
After the user clicks the button, the WeChat client will pop up the selection The tool allows users to choose to "take a photo" or "select from the mobile phone album". After the user selects, he will go through the other two processes.
5). Pop-up WeChat photo album sender
After the user clicks the button, the WeChat client will be launched In the WeChat photo album, after completing the selection operation, the selected photos will be sent to the developer's server, and events will be pushed to the developer. At the same time, the photo album will be closed, and you may receive messages from the developer later.
6). Pop up the geographical location selector
After the user clicks the button, the WeChat client will call up the geographical location selector After completing the selection operation, the location selection tool will send the selected geographical location to the developer's server, and at the same time close the location selection tool. You may then receive a message from the developer.
However, please note that the above new capabilities only support WeChat iPhone 5.4.1 or above, and Android 5.4 or above WeChat users. Older version WeChat users will not respond after clicking, and developers will not be able to receive events normally. push.
2. Test public account for WeChat’s new menu function
WeChat not only adds support for these functional modules, but also considers the convenience of our developers and adds a The public account called "menutest" is convenient for us to test. We search for "menutest" on the official account, and then follow it to test several new features.
The name of the official account of "menutest" is "Custom Menu Expansion Test". I followed it and tested it. The QR code, pictures and geographical location are all OK. , itself can respond to these events, and a corresponding event can appear in the picture and geographical location itself, as shown below.
Picture sending can be divided into three categories: taking pictures, taking pictures and photo albums, and WeChat photo albums. I feel that the latter two are a bit similar, but these functions are very good.
3. Improve menu objects and submit menus
As mentioned earlier, WeChat provides these functions and can be integrated in the menu, that is The menu types have changed from the original two types of CLICK/VIEW to the current 8 types, with 2 QR code scanning operations, 3 picture operations, and 1 geographical location operation added.
So expand the menu’s enumeration type as follows.
????///?<summary> ????///?菜單按鈕類型????///?</summary> ????public?enum?ButtonType ????{????????///?<summary> ????????///?點(diǎn)擊????????///?</summary>????????click,????????///?<summary> ????????///?Url????????///?</summary>????????view,????????///?<summary> ????????///?掃碼推事件的事件推送????????///?</summary>????????scancode_push,????????///?<summary> ????????///?掃碼推事件且彈出“消息接收中”提示框的事件推送????????///?</summary>????????scancode_waitmsg,????????///?<summary> ????????///?彈出系統(tǒng)拍照發(fā)圖的事件推送????????///?</summary>????????pic_sysphoto,????????///?<summary> ????????///?彈出拍照或者相冊發(fā)圖的事件推送????????///?</summary>????????pic_photo_or_album,????????///?<summary> ????????///?彈出微信相冊發(fā)圖器的事件推送????????///?</summary>????????pic_weixin,????????///?<summary> ????????///?彈出地理位置選擇器的事件推送????????///?</summary>????????location_select ????}
Then call the create menu operation code in Winform as follows:
????????private?void?btnCreateMenu_Click(object?sender,?EventArgs?e) ????????{ ????????????MenuJson?productInfo?=?new?MenuJson("新功能測試",?new?MenuJson[]?{? ????????????????new?MenuJson("掃碼推事件",?ButtonType.scancode_push,?"scancode_push")? ????????????????,new?MenuJson("系統(tǒng)拍照發(fā)圖",?ButtonType.pic_sysphoto,?"pic_sysphoto")? ????????????????,?new?MenuJson("拍照相冊發(fā)圖",?ButtonType.pic_photo_or_album,?"pic_photo_or_album")? ????????????????,?new?MenuJson("微信相冊發(fā)圖",?ButtonType.pic_weixin,?"pic_weixin")? ????????????????,?new?MenuJson("地理位置選擇",?ButtonType.location_select,?"location_select")? ????????????});???????????????????????????????????? ????????????MenuJson?frameworkInfo?=?new?MenuJson("框架產(chǎn)品",?new?MenuJson[]?{? ????????????????new?MenuJson("Win開發(fā)框架",?ButtonType.click,?"win"),????????????????new?MenuJson("WCF開發(fā)框架",?ButtonType.click,?"wcf"),????????????????new?MenuJson("混合式框架",?ButtonType.click,?"mix"),? ????????????????new?MenuJson("Web開發(fā)框架",?ButtonType.click,?"web") ????????????????,new?MenuJson("代碼生成工具",?ButtonType.click,?"database2sharp") ????????????}); ????????????MenuJson?relatedInfo?=?new?MenuJson("相關(guān)鏈接",?new?MenuJson[]?{? ????????????????new?MenuJson("公司介紹",?ButtonType.click,?"event_company"),???????????????? ????????????????new?MenuJson("官方網(wǎng)站",?ButtonType.view,?"http://ipnx.cn/"),???????????????? ????????????????new?MenuJson("聯(lián)系我們",?ButtonType.click,?"event_contact"),???????????????? ????????????????new?MenuJson("應(yīng)答系統(tǒng)",?ButtonType.click,?"set-1"),???????????????? ????????????????new?MenuJson("人工客服",?ButtonType.click,?"event_customservice")????????????}); ????????????MenuListJson?menuJson?=?new?MenuListJson(); ????????????menuJson.button.AddRange(new?MenuJson[]?{?productInfo,?frameworkInfo,?relatedInfo?}); ????????????if?(MessageUtil.ShowYesNoAndWarning("您確認(rèn)要創(chuàng)建菜單嗎")?==?System.Windows.Forms.DialogResult.Yes) ????????????{ ????????????????IMenuApi?menuBLL?=?new?MenuApi(); ????????????????CommonResult?result?=?menuBLL.CreateMenu(token,?menuJson); ????????????????Console.WriteLine("創(chuàng)建菜單:"?+?(result.Success???"成功"?:?"失敗:"?+?result.ErrorMessage)); ????????????} ????????}
當(dāng)然,一般情況下我們都是在Web后臺系統(tǒng)進(jìn)行的,維護(hù)菜單都是在自己微信平臺上進(jìn)行菜單管理,然后一次性提交到微信服務(wù)器即可。
而在Web后臺,只需要把數(shù)據(jù)庫的數(shù)據(jù)變化為Json數(shù)據(jù)提交即可,操作和上面的類似。
????????///?<summary> ????????///更新微信菜單????????///?</summary> ????????///?<returns></returns> ????????public?ActionResult?UpdateWeixinMenu() ????????{????????????string?token?=?base.GetAccessToken(); ????????????MenuListJson?menuJson?=?GetWeixinMenu(); ????????????IMenuApi?menuApi?=?new?MenuApi(); ????????????CommonResult?result?=?menuApi.CreateMenu(token,?menuJson);????????????return?ToJsonContent(result); ????????}
4、微信掃一掃功能集成?
?前面講了,有了最新的功能,我們就可以實現(xiàn)掃一掃功能,從而可以掃描條形碼,二維碼的功能。有了條形碼、二維碼的快速和識別,我們就能開發(fā)一些如條碼查詢、商品處理等功能了。
這里我們介紹如何在我的微信開發(fā)框架里面整合這個掃一掃的功能處理操作。
前面已經(jīng)增加了一些新功能的測試菜單,我們要做的就是響應(yīng)這些事件處理,然后對他們進(jìn)行應(yīng)答處理就可以了。
下面是根據(jù)事件進(jìn)行的一些API跳轉(zhuǎn)處理,我們同時定義了幾個相關(guān)的實體類用來處理他們的信息,如RequestEventScancodePush、RequestEventScancodeWaitmsg、RequestEventPicSysphoto等等。
RequestEventScancodeWaitmsg實體類的代碼如下所示,其他的類似處理。
????///?<summary> ????///?掃碼推事件且彈出“消息接收中”提示框的事件推送????///?</summary> ????[System.Xml.Serialization.XmlRoot(ElementName?=?"xml")]????public?class?RequestEventScancodeWaitmsg?:?BaseEvent ????{????????public?RequestEventScancodeWaitmsg() ????????{????????????this.MsgType?=?RequestMsgType.Event.ToString().ToLower();????????????this.Event?=?RequestEvent.scancode_waitmsg.ToString();????????????this.ScanCodeInfo?=?new?ScanCodeInfo(); ????????}????????///?<summary> ????????///?事件KEY值,由開發(fā)者在創(chuàng)建菜單時設(shè)定????????///?</summary> ????????public?string?EventKey?{?get;?set;?}????????///?<summary> ????????///?掃描信息????????///?</summary> ????????public?ScanCodeInfo?ScanCodeInfo?{?get;?set;?} ????}
而根據(jù)實體類強(qiáng)類型的處理接口流轉(zhuǎn)操作如下所示。
???????????????????????????????case?RequestEvent.scancode_push: ????????????????????????????????{????????????????????????????????????//掃碼推事件的事件推送 ????????????????????????????????????RequestEventScancodePush?info?=?XmlConvertor.XmlToObject(postStr,?typeof(RequestEventScancodePush))?as?RequestEventScancodePush;????????????????????????????????????if?(info?!=?null) ????????????????????????????????????{ ????????????????????????????????????????responseContent?=?actionBLL.HandleEventScancodePush(info); ????????????????????????????????????} ????????????????????????????????}????????????????????????????????break;????????????????????????????case?RequestEvent.scancode_waitmsg: ????????????????????????????????{????????????????????????????????????//掃碼推事件且彈出“消息接收中”提示框的事件推送 ????????????????????????????????????RequestEventScancodeWaitmsg?info?=?XmlConvertor.XmlToObject(postStr,?typeof(RequestEventScancodeWaitmsg))?as?RequestEventScancodeWaitmsg;????????????????????????????????????if?(info?!=?null) ????????????????????????????????????{ ????????????????????????????????????????responseContent?=?actionBLL.HandleEventScancodeWaitmsg(info); ????????????????????????????????????} ????????????????????????????????}????????????????????????????????break;????????????????????????????case?RequestEvent.pic_sysphoto: ????????????????????????????????{????????????????????????????????????//彈出系統(tǒng)拍照發(fā)圖的事件推送 ????????????????????????????????????RequestEventPicSysphoto?info?=?XmlConvertor.XmlToObject(postStr,?typeof(RequestEventPicSysphoto))?as?RequestEventPicSysphoto;????????????????????????????????????if?(info?!=?null) ????????????????????????????????????{ ????????????????????????????????????????responseContent?=?actionBLL.HandleEventPicSysphoto(info); ????????????????????????????????????} ????????????????????????????????}????????????????????????????????break; ..................
處理掃描結(jié)果并返回的最終代碼如下所示。
????????///?<summary> ????????///?掃碼推事件且彈出“消息接收中”提示框的事件推送的處理????????///?</summary> ????????///?<param name="info">掃描信息</param> ????????///?<returns></returns> ????????public?string?HandleEventScancodeWaitmsg(RequestEventScancodeWaitmsg?info) ????????{ ????????????ResponseText?response?=?new?ResponseText(info); ????????????response.Content?=?string.Format("您的信息為:{0},可以結(jié)合后臺進(jìn)行數(shù)據(jù)查詢。",?info.ScanCodeInfo.ScanResult);????????????return?response.ToXml(); ????????}
最后我們測試掃描一個條形碼,可以看到返回的結(jié)果界面操作如下所示。
?
5、新菜單功能測試發(fā)現(xiàn)的問題
前面介紹了一些新菜單功能模塊的集成,我個人對這種掃一掃菜單功能非常贊賞,這也是微信逐步整合更多硬件資源和接口處理的趨向,不過在集成使用的時候,發(fā)現(xiàn)公眾號偶爾出現(xiàn)閃退的情況,還有就是這些新功能雖然后臺能夠?qū)崿F(xiàn)數(shù)據(jù)的處理和接收,但是有一些不能返回應(yīng)答消息,很郁悶。也許隨著版本研發(fā)的加快,這些功能很快得到完善和解決。
另外微信開放平臺也投入使用了,好些認(rèn)證也是300元一年,不過暫時沒有其應(yīng)用的場景,我只是用到了它來獲取微信賬號的unionid的功能,其他功能慢慢了解吧。
還有就是微信的企業(yè)號也已經(jīng)出來了,而且我也已經(jīng)申請認(rèn)證通過,它的開發(fā)用戶的API也有不少,有空繼續(xù)研究并整合到微信開發(fā)框架里面吧。
?
更多C#開發(fā)微信門戶及應(yīng)用(15)-微信菜單增加掃一掃、發(fā)圖片、發(fā)地理位置功能?相關(guān)文章請關(guān)注PHP中文網(wǎng)!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)