ASP.NET MVC5+EF6+EasyUI 後臺管理系統(75)-微信公衆平臺開發-用戶管理

系列目錄

前言

本節主要是關注者(即用戶)和用戶組的管理,微信公衆號提供了用戶和用戶組的管理,咱們能夠在微信公衆號官方里面進行操做,添加備註和標籤,以及移動用戶組別,同時,微信公衆號也提供了相應的接口另咱們本地能夠操做.咱們主要是同步到本地,這樣咱們能夠本身爲用戶定義更多的信息,以及與本地的業務更好的對接起來.html

實現

1、關注與訂閱事件

看到咱們以前的消息處理類 咱們須要擴展一個事件類,這個類與以前消息處理類是同樣的分部類,一樣來自Senpars微信的SDK前端

/*----------------------------------------------------------------
    Copyright (C) 2016 Senparc
    
    文件名:CustomMessageHandler_Events.cs
    文件功能描述:自定義MessageHandler
    
    
    建立標識:Senparc - 20150312
----------------------------------------------------------------*/

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Senparc.Weixin.MP.Agent;
using Senparc.Weixin.Context;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.Helpers;
using Senparc.Weixin.MP.MessageHandlers;
using Senparc.Weixin.MP;
using Apps.WC.DAL;
using Apps.WC.IDAL;
using System.Collections.Generic;
using Apps.Models;
using Apps.WC.IBLL;
using Apps.WC.BLL;
using Apps.Models.WC;
using Apps.Models.Enum;
using Apps.Common;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.AdvancedAPIs.User;

namespace Apps.Web.Areas.WC.Core
{
    /// <summary>
    /// 自定義MessageHandler
    /// </summary>
    public partial class CustomMessageHandler
    {
        public override IResponseMessageBase OnTextOrEventRequest(RequestMessageText requestMessage)
        {
            // 預處理文字或事件類型請求。
            // 這個請求是一個比較特殊的請求,一般用於統一處理來自文字或菜單按鈕的同一個執行邏輯,
            // 會在執行OnTextRequest或OnEventRequest以前觸發,具備如下一些特徵:
            // 一、若是返回null,則繼續執行OnTextRequest或OnEventRequest
            // 二、若是返回不爲null,則終止執行OnTextRequest或OnEventRequest,返回最終ResponseMessage
            // 三、若是是事件,則會將RequestMessageEvent自動轉爲RequestMessageText類型,其中RequestMessageText.Content就是RequestMessageEvent.EventKey

            if (requestMessage.Content == "OneClick")
            {
                var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
                strongResponseMessage.Content = "您點擊了底部按鈕。\r\n爲了測試微信軟件換行bug的應對措施,這裏作了一個——\r\n換行";
                return strongResponseMessage;
            }
            return null;//返回null,則繼續執行OnTextRequest或OnEventRequest
        }

