示例#1
0
        public override FullSystemConfig Update()
        {
            SystemConfig systemConfig = null;

            try
            {
                systemConfig = (_dataContext.BaseDataContext as SenparcEntitiesBase).SystemConfigs.FirstOrDefault();
            }
            catch
            {
                new NcfUninstallException("FullSystemConfigCache 访问数据库异常,推测系统未安装或未正确配置数据库");
            }

            FullSystemConfig fullSystemConfig;

            if (systemConfig != null)
            {
                fullSystemConfig = FullSystemConfig.CreateEntity <FullSystemConfig>(systemConfig);
            }
            else
            {
                string hostName = null;
                try
                {
                    var    httpContextAccessor = SenparcDI.GetServiceProvider().GetService <IHttpContextAccessor>();
                    var    httpContext         = httpContextAccessor.HttpContext;
                    var    urlData             = httpContext.Request;
                    var    scheme      = urlData.Scheme;               //协议
                    var    host        = urlData.Host.Host;            //主机名(不带端口)
                    var    port        = urlData.Host.Port ?? -1;      //端口(因为从.NET Framework移植,因此不直接使用urlData.Host)
                    string portSetting = null;                         //Url中的端口部分
                    string schemeUpper = scheme.ToUpper();             //协议(大写)
                    string baseUrl     = httpContext.Request.PathBase; //子站点应用路径

                    if (port == -1 ||                                  //这个条件只有在 .net core 中, Host.Port == null 的情况下才会发生
                        (schemeUpper == "HTTP" && port == 80) ||
                        (schemeUpper == "HTTPS" && port == 443))
                    {
                        portSetting = "";//使用默认值
                    }
                    else
                    {
                        portSetting = ":" + port;//添加端口
                    }

                    hostName = $"{scheme}://{host}{portSetting}";
                }
                catch
                {
                }

                //尝试安装

                throw new NcfUninstallException($"NCF 系统未初始化,请先执行 {hostName}/Install 进行数据初始化");
            }


            base.SetData(fullSystemConfig, base.TimeOut, null);
            return(base.Data);
        }
示例#2
0
        private static string GetPageLink(int linkPageIndex, int currentPageIndex, string text, string currentPageWordFormat, string onclick, string url, string barMark, bool inInframe)
        {
            //var pageData = "?page=";//string.Format("{0}page=", (Request.QueryString.Count == 0) ? "?" : "&") + "{0}";//页码参数

            onclick = (onclick != null) ? "onclick=\"" + onclick + "\"" : "";
            onclick = onclick.Replace("{pageindex}", linkPageIndex.ToString());

            var httpContext = SenparcDI.GetServiceProvider().GetService <IHttpContextAccessor>().HttpContext;

            url = (!string.IsNullOrEmpty(url)) ? url.UrlDecode().Replace(currentPageWordFormat, linkPageIndex.ToString()) : "";
            var href = (onclick != null && onclick.IndexOf("return false") != -1) ? "href=\"#" + barMark + "\" " : "href=\"" + url + "\" ";

            var linkHTML = "";

            string formatedText = currentPageWordFormat.IndexOf("{0}") >= 0 ? string.Format(currentPageWordFormat, text) : text;//判断是{0}形式还是自定义替换字符串。

            if (linkPageIndex == currentPageIndex)
            {
                linkHTML = $"<span class=\"current\">{formatedText}</span>";
            }
            else
            {
                linkHTML = "<a " + href + onclick + $" {(inInframe ? "data - iniframe =\"1\"" : null)}>{text}</a>";
            }
            return(linkHTML);
        }
示例#3
0
        /// <summary>
        /// LocalCacheStrategy的构造函数
        /// </summary>
        MemcachedObjectCacheStrategy(/*ILoggerFactory loggerFactory, IOptions<MemcachedClientOptions> optionsAccessor*/)
        {
            _config = GetMemcachedClientConfiguration();
#if NET45 //|| NET461
            Cache = new MemcachedClient(_config);
#else
            var            provider      = SenparcDI.GetServiceProvider();
            ILoggerFactory loggerFactory = provider.GetService <ILoggerFactory>();
            Cache = new MemcachedClient(loggerFactory, _config);
#endif
        }
