Exemplo n.º 1
0
        public ActionResult Index()
        {
            try
            {
                //string echostring = Request.QueryString["echoStr"];
                //string signature = Request.QueryString["signature"];
                //string timestamp = Request.QueryString["timestamp"];
                //string nonce = Request.QueryString["nonce"];
                //string tmpsignature = Ycbase.makesignature(Ycbase.token, timestamp, nonce);
                //if (tmpsignature == signature && !string.IsNullOrEmpty(echostring))
                //{
                //    Response.Write(echostring);
                //    Response.End();
                //}
                //else
                //{
                //    Response.Write("Invalid request");
                //    Response.End();
                //}
                if (Request.RequestType.ToUpper() == "POST")
                {
                    string         message = PostInput();
                    wxModelMessage mm      = new wxModelMessage();
                    mm.ParseXML(message);
                    LogService.Write("发送方:" + mm.FromUserName);
                    LogService.Write("接收方:" + mm.ToUserName);
                    LogService.Write("事件值:" + mm.Event);
                    if (mm.Event == "subscribe")
                    {
                        LogService.Write("关注ID" + mm.FromUserName);
                        wxModelMessage.sendMessage(mm.FromUserName, "因公众号功能还在完善中。。。。需要咨询礼包和各类游戏活动的朋友请加微信号:He0705h  回复消息有惊喜");
                    }
                    if (mm.MsgType == "text")
                    {
                        LogService.Write("收到消息" + mm.Content);
                        //wxModelMessage.sendMessage(mm.FromUserName, "收到"+mm.Content);
                        wxModelMessage.sendImageMessage(mm.FromUserName, "pt3-_5yeWi40YpNGW3eLJTo6WH8ecxJPGHhsIVF_pQv4gEJD7K1XJPocdSkmBFuw");
                        LogService.Write("发送二维码成功");
                    }
                }
                //if (Request["code"] == null || Request["code"] == "")
                //{
                //    string url = weixinService.OAuth2("http://www.7893927.cn/Home/Index", 0);

                //    System.Web.HttpContext.Current.Response.Redirect(url);
                //    return View();
                //}
                //var result = weixinService.get_accesstoken_bycode(Request["code"]);
                //if (result != null)
                //{
                //    HttpContext.Session["OpenID"] = result.openid;
                //}
                //HttpContext.Session.Add("OpenID", result.openid);   //获取openid
                //else
                //{
                //    Response.Write("登录失败,请重新登录。");
                //    return View();
                //}
                var OpenId = OpenID;
                if ((Session["ID"] == null || Session["ID"].ToString() == "") || string.IsNullOrEmpty(Session["OpenID"].ToString()))
                {
                    if (Session["OpenID"] != null)
                    {
                        //var user = db.User.FirstOrDefault(x => x.OpenID == OpenID);
                        string q    = Session["OpenID"].ToString();
                        var    user = (from x in db.User where x.OpenID == q select x).FirstOrDefault();
                        if (user != null)
                        {
                            Session["ID"] = user.ID.ToString();
                            ////ID = user.ID.ToString();
                            Session["NickName"] = user.nickname;
                        }
                        else
                        {
                            return(RedirectToAction("Login", "Home"));
                        }
                    }
                    else
                    {
                        return(RedirectToAction("Login", "Home"));
                    }
                }

                //else
                //weixinService.AutoLogin(OpenID, Convert.ToInt32(Session["ID"])); //当openid或用户ID有一个不为空时自动登录
                weixinService.AutoLogin(OpenID);
                //return Content("用户" + Session["NickName"] + "登陆成功" + Session["OpenID"]);
                //wxModelMessage.sendMessage("", weixinService.Access_token + ","+ result.openid);
                //return Content(weixinService.Access_token + "," + result.openid);
                //ViewBag.OpenID = OpenID;
                return(View());
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                return(View());
            }
        }
        // GET: echo
        public ActionResult Index()
        {
            // 从请求中获取timestamp参数、nonce参数和signature参数值
            string echoString    = Request.QueryString["echoStr"];
            string signature     = Request.QueryString["signature"];
            string timestamp     = Request.QueryString["timestamp"];
            string encrypt_type  = Request.QueryString["encrypt_type"];
            string msg_signature = Request.QueryString["msg_signature"];
            string nonce         = Request.QueryString["nonce"];

            //记录日志
            LogService.Write("msg_signature:" + msg_signature);
            LogService.Write("signature:" + signature);
            LogService.Write("timestamp:" + timestamp);
            LogService.Write("nonce:" + nonce);
            LogService.Write("encrypt_type:" + encrypt_type);

            if (Request.RequestType.ToUpper() == "POST")//如果是POST的数据,则记录内容
            {
                string         message = PostInput();
                wxModelMessage mm      = new wxModelMessage();
                LogService.Write("收到信息:" + message);
                mm.ParseXML(message);

                if (encrypt_type != "")
                {
                    LogService.Write("密文信息:" + mm.Encrypt);
                    WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(weixinService.token, weixinService.EncodingAESKey, weixinService.appid);
                    int           ret   = 0;
                    string        sMsg  = "";
                    ret = wxcpt.DecryptMsg(msg_signature, timestamp, nonce, mm.Encrypt, ref sMsg);
                    if (ret != 0)
                    {
                        LogService.Write("ERR: Decrypt fail, ret: " + ret);
                        //return;
                    }
                    LogService.Write("解析后的明文:" + sMsg);

                    mm.ParseXML(sMsg);
                }
                else
                {
                    mm.ParseXML(message);
                }
                switch (mm.MsgType.ToLower())
                {
                case "text":
                    LogService.Write("--收到来自【" + mm.FromUserName + "】的消息。消息类型:" + mm.MsgType + ", 消息id:" + mm.MsgId + ",Content:" + mm.Content + "。时间:" + mm.CreateTime);
                    break;

                case "image":
                    LogService.Write("收到来自【" + mm.FromUserName + "】的消息。消息类型:" + mm.MsgType + ", 消息id:" + mm.MsgId + ",PicUrl:" + mm.PicUrl + ",media_id:" + mm.media_id + "。时间:" + mm.CreateTime);
                    break;

                case "voice":
                    LogService.Write("收到来自【" + mm.FromUserName + "】的消息。消息类型:" + mm.MsgType + ", 消息id:" + mm.MsgId + ",Format:" + mm.Format + ",media_id:" + mm.media_id + "。时间:" + mm.CreateTime);
                    break;

                case "video":
                    LogService.Write("收到来自【" + mm.FromUserName + "】的消息。消息类型:" + mm.MsgType + ", 消息id:" + mm.MsgId + ",ThumbMediaId:" + mm.ThumbMediaId + ",media_id:" + mm.media_id + "。时间:" + mm.CreateTime);
                    break;

                case "shortvideo":
                    LogService.Write("收到来自【" + mm.FromUserName + "】的消息。消息类型:" + mm.MsgType + ", 消息id:" + mm.MsgId + ",ThumbMediaId:" + mm.ThumbMediaId + ",media_id:" + mm.media_id + "。时间:" + mm.CreateTime);
                    break;

                case "location":
                    LogService.Write("收到来自【" + mm.FromUserName + "】的消息。消息类型:" + mm.MsgType + ", 消息id:" + mm.MsgId + ",地址:" + mm.Label + "(" + mm.Location_X + "," + mm.Location_Y + "), scale:" + mm.Scale + "时间:" + mm.CreateTime);
                    break;

                case "link":
                    LogService.Write("收到来自【" + mm.FromUserName + "】的消息。消息类型:" + mm.MsgType + ", 消息id:" + mm.MsgId + ",标题:" + mm.Title + "消息描述" + mm.Description + "Url:" + mm.Url + "时间:" + mm.CreateTime);
                    break;

                case "event":
                    LogService.Write("收到来自【" + mm.FromUserName + "】的消息。消息类型:" + mm.MsgType + ", 消息id:" + mm.MsgId + "事件类型:" + mm.Event + ", 时间:" + mm.CreateTime);
                    Response.Write("");
                    break;
                }
                if (mm.Content == "文本")
                {
                    wxModelMessage.sendMessage(mm.FromUserName, "您好,欢迎光临!");
                }
                else if (mm.Content == "图片")
                {
                    wxModelMessage.sendImageMessage(mm.FromUserName, "D1gMtCf2t2HK8-iPBHVGV77b120z9M6J6L0jn0K8Zaw");
                }
                else if (mm.Content == "音乐")
                {
                    wxModelMessage.sendMusicMessage(mm.FromUserName, "水边的阿迪丽娜", "好听的钢琴曲", "http://mstar9.com/media/%E6%B0%B4%E8%BE%B9%E7%9A%84%E9%98%BF%E8%92%82%E4%B8%BD%E5%A8%9C.mp3", "http://mstar9.com/media/%E6%B0%B4%E8%BE%B9%E7%9A%84%E9%98%BF%E8%92%82%E4%B8%BD%E5%A8%9C.mp3", "zetp9PuX5LisLjqXI8ZKHOiQ2Sr7uB6U6MbtWQK2QEZzcflSSDMEjnopEIb19Nwj");
                }
                else if (mm.Content == "客服")
                {
                    wxMessageService.Sendkftext("xiaoqiang@deyuyanxue", mm.FromUserName, "客服小强为您服务");
                }
            }
            else//如果是GET的数据,则验证消息
            {
                LogService.Write("echoString:" + echoString);
                LogService.Write("signature:" + signature);
                LogService.Write("timestamp:" + timestamp);
                LogService.Write("nonce:" + nonce);
                // 对token、timestamp、nonce三个参数进行加密,得到临时signature字符串
                string tmp_signature = weixinService.make_signature(timestamp, nonce);
                LogService.Write("tmp_signature:" + tmp_signature);
                if (tmp_signature == signature && !string.IsNullOrEmpty(echoString))
                {
                    Response.Write(echoString);
                    Response.End();
                }
                else
                {
                    Response.Write("Invalid request!");
                    Response.End();
                }
            }
            return(View());
        }