        public override IResponseMessageBase OnEvent_ClickRequest(RequestMessageEvent_Click requestMessage)
        {
            IResponseMessageBase reponseMessage = null;
            ////菜單點擊,須要跟建立菜單時的Key匹配
            //switch (requestMessage.EventKey)
            //{
            //    case "OneClick":
            //        {
            //            //這個過程實際已經在OnTextOrEventRequest中完成,這裏不會執行到。
            //            var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
            //            reponseMessage = strongResponseMessage;
            //            strongResponseMessage.Content = "您點擊了底部按鈕。\r\n爲了測試微信軟件換行bug的應對措施,這裏作了一個——\r\n換行";
            //        }
            //        break;
            //    case "SubClickRoot_Text":
            //        {
            //            var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
            //            reponseMessage = strongResponseMessage;
            //            strongResponseMessage.Content = "您點擊了子菜單按鈕。";
            //        }
            //        break;
            //    case "SubClickRoot_News":
            //        {
            //            var strongResponseMessage = CreateResponseMessage<ResponseMessageNews>();
            //            reponseMessage = strongResponseMessage;
            //            strongResponseMessage.Articles.Add(new Article()
            //            {
            //                Title = "您點擊了子菜單圖文按鈕",
            //                Description = "您點擊了子菜單圖文按鈕,這是一條圖文信息。",
            //                PicUrl = "http://sdk.weixin.senparc.com/Images/qrcode.jpg",
            //                Url = "http://sdk.weixin.senparc.com"
            //            });
            //        }
            //        break;
            //    case "SubClickRoot_Music":
            //        {
            //            //上傳縮略圖
            //            var accessToken = Senparc.Weixin.MP.Containers.AccessTokenContainer.TryGetAccessToken(appId, appSecret);
            //            var uploadResult = Senparc.Weixin.MP.AdvancedAPIs.MediaApi.UploadTemporaryMedia(accessToken, UploadMediaFileType.thumb,
            //                                                         Server.GetMapPath("~/Images/Logo.jpg"));
            //            //設置音樂信息
            //            var strongResponseMessage = CreateResponseMessage<ResponseMessageMusic>();
            //            reponseMessage = strongResponseMessage;
            //            strongResponseMessage.Music.Title = "天籟之音";
            //            strongResponseMessage.Music.Description = "真的是天籟之音";
            //            strongResponseMessage.Music.MusicUrl = "http://sdk.weixin.senparc.com/Content/music1.mp3";
            //            strongResponseMessage.Music.HQMusicUrl = "http://sdk.weixin.senparc.com/Content/music1.mp3";
            //            strongResponseMessage.Music.ThumbMediaId = uploadResult.thumb_media_id;
            //        }
            //        break;
            //    case "SubClickRoot_Image":
            //        {
            //            //上傳圖片
            //            var accessToken = Senparc.Weixin.MP.Containers.AccessTokenContainer.TryGetAccessToken(appId, appSecret);
            //            var uploadResult = Senparc.Weixin.MP.AdvancedAPIs.MediaApi.UploadTemporaryMedia(accessToken, UploadMediaFileType.image,
            //                                                         Server.GetMapPath("~/Images/Logo.jpg"));
            //            //設置圖片信息
            //            var strongResponseMessage = CreateResponseMessage<ResponseMessageImage>();
            //            reponseMessage = strongResponseMessage;
            //            strongResponseMessage.Image.MediaId = uploadResult.media_id;
            //        }
            //        break;
            //    case "SubClickRoot_Agent"://代理消息
            //        {
            //            //獲取返回的XML
            //            DateTime dt1 = DateTime.Now;
            //            reponseMessage = MessageAgent.RequestResponseMessage(this, agentUrl, agentToken, RequestDocument.ToString());
            //            //上面的方法也可使用擴展方法:this.RequestResponseMessage(this,agentUrl, agentToken, RequestDocument.ToString());

            //            DateTime dt2 = DateTime.Now;

            //            if (reponseMessage is ResponseMessageNews)
            //            {
            //                (reponseMessage as ResponseMessageNews)
            //                    .Articles[0]
            //                    .Description += string.Format("\r\n\r\n代理過程總耗時:{0}毫秒", (dt2 - dt1).Milliseconds);
            //            }
            //        }
            //        break;
            //    case "Member"://託管代理會員信息
            //        {
            //            //原始方法爲:MessageAgent.RequestXml(this,agentUrl, agentToken, RequestDocument.ToString());//獲取返回的XML
            //            reponseMessage = this.RequestResponseMessage(agentUrl, agentToken, RequestDocument.ToString());
            //        }
            //        break;
            //    case "OAuth"://OAuth受權測試
            //        {
            //            var strongResponseMessage = CreateResponseMessage<ResponseMessageNews>();
            //            strongResponseMessage.Articles.Add(new Article()
            //            {
            //                Title = "OAuth2.0測試",
            //                Description = "點擊【查看全文】進入受權頁面。\r\n注意:此頁面僅供測試(是專門的一個臨時測試帳號的受權,並不是Senparc.Weixin.MP SDK官方帳號,因此若是受權後出現錯誤頁面數正常狀況),測試號隨時可能過時。請將此DEMO部署到您本身的服務器上,並使用本身的appid和secret。",
            //                Url = "http://sdk.weixin.senparc.com/oauth2",
            //                PicUrl = "http://sdk.weixin.senparc.com/Images/qrcode.jpg"
            //            });
            //            reponseMessage = strongResponseMessage;
            //        }
            //        break;
            //    case "Description":
            //        {
            //            var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
            //            strongResponseMessage.Content = "GetWelcomeInfo";
            //            reponseMessage = strongResponseMessage;
            //        }
            //        break;
            //    case "SubClickRoot_PicPhotoOrAlbum":
            //        {
            //            var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
            //            reponseMessage = strongResponseMessage;
            //            strongResponseMessage.Content = "您點擊了【微信拍照】按鈕。系統將會彈出拍照或者相冊發圖。";
            //        }
            //        break;
            //    case "SubClickRoot_ScancodePush":
            //        {
            //            var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
            //            reponseMessage = strongResponseMessage;
            //            strongResponseMessage.Content = "您點擊了【微信掃碼】按鈕。";
            //        }
            //        break;
            //    case "ConditionalMenu_Male":
            //        {
            //            var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
            //            reponseMessage = strongResponseMessage;
            //            strongResponseMessage.Content = "您點擊了個性化菜單按鈕,您的微信性別設置爲:男。";
            //        }
            //        break;
            //    case "ConditionalMenu_Femle":
            //        {
            //            var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
            //            reponseMessage = strongResponseMessage;
            //            strongResponseMessage.Content = "您點擊了個性化菜單按鈕,您的微信性別設置爲:女。";
            //        }
            //        break;
            //    default:
            //        {
            //            var strongResponseMessage = CreateResponseMessage<ResponseMessageText>();
            //            strongResponseMessage.Content = "您點擊了按鈕,EventKey:" + requestMessage.EventKey;
            //            reponseMessage = strongResponseMessage;
            //        }
            //        break;
            //}

            return reponseMessage;
        }

