public FileDownloadAgentResult DownloadAgent(FileDownloadAgentArgs args)
        {
            FileDownloadAgentResult result;

            HttpRequestArgs requestArgs = new HttpRequestArgs();

            requestArgs.Method  = "POST";
            requestArgs.Url     = _fileServiceUri + "File/DownloadAgent";
            requestArgs.Content = JsonConvert.SerializeObject(args);

            _log.Write("发起文件代理下载请求", requestArgs.Content, TraceEventType.Verbose);

            HttpRequestResult requestResult = _httpService.Request(requestArgs);

            if (requestResult.Success)
            {
                _log.Write("文件代理下载请求返回", requestResult.Content, TraceEventType.Verbose);
                result = JsonConvert.DeserializeObject <FileDownloadAgentResult>(requestResult.Content);
            }
            else
            {
                result         = new FileDownloadAgentResult();
                result.Message = requestResult.Exception.Message;
            }
            return(result);
        }
        private FileDownloadAgentResult DownloadFile(FileDownloadAgentArgs args)
        {
            _log.Write("发起文件代理下载请求", JsonConvert.SerializeObject(args), TraceEventType.Verbose);

            string targetDir = Path.Combine(args.ServerRootDir, "FileStore", args.Domain.ToString());

            if (Directory.Exists(targetDir) == false)
            {
                Directory.CreateDirectory(targetDir);
            }

            //string storeFileName = Guid.NewGuid().ToString() + args.FileExtension;
            //string outputFileName = Path.Combine(targetDir, storeFileName);

            HttpDownloadArgs downloadArgs = new HttpDownloadArgs();

            downloadArgs.Url       = args.Url;
            downloadArgs.TargetDir = targetDir;
            HttpDownloadResult downloadResult = _httpService.Download(downloadArgs);

            FileDownloadAgentResult result = new FileDownloadAgentResult();

            result.Success     = downloadResult.Success;
            result.Message     = downloadResult.Message;
            result.OutputFile  = String.Format("FileStore/{0}/{1}", args.Domain, downloadResult.StoreFileName);
            result.ContentType = downloadResult.ContentType;
            result.Tag         = args.Tag;

            _log.Write("文件代理下载请求返回", JsonConvert.SerializeObject(result), TraceEventType.Verbose);

            return(result);
        }
        private FileDownloadAgentResult DownloadFileTask(object state)
        {
            FileDownloadAgentArgs   args   = (FileDownloadAgentArgs)state;
            FileDownloadAgentResult result = DownloadFile(args);

            return(result);
        }
        public NormalResult UpdateAuthorizerAccountInfo(Guid domainId, string appId)
        {
            //获取公众号帐户信息
            RequestApiResult <WeixinThirdPartyGetAuthorizerAccountInfoResult> accountInfoResult =
                ThirdPartyApiWrapper.GetAuthorizerAccountInfo(appId);

            if (accountInfoResult.Success)
            {
                WeixinThirdPartyAuthorizerAccountInfo account = accountInfoResult.ApiResult.AccountInfo;

                //微信返回的二维码图片不允许外部引用,此处必须把图片下载到本地
                FileDownloadAgentArgs downloadAgentArgs = new FileDownloadAgentArgs();
                downloadAgentArgs.Domain = domainId;
                downloadAgentArgs.Url    = account.QRCodeUrl;
                FileDownloadAgentResult result = _fileService.DownloadAgent(downloadAgentArgs);
                string qrCodeUrl;
                if (result.Success)
                {
                    _log.Write("下载二维码返回", JsonConvert.SerializeObject(result), TraceEventType.Verbose);
                    qrCodeUrl = _fileService.FileServiceUri + result.OutputFile;
                }
                else
                {
                    qrCodeUrl = account.QRCodeUrl;
                }

                SqlStructureBuild sqlBuild = new SqlStructureBuild();
                sqlBuild.Table = "Authorizer";
                sqlBuild.Type  = SqlExpressionType.Update;
                sqlBuild.AddParameter("AppId", appId, true);
                sqlBuild.AddParameter("Domain", domainId, true);
                sqlBuild.AddParameter("NickName", account.NickName);
                sqlBuild.AddParameter("HeadImg", account.HeadImg);
                sqlBuild.AddParameter("ServiceType", account.ServiceType.Id);
                sqlBuild.AddParameter("VerifyType", account.VerifyType.Id);
                sqlBuild.AddParameter("UserName", account.UserName);
                sqlBuild.AddParameter("Alias", account.Alias);
                sqlBuild.AddParameter("QRCodeUrl", qrCodeUrl);
                sqlBuild.AddParameter("Store", account.Business.Store);
                sqlBuild.AddParameter("Scan", account.Business.Scan);
                sqlBuild.AddParameter("Pay", account.Business.Pay);
                sqlBuild.AddParameter("Card", account.Business.Card);
                sqlBuild.AddParameter("Shake", account.Business.Shake);
                sqlBuild.AddParameter("FuncScopeCategory",
                                      accountInfoResult.ApiResult.AuthorizationInfo.FuncScopeCategoryList.ToString());

                _dataBase.ExcuteSqlExpression(sqlBuild.GetSqlExpression());

                //更新LastUpdateTime
                DomainManager.Instance.UpdateLastUpdateTime(domainId);
            }

            NormalResult normalResult = new NormalResult();

            normalResult.Success = accountInfoResult.Success;
            normalResult.Message = accountInfoResult.Message;

            return(normalResult);
        }
        public NormalResult Create(DomainContext domainContext, ScenicQRCodeEntity scenicQRCode)
        {
            if (scenicQRCode == null)
            {
                Debug.Assert(false, "scenicQRCode 为空");
                return(new NormalResult("参数错误"));
            }

            //if (domainContext.Domain.Type == EnumDomainType.Free)
            //{
            //    //最大数量不允许超过10个
            //    if (GetTotalCount(domainContext) >= 5)
            //    {
            //        return new NormalResult("免费帐户创建场景二维码数量最多不可超过5个。");
            //    }
            //}

            WeixinCreateQRCodeArgs createArgs = new WeixinCreateQRCodeArgs();

            createArgs.ActionInfo.Scene.SceneId = scenicQRCode.Id.ToString();
            RequestApiResult <WeixinCreateQRCodeResult> createResult = QRCodeApiWrapper.Create(domainContext, createArgs);

            if (createResult.Success == false)
            {
                return(new NormalResult(createResult.Message));
            }

            scenicQRCode.Ticket = createResult.ApiResult.Ticket;
            scenicQRCode.Url    = createResult.ApiResult.Url;

            //下载二维码图片
            FileDownloadAgentArgs downloadAgentArgs = new FileDownloadAgentArgs();

            downloadAgentArgs.Domain = domainContext.Domain.Id;
            downloadAgentArgs.Url    = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" +
                                       System.Web.HttpUtility.UrlEncode(createResult.ApiResult.Ticket);
            FileDownloadAgentResult downloadAgentResult = _fileService.DownloadAgent(downloadAgentArgs);

            if (downloadAgentResult.Success)
            {
                _log.Write("下载二维码返回", JsonConvert.SerializeObject(downloadAgentResult), TraceEventType.Verbose);
                scenicQRCode.QRCodeImageUrl = _fileService.FileServiceUri + downloadAgentResult.OutputFile;
            }
            else
            {
                return(new NormalResult(downloadAgentResult.Message));
            }

            _dataBase.Insert(scenicQRCode);

            return(new NormalResult());
        }
        internal FileDownloadAgentResult Download(FileDownloadAgentArgs args)
        {
            if (args.Sync)
            {
                return(DownloadFile(args));
            }
            else
            {
                Task <FileDownloadAgentResult> downloadTask = new Task <FileDownloadAgentResult>(DownloadFileTask, args);
                downloadTask.ContinueWith((task) =>
                {
                    FileDownloadAgentResult downloadResult;
                    if (task.IsFaulted)
                    {
                        downloadResult = new FileDownloadAgentResult();
                        if (task.Exception.InnerException != null)
                        {
                            downloadResult.Message = task.Exception.InnerException.Message;
                            _log.Write("异步下载失败", task.Exception.InnerException.Message + "\r\n" +
                                       task.Exception.InnerException.StackTrace, TraceEventType.Error);
                        }
                        else
                        {
                            downloadResult.Message = task.Exception.Message;
                            _log.Write("异步下载失败", task.Exception.Message + "\r\n" +
                                       task.Exception.StackTrace, TraceEventType.Error);
                        }
                    }
                    else
                    {
                        downloadResult = task.Result;
                    }

                    //把下载结果POST到回调地址上
                    HttpRequestArgs requestArgs = new HttpRequestArgs();
                    requestArgs.Method          = "POST";
                    requestArgs.Url             = args.CallbackUrl;
                    requestArgs.Content         = JsonConvert.SerializeObject(downloadResult);

                    _httpService.Request(requestArgs);
                });
                downloadTask.Start();


                FileDownloadAgentResult result = new FileDownloadAgentResult();
                result.Message = "这是一个异步下载任务。";
                return(result);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// 下载代理
        /// 微信服务器上的一些图片禁止直接引用,必须下载到本地
        /// 此处根据一个绝对URL地址进行下载
        /// </summary>
        /// <returns></returns>
        public ActionResult DownloadAgent()
        {
            FileDownloadAgentArgs args = RequestArgs <FileDownloadAgentArgs>();

            if (args == null)
            {
                return(RespondResult(false, "参数无效。"));
            }
            args.ServerRootDir = Server.MapPath("/");

            FileDownloadAgentResult result = _fileManager.Download(args);

            return(new ContentResult()
            {
                Content = JsonConvert.SerializeObject(result)
            });
        }
        internal GetMemberQRCodeImageResult GetMemberQRCodeImage(string serverRootDir, GetCampaign_MemberQRCodeImageArgs args)
        {
            GetMemberQRCodeImageResult result = new GetMemberQRCodeImageResult();

            if (args == null || String.IsNullOrEmpty(args.LandingUrl))
            {
                result.Message = "参数不正确";
                return(result);
            }

            MemberEntity member = _memberManager.GetMember(args.MemberId);

            if (member == null)
            {
                result.Message = "会员信息不存在";
                return(result);
            }

            FileRecord fileRecord = _fileManager.Get(args.BackgroundImageId);

            if (fileRecord == null)
            {
                result.Message = "背景图信息不存在";
                return(result);
            }

            string backgroundImageFile =
                Path.Combine(serverRootDir, String.Format("FileStore/{0}/{1}", args.Domain, fileRecord.StoredFileName));

            if (File.Exists(backgroundImageFile) == false)
            {
                result.Message = "背景图文件不存在";
                return(result);
            }

            FileDownloadAgentArgs downloadArgs = new FileDownloadAgentArgs();

            downloadArgs.Domain        = args.Domain;
            downloadArgs.Url           = member.Headimgurl_96;
            downloadArgs.ServerRootDir = serverRootDir;

            FileDownloadAgentResult downloadResult = _fileManager.Download(downloadArgs);

            if (downloadResult.Success == false)
            {
                result.Message = downloadResult.Message;
                return(result);
            }

            string headImageFile = Path.Combine(serverRootDir, downloadResult.OutputFile);

            Image qrImage         = null;
            Image backgroundImage = null;
            Image headImage       = null;

            try
            {
                qrImage = QRCodeHelper.GetQRCode(args.LandingUrl);

                if (qrImage == null)
                {
                    result.Message = "二维码生成失败。";
                    return(result);
                }

                backgroundImage = Image.FromFile(backgroundImageFile);
                Graphics g = Graphics.FromImage(backgroundImage);

                //qrImage
                float x = backgroundImage.Width - qrImage.Width - 20;
                float y = backgroundImage.Height - qrImage.Height - 20;
                g.DrawImage(qrImage, x, y);

                //headImage
                x         = 20;
                y         = backgroundImage.Height - 20 - 96;
                headImage = Image.FromFile(headImageFile);
                g.DrawImage(headImage, x, y, 96, 96);

                ////nickName
                //Font font = new Font("黑体", 35f);
                //x = 96 + 40;
                //y = backgroundImage.Height - font.Size - 50;
                //g.DrawString(member.NickName, font, Brushes.Black, new PointF(x, y));

                g.Save();

                string targetDir      = Path.Combine(serverRootDir, "FileStore", args.Domain.ToString());
                string storeFileName  = Guid.NewGuid().ToString() + ".jpg";
                string outputFileName = Path.Combine(targetDir, storeFileName);
                backgroundImage.Save(outputFileName, ImageFormat.Jpeg);

                result.Success  = true;
                result.FileName = String.Format("FileStore/{0}/{1}", args.Domain, storeFileName);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }
            finally
            {
                if (qrImage != null)
                {
                    qrImage.Dispose();
                }

                if (backgroundImage != null)
                {
                    backgroundImage.Dispose();
                }

                if (headImage != null)
                {
                    headImage.Dispose();
                }
            }

            return(result);
        }