123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Text;
- using System.Web.Http;
- using Lottomat.Application.Busines.InformationManage;
- using Lottomat.Application.Code;
- using Lottomat.Application.Entity.CommonEntity;
- using Lottomat.Application.Entity.InformationManage;
- using Lottomat.Application.Entity.LotteryNumberManage.Parameter;
- using Lottomat.Application.Entity.LotteryNumberManage.ViewModel;
- using Lottomat.Application.Entity.ViewModel;
- using Lottomat.Cache.Factory;
- using Lottomat.SOA.API.Controllers.Base;
- using Lottomat.Util.Extension;
- using Lottomat.Utils.Date;
- using System.Web.Http.Cors;
- namespace Lottomat.SOA.API.Controllers.V1
- {
- /// <summary>
- /// 广告接口
- /// </summary>
- public class AdvertisementController : BaseApiController
- {
- /// <summary>
- /// 广告BLL
- /// </summary>
- private static AdvertisementBLL advertisementBll = new AdvertisementBLL();
- private static readonly object _lock = new object();
- /// <summary>
- /// 保存广告
- /// </summary>
- /// <returns></returns>
- [HttpPost]
- public HttpResponseMessage SaveAdvertisement(SaveAdvertisementArgEntity arg)
- {
- BaseJson<string> resultMsg = new BaseJson<string> { Status = (int)JsonObjectStatus.Error, Message = "服务器未知错误。", Data = null };
- Logger(typeof(AdvertisementController), arg.TryToJson(), "保存广告-SaveAdvertisement", () =>
- {
- if (!string.IsNullOrEmpty(arg.t) && !string.IsNullOrEmpty(arg.Appkey) && !string.IsNullOrEmpty(arg.AccessToken))
- {
- if (arg.t.CheckTimeStamp())
- {
- //获取缓存Token信息
- Token_Preview token = CacheFactory.Cache().GetCache<Token_Preview>(arg.Appkey);
- if (token != null)
- {
- //校验授权码
- string tokenStr = token.AccessToken;
- if (!string.IsNullOrEmpty(tokenStr) && tokenStr.Equals(arg.AccessToken))
- {
- AdvertisementEntity entity = new AdvertisementEntity
- {
- Title = arg.Title,
- Category = arg.Which == "0" ? "主站" : arg.Which == "1" ? "开奖网" : arg.Which == "2" ? "手机站":arg.Which=="3"?"彩吧资讯":arg.Which=="4"?"彩吧图库":"",
- CategoryId = arg.Which,
- Position = arg.Position,
- Href = arg.Href,
- TermOfValidity = arg.OverTime
- };
- if (!string.IsNullOrEmpty(arg.Id))
- {
- entity.IsEnable = true;
- advertisementBll.SaveForm(arg.Id, entity);
- }
- else
- {
- AdvertisementEntity temp = advertisementBll.GetEntity(a => a.CategoryId.Equals(arg.Which) && a.Position == arg.Position);
- if (temp != null)
- {
- entity.IsEnable = true;
- advertisementBll.SaveForm(temp.ID, entity);
- }
- }
- //清理缓存
- Cache.Factory.CacheFactory.Cache().RemoveCache("Advertisement_Html_" + arg.Which);
- //清除Token
- //Cache.Factory.CacheFactory.Cache().RemoveCache(arg.Appkey);
- resultMsg = new BaseJson<string>
- {
- Status = (int)JsonObjectStatus.Success,
- Data = null,
- Message = JsonObjectStatus.Success.GetEnumText(),
- BackUrl = null
- };
- }
- else
- {
- resultMsg = new BaseJson<string>
- {
- Status = (int)JsonObjectStatus.TokenInvalid,
- Data = null,
- Message = JsonObjectStatus.TokenInvalid.GetEnumText(),
- BackUrl = null
- };
- }
- }
- else
- {
- resultMsg = new BaseJson<string>
- {
- Status = (int)JsonObjectStatus.Unauthorized,
- Data = null,
- Message = JsonObjectStatus.Unauthorized.GetEnumText(),
- BackUrl = null
- };
- }
- }
- else
- {
- resultMsg = new BaseJson<string>
- {
- Status = (int)JsonObjectStatus.Fail,
- Data = null,
- Message = JsonObjectStatus.Fail.GetEnumText() + ",无效参数。",
- BackUrl = null
- };
- }
- }
- else
- {
- resultMsg = new BaseJson<string>
- {
- Status = (int)JsonObjectStatus.Fail,
- Data = null,
- Message = JsonObjectStatus.Fail.GetEnumText() + ",请求参数为空。",
- BackUrl = null
- };
- }
- }, e =>
- {
- resultMsg = new BaseJson<string>
- {
- Status = (int)JsonObjectStatus.Exception,
- Data = null,
- Message = JsonObjectStatus.Exception.GetEnumText() + ",异常信息:" + e.Message,
- BackUrl = null
- };
- });
- return resultMsg.TryToJson().ToHttpResponseMessage();
- }
- /// <summary>
- /// 获取广告列表
- /// </summary>
- /// <returns></returns>
- [HttpPost]
- public HttpResponseMessage GetAdvertisementList(GetAdvertisementArgEntity arg)
- {
- BaseJson<List<AdvertisementViewEntity>> resultMsg = new BaseJson<List<AdvertisementViewEntity>> { Status = (int)JsonObjectStatus.Error, Message = "服务器未知错误。", Data = null };
- Logger(typeof(AdvertisementController), arg.TryToJson(), "获取广告列表-GetAdvertisementList", () =>
- {
- if (!string.IsNullOrEmpty(arg.t))
- {
- if (arg.t.CheckTimeStamp())
- {
- List<AdvertisementEntity> list = advertisementBll.GetList(a => a.CategoryId.Equals(arg.Which) && a.Title != "" && a.TermOfValidity > DateTimeHelper.Now && a.IsEnable == true && a.IsDelete == false).ToList();
- List<AdvertisementViewEntity> res = list.Select(n => new AdvertisementViewEntity
- {
- Id = n.ID,
- Title = n.Title ?? "",
- Href = n.Href != null ? n.Href.ToLower().Contains("http://") || n.Href.ToLower().Contains("https://") ? n.Href : "http://" + n.Href : "",
- Position = n.Position,
- Which = n.CategoryId,
- OverTime = n.TermOfValidity.TryToDateTimeToString("yyyy-MM-dd"),
- IsEnable = n.IsEnable ?? false,
- IsExpire = n.TermOfValidity != null && n.TermOfValidity < DateTimeHelper.Now
- }).ToList();
- res = res.OrderBy(r => r.Position).ToList();
- resultMsg = new BaseJson<List<AdvertisementViewEntity>>
- {
- Status = (int)JsonObjectStatus.Success,
- Data = res,
- Message = JsonObjectStatus.Success.GetEnumText(),
- BackUrl = null
- };
- }
- else
- {
- resultMsg = new BaseJson<List<AdvertisementViewEntity>>
- {
- Status = (int)JsonObjectStatus.Fail,
- Data = null,
- Message = JsonObjectStatus.Fail.GetEnumText() + ",无效参数。",
- BackUrl = null
- };
- }
- }
- else
- {
- resultMsg = new BaseJson<List<AdvertisementViewEntity>>
- {
- Status = (int)JsonObjectStatus.Fail,
- Data = null,
- Message = JsonObjectStatus.Fail.GetEnumText() + ",请求参数为空。",
- BackUrl = null
- };
- }
- }, e =>
- {
- resultMsg = new BaseJson<List<AdvertisementViewEntity>>
- {
- Status = (int)JsonObjectStatus.Exception,
- Data = null,
- Message = JsonObjectStatus.Exception.GetEnumText() + ",异常信息:" + e.Message,
- BackUrl = null
- };
- });
- return resultMsg.TryToJson().ToHttpResponseMessage();
- }
- /// <summary>
- /// 获取广告Html
- /// </summary>
- /// <returns></returns>
- [HttpPost]
- public HttpResponseMessage GetAdvertisementHtml(GetAdvertisementArgEntity arg)
- {
- BaseJson<string> resultMsg = new BaseJson<string> { Status = (int)JsonObjectStatus.Error, Message = "服务器未知错误。", Data = null };
- Logger(typeof(AdvertisementController), arg.TryToJson(), "获取广告Html-GetAdvertisementHtml", () =>
- {
- lock (_lock)
- {
- if (arg != null)
- {
- if (!string.IsNullOrEmpty(arg.t))
- {
- if (arg.t.CheckTimeStamp())
- {
- string htmlbase64 = Cache.Factory.CacheFactory.Cache().GetCache<string>("Advertisement_Html_" + arg.Which);
- if (string.IsNullOrEmpty(htmlbase64))
- {
- List<AdvertisementEntity> list = advertisementBll.GetList(a => a.CategoryId.Equals(arg.Which) && a.IsEnable == true && a.IsDelete == false).ToList();
- //List<AdvertisementViewEntity> res = list.Select(n => new AdvertisementViewEntity
- //{
- // Id = n.ID,
- // Title = n.Title,
- // Href = n.Href.ToLower().Contains("http://") || n.Href.ToLower().Contains("https://") ? n.Href : "http://" + n.Href,
- // Position = n.Position,
- // Which = n.CategoryId,
- // OverTime = n.TermOfValidity.TryToDateTimeToString("yyyy-MM-dd"),
- // IsEnable = n.IsEnable ?? false
- //}).ToList();
- //按位置排序
- list = list.OrderBy(r => r.Position).ToList();
- string html = GetHtml(list, arg.Which);
- htmlbase64 = Convert.ToBase64String(System.Text.Encoding.GetEncoding("utf-8").GetBytes(html.ToString()));
- Cache.Factory.CacheFactory.Cache().WriteCache(htmlbase64, "Advertisement_Html_" + arg.Which, DateTimeHelper.Now.AddDays(10));
- }
- resultMsg = new BaseJson<string>
- {
- Status = (int)JsonObjectStatus.Success,
- Data = htmlbase64,
- Message = JsonObjectStatus.Success.GetEnumText(),
- BackUrl = null
- };
- }
- else
- {
- resultMsg = new BaseJson<string>
- {
- Status = (int)JsonObjectStatus.Fail,
- Data = null,
- Message = JsonObjectStatus.Fail.GetEnumText() + ",无效参数。",
- BackUrl = null
- };
- }
- }
- else
- {
- resultMsg = new BaseJson<string>
- {
- Status = (int)JsonObjectStatus.Fail,
- Data = null,
- Message = JsonObjectStatus.Fail.GetEnumText() + ",请求参数为空。",
- BackUrl = null
- };
- }
- }
- }
- }, e =>
- {
- resultMsg = new BaseJson<string>
- {
- Status = (int)JsonObjectStatus.Exception,
- Data = null,
- Message = JsonObjectStatus.Exception.GetEnumText() + ",异常信息:" + e.Message,
- BackUrl = null
- };
- });
- return resultMsg.TryToJson().ToHttpResponseMessage();
- }
- private string GetHtml(List<AdvertisementEntity> res, string which)
- {
- StringBuilder builderRes = new StringBuilder();
- StringBuilder builder = new StringBuilder();
- for (int i = 1; i <= res.Count; i++)
- {
- if (res[i - 1].IsEnable ?? false)
- {
- //检查是否过期
- bool isExpire = res[i - 1].TermOfValidity != null && res[i - 1].TermOfValidity < DateTimeHelper.Now;
- //检测广告位是否已经添加
- if (isExpire)
- {
- builder.Append(NoSoldTemplate);
- }
- else
- {
- if (string.IsNullOrEmpty(res[i - 1].Title) && string.IsNullOrEmpty(res[i - 1].Href))
- {
- builder.Append(NoSoldTemplate);
- }
- else
- {
- if (which.TryToInt32() == 1)
- {
- builder.Append(string.Format(BeSoldTemplate_kjh, res[i - 1].Href, string.Format("{0}({1})", res[i - 1].Title, res[i - 1].Href), res[i - 1].Title));
- }
- else if (which.TryToInt32() == 2)
- {
- builder.Append(string.Format(BeSoldTemplate, res[i - 1].Href, string.Format("{0}({1})", res[i - 1].Title, res[i - 1].Href), res[i - 1].Title));
- }
- else {
- builder.Append(string.Format(BeSoldTemplate_4, res[i - 1].Href, string.Format("{0}({1})", res[i - 1].Title, res[i - 1].Href), res[i - 1].Title));
- }
- }
- }
- //builder.Append(isExpire
- // ? NoSoldTemplate
- // : string.Format(BeSoldTemplate, res[i - 1].Href, string.Format("{0}({1})", res[i - 1].Title, res[i - 1].Href), res[i - 1].Title));
- }
- else
- {
- builder.Append(NoSoldTemplate);
- }
- int j = which.TryToInt32() == 0 || which.TryToInt32() == 1 || which.TryToInt32() == 3 || which.TryToInt32() == 4 ? 4 : which.TryToInt32() == 2 ? 2 : 4;
- if (i % j == 0)
- {
- builderRes.Append("<tr>");
- builderRes.Append(builder.ToString());
- builderRes.Append("</tr>");
- //清空
- builder.Clear();
- }
- }
- return which.TryToInt32() == 0 ? string.Format(TableTemplate_4, builderRes) : which.TryToInt32() == 2 ? string.Format(TableTemplate_3, builderRes) : which.TryToInt32() == 1? string.Format(TableTemplate_kjh, builderRes):which.TryToInt32()==3? string.Format(TableTemplate_4, builderRes):which.TryToInt32()==4?string.Format(TableTemplate_4, builderRes):"";
- }
- //PC
- private string TableTemplate_5 = @"<table style='width: 100%;background-color: #faf8f2;'><tr><td rowspan = '5' style='line-height: 18px;text-align: center; width: 60px;border:1px solid #ddd;'>站<br> 外<br>推<br> 广</td></tr>{0}</table>";
- //PC
- private string TableTemplate_4 = @"<table style='width: 100%;background-color: #faf8f2;'><tr><td rowspan = '4' style='line-height: 18px;text-align: center; width: 60px;border:1px solid #ddd;'>站<br> 外<br>推<br> 广</td></tr>{0}</table>";
- //移动
- private string TableTemplate_3 = @"<table style='width: 100%;background-color: #faf8f2;font-size: 9px;'><tr><td rowspan = '4' style='line-height: 15px;text-align: center; width: 40px;border:1px solid #ddd;font-size: 8px;'>站<br> 外<br>推<br> 广</td></tr>{0}</table>";
- //开奖号
- private string TableTemplate_kjh = @"<section class='adsd-wrapper'><div class='ads-desc desc-pc'><p>站<br />外<br />推<br />广</p></div><div class='ads-desc desc-mobile'><p>站外推广</p></div><section class='ads-right'><div class='ads-box'>{0}</div></section></section>";
- /// <summary>
- /// 已经销售了的广告样式模板
- /// </summary>
- private string BeSoldTemplate = "<td style='border:1px solid #ddd;text-align: center;white-space:nowrap;overflow: hidden;text-overflow: ellipsis;line-height:20px;'><a href='{0}' rel='nofollow' target='_blank' title='{1}'>{2}</a></td>";
- /// <summary>
- /// 已经销售了的广告样式模板
- /// </summary>
- private string BeSoldTemplate_kjh = "<div><a target='_blank' href='{0}' title='{1}' rel='nofollow'>{2}</a></div>";
- /// <summary>
- /// 已经销售了的广告样式模板
- /// </summary>
- private string BeSoldTemplate_4 = "<td style='border:1px solid #ddd;text-align: center;white-space:nowrap;overflow: hidden;text-overflow: ellipsis; line-height: 50px;font-size: 22px;'><a href='{0}' rel='nofollow' target='_blank' title='{1}'>{2}</a></td>";
- /// <summary>
- /// 招商
- /// </summary>
- private string NoSoldTemplate = "<td style='border:1px solid #ddd;text-align: center;white-space:nowrap;overflow: hidden;text-overflow: ellipsis;line-height:20px;'><i class='layui-icon' style='font-size: 10px; color: #FF5722;'>招商中...</i></td>";
- }
- }
|