        public override IResponseMessageBase OnEvent_EnterRequest(RequestMessageEvent_Enter requestMessage)
        {
            var responseMessage = ResponseMessageBase.CreateFromRequestMessage<ResponseMessageText>(requestMessage);
            responseMessage.Content = "您剛纔發送了ENTER事件請求。";
            return responseMessage;
        }

        public override IResponseMessageBase OnEvent_LocationRequest(RequestMessageEvent_Location requestMessage)
        {
            //這裏是微信客戶端(經過微信服務器)自動發送過來的位置信息
            var responseMessage = CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "這裏寫什麼都無所謂,好比:上帝愛你!";
            return responseMessage;//這裏也能夠返回null(須要注意寫日誌時候null的問題)
        }

        public override IResponseMessageBase OnEvent_ScanRequest(RequestMessageEvent_Scan requestMessage)
        {
            //經過掃描關注
            var responseMessage = CreateResponseMessage<ResponseMessageText>();

            

            responseMessage.Content = responseMessage.Content ?? string.Format("經過掃描二維碼進入,場景值:{0}", requestMessage.EventKey);



            return responseMessage;
        }

        public override IResponseMessageBase OnEvent_ViewRequest(RequestMessageEvent_View requestMessage)
        {
            //說明:這條消息只做爲接收,下面的responseMessage到達不了客戶端,相似OnEvent_UnsubscribeRequest
            var responseMessage = CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "您點擊了view按鈕,將打開網頁:" + requestMessage.EventKey;
            return responseMessage;
        }

        public override IResponseMessageBase OnEvent_MassSendJobFinishRequest(RequestMessageEvent_MassSendJobFinish requestMessage)
        {
            var responseMessage = CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "接收到了羣發完成的信息。";
            return responseMessage;
        }

        /// <summary>
        /// 訂閱(關注)事件
        /// </summary>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "歡迎關注";
            return responseMessage;
        }

