Esempio n. 1
0
        /// <summary>
        /// 通过二维码扫描关注扫描事件
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_ScanRequest(RequestMessageEvent_Scan requestMessage)
        {
            _logger.LogTrace(Newtonsoft.Json.JsonConvert.SerializeObject(requestMessage));

            var responseMessage = this.CreateResponseMessage <ResponseMessageText>();

            var sceneId   = int.Parse(requestMessage.EventKey);
            var eventTask = db.QueryWechatQRScene()
                            .Where(m => m.SceneId == sceneId && m.QrCodeTicket == requestMessage.Ticket)
                            .OrderByDescending(m => m.Id)
                            .FirstOrDefault();

            if (eventTask == null || eventTask.Status == WechatQRSceneStatus.已处理)
            {
                responseMessage.Content = "您刚才扫的二维码已经失效";
            }

            if (eventTask.Category == "BindMember")
            {
                var bindMemberTask = db.QueryMemberWeChatBindTask()
                                     .Where(m => m.Code == sceneId.ToString())
                                     .FirstOrDefault();
                if (bindMemberTask == null || bindMemberTask.Status == MemberWeChatBindTaskStatus.已使用)
                {
                    responseMessage.Content = "您进行绑定的任务不存在或者已经完成";
                    return(responseMessage);
                }

                var memberWechat = db.QueryMemberWechat().Where(m => m.MemberId == bindMemberTask.MemberId)
                                   .FirstOrDefault();
                if (memberWechat != null && !memberWechat.IsDel)
                {
                    throw new Exception("您已经绑定了微信,如需要重新绑定,请先取消");
                }

                bindMemberTask.OpenId = requestMessage.FromUserName;
                bindMemberTask.Status = MemberWeChatBindTaskStatus.已使用;

                memberWechat = new MemberWechat()
                {
                    IsDel    = false,
                    MemberId = bindMemberTask.MemberId,
                    OpenId   = requestMessage.FromUserName
                };
                db.AddToMemberWechat(memberWechat);

                db.SaveChanges();

                responseMessage.Content = "绑定成功!";
            }
            else if (eventTask.Category == "Login")
            {
                var bindMemberTask = db.QueryMemberWeChatLoginTask()
                                     .Where(m => m.Code == sceneId.ToString())
                                     .FirstOrDefault();
                if (bindMemberTask == null || bindMemberTask.Status != MemberWeChatLoginTaskStatus.扫二维码进行中)
                {
                    responseMessage.Content = "您进行登陆的任务不存在或者已经完成";
                    return(responseMessage);
                }

                var memberWechat = db.QueryMemberWechat()
                                   .Where(m => m.OpenId == requestMessage.FromUserName)
                                   .Where(m => !m.IsDel)
                                   .FirstOrDefault();
                if (memberWechat == null)
                {
                    responseMessage.Content = "您未绑定微信,登陆失败!";
                    return(responseMessage);
                }
                bindMemberTask.OpenId = requestMessage.FromUserName;
                bindMemberTask.Status = MemberWeChatLoginTaskStatus.扫二维码完成;
                db.SaveChanges();

                responseMessage.Content = "您进行了扫码登陆,登陆时间" + DateTime.Now.ToString() + ".";
            }
            else
            {
                return(base.OnEvent_ScanRequest(requestMessage));
            }

            return(responseMessage);
        }
Esempio n. 2
0
 public static MemberWechat AddToMemberWechat(this DbContext context, MemberWechat model)
 {
     context.Set <MemberWechat>().Add(model);
     return(model);
 }
