Ejemplo n.º 1
0
        public IActionResult EditAds(Ads model)
        {
            if (string.IsNullOrEmpty(model.Title))
            {
                tip.Message = "广告标题不能为空!";
                return(Json(tip));
            }
            if (model.Id <= 0)
            {
                tip.Message = "错误参数传递!";
                Json(tip);
            }
            Ads entity = Ads.Find(Ads._.Id == model.Id);

            if (entity == null)
            {
                tip.Message = "系统找不到本记录!";
                return(Json(tip));
            }

            string   content = "";
            DateTime StartTime, EndTime;

            StartTime = Convert.ToDateTime("2000-01-01");
            EndTime   = Convert.ToDateTime("2999-01-01");

            #region 获取数据
            switch (model.TId)
            {
            case 0:    //代码
                ScriptAds script = new ScriptAds();
                script.content = Request.Form["txtScript"];
                if (string.IsNullOrEmpty(script.content))
                {
                    tip.Message = "代码不能为空!";
                    return(Json(tip));
                }
                content = JsonConvert.SerializeObject(script);
                break;

            case 1:    //文字
                TextAds text = new TextAds();
                text.txt   = Request.Form["txt_Txt"];
                text.link  = Request.Form["txt_Link"];
                text.style = Request.Form["txt_Style"];
                if (string.IsNullOrEmpty(text.txt))
                {
                    tip.Message = "文字内容不能为空!";
                    return(Json(tip));
                }
                if (string.IsNullOrEmpty(text.link))
                {
                    tip.Message = "文字链接不能为空!";
                    return(Json(tip));
                }
                content = JsonConvert.SerializeObject(text);
                break;

            case 2:    //图片
                ImgAds img = new ImgAds();
                img.img  = Request.Form["img_Img"];
                img.link = Request.Form["img_Link"];
                img.alt  = Request.Form["img_Alt"];
                string iw = Request.Form["img_Width"];
                string ih = Request.Form["img_Height"];
                if (!string.IsNullOrEmpty(iw) && Utils.IsInt(iw))
                {
                    img.width = int.Parse(iw);
                }
                if (!string.IsNullOrEmpty(ih) && Utils.IsInt(ih))
                {
                    img.height = int.Parse(ih);
                }
                if (string.IsNullOrEmpty(img.img))
                {
                    tip.Message = "图片地址不能为空!";
                    return(Json(tip));
                }
                if (string.IsNullOrEmpty(img.link))
                {
                    tip.Message = "图片链接不能为空!";
                    return(Json(tip));
                }
                content = JsonConvert.SerializeObject(img);
                break;

            case 3:    //Flash
                FlashAds flash = new FlashAds();
                flash.swf = Request.Form["flash_Swf"];
                string fw = Request.Form["flash_Width"];
                string fh = Request.Form["flash_Height"];
                if (string.IsNullOrEmpty(flash.swf))
                {
                    tip.Message = "Flash 地址不能为空!";
                    return(Json(tip));
                }

                if (string.IsNullOrEmpty(fw) || !Utils.IsInt(fw))
                {
                    tip.Message = "Flash 宽度不能为空或者不是整数!";
                    return(Json(tip));
                }
                else
                {
                    flash.width = int.Parse(fw);
                }
                if (string.IsNullOrEmpty(fh) || !Utils.IsInt(fh))
                {
                    tip.Message = "Flash 高度不能为空或者不是整数!";
                    return(Json(tip));
                }
                else
                {
                    flash.height = int.Parse(fh);
                }
                content = JsonConvert.SerializeObject(flash);
                break;

            case 4:    //幻灯片
                string sw = Request.Form["slide_Width"];
                string sh = Request.Form["slide_Height"];
                if (string.IsNullOrEmpty(sw) || !Utils.IsInt(sw))
                {
                    tip.Message = "幻灯片宽度不能为空或者不是整数!";
                    return(Json(tip));
                }
                if (string.IsNullOrEmpty(sh) || !Utils.IsInt(sh))
                {
                    tip.Message = "幻灯片高度不能为空或者不是整数!";
                    return(Json(tip));;
                }
                string[]      arrsimg  = Request.Form["slide_Img"];  // SImg.Split(new string[] { "," }, StringSplitOptions.None);
                string[]      arrslink = Request.Form["slide_Link"]; //SLink.Split(new string[] { "," }, StringSplitOptions.None);
                string[]      arralt   = Request.Form["slide_Alt"];  //SAlt.Split(new string[] { "," }, StringSplitOptions.None);
                List <ImgAds> listimg  = new List <ImgAds>();
                for (int i = 0; i < arrsimg.Length; i++)
                {
                    if (!string.IsNullOrEmpty(arrsimg[i]) && !string.IsNullOrEmpty(arrslink[i]))    //图片跟地址都不为空
                    {
                        ImgAds tplimgads = new ImgAds();
                        tplimgads.img    = arrsimg[i];
                        tplimgads.link   = arrslink[i];
                        tplimgads.height = int.Parse(sh);
                        tplimgads.width  = int.Parse(sw);
                        tplimgads.alt    = arralt[i];
                        listimg.Add(tplimgads);
                    }
                }
                if (listimg == null || listimg.Count <= 0)
                {
                    tip.Message = "幻灯片请至少添加一张图片!";
                    return(Json(tip));;
                }
                content = JsonConvert.SerializeObject(listimg);
                break;

            case 7:    //视频广告
                VideoAds video = new VideoAds();
                video.poster = Request.Form["video_Poster"];
                video.src    = Request.Form["video_Src"];
                video.title  = Request.Form["video_Title"];
                string vw = Request.Form["video_Width"];
                string vh = Request.Form["video_Height"];
                if (!string.IsNullOrEmpty(vw) && Utils.IsInt(vw))
                {
                    video.width = int.Parse(vw);
                }
                if (!string.IsNullOrEmpty(vh) && Utils.IsInt(vh))
                {
                    video.height = int.Parse(vh);
                }
                if (string.IsNullOrEmpty(video.src))
                {
                    tip.Message = "视频广告地址不能为空!";
                    return(Json(tip));
                }
                content = JsonConvert.SerializeObject(video);
                break;
            }
            #endregion

            model.StartTime = StartTime;
            model.EndTime   = EndTime;
            model.Content   = content;
            model.Update();

            tip.Status    = JsonTip.SUCCESS;
            tip.Message   = "编辑广告成功";
            tip.ReturnUrl = "close";

            Core.Admin.WriteLogActions("编辑广告(" + model.Id + ");");
            return(Json(tip));
        }
