Ejemplo n.º 1
0
        public string GetAccessToken(bool cache = false)
        {
            string token     = "";
            string _cachekey = "GetWeChatAccessToken";

            object obj = MemCache.GetCache(_cachekey);

            //从缓存获取
            if (cache && obj != null)
            {
                WeChatTokenLogEntity log = (WeChatTokenLogEntity)obj;
                if (log.EndTime > DateTime.Now)
                {
                    token = log.AccessToken;
                }
            }
            //从数据库获取
            if (token == "")
            {
                string appid             = WeiXinConfig.GetAppId();
                WeChatTokenLogEntity log = WeChatTokenLogBLL.Instance.GetTokenByAppid(appid);
                if (log != null && log.Id > 0 && log.EndTime > DateTime.Now)
                {
                    token = log.AccessToken;
                    TimeSpan ts1   = log.EndTime - DateTime.Now;
                    int      tsSen = ts1.Seconds;
                    if (cache)
                    {
                        MemCache.AddCache(_cachekey, log, tsSen);
                    }
                }
                if (token == "")
                {
                    string result = WebServiceClient.QueryGetWebService(string.Format(WeiXinConfig.URL_FORMAT_TOKEN, WeiXinConfig.GetAppId(), WeiXinConfig.GetAppSecret()), "", null);
                    JavaScriptSerializer        serializer = new JavaScriptSerializer();
                    Dictionary <string, object> jsonObj    = serializer.Deserialize <dynamic>(result);
                    if (jsonObj.ContainsKey("access_token"))
                    {
                        token = jsonObj["access_token"].ToString();
                        WeChatTokenLogEntity tokenlog = new WeChatTokenLogEntity();
                        tokenlog.Appid       = appid;
                        tokenlog.CreateTime  = DateTime.Now;
                        tokenlog.EndTime     = DateTime.Now.AddSeconds(7000);
                        tokenlog.AccessToken = token;
                        WeChatTokenLogBLL.Instance.AddWeChatTokenLog(tokenlog);
                        if (cache)
                        {
                            MemCache.AddCache(_cachekey, tokenlog, 7000);
                        }
                    }
                    else
                    {
                        token = "";
                    }
                }
            }

            return(token);
        }
Ejemplo n.º 2
0
        public void RemoveAccessToken()
        {
            string _cachekey = "GetWeChatAccessToken";

            MemCache.RemoveCache(_cachekey);
            string appid = WeiXinConfig.GetAppId();

            WeChatTokenLogBLL.Instance.RemoveTokenByAppid(appid);
        }