Esempio n. 3
0
        /// <summary>
        /// 处理文字请求
        /// </summary>
        /// <returns></returns>
        public override IResponseMessageBase OnTextRequest(RequestMessageText requestMessage)
        {
            _logger.LogTrace(string.Format("post:进入handler"));
            try
            {
                var defaultResponseMessage = base.CreateResponseMessage <ResponseMessageText>();

                var xml = RequestDocument.ToString();
                _logger.LogTrace(string.Format("post:{0}", xml));

                var data = new RequestMsgData();
                data.MsgId   = requestMessage.MsgId;
                data.Content = Newtonsoft.Json.JsonConvert.SerializeObject(new
                {
                    msgType = "text",
                    text    = requestMessage.Content
                });
                data.CreateTime   = requestMessage.CreateTime;
                data.FromUserName = requestMessage.FromUserName;
                data.ToUserName   = requestMessage.ToUserName;
                data.MsgType      = requestMessage.MsgType;
                db.AddToRequestMsgData(data);
                db.SaveChanges();

                _logger.LogTrace(string.Format("post:原始数据保存成功"));


                //绑定会员功能,此处用输入账号密码来实现
                if (!string.IsNullOrWhiteSpace(requestMessage.Content) && requestMessage.Content.StartsWith("bd#"))
                {
                    string emailPwd = requestMessage.Content.Substring(2);
                    var    member   = db.MemberDbSet().FirstOrDefault(p => p.Email != null && p.Email.Trim() != string.Empty && (p.Email + p.Password) == emailPwd);
                    if (member != null)
                    {
                        var memberWechat = db.QueryMemberWechat().FirstOrDefault(p => p.MemberId == member.Id);
                        if (memberWechat != null)
                        {
                            memberWechat.OpenId = requestMessage.FromUserName;
                        }
                        else
                        {
                            memberWechat = new MemberWechat()
                            {
                                OpenId = requestMessage.FromUserName, MemberId = member.Id
                            };
                            db.Add(memberWechat);
                        }
                        db.SaveChanges();
                    }
                }

                var robotMessages = db.QueryRobotMessage()
                                    .Where(m => !m.IsDel)
                                    .Where(m => m.Status == RobotMessageStatus.正常)
                                    .Select(m => new
                {
                    Question     = m.Question,
                    Answer       = m.Answer,
                    QuestionType = m.QuestionType
                })
                                    .ToList();
                _logger.LogTrace($"post:尝试自动回复,共条{robotMessages.Count}自动回复内容");

                var question     = requestMessage.Content;
                var textResponse = robotMessages.Where(m => m.QuestionType == RobotMessageQuestionType.文本)
                                   .Where(m => m.Question == question)
                                   .FirstOrDefault();
                if (textResponse != null)
                {
                    if (textResponse.Answer.StartsWith("image:"))
                    {
                        var tmpMessage = CreateResponseMessage <ResponseMessageImage>();
                        tmpMessage.Image.MediaId = textResponse.Answer.Replace("images:", "");
                        return(tmpMessage);
                    }
                    else if (textResponse.Answer.StartsWith("video:"))
                    {
                        var tmpMessage = CreateResponseMessage <ResponseMessageVideo>();
                        tmpMessage.Video.MediaId = textResponse.Answer.Replace("video:", "");
                        return(tmpMessage);
                    }
                    else if (textResponse.Answer.StartsWith("voice:"))
                    {
                        var tmpMessage = CreateResponseMessage <ResponseMessageVoice>();
                        tmpMessage.Voice.MediaId = textResponse.Answer.Replace("voice:", "");
                        return(tmpMessage);
                    }
                    else if (textResponse.Answer.StartsWith("music:"))
                    {
                        var tmpMessage = CreateResponseMessage <ResponseMessageMusic>();
                        tmpMessage.Music.ThumbMediaId = textResponse.Answer.Replace("music:", "");
                        return(tmpMessage);
                    }
                    else if (textResponse.Answer.StartsWith("news:"))
                    {
                        var mediaId = textResponse.Answer.Replace("news:", "");
                        Task.Factory.StartNew(() =>
                        {
                            Senparc.Weixin.MP.AdvancedAPIs.CustomApi.SendMpNews(GetAccessToken(), base.WeixinOpenId, mediaId);
                        });
                        return(null);
                    }
                    else
                    {
                        defaultResponseMessage.Content = textResponse.Answer;
                        return(defaultResponseMessage);
                    }
                }

                var regexResponse = robotMessages.Where(m => m.QuestionType == RobotMessageQuestionType.正则)
                                    .Where(m => System.Text.RegularExpressions.Regex.IsMatch(question, m.Question, System.Text.RegularExpressions.RegexOptions.None))
                                    .FirstOrDefault();

                if (regexResponse != null)
                {
                    defaultResponseMessage.Content = regexResponse.Answer;
                    return(defaultResponseMessage);
                }

                //这个没效果,仍然会弹出“该公众号暂时无法提供服务,请稍后再试”
                //return base.CreateResponseMessage<ResponseMessageNoResponse>();
                return(new SuccessResponseMessage());
            }
            catch (Exception ex)
            {
                _logger.LogError(new EventId(-100), ex, "OnTextRequest Error");
                return(new SuccessResponseMessage());
            }
        }