Ejemplo n.º 2
0
        /*
         * 获取广告 GetAds(广告ID)、GetAdsByKinds(广告类别ID)
         * 先读取广告实体,再根据广告类型进行相关的处理
         *
         *
         *
         */

        /// <summary>
        /// 获取单条广告代码
        /// </summary>
        /// <param name="aid">广告ID</param>
        /// <returns></returns>
        public static string GetAds(int aid)
        {
            StringBuilder str   = new StringBuilder();
            Ads           model = new Ads();

            model = Find(_.Id == aid);
            if (model != null && DateTime.Compare(DateTime.Now, model.StartTime) > 0 && DateTime.Compare(model.EndTime, DateTime.Now) > 0 && !model.IsDisabled)
            {
                switch (model.TId)
                {
                case 0:    //0为代码广告
                    ScriptAds script = new ScriptAds();
                    script = JsonConvert.DeserializeObject <ScriptAds>(model.Content);
                    str.Append(script.content);
                    break;

                case 1:    //1为文字广告
                    TextAds txt = new TextAds();
                    txt = JsonConvert.DeserializeObject <TextAds>(model.Content);
                    string txttpl = "<a href=\"{0}\" target=\"_blank\"><font style=\"{1}\">{2}</font></a>";
                    str.Append(string.Format(txttpl, txt.link, txt.style, txt.txt));
                    break;

                case 2:    //2为图片广告
                    ImgAds img = new ImgAds();
                    img = JsonConvert.DeserializeObject <ImgAds>(model.Content);
                    if (!string.IsNullOrEmpty(img.link) && img.link != "#")
                    {
                        str.Append("<a href =\"" + img.link + "\" title=\"" + img.alt + "\" target=\"_blank\">");
                    }
                    str.Append("<img style=\"");
                    if (img.width > 0)
                    {
                        str.Append("width:" + img.width.ToString() + "px;");
                    }
                    if (img.height > 0)
                    {
                        str.Append("height:" + img.height.ToString() + "px;");
                    }
                    str.Append("\" src=\"" + img.img + "\" alt=\"" + img.alt + "\" />");
                    if (!string.IsNullOrEmpty(img.link) && img.link != "#")
                    {
                        str.Append("</a>");
                    }
                    break;

                case 3:    //3为flash广告
                    FlashAds flash = new FlashAds();
                    flash = JsonConvert.DeserializeObject <FlashAds>(model.Content);
                    str.Append("<object classid=\"clsid:D27CDB6E-AE6D-11CF-96B8-444553540000\" id=\"swfobj" + model.Id.ToString() + "\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,40,0\" border=\"0\" ");
                    if (flash.width > 0)
                    {
                        str.Append("width=\"" + flash.width.ToString() + "\" ");
                    }
                    if (flash.height > 0)
                    {
                        str.Append("height=\"" + flash.height.ToString() + "\" ");
                    }
                    str.Append(">");
                    str.Append("<param name=\"movie\" value=\"" + flash.swf + "\">");
                    str.Append("<param name=\"quality\" value=\"high\">");
                    str.Append("<param name=\"wmode\" value=\"transparent\">");
                    str.Append("<param name=\"menu\" value=\"false\">");
                    str.Append("<embed src=\"" + flash.swf + "\"  pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" name=\"swfobj" + model.Id.ToString() + "\"  quality=\"High\" wmode=\"transparent\" ");
                    if (flash.width > 0)
                    {
                        str.Append("width=\"" + flash.width.ToString() + "\" ");
                    }
                    if (flash.height > 0)
                    {
                        str.Append("height=\"" + flash.height.ToString() + "\" ");
                    }
                    str.Append("></embed>");
                    str.Append("</object>");
                    break;

                case 4:    //4为幻灯片广告
                    List <ImgAds> listimg = new List <ImgAds>();
                    listimg = JsonConvert.DeserializeObject <List <ImgAds> >(model.Content);
                    if (listimg != null && listimg.Count > 0)
                    {
                        //图片数量不止1才显示:
                        str.Append("<script type=\"text/javascript\" src=\"/static/js/swfobject.js\"></script>");                                                                                                                                                                                                                                                   //swfobject js
                        str.Append("<div id=\"SlideAds_" + model.Id.ToString() + "\"></div>");                                                                                                                                                                                                                                                                      //div
                        str.Append("<script type=\"text/javascript\">swfobject.embedSWF(\"/static/images/common/bcastr4.swf?xml=/Tools/slidexml/" + model.Id.ToString() + "\", \"SlideAds_" + model.Id.ToString() + "\", \"" + listimg[0].width.ToString() + "\", \"" + listimg[0].height.ToString() + "\", \"9.0.0\",null,null,{wmode:'transparent'});</script>"); //js调用内容
                    }
                    break;

                default:
                    break;
                }
            }
            return(str.ToString());
        }