/// <summary> /// 订阅(关注)事件。关注时需要把用户信息导入到数据库中 /// </summary> /// <returns></returns> public override List <IResponseMessageBase> OnEvent_SubscribeRequest(RequestMessageEvent_Subscribe requestMessage) { //var responseMessage = ResponseMessageBase.CreateFromRequestMessage<ResponseMessageText>(requestMessage); //responseMessage.Content = GetWelcomeInfo(); //Add user to local database if (!string.IsNullOrEmpty(WeixinOpenId)) { // SycUserFromWeixin(WeixinOpenId); //同步用户 var weChatConfig = GetWechatConfig(); lock (objLockUser) //不lock的话,会插入多次 { WechatMPUser objUser = wechatMPUserService.Repository.Entities.FirstOrDefault(u => u.OpenId.Equals(WeixinOpenId)); if (null != objUser) { log.Debug("user :{0}, {1}", objUser.Id, objUser.IsCanceled); } WeChatCommonService.SycUserFromWeixinMP(WeixinOpenId, ref objUser, weChatConfig); } log.Debug("EventKey:{0} Event:{1}", requestMessage.EventKey, requestMessage.Event); #region 通过扫码关注 int orCodeSceneId; string prefix = "qrscene_"; if (!string.IsNullOrEmpty(requestMessage.EventKey) && requestMessage.EventKey.StartsWith(prefix) && int.TryParse(requestMessage.EventKey.Substring(prefix.Length), out orCodeSceneId)) { var entity = new FocusHistory() { CreatedTime = DateTime.Now, QrCodeSceneId = orCodeSceneId, UserId = requestMessage.FromUserName, PeopleCount = 1, PurePeopleCount = 1, Status = 1, }; focusHistoryService.Repository.Insert(entity); //添加关联标签,分组, log.Debug("SubscribeWithScan :{0}", orCodeSceneId.ToString()); return(CreateResponseMessage(orCodeSceneId.ToString(), (int)AutoReplyMPKeywordEnum.SubscribeWithScan)); } #endregion return(CreateResponseMessage(string.Empty, (int)AutoReplyMPKeywordEnum.SubscribeEvent)); } log.Error("WeixinOpenId is null"); return(null); }
//private void SycUserFromWeixin(string WeixinOpenId) //{ // log.Debug("SycUserFromWeixin"); // var chat = GetWechatConfig(); // MP.AdvancedAPIs.User.BatchGetUserInfoData user = new MP.AdvancedAPIs.User.BatchGetUserInfoData // { // openid = WeixinOpenId, // lang = Language.zh_CN.ToString() // }; // var result = Innocellence.Weixin.MP.AdvancedAPIs.UserApi.BatchGetUserInfo(chat.WeixinAppId, // chat.WeixinCorpSecret, // new List<MP.AdvancedAPIs.User.BatchGetUserInfoData> { // user // }); // log.Debug("SycUserFromWeixin result"); // if (result != null && result.user_info_list != null) // { // var userInfo = result.user_info_list.Select(a => WechatMPUserView.ConvertWeChatUserToMpUser(a, chat.AccountManageId.Value, chat.Id)).ToList().FirstOrDefault(); // if (userInfo != null) // { // log.Debug("SycUserFromWeixin userInfo: " + userInfo.OpenId + userInfo.NickName); // userInfo.AccountManageId = chat.AccountManageId; // wechatMPUserService.RegistToWeiXin(userInfo); // log.Debug("SycUserFromWeixin userInfo updated."); // } // } //} /// <summary> /// 退订 /// 实际上用户无法收到非订阅账号的消息,所以这里可以随便写。 /// unsubscribe事件的意义在于及时删除网站应用中已经记录的OpenID绑定,消除冗余数据。并且关注用户流失的情况。 /// </summary> /// <returns></returns> public override List <IResponseMessageBase> OnEvent_UnsubscribeRequest(RequestMessageEvent_UnSubscribe requestMessage) { var responseMessage = base.CreateResponseMessage <ResponseMessageText>(); responseMessage.Content = "有空再来"; //unregist to database if (!string.IsNullOrEmpty(WeixinOpenId)) { wechatMPUserService.CancelRegist(WeixinOpenId); } var last = focusHistoryService.Repository.Entities.Where(a => a.Status == 1 && a.UserId == requestMessage.FromUserName).OrderByDescending(a => a.CreatedTime).FirstOrDefault(); if (last != null) { var entity = new FocusHistory() { CreatedTime = DateTime.Now, QrCodeSceneId = last.QrCodeSceneId, UserId = requestMessage.FromUserName, PeopleCount = 1, PurePeopleCount = 1, Status = 4, }; focusHistoryService.Repository.Insert(entity); } //StringBuilder builder = new StringBuilder(); //builder.Append("MsgId:").Append(requestMessage.MsgId).Append("\n"); //builder.Append("MsgType:").Append(requestMessage.MsgType).Append("\n"); //builder.Append("ToUserName:"******"\n"); //builder.Append("FromUserName:"******"\n"); //builder.Append("Event:").Append(requestMessage.Event.ToString()).Append("\n"); //var mlog = builder.ToString(); //log.Debug(mlog); return(CreateResponseMessage(string.Empty, (int)AutoReplyMPKeywordEnum.ALL)); }