        /// <summary>
        /// 退訂
        /// 實際上用戶沒法收到非訂閱帳號的消息,因此這裏能夠隨便寫。
        /// unsubscribe事件的意義在於及時刪除網站應用中已經記錄的OpenID綁定,消除冗餘數據。而且關注用戶流失的狀況。
        /// </summary>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_UnsubscribeRequest(RequestMessageEvent_Unsubscribe requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "有空再來";
            return responseMessage;
        }

        /// <summary>
        /// 事件之掃碼推事件(scancode_push)
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_ScancodePushRequest(RequestMessageEvent_Scancode_Push requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "事件之掃碼推事件";
            return responseMessage;
        }

        /// <summary>
        /// 事件之掃碼推事件且彈出「消息接收中」提示框(scancode_waitmsg)
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_ScancodeWaitmsgRequest(RequestMessageEvent_Scancode_Waitmsg requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "事件之掃碼推事件且彈出「消息接收中」提示框";
            return responseMessage;
        }

        /// <summary>
        /// 事件之彈出拍照或者相冊發圖(pic_photo_or_album)
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_PicPhotoOrAlbumRequest(RequestMessageEvent_Pic_Photo_Or_Album requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "事件之彈出拍照或者相冊發圖";
            return responseMessage;
        }

        /// <summary>
        /// 事件之彈出系統拍照發圖(pic_sysphoto)
        /// 實際測試時發現微信並無推送RequestMessageEvent_Pic_Sysphoto消息,只能接收到用戶在微信中發送的圖片消息。
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_PicSysphotoRequest(RequestMessageEvent_Pic_Sysphoto requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "事件之彈出系統拍照發圖";
            return responseMessage;
        }

        /// <summary>
        /// 事件之彈出微信相冊發圖器(pic_weixin)
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_PicWeixinRequest(RequestMessageEvent_Pic_Weixin requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "事件之彈出微信相冊發圖器";
            return responseMessage;
        }

        /// <summary>
        /// 事件之彈出地理位置選擇器(location_select)
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_LocationSelectRequest(RequestMessageEvent_Location_Select requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "事件之彈出地理位置選擇器";
            return responseMessage;
        }
    }
}
CustomMessageHandler.cs
        /// <summary>
        /// 訂閱(關注)事件
        /// </summary>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "歡迎關注";
            return responseMessage;
        }

        /// <summary>
        /// 退訂
        /// 實際上用戶沒法收到非訂閱帳號的消息,因此這裏能夠隨便寫。
        /// unsubscribe事件的意義在於及時刪除網站應用中已經記錄的OpenID綁定,消除冗餘數據。而且關注用戶流失的狀況。
        /// </summary>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_UnsubscribeRequest(RequestMessageEvent_Unsubscribe requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "有空再來";
            return responseMessage;
        }

裏面有2個事件,一個用戶關注時觸發,一個退訂時候觸發,因此基本咱們無須要進行什麼配置,只要加入這個類就能夠了數據庫

2、本地數據庫表