示例#4
0
        /// <summary>
        /// 下载图片到指定文件
        /// </summary>
        /// <param name="picUrl"></param>
        /// <param name="fileName"></param>
        public async Task DownLoadPicAsync(string picUrl, string fileName)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                var serviceProvider = SenparcDI.GetServiceProvider();
                await CO2NET.HttpUtility.Get.DownloadAsync(serviceProvider, picUrl, stream);

                using (var fs = new FileStream(Server.GetMapPath("~" + fileName), FileMode.CreateNew))
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    stream.CopyTo(fs);
                    fs.Flush();
                }
            }
        }
示例#5
0
        public override IResponseMessageBase OnEvent_ScanRequest(RequestMessageEvent_Scan requestMessage)
        {
            try
            {
                if (int.TryParse(requestMessage.EventKey, out int sceneId))
                {
                    var serviceProvider = SenparcDI.GetServiceProvider();

                    //临时二维码
                    var qrCodeRegCache = serviceProvider.GetService <QrCodeRegCache>();
                    var qrCodeRegData  = qrCodeRegCache.Get(sceneId.ToString());

                    if (qrCodeRegData?.Data != null && qrCodeRegData.Data.Ticket == requestMessage.Ticket)
                    {
                        var responseRegMessage = CreateResponseMessage <ResponseMessageText>();
                        if (qrCodeRegData.Data.QrCodeRegDataType == QrCodeRegDataType.Reg)
                        {
                            var userService = serviceProvider.GetService <AccountService>();
                            //判断是否已经绑定[CodeChecked]
                            if (userService.GetCount(z => z.WeixinOpenId == requestMessage.FromUserName) <= 0)
                            {
                                var code = new Random().Next(100000, 999999).ToString();
                                qrCodeRegData.Data.Code   = code;
                                qrCodeRegData.Data.OpenId = requestMessage.FromUserName;
                                qrCodeRegCache.Remove(sceneId.ToString());
                                qrCodeRegCache.Insert(qrCodeRegData.Data, sceneId.ToString());
                                responseRegMessage.Content =
                                    "验证码:" + code;
                            }
                            else
                            {
                                responseRegMessage.Content = "您的微信已绑定NCF账户,不能重复绑定!";
                            }
                            return(responseRegMessage);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtility.Weixin.Error(ex.Message, ex);
            }
            return(base.OnEvent_ScanRequest(requestMessage));
        }
示例#6
0
        private static MemcachedClientConfiguration GetMemcachedClientConfiguration(/*ILoggerFactory loggerFactory, IOptions<MemcachedClientOptions> optionsAccessor*/)
#endif
        {
            //每次都要新建

#if NET45
            var config = new MemcachedClientConfiguration();
            foreach (var server in _serverlist)
            {
                config.Servers.Add(new IPEndPoint(IPAddress.Parse(server.Key), server.Value));
            }
            config.Protocol = MemcachedProtocol.Binary;
#else
            var            provider      = SenparcDI.GetServiceProvider();
            ILoggerFactory loggerFactory = provider.GetService <ILoggerFactory>();
            IOptions <MemcachedClientOptions> optionsAccessor = provider.GetService <IOptions <MemcachedClientOptions> >();

            var config = new MemcachedClientConfiguration(loggerFactory, optionsAccessor);
#endif
            return(config);
        }
示例#7
0
        /// <summary>
        /// 操作列队
        /// </summary>
        public static void OperateQueue()
        {
            lock (FlushCacheLock)
            {
                var mq              = new OperationQueue();
                var key             = mq.GetCurrentKey(); //获取最新的Key
                var serviceProvider = SenparcDI.GetServiceProvider();
                while (!string.IsNullOrEmpty(key))
                {
                    try
                    {
                        var operationQueueService = serviceProvider.GetService <OperationQueueService>();
                        var mqItem = mq.GetItem(key); //获取任务项

                        //识别类型
                        switch (mqItem.OperationQueueType)
                        {
                        case OperationQueueType.更新用户头像:
                            operationQueueService.UpdateAccountHeadImgAsync(serviceProvider, (int)mqItem.Data[0], mqItem.Data[1] as string).Wait();
                            break;

                        default:
                            LogUtility.OperationQueue.ErrorFormat("OperationQueueType未处理:{0}", mqItem.OperationQueueType.ToString());
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogUtility.OperationQueue.ErrorFormat($"OperationQueue列队操作失败,已经跳过:{ex.Message}", ex);
                    }

                    mq.Remove(key);           //清除
                    key = mq.GetCurrentKey(); //获取最新的Key
                }
            }
        }
示例#8
0
        public override TSenparcEntities GetDbContextInstance(DbContextOptions <TSenparcEntities> dbContextOptions)
        {
            //获取 XncfDatabase 对象
            //var databaseRegister = Activator.CreateInstance(typeof(TXncfDatabaseRegister)) as TXncfDatabaseRegister;

            //获取当前适用的 DbContext 类型
            var dbContextType = MultipleDatabasePool.Instance.GetXncfDbContextType(typeof(TXncfDatabaseRegister));

            var constructorParams = new List <object>()
            {
                dbContextOptions
            };

            //判断构造函数参数个数
            if (dbContextType.GetConstructors().First().GetParameters().Length == 2)
            {
                constructorParams.Add(SenparcDI.GetServiceProvider());//添加第二个参数(目前只为系统基础对象,如 SystemServiceEntities 使用)
            }

            //获取 XncfSenparcEntities 实例
            var xncfSenparcEntities = Activator.CreateInstance(dbContextType, constructorParams.ToArray()) as TSenparcEntities;

            return(xncfSenparcEntities);
        }
        /// <summary>
        /// 点击事件
        /// </summary>
        /// <param name="requestMessage">请求消息</param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_ClickRequest(RequestMessageEvent_Click requestMessage)
        {
            IResponseMessageBase reponseMessage = null;

            //菜单点击,需要跟创建菜单时的Key匹配

            switch (requestMessage.EventKey)
            {
            case "OneClick":
            {
                //这个过程实际已经在OnTextOrEventRequest中命中“OneClick”关键字,并完成回复,这里不会执行到。
                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 = "您点击了子菜单图文按钮,这是一条图文信息。这个区域是Description内容\r\n可以使用\\r\\n进行换行。",
                        PicUrl      = "https://sdk.weixin.senparc.com/Images/qrcode.jpg",
                        Url         = "https://sdk.weixin.senparc.com"
                    });

                //随机添加一条图文,或只输出一条图文信息
                if (SystemTime.Now.Second % 2 == 0)
                {
                    strongResponseMessage.Articles.Add(new Article()
                        {
                            Title       = "这是随机产生的第二条图文信息,用于测试多条图文的样式",
                            Description = "这是随机产生的第二条图文信息,用于测试多条图文的样式",
                            PicUrl      = "https://sdk.weixin.senparc.com/Images/qrcode.jpg",
                            Url         = "https://sdk.weixin.senparc.com"
                        });
                }
            }
            break;

            case "SubClickRoot_Music":
            {
                //上传缩略图

#if NET45
                var filePath = "~/Images/Logo.thumb.jpg";
#else
                var filePath = "~/wwwroot/Images/Logo.thumb.jpg";
#endif

                var uploadResult = Senparc.Weixin.MP.AdvancedAPIs.MediaApi.UploadTemporaryMedia(appId, Senparc.Weixin.MP.UploadMediaFileType.thumb,
                                                                                                ServerUtility.ContentRootMapPath(filePath));
                //PS:缩略图官方没有特别提示文件大小限制,实际测试哪怕114K也会返回文件过大的错误,因此尽量控制在小一点(当前图片39K)

                //设置音乐信息
                var strongResponseMessage = CreateResponseMessage <ResponseMessageMusic>();
                reponseMessage = strongResponseMessage;
                strongResponseMessage.Music.Title        = "天籁之音";
                strongResponseMessage.Music.Description  = "真的是天籁之音";
                strongResponseMessage.Music.MusicUrl     = "https://sdk.weixin.senparc.com/Content/music1.mp3";
                strongResponseMessage.Music.HQMusicUrl   = "https://sdk.weixin.senparc.com/Content/music1.mp3";
                strongResponseMessage.Music.ThumbMediaId = uploadResult.thumb_media_id;
            }
            break;

            case "SubClickRoot_Image":
            {
                //上传图片
#if NET45
                var filePath = "~/Images/Logo.jpg";
#else
                var filePath = "~/wwwroot/Images/Logo.jpg";
#endif

                var uploadResult = Senparc.Weixin.MP.AdvancedAPIs.MediaApi.UploadTemporaryMedia(appId, Senparc.Weixin.MP.UploadMediaFileType.image,
                                                                                                ServerUtility.ContentRootMapPath(filePath));
                //设置图片信息
                var strongResponseMessage = CreateResponseMessage <ResponseMessageImage>();
                reponseMessage = strongResponseMessage;
                strongResponseMessage.Image.MediaId = uploadResult.media_id;
            }
            break;

            case "SendMenu":    //菜单消息
            {
                //注意:
                //1、此接口可以在任意地方调用(包括后台线程),此处演示为通过
                //2、一下"s:"前缀只是 Senparc.Weixin 的内部约定,可以使用 OnTextRequest事件中的 requestHandler.SelectMenuKeyword() 方法自动匹配到后缀(如101)

                var menuContentList = new List <SendMenuContent>()
                {
                    new SendMenuContent("101", "满意"),
                    new SendMenuContent("102", "一般"),
                    new SendMenuContent("103", "不满意")
                };
                //使用异步接口
                CustomApi.SendMenuAsync(appId, OpenId, "请对 Senparc.Weixin SDK 给出您的评价", menuContentList, "感谢您的参与!");

                reponseMessage = new ResponseMessageNoResponse();        //不返回任何消息
            }
            break;

            case "SubClickRoot_Agent":    //代理消息
            {
                //获取返回的XML
                var dt1 = SystemTime.Now;
                reponseMessage = MessageAgent.RequestResponseMessage(this, SenparcDI.GetServiceProvider(), agentUrl, agentToken, RequestDocument.ToString());
                //上面的方法也可以使用扩展方法:this.RequestResponseMessage(this,agentUrl, agentToken, RequestDocument.ToString());

                var dt2 = SystemTime.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(SenparcDI.GetServiceProvider(), agentUrl, agentToken, RequestDocument.ToString());
            }
            break;

            case "OAuth":    //OAuth授权测试
            {
                var strongResponseMessage = CreateResponseMessage <ResponseMessageNews>();

                strongResponseMessage.Articles.Add(new Article()
                    {
                        Title       = "OAuth2.0测试",
                        Description = "选择下面两种不同的方式进行测试,区别在于授权成功后,最后停留的页面。",
                        //Url = "https://sdk.weixin.senparc.com/oauth2",
                        //PicUrl = "https://sdk.weixin.senparc.com/Images/qrcode.jpg"
                    });

                strongResponseMessage.Articles.Add(new Article()
                    {
                        Title       = "OAuth2.0测试(不带returnUrl),测试环境可使用",
                        Description = "OAuth2.0测试(不带returnUrl)",
                        Url         = "https://sdk.weixin.senparc.com/oauth2",
                        PicUrl      = "https://sdk.weixin.senparc.com/Images/qrcode.jpg"
                    });

                var returnUrl = "/OAuth2/TestReturnUrl";
                strongResponseMessage.Articles.Add(new Article()
                    {
                        Title       = "OAuth2.0测试(带returnUrl),生产环境强烈推荐使用",
                        Description = "OAuth2.0测试(带returnUrl)",
                        Url         = "https://sdk.weixin.senparc.com/oauth2?returnUrl=" + returnUrl.UrlEncode(),
                        PicUrl      = "https://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;

            case "GetNewMediaId":    //获取新的MediaId
            {
                var strongResponseMessage = CreateResponseMessage <ResponseMessageText>();
                try
                {
                    var result = Senparc.Weixin.MP.AdvancedAPIs.MediaApi.UploadForeverMedia(appId, ServerUtility.ContentRootMapPath("~/Images/logo.jpg"));
                    strongResponseMessage.Content = result.media_id;
                }
                catch (Exception e)
                {
                    strongResponseMessage.Content = "发生错误:" + e.Message;
                    Senparc.Weixin.WeixinTrace.SendCustomLog("调用UploadForeverMedia()接口发生异常", e.Message);
                }
            }
            break;

            default:
            {
                var strongResponseMessage = CreateResponseMessage <ResponseMessageText>();
                strongResponseMessage.Content = "您点击了按钮,EventKey:" + requestMessage.EventKey;
                reponseMessage = strongResponseMessage;
            }
            break;
            }

            return(reponseMessage);
        }
示例#10
0
        /// <summary>
        /// 获取页面提交的get和post参数
        /// 注意:.NetCore环境必须传入HttpContext实例,不能传Null,这个接口调试特别困难,千万别出错!
        /// </summary>
        /// <param name="httpContext"></param>
        public ResponseHandler(HttpContext httpContext)
        {
#if NET45
            Parameters = new Hashtable();

            this.HttpContext = httpContext ?? HttpContext.Current;
            NameValueCollection collection;
            //post data
            if (this.HttpContext.Request.HttpMethod == "POST")
            {
                collection = this.HttpContext.Request.Form;
                foreach (string k in collection)
                {
                    string v = (string)collection[k];
                    this.SetParameter(k, v);
                }
            }
            //query string
            collection = this.HttpContext.Request.QueryString;
            foreach (string k in collection)
            {
                string v = (string)collection[k];
                this.SetParameter(k, v);
            }
            if (this.HttpContext.Request.InputStream.Length > 0)
            {
                XmlDocument xmlDoc = new Senparc.CO2NET.ExtensionEntities.XmlDocument_XxeFixed();
                xmlDoc.XmlResolver = null;
                xmlDoc.Load(this.HttpContext.Request.InputStream);
                XmlNode     root = xmlDoc.SelectSingleNode("xml");
                XmlNodeList xnl  = root.ChildNodes;

                foreach (XmlNode xnf in xnl)
                {
                    this.SetParameter(xnf.Name, xnf.InnerText);
                }
            }
#else
            Parameters = new Hashtable();

            //#if NETSTANDARD2_0 || NETSTANDARD2_1
            //            HttpContext = httpContext ?? throw new WeixinException(".net standard 2.0 环境必须传入HttpContext的实例");
            //#else

            var serviceProvider = SenparcDI.GetServiceProvider();
            HttpContext = httpContext ?? serviceProvider.GetService <IHttpContextAccessor>()?.HttpContext;
            //#endif

            //post data
            if (HttpContext.Request.Method.ToUpper() == "POST" && HttpContext.Request.HasFormContentType)
            {
                foreach (var k in HttpContext.Request.Form)
                {
                    SetParameter(k.Key, k.Value[0]);
                }
            }
            //query string
            foreach (var k in HttpContext.Request.Query)
            {
                SetParameter(k.Key, k.Value[0]);
            }
            if (HttpContext.Request.ContentLength > 0)
            {
                var xmlDoc = new Senparc.CO2NET.ExtensionEntities.XmlDocument_XxeFixed();
                xmlDoc.XmlResolver = null;
                //xmlDoc.Load(HttpContext.Request.Body);
                try
                {
                    var requestStream = HttpContext.Request.GetRequestMemoryStream();
                    requestStream.Seek(0, System.IO.SeekOrigin.Begin);
                    xmlDoc.Load(requestStream);

                    //using (var reader = new System.IO.StreamReader(HttpContext.Request.Body))
                    //{
                    //    xmlDoc.Load(reader);
                    //}

                    var root = xmlDoc.SelectSingleNode("xml");

                    foreach (XmlNode xnf in root.ChildNodes)
                    {
                        SetParameter(xnf.Name, xnf.InnerText);
                    }
                }
                catch (Exception ex)
                {
                    var weixinEx = new WeixinException("微信支付ResponseHandler错误", ex, true);
                    Senparc.Weixin.WeixinTrace.WeixinExceptionLog(weixinEx);
                }
            }
#endif
        }