AdvertisementController.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Text;
  7. using System.Web.Http;
  8. using Lottomat.Application.Busines.InformationManage;
  9. using Lottomat.Application.Code;
  10. using Lottomat.Application.Entity.CommonEntity;
  11. using Lottomat.Application.Entity.InformationManage;
  12. using Lottomat.Application.Entity.LotteryNumberManage.Parameter;
  13. using Lottomat.Application.Entity.LotteryNumberManage.ViewModel;
  14. using Lottomat.Application.Entity.ViewModel;
  15. using Lottomat.Cache.Factory;
  16. using Lottomat.SOA.API.Controllers.Base;
  17. using Lottomat.Util.Extension;
  18. using Lottomat.Utils.Date;
  19. using System.Web.Http.Cors;
  20. namespace Lottomat.SOA.API.Controllers.V1
  21. {
  22. /// <summary>
  23. /// 广告接口
  24. /// </summary>
  25. public class AdvertisementController : BaseApiController
  26. {
  27. /// <summary>
  28. /// 广告BLL
  29. /// </summary>
  30. private static AdvertisementBLL advertisementBll = new AdvertisementBLL();
  31. private static readonly object _lock = new object();
  32. /// <summary>
  33. /// 保存广告
  34. /// </summary>
  35. /// <returns></returns>
  36. [HttpPost]
  37. public HttpResponseMessage SaveAdvertisement(SaveAdvertisementArgEntity arg)
  38. {
  39. BaseJson<string> resultMsg = new BaseJson<string> { Status = (int)JsonObjectStatus.Error, Message = "服务器未知错误。", Data = null };
  40. Logger(typeof(AdvertisementController), arg.TryToJson(), "保存广告-SaveAdvertisement", () =>
  41. {
  42. if (!string.IsNullOrEmpty(arg.t) && !string.IsNullOrEmpty(arg.Appkey) && !string.IsNullOrEmpty(arg.AccessToken))
  43. {
  44. if (arg.t.CheckTimeStamp())
  45. {
  46. //获取缓存Token信息
  47. Token_Preview token = CacheFactory.Cache().GetCache<Token_Preview>(arg.Appkey);
  48. if (token != null)
  49. {
  50. //校验授权码
  51. string tokenStr = token.AccessToken;
  52. if (!string.IsNullOrEmpty(tokenStr) && tokenStr.Equals(arg.AccessToken))
  53. {
  54. AdvertisementEntity entity = new AdvertisementEntity
  55. {
  56. Title = arg.Title,
  57. Category = arg.Which == "0" ? "主站" : arg.Which == "1" ? "开奖网" : arg.Which == "2" ? "手机站":arg.Which=="3"?"彩吧资讯":arg.Which=="4"?"彩吧图库":"",
  58. CategoryId = arg.Which,
  59. Position = arg.Position,
  60. Href = arg.Href,
  61. TermOfValidity = arg.OverTime
  62. };
  63. if (!string.IsNullOrEmpty(arg.Id))
  64. {
  65. entity.IsEnable = true;
  66. advertisementBll.SaveForm(arg.Id, entity);
  67. }
  68. else
  69. {
  70. AdvertisementEntity temp = advertisementBll.GetEntity(a => a.CategoryId.Equals(arg.Which) && a.Position == arg.Position);
  71. if (temp != null)
  72. {
  73. entity.IsEnable = true;
  74. advertisementBll.SaveForm(temp.ID, entity);
  75. }
  76. }
  77. //清理缓存
  78. Cache.Factory.CacheFactory.Cache().RemoveCache("Advertisement_Html_" + arg.Which);
  79. //清除Token
  80. //Cache.Factory.CacheFactory.Cache().RemoveCache(arg.Appkey);
  81. resultMsg = new BaseJson<string>
  82. {
  83. Status = (int)JsonObjectStatus.Success,
  84. Data = null,
  85. Message = JsonObjectStatus.Success.GetEnumText(),
  86. BackUrl = null
  87. };
  88. }
  89. else
  90. {
  91. resultMsg = new BaseJson<string>
  92. {
  93. Status = (int)JsonObjectStatus.TokenInvalid,
  94. Data = null,
  95. Message = JsonObjectStatus.TokenInvalid.GetEnumText(),
  96. BackUrl = null
  97. };
  98. }
  99. }
  100. else
  101. {
  102. resultMsg = new BaseJson<string>
  103. {
  104. Status = (int)JsonObjectStatus.Unauthorized,
  105. Data = null,
  106. Message = JsonObjectStatus.Unauthorized.GetEnumText(),
  107. BackUrl = null
  108. };
  109. }
  110. }
  111. else
  112. {
  113. resultMsg = new BaseJson<string>
  114. {
  115. Status = (int)JsonObjectStatus.Fail,
  116. Data = null,
  117. Message = JsonObjectStatus.Fail.GetEnumText() + ",无效参数。",
  118. BackUrl = null
  119. };
  120. }
  121. }
  122. else
  123. {
  124. resultMsg = new BaseJson<string>
  125. {
  126. Status = (int)JsonObjectStatus.Fail,
  127. Data = null,
  128. Message = JsonObjectStatus.Fail.GetEnumText() + ",请求参数为空。",
  129. BackUrl = null
  130. };
  131. }
  132. }, e =>
  133. {
  134. resultMsg = new BaseJson<string>
  135. {
  136. Status = (int)JsonObjectStatus.Exception,
  137. Data = null,
  138. Message = JsonObjectStatus.Exception.GetEnumText() + ",异常信息:" + e.Message,
  139. BackUrl = null
  140. };
  141. });
  142. return resultMsg.TryToJson().ToHttpResponseMessage();
  143. }
  144. /// <summary>
  145. /// 获取广告列表
  146. /// </summary>
  147. /// <returns></returns>
  148. [HttpPost]
  149. public HttpResponseMessage GetAdvertisementList(GetAdvertisementArgEntity arg)
  150. {
  151. BaseJson<List<AdvertisementViewEntity>> resultMsg = new BaseJson<List<AdvertisementViewEntity>> { Status = (int)JsonObjectStatus.Error, Message = "服务器未知错误。", Data = null };
  152. Logger(typeof(AdvertisementController), arg.TryToJson(), "获取广告列表-GetAdvertisementList", () =>
  153. {
  154. if (!string.IsNullOrEmpty(arg.t))
  155. {
  156. if (arg.t.CheckTimeStamp())
  157. {
  158. List<AdvertisementEntity> list = advertisementBll.GetList(a => a.CategoryId.Equals(arg.Which) && a.Title != "" && a.TermOfValidity > DateTimeHelper.Now && a.IsEnable == true && a.IsDelete == false).ToList();
  159. List<AdvertisementViewEntity> res = list.Select(n => new AdvertisementViewEntity
  160. {
  161. Id = n.ID,
  162. Title = n.Title ?? "",
  163. Href = n.Href != null ? n.Href.ToLower().Contains("http://") || n.Href.ToLower().Contains("https://") ? n.Href : "http://" + n.Href : "",
  164. Position = n.Position,
  165. Which = n.CategoryId,
  166. OverTime = n.TermOfValidity.TryToDateTimeToString("yyyy-MM-dd"),
  167. IsEnable = n.IsEnable ?? false,
  168. IsExpire = n.TermOfValidity != null && n.TermOfValidity < DateTimeHelper.Now
  169. }).ToList();
  170. res = res.OrderBy(r => r.Position).ToList();
  171. resultMsg = new BaseJson<List<AdvertisementViewEntity>>
  172. {
  173. Status = (int)JsonObjectStatus.Success,
  174. Data = res,
  175. Message = JsonObjectStatus.Success.GetEnumText(),
  176. BackUrl = null
  177. };
  178. }
  179. else
  180. {
  181. resultMsg = new BaseJson<List<AdvertisementViewEntity>>
  182. {
  183. Status = (int)JsonObjectStatus.Fail,
  184. Data = null,
  185. Message = JsonObjectStatus.Fail.GetEnumText() + ",无效参数。",
  186. BackUrl = null
  187. };
  188. }
  189. }
  190. else
  191. {
  192. resultMsg = new BaseJson<List<AdvertisementViewEntity>>
  193. {
  194. Status = (int)JsonObjectStatus.Fail,
  195. Data = null,
  196. Message = JsonObjectStatus.Fail.GetEnumText() + ",请求参数为空。",
  197. BackUrl = null
  198. };
  199. }
  200. }, e =>
  201. {
  202. resultMsg = new BaseJson<List<AdvertisementViewEntity>>
  203. {
  204. Status = (int)JsonObjectStatus.Exception,
  205. Data = null,
  206. Message = JsonObjectStatus.Exception.GetEnumText() + ",异常信息:" + e.Message,
  207. BackUrl = null
  208. };
  209. });
  210. return resultMsg.TryToJson().ToHttpResponseMessage();
  211. }
  212. /// <summary>
  213. /// 获取广告Html
  214. /// </summary>
  215. /// <returns></returns>
  216. [HttpPost]
  217. public HttpResponseMessage GetAdvertisementHtml(GetAdvertisementArgEntity arg)
  218. {
  219. BaseJson<string> resultMsg = new BaseJson<string> { Status = (int)JsonObjectStatus.Error, Message = "服务器未知错误。", Data = null };
  220. Logger(typeof(AdvertisementController), arg.TryToJson(), "获取广告Html-GetAdvertisementHtml", () =>
  221. {
  222. lock (_lock)
  223. {
  224. if (arg != null)
  225. {
  226. if (!string.IsNullOrEmpty(arg.t))
  227. {
  228. if (arg.t.CheckTimeStamp())
  229. {
  230. string htmlbase64 = Cache.Factory.CacheFactory.Cache().GetCache<string>("Advertisement_Html_" + arg.Which);
  231. if (string.IsNullOrEmpty(htmlbase64))
  232. {
  233. List<AdvertisementEntity> list = advertisementBll.GetList(a => a.CategoryId.Equals(arg.Which) && a.IsEnable == true && a.IsDelete == false).ToList();
  234. //List<AdvertisementViewEntity> res = list.Select(n => new AdvertisementViewEntity
  235. //{
  236. // Id = n.ID,
  237. // Title = n.Title,
  238. // Href = n.Href.ToLower().Contains("http://") || n.Href.ToLower().Contains("https://") ? n.Href : "http://" + n.Href,
  239. // Position = n.Position,
  240. // Which = n.CategoryId,
  241. // OverTime = n.TermOfValidity.TryToDateTimeToString("yyyy-MM-dd"),
  242. // IsEnable = n.IsEnable ?? false
  243. //}).ToList();
  244. //按位置排序
  245. list = list.OrderBy(r => r.Position).ToList();
  246. string html = GetHtml(list, arg.Which);
  247. htmlbase64 = Convert.ToBase64String(System.Text.Encoding.GetEncoding("utf-8").GetBytes(html.ToString()));
  248. Cache.Factory.CacheFactory.Cache().WriteCache(htmlbase64, "Advertisement_Html_" + arg.Which, DateTimeHelper.Now.AddDays(10));
  249. }
  250. resultMsg = new BaseJson<string>
  251. {
  252. Status = (int)JsonObjectStatus.Success,
  253. Data = htmlbase64,
  254. Message = JsonObjectStatus.Success.GetEnumText(),
  255. BackUrl = null
  256. };
  257. }
  258. else
  259. {
  260. resultMsg = new BaseJson<string>
  261. {
  262. Status = (int)JsonObjectStatus.Fail,
  263. Data = null,
  264. Message = JsonObjectStatus.Fail.GetEnumText() + ",无效参数。",
  265. BackUrl = null
  266. };
  267. }
  268. }
  269. else
  270. {
  271. resultMsg = new BaseJson<string>
  272. {
  273. Status = (int)JsonObjectStatus.Fail,
  274. Data = null,
  275. Message = JsonObjectStatus.Fail.GetEnumText() + ",请求参数为空。",
  276. BackUrl = null
  277. };
  278. }
  279. }
  280. }
  281. }, e =>
  282. {
  283. resultMsg = new BaseJson<string>
  284. {
  285. Status = (int)JsonObjectStatus.Exception,
  286. Data = null,
  287. Message = JsonObjectStatus.Exception.GetEnumText() + ",异常信息:" + e.Message,
  288. BackUrl = null
  289. };
  290. });
  291. return resultMsg.TryToJson().ToHttpResponseMessage();
  292. }
  293. private string GetHtml(List<AdvertisementEntity> res, string which)
  294. {
  295. StringBuilder builderRes = new StringBuilder();
  296. StringBuilder builder = new StringBuilder();
  297. for (int i = 1; i <= res.Count; i++)
  298. {
  299. if (res[i - 1].IsEnable ?? false)
  300. {
  301. //检查是否过期
  302. bool isExpire = res[i - 1].TermOfValidity != null && res[i - 1].TermOfValidity < DateTimeHelper.Now;
  303. //检测广告位是否已经添加
  304. if (isExpire)
  305. {
  306. builder.Append(NoSoldTemplate);
  307. }
  308. else
  309. {
  310. if (string.IsNullOrEmpty(res[i - 1].Title) && string.IsNullOrEmpty(res[i - 1].Href))
  311. {
  312. builder.Append(NoSoldTemplate);
  313. }
  314. else
  315. {
  316. if (which.TryToInt32() == 1)
  317. {
  318. 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));
  319. }
  320. else if (which.TryToInt32() == 2)
  321. {
  322. 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));
  323. }
  324. else {
  325. 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));
  326. }
  327. }
  328. }
  329. //builder.Append(isExpire
  330. // ? NoSoldTemplate
  331. // : string.Format(BeSoldTemplate, res[i - 1].Href, string.Format("{0}({1})", res[i - 1].Title, res[i - 1].Href), res[i - 1].Title));
  332. }
  333. else
  334. {
  335. builder.Append(NoSoldTemplate);
  336. }
  337. int j = which.TryToInt32() == 0 || which.TryToInt32() == 1 || which.TryToInt32() == 3 || which.TryToInt32() == 4 ? 4 : which.TryToInt32() == 2 ? 2 : 4;
  338. if (i % j == 0)
  339. {
  340. builderRes.Append("<tr>");
  341. builderRes.Append(builder.ToString());
  342. builderRes.Append("</tr>");
  343. //清空
  344. builder.Clear();
  345. }
  346. }
  347. 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):"";
  348. }
  349. //PC
  350. 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>";
  351. //PC
  352. 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>";
  353. //移动
  354. 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>";
  355. //开奖号
  356. 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>";
  357. /// <summary>
  358. /// 已经销售了的广告样式模板
  359. /// </summary>
  360. 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>";
  361. /// <summary>
  362. /// 已经销售了的广告样式模板
  363. /// </summary>
  364. private string BeSoldTemplate_kjh = "<div><a target='_blank' href='{0}' title='{1}' rel='nofollow'>{2}</a></div>";
  365. /// <summary>
  366. /// 已经销售了的广告样式模板
  367. /// </summary>
  368. 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>";
  369. /// <summary>
  370. /// 招商
  371. /// </summary>
  372. 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;'>&#xe64d;招商中...</i></td>";
  373. }
  374. }