CREATE TABLE [dbo].[WC_Group](
    [Id] [varchar](50) NOT NULL,
    [Name] [varchar](200) NOT NULL,
    [Count] [int] NOT NULL,
    [OfficalAccountId] [varchar](50) NOT NULL,
 CONSTRAINT [PK_WC_Group] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[WC_Group]  WITH CHECK ADD  CONSTRAINT [FK_WC_Group_WC_OfficalAccounts] FOREIGN KEY([OfficalAccountId])
REFERENCES [dbo].[WC_OfficalAccounts] ([Id])
GO

ALTER TABLE [dbo].[WC_Group] CHECK CONSTRAINT [FK_WC_Group_WC_OfficalAccounts]
GO
分組表
CREATE TABLE [dbo].[WC_User](
    [Id] [varchar](50) NOT NULL,
    [OpenId] [varchar](50) NOT NULL,
    [NickName] [varchar](200) NULL,
    [Sex] [int] NOT NULL,
    [Language] [varchar](50) NULL,
    [City] [varchar](50) NULL,
    [Province] [varchar](50) NULL,
    [Country] [varchar](50) NULL,
    [HeadImgUrl] [varchar](1000) NULL,
    [SubscribeTime] [datetime] NOT NULL,
    [UnionId] [varchar](200) NULL,
    [Remark] [varchar](1000) NULL,
    [GroupId] [varchar](50) NULL,
    [TagidList] [varchar](1000) NULL,
    [Subscribe] [int] NOT NULL,
    [OfficalAccountId] [varchar](50) NOT NULL,
 CONSTRAINT [PK_WC_User] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[WC_User]  WITH NOCHECK ADD  CONSTRAINT [FK_WC_User_WC_Group] FOREIGN KEY([GroupId])
REFERENCES [dbo].[WC_Group] ([Id])
GO

ALTER TABLE [dbo].[WC_User] NOCHECK CONSTRAINT [FK_WC_User_WC_Group]
GO

ALTER TABLE [dbo].[WC_User]  WITH CHECK ADD  CONSTRAINT [FK_WC_User_WC_OfficalAccounts] FOREIGN KEY([OfficalAccountId])
REFERENCES [dbo].[WC_OfficalAccounts] ([Id])
GO

ALTER TABLE [dbo].[WC_User] CHECK CONSTRAINT [FK_WC_User_WC_OfficalAccounts]
GO
用戶表

表字段是根據官網,用戶管理對應字段而創建,基本與官方提供的用戶信息是同樣的,無非是存點用戶的姓名,籍貫,頭像,關注事件等json

3、同步數據

這時咱們就能夠實現OnEvent_SubscribeRequest (關注時)的代碼,基本沒有實現難度,步驟以下:後端

  1. 得到OpenID(requestMessage.FromUserName)
  2. 調用信息接口( UserInfoJson userJson = UserApi.Info(account.AccessToken, requestMessage.FromUserName);)
  3. 把信息添加到本地
  4. 啓用訂閱時回覆的內容(回覆在前面已經放出)
    /// <summary>
        /// 訂閱(關注)事件
        /// </summary>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage)
        {
            ValidationErrors errors = new ValidationErrors();

            using (DBContainer db = new DBContainer())
            {

                IWC_OfficalAccountsBLL account_BLL = new WC_OfficalAccountsBLL()
                {
                    m_Rep = new WC_OfficalAccountsRepository(db)
                };

                //得到當前公衆號
                WC_OfficalAccountsModel account = account_BLL.GetById(Id);

                //將用戶提取到本地數據庫
                WC_UserModel userModel = new WC_UserModel();
                IWC_UserBLL user_BLL = new WC_UserBLL()
                {
                    m_Rep = new WC_UserRepository(db)
                };
                user_BLL.Create(ref errors, userModel);
                UserInfoJson userJson = UserApi.Info(account.AccessToken, requestMessage.FromUserName);
                userModel.Id = ResultHelper.NewId;
                userModel.OpenId = userJson.openid;
                userModel.NickName = userJson.nickname;
                userModel.Sex = userJson.sex;
                userModel.Language = userJson.language;
                userModel.City = userJson.city;
                userModel.Province = userJson.province;
                userModel.Country = userJson.country;
                userModel.HeadImgUrl = userJson.headimgurl;
                userModel.SubscribeTime = ResultHelper.GetTimeByLong(userJson.subscribe_time);
                userModel.UnionId = userJson.unionid;
                userModel.Remark = userJson.remark;
                userModel.GroupId = userJson.groupid.ToString();
                userModel.TagidList = string.Join(",", userJson.tagid_list.ToArray());
                userModel.Subscribe = userJson.subscribe;
                userModel.OfficalAccountId = account.Id;
                IWC_MessageResponseRepository m_Rep = new WC_MessageResponseRepository(db);
                //訂閱回覆
                List<WC_MessageResponse> messageList = m_Rep.GetSubscribeResponseContent(account.Id);
                if (messageList.Count() > 0)
                {
                    if (messageList[0].Category == (int)WeChatReplyCategory.Text)
                    {
                        var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
                        responseMessage.CreateTime = ResultHelper.NowTime;
                        responseMessage.ToUserName = requestMessage.FromUserName;
                        responseMessage.FromUserName = account.OfficalId;
                        responseMessage.Content = messageList[0].TextContent;
                        return responseMessage;
                    }
                    //圖文方式
                    else if (messageList[0].Category == (int)WeChatReplyCategory.Image)
                    {
                        var responseMessage = CreateResponseMessage<ResponseMessageNews>();
                        foreach (var model in messageList)
                        {
                            responseMessage.Articles.Add(new Article()
                            {
                                Title = model.TextContent,
                                Description = model.ImgTextContext,
                                PicUrl = WebChatPara.SiteUrl + model.ImgTextUrl,
                                Url = model.ImgTextLink
                            });
                        }
                        return responseMessage;
                    }//通常不多用到
                    else if (messageList[0].Category == (int)WeChatReplyCategory.Voice)
                    {
                        var responseMessage = base.CreateResponseMessage<ResponseMessageMusic>();
                        responseMessage.Music.MusicUrl = WebChatPara.SiteUrl + messageList[0].MeidaUrl;
                        responseMessage.Music.Title = messageList[0].TextContent;
                        responseMessage.Music.Description = messageList[0].Remark;
                        return responseMessage;
                    }
                }
                var errorResponseMessage = requestMessage.CreateResponseMessage<ResponseMessageText>();
                return errorResponseMessage;
            }
        }

退訂事件:OnEvent_UnsubscribeRequest(退訂沒有什麼代碼,只是標記這個openID已經取消關注)服務器

 /// <summary>
        /// 訂閱(關注)事件
        /// </summary>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage)
        {
            ValidationErrors errors = new ValidationErrors();

            using (DBContainer db = new DBContainer())
            {

                IWC_OfficalAccountsBLL account_BLL = new WC_OfficalAccountsBLL()
                {
                    m_Rep = new WC_OfficalAccountsRepository(db)
                };
                //得到當前公衆號
                WC_OfficalAccountsModel account = account_BLL.GetById(Id);
                //將用戶提取到本地數據庫
                IWC_UserBLL user_BLL = new WC_UserBLL()
                {
                    m_Rep = new WC_UserRepository(db)
                };
                WC_UserModel userModel = user_BLL.GetById(Id);
                //標記爲取消關注
                userModel.Subscribe = 0;
                user_BLL.Edit(ref errors, userModel);
                var errorResponseMessage = requestMessage.CreateResponseMessage<ResponseMessageText>();
                return errorResponseMessage;
            }
        }

後臺管理

後臺管理最直接的效益是分析統計用戶的數據情況,這裏只演示從服務器同步用戶信息,其餘都是之前寫得發愁的,增刪改查了微信

 前端JSapp

 $("#btnSync").click(function () {
            var rows = $('#List').datagrid('getSelections');
            if (rows.length > 0) {
                var ids = rows.join(",");
                $.each(rows, function (index, row) {
                    ids = ids + row.value + ",";
                });
                $.post("@Url.Action("SyncUser")?ids=" + ids, function (data) {
                    if (data.type == 1)
                        $("#List").datagrid('load');
                        $.messageBox5s('@Resource.Tip', data.message);
                }, "json");
            } else { $.messageBox5s('@Resource.Tip', '@Resource.PlaseChooseToOperatingRecords'); }
        });

後端控制器代碼ide

 public JsonResult SyncUser(string ids,string officeId)
        {
            if (!string.IsNullOrWhiteSpace(ids))
            {
                //填充數據
                string[] arrs = ids.Split(',');
                List<BatchGetUserInfoData> list = new List<BatchGetUserInfoData>();
                foreach (var m in arrs)
                {
                    list.Add(new BatchGetUserInfoData() {
                        openid = m
                    });
                }

                //批量同步數據
                WC_OfficalAccountsModel accountModel =  account_BLL.GetById(officeId);
                var batchList =  Senparc.Weixin.MP.AdvancedAPIs.UserApi.BatchGetUserInfo(accountModel.AccessToken, list);
                foreach (var info in batchList.user_info_list)
                {
                    WC_UserModel userModel = m_BLL.GetById(info.openid);
                    if (userModel != null)
                    {
                        userModel.City = info.city;
                        userModel.OpenId = info.openid;
                        userModel.Id = info.openid;
                        userModel.HeadImgUrl = info.headimgurl;
                        userModel.Language = info.language;
                        userModel.NickName = info.nickname;
                        userModel.Province = info.province;
                        userModel.Sex = info.sex;
                        m_BLL.Edit(ref errors, userModel);
                    }
                }

                LogHandler.WriteServiceLog(GetUserId(), "Ids:" + ids, "成功", "刪除", "WC_User");
                return Json(JsonHandler.CreateMessage(1, Resource.SaveSucceed));
            }
            else
            {
                return Json(JsonHandler.CreateMessage(0, Resource.SaveFail));
            }


        }

代碼簡單易懂:經過OpenID到公衆號同步到數據,再將本地數據修改post

同步接口在Senparc WX SDK下的Senparc.Weixin.MP.AdvancedAPIs  UserApi

同時這個類也提供了:獲取用戶信息,獲取關注者OpenId信息,修改關注者備註信息,批量獲取用戶基本信息

相關文章
相關標籤/搜索