Ejemplo n.º 3
0
        public bool SendCGUrlToManager(string oldurl, int memid)
        {
            bool                   returnresult = false;
            string                 _redirecturl = oldurl;
            VWMemberEntity         _memen       = MemberBLL.Instance.GetVWMember(memid);
            WeChatNavigationEntity _entity      = WeChatNavigationBLL.Instance.GetNavigationByUrl(_redirecturl);

            if (_entity == null || _entity.Id == 0)
            {
                _entity.RedirectUrl = _redirecturl;
                //_entity.Id = WeChatNavigationBLL.Instance.AddWeChatNavigation(_entity);
            }
            _entity.WeChatUrlType = (int)WeChatUrTypeEnum.Temp;
            _entity.Remark        = "供应商临时登录报价";
            //_entity.WeChatUrl = string.Format(WeiXinConfig.URL_FORMAT_KHRedirect, WeiXinConfig.GetAppId(), System.Web.HttpContext.Current.Server.UrlEncode(SuperMarket.Core.ConfigCore.Instance.ConfigCommonEntity.WeChatWebUrl), _entity.Id);
            _entity.WeChatUrl = string.Format(WeiXinConfig.URL_WeiXin_Redirect, WeiXinConfig.GetAppId(), System.Web.HttpContext.Current.Server.UrlEncode(_redirecturl), _entity.Id);

            int _result = WeChatNavigationBLL.Instance.UpdateWeChatNavigation(_entity);

            IList <VWMemberEntity> memlist = MemberBLL.Instance.GetMemByAuthCode(MemberAuthEnum.InquiryOrderQuote);

            if (memlist != null && memlist.Count > 0)
            {
                foreach (VWMemberEntity mem in memlist)
                {
                    if (!string.IsNullOrEmpty(mem.WeChat))
                    {
                        MemWeChatMsgEntity wecharmsg = MemWeChatMsgBLL.Instance.GetMsgByAppUnionId(WeiXinConfig.GetAppId(), mem.WeChat);
                        if (wecharmsg != null && !string.IsNullOrEmpty(wecharmsg.OpenId))
                        {
                            WeiXinCustomerEntity customer = new WeiXinCustomerEntity();
                            customer.touser  = wecharmsg.OpenId;
                            customer.msgtype = WeiXinCustomerMsgtypeEnum.text;
                            WeiXinCustomerTextEntity text = new WeiXinCustomerTextEntity();
                            text.content  = "亲爱的" + _memen.CompanyName + "\n易店心来新的询价单,赶紧点击网址抢单额\n抢单网址:" + _entity.WeChatUrl;
                            customer.text = text;
                            returnresult  = WeiXinJsSdk.Instance.SendWeiXinCustomerNote(customer);
                        }
                    }
                }
            }
            return(returnresult);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 通知供应商
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public bool NoteToCGMemForQuote(string code)
        {
            bool result = false;

            string emailnote = "供应商发送微信情况<br/>";

            emailnote += "订单号:" + code + "<br/>";
            InquiryOrderEntity order     = InquiryOrderBLL.Instance.GetInquiryOrderByCode(code);
            string             memidsold = CGMemQuotedBLL.Instance.GetQuotedCGMemByCode(order.Code);
            string             memidsnew = MemCGScopeBLL.Instance.GetCGMemIdsByCarBrand(order.ScopeType, order.CarBrandName, memidsold);
            //获取虚拟报价员
            IList <VWMemberEntity> memlist = MemberBLL.Instance.GetMemByAuthCode(MemberAuthEnum.InquiryOrderQuote);

            string[] memidsoldattr = memidsold.Split(',');
            string[] memidnewattr  = memidsnew.Split(',');
            bool     hascg         = false;

            if (!string.IsNullOrEmpty(memidsnew))
            {
                hascg = true;
            }
            if (memlist != null && memlist.Count > 0)
            {
                foreach (VWMemberEntity cgmemsup in memlist)
                {
                    bool oldhas = false;
                    if (memidsoldattr != null && memidsoldattr.Length > 0)
                    {
                        foreach (string memidoleen in memidsoldattr)
                        {
                            if (cgmemsup.MemId.ToString() == memidoleen)
                            {
                                oldhas = true;
                                break;
                            }
                        }
                        if (!oldhas)
                        {
                            foreach (string memidnewen in memidnewattr)
                            {
                                if (cgmemsup.MemId.ToString() == memidnewen)
                                {
                                    oldhas = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (!oldhas)
                    {
                        memidsnew += "," + cgmemsup.MemId.ToString();
                    }
                }
            }
            int resultrowi = CGMemQuotedBLL.Instance.AddInquiryToCGMemQuoted(order.Code, memidsnew);
            //获取要发送的供应商,并发送通知
            //InquiryOrderBLL.Instance.UpdateQuoteStatus(order.Code, (int)QuoteStatusEnum.HasSend);
            IList <CGMemQuotedEntity> _sendlist = CGMemQuotedBLL.Instance.GetCGMemQuotedNeedSend(order.Code);
            string redirecturl = ConfigCore.Instance.ConfigCommonEntity.InquiryWebUrl + string.Format(WebUrlEnum.InquiryCGMemNote, code);

            //int navid = WeChatNavigationBLL.Instance.GetIdByUrl(redirecturl);
            //string url = string.Format(WeiXinConfig.URL_FORMAT_SendMsg, WeiXinJsSdk.Instance.GetAccessToken(false));
            if (!hascg)
            {
                emailnote += "没有找到供应商<br/>";
            }
            if (_sendlist != null && _sendlist.Count > 0)
            {
                foreach (CGMemQuotedEntity entity in _sendlist)
                {
                    if (!hascg)
                    {
                        string title     = "您的询价单没有找到供应商额,订单编号:" + order.Code;
                        string ordercode = entity.InquiryOrderCode;
                        try
                        {
                            //获取链接导航Id
                            int            memid = entity.CGMemId;
                            VWMemberEntity memen = MemberBLL.Instance.GetVWMember(memid);
                            emailnote += memen.CompanyName + memen.MobilePhone + memen.CompanyAddress + "     ";
                            if (!string.IsNullOrEmpty(memen.WeChat))
                            {
                                Hashtable hashentity = new Hashtable();
                                hashentity.Add("first", new WeiXinUnitEntity()
                                {
                                    value = title
                                });
                                hashentity.Add("keyword1", new WeiXinUnitEntity()
                                {
                                    value = "无"
                                });
                                hashentity.Add("keyword2", new WeiXinUnitEntity()
                                {
                                    value = order.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")
                                });
                                hashentity.Add("keyword3", new WeiXinUnitEntity()
                                {
                                    value = order.CarBrandName
                                });
                                hashentity.Add("keyword4", new WeiXinUnitEntity()
                                {
                                    value = "备注" + order.Remark
                                });
                                hashentity.Add("remark", new WeiXinUnitEntity()
                                {
                                    value = ""
                                });
                                bool sendsuccess = WeiXinJsSdk.Instance.SendWeiXinMsgNote(memen.WeChat, redirecturl, WeiXinTemplet.InquiryNeedNote, hashentity);
                                if (sendsuccess)
                                {
                                    result = sendsuccess;
                                    //发送微信成功备份
                                    CGMemQuotedBLL.Instance.CGQuotedSend(memid, ordercode);
                                    emailnote += "发送成功<br/>";
                                }
                            }
                            else
                            {
                                emailnote += "没有绑定微信<br/>";
                                ////需发邮件提醒
                                //EmailSendEntity email = new EmailSendEntity();
                                //email.CreateTime = DateTime.Now;
                                //email.Body = "询价单编号:" + order.Code + "<br/> 供应商:" + memen.MemCode + "(" + memen.MobilePhone + ")" + memen.MemGradeName + ",<br/>网址:" + redirecturl;
                                //var emailsendstr = SuperMarket.Core.ConfigCore.Instance.ConfigCommonEntity.SendMailManager;
                                //if (emailsendstr == null || string.IsNullOrEmpty(emailsendstr.ToString()))
                                //{
                                //    email.Email = "*****@*****.**";
                                //}
                                //else
                                //{
                                //    email.Email = emailsendstr.ToString();
                                //}
                                //email.Title = "询价单发送微信错误供应商未登记微信";
                                //email.Status = 0;
                                //EmailSendBLL.Instance.AddEmailSend(email);
                            }
                        }
                        catch (Exception ex)
                        {
                            emailnote += "发送错误" + ex.Message + "<br/>";
                            LogUtil.Log("微信发送询价出错:订单号:" + ordercode, ex.Message);
                        }
                    }
                    else
                    {
                        string title = "您有订单需要报价啦,赶紧抢单,订单编号:" + order.Code;
                        if (entity.HasSend == 1)
                        {
                            title = "客户修改了询价单信息,请完善报价,订单编号:" + order.Code;
                        }
                        string ordercode = entity.InquiryOrderCode;
                        try
                        {
                            //获取链接导航Id
                            int            memid = entity.CGMemId;
                            VWMemberEntity memen = MemberBLL.Instance.GetVWMember(memid);

                            emailnote += memen.CompanyName + memen.MobilePhone + memen.CompanyAddress + "     ";
                            if (!string.IsNullOrEmpty(memen.WeChat))
                            {
                                Hashtable hashentity = new Hashtable();
                                hashentity.Add("first", new WeiXinUnitEntity()
                                {
                                    value = title
                                });
                                hashentity.Add("tradeDateTime", new WeiXinUnitEntity()
                                {
                                    value = order.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")
                                });
                                hashentity.Add("orderType", new WeiXinUnitEntity()
                                {
                                    value = "询价订单"
                                });
                                hashentity.Add("customerInfo", new WeiXinUnitEntity()
                                {
                                    value = "易店心"
                                });
                                hashentity.Add("orderItemName", new WeiXinUnitEntity()
                                {
                                    value = "备注"
                                });
                                hashentity.Add("orderItemData", new WeiXinUnitEntity()
                                {
                                    value = order.Remark
                                });
                                hashentity.Add("remark", new WeiXinUnitEntity()
                                {
                                    value = ""
                                });
                                bool sendsuccess = WeiXinJsSdk.Instance.SendWeiXinMsgNote(memen.WeChat, redirecturl, WeiXinTemplet.InquiryQuoteSend, hashentity);
                                if (sendsuccess)
                                {
                                    result = sendsuccess;
                                    //发送微信成功备份
                                    CGMemQuotedBLL.Instance.CGQuotedSend(memid, ordercode);
                                    emailnote += "发送成功<br/>";
                                }
                                else
                                {
                                    emailnote += "发送失败<br/>";
                                }
                                //else
                                //{
                                //    //需发邮件提醒
                                //    EmailSendEntity email = new EmailSendEntity();
                                //    email.CreateTime = DateTime.Now;
                                //    email.Body = "询价单编号:" + order.Code + "<br/> 供应商:" + memen.MemCode + "(" + memen.MobilePhone + ")" + memen.MemGradeName + ",<br/>网址:" + redirecturl;
                                //    var emailsendstr = SuperMarket.Core.ConfigCore.Instance.ConfigCommonEntity.SendMailManager;
                                //    if (emailsendstr == null || string.IsNullOrEmpty(emailsendstr.ToString()))
                                //    {
                                //        email.Email = "*****@*****.**";
                                //    }
                                //    else
                                //    {
                                //        email.Email = emailsendstr.ToString();
                                //    }
                                //    email.Title = "询价单发送微信通知失败";
                                //    email.Status = 0;
                                //    EmailSendBLL.Instance.AddEmailSend(email);
                                //}
                            }
                            else
                            {
                                emailnote += "没有绑定微信<br/>";
                                ////需发邮件提醒
                                //EmailSendEntity email = new EmailSendEntity();
                                //email.CreateTime = DateTime.Now;
                                //email.Body = "询价单编号:" + order.Code + "<br/> 供应商:" + memen.MemCode + "(" + memen.MobilePhone + ")" + memen.MemGradeName + ",<br/>网址:" + redirecturl;
                                //var emailsendstr = SuperMarket.Core.ConfigCore.Instance.ConfigCommonEntity.SendMailManager;
                                //if (emailsendstr == null || string.IsNullOrEmpty(emailsendstr.ToString()))
                                //{
                                //    email.Email = "*****@*****.**";
                                //}
                                //else
                                //{
                                //    email.Email = emailsendstr.ToString();
                                //}
                                //email.Title = "询价单发送微信错误供应商未登记微信";
                                //email.Status = 0;
                                //EmailSendBLL.Instance.AddEmailSend(email);
                            }
                        }
                        catch (Exception ex)
                        {
                            emailnote += "发送错误" + ex.Message + "<br/>";

                            LogUtil.Log("微信发送询价出错:订单号:" + ordercode, ex.Message);
                        }
                    }
                }
            }
            else
            {
                InquiryOrderBLL.Instance.UpdateQuoteStatus(order.Code, (int)QuoteStatusEnum.SendFail);
            }

            string notemsg = emailnote + "报价网址" + redirecturl + "<br/>";

            notemsg += "转移到线下网址:" + SuperMarket.Core.ConfigCore.Instance.ConfigCommonEntity.InquiryMobileWebUrl + "/Check/copyinquiry?code=" + order.Code + "<br/>";
            //notemsg+="订单信息:订单号:" + order.Code+"<br/>";
            //notemsg+="备注" + order.Remark+ "<br/>";
            //notemsg+="图片信息" + "<br/>";
            //if(!string.IsNullOrEmpty(order.VinPic))
            //{
            //    notemsg += "<img src='" + SuperMarket.Core.ConfigCore.Instance.ConfigCommonEntity.ImagesServerUrl + order.VinPic + "' />";
            //}
            //if (!string.IsNullOrEmpty(order.EngineModelPic))
            //{
            //    notemsg += "<img src='" + SuperMarket.Core.ConfigCore.Instance.ConfigCommonEntity.ImagesServerUrl + order.EngineModelPic + "' />";
            //}
            //if (order.OrderPics!=null&& order.OrderPics.Count>0)
            //{
            //    foreach(InquiryOrderPicsEntity pic in order.OrderPics)
            //    {
            //              notemsg += "<img src='" + SuperMarket.Core.ConfigCore.Instance.ConfigCommonEntity.ImagesServerUrl + pic.PicUrl + "' />";
            //    }
            //}
            //notemsg +="<br/>";
            //notemsg += "产品信息" + "<br/>";
            //if (order.OrderProducts != null && order.OrderProducts.Count > 0 && order.OrderProductSubs!=null && order.OrderProductSubs.Count>0)
            //{
            //    Dictionary<int, IList<InquiryProductSubEntity>> prosubdic = new Dictionary<int, IList<InquiryProductSubEntity>>();
            //    foreach (InquiryProductSubEntity prosub in order.OrderProductSubs)
            //    {
            //        if(!prosubdic.ContainsKey(prosub.InquiryProductId))
            //        {
            //            prosubdic.Add(prosub.InquiryProductId, new List<InquiryProductSubEntity>());
            //        }
            //        prosubdic[prosub.InquiryProductId].Add(prosub);
            //    }
            //    foreach(InquiryProductEntity pro in order.OrderProducts)
            //    {
            //        if(prosubdic.ContainsKey(pro.Id))
            //        {
            //            IList <InquiryProductSubEntity> prosublist = prosubdic[pro.Id];
            //            if(prosublist!=null&& prosublist.Count>0)
            //            {
            //                foreach(InquiryProductSubEntity prosub in prosublist)
            //                {
            //                    notemsg += pro.ProductName + " " + prosub.InquiryProductTypeName + "<br/>";
            //                }
            //            }
            //        }
            //    }
            //}
            if (memlist != null && memlist.Count > 0)
            {
                foreach (VWMemberEntity cgmemsup in memlist)
                {
                    MemWeChatMsgEntity   wecharmsg = MemWeChatMsgBLL.Instance.GetMsgByAppUnionId(WeiXinConfig.GetAppId(), cgmemsup.WeChat);
                    WeiXinCustomerEntity customer  = new WeiXinCustomerEntity();
                    customer.touser  = wecharmsg.OpenId;
                    customer.msgtype = WeiXinCustomerMsgtypeEnum.text;
                    WeiXinCustomerTextEntity text = new WeiXinCustomerTextEntity();
                    text.content  = notemsg.Replace("<br/>", "\n");
                    customer.text = text;
                    WeiXinJsSdk.Instance.SendWeiXinCustomerNote(customer);
                }
            }
            //需发邮件提醒
            EmailSendEntity email = new EmailSendEntity();

            email.CreateTime = DateTime.Now;
            email.Body       = notemsg;
            var emailsendstr = SuperMarket.Core.ConfigCore.Instance.ConfigCommonEntity.SendMailManager;

            if (emailsendstr == null || string.IsNullOrEmpty(emailsendstr.ToString()))
            {
                email.Email = "*****@*****.**";
            }
            else
            {
                email.Email = emailsendstr.ToString();
            }
            email.Title  = "询价单没有找到供应商进行报价";
            email.Status = 0;
            EmailSendBLL.Instance.AddEmailSend(email);
            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 发送微信通知
        /// </summary>
        /// <param name="WeChatUnionId"></param>
        /// <param name="redirecturl"></param>
        /// <param name="templetid"></param>
        /// <param name="hashen"></param>
        /// <returns></returns>

        public bool SendWeiXinMsgNote(string WeChatUnionId, string redirecturl, string templetid, Hashtable hashen)
        {
            bool success = false;
            //获取链接导航Id
            //int navid = WeChatNavigationBLL.Instance.GetIdByUrl(redirecturl);
            MemWeChatMsgEntity wecharmsg = MemWeChatMsgBLL.Instance.GetMsgByAppUnionId(WeiXinConfig.GetAppId(), WeChatUnionId);

            if (wecharmsg != null && !string.IsNullOrEmpty(wecharmsg.OpenId))
            {
                WeiXinSendMsgEntity send = new WeiXinSendMsgEntity();
                send.touser      = wecharmsg.OpenId;
                send.template_id = templetid;
                string resulturl = string.Format(WeiXinConfig.URL_WeiXin_Redirect, WeiXinConfig.GetAppId(), System.Web.HttpContext.Current.Server.UrlEncode(redirecturl), "0");
                send.url  = resulturl;
                send.data = hashen;
                string json = JsonJC.ObjectToJson(send);
                string url  = string.Format(WeiXinConfig.URL_FORMAT_SendMsg, WeiXinJsSdk.Instance.GetAccessToken(false));
                ///发送微信备案
                WeChatMsgEntity msg = new WeChatMsgEntity();
                msg.ParamStr     = json;
                msg.WeChatOpenId = wecharmsg.OpenId;
                msg.RedirectUrl  = redirecturl;
                msg.WeChatUrl    = url;
                msg.TemplateIid  = templetid;
                msg.Id           = WeChatMsgBLL.Instance.AddWeChatMsg(msg);
                string           result   = WebServiceClient.QueryPostWebServiceJson(url, json);
                WeiXinFailEntity resulten = JsonJC.JsonToObject <WeiXinFailEntity>(result);
                msg.Result = result;
                WeChatMsgBLL.Instance.UpdateWeChatMsg(msg);
                if (resulten.errmsg.ToLower() == "ok")
                {
                    success = true;
                }
                else if (resulten.errcode == "40001" && resulten.errmsg.Contains("access_token is invalid or not latest hint"))
                {
                    ///发现accesstoken过期后再次发送
                    WeiXinJsSdk.Instance.RemoveAccessToken();
                    url = string.Format(WeiXinConfig.URL_FORMAT_SendMsg, WeiXinJsSdk.Instance.GetAccessToken(false));
                    ///发送微信备案
                    msg.WeChatUrl = url;
                    msg.Result    = "";
                    msg.Id        = 0;
                    msg.Id        = WeChatMsgBLL.Instance.AddWeChatMsg(msg);
                    result        = WebServiceClient.QueryPostWebServiceJson(url, json);
                    resulten      = JsonJC.JsonToObject <WeiXinFailEntity>(result);
                    msg.Result    = result;
                    WeChatMsgBLL.Instance.UpdateWeChatMsg(msg);
                    if (resulten.errmsg.ToLower() == "ok")
                    {
                        success = true;
                    }
                    else
                    {
                        //需发邮件提醒
                        EmailSendBLL.Instance.WeiXinSendFail(WeChatUnionId, redirecturl);
                    }
                }
                else
                {
                    //需发邮件提醒
                    EmailSendBLL.Instance.WeiXinSendFail(WeChatUnionId, redirecturl);
                }
            }
            return(success);
        }