MsgPlacardController.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.Extensions.Logging;
  7. using Microsoft.Extensions.Options;
  8. using Microsoft.AspNetCore.Authorization;
  9. using System.Web;
  10. using MySystem.MainModels;
  11. using LitJson;
  12. using Library;
  13. namespace MySystem.Areas.Api.Controllers.v1
  14. {
  15. [Area("Api")]
  16. [Route("Api/v1/[controller]/[action]")]
  17. public class MsgPlacardController : BaseController
  18. {
  19. public MsgPlacardController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  20. {
  21. }
  22. #region 商户-未读消息数
  23. [Authorize]
  24. public JsonResult NotRead(string value)
  25. {
  26. value = DesDecrypt(value);
  27. JsonData data = JsonMapper.ToObject(value);
  28. Dictionary<string, object> Obj = NotReadDo(value);
  29. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  30. }
  31. public Dictionary<string, object> NotReadDo(string value)
  32. {
  33. JsonData data = JsonMapper.ToObject(value);
  34. Dictionary<string, object> Obj = new Dictionary<string, object>();
  35. string UserIdString = data["UserId"].ToString();
  36. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString()));
  37. int PlacardCount = maindb.MsgPlacard.Where(m => m.Status > -1).Count() - maindb.MsgPlacardRead.Where(m => m.UserId == UserIdString).Count();
  38. int PersonalCount = maindb.MsgPersonal.Where(m => m.UserId == UserId && m.ReadFlag == 0).Count();
  39. if (PlacardCount < 0) PlacardCount = 0;
  40. Obj.Add("PlacardCount", PlacardCount); //系统消息
  41. Obj.Add("PersonalCount", PersonalCount); //个人消息
  42. return Obj;
  43. }
  44. #endregion
  45. #region 商户-系统消息列表
  46. [Authorize]
  47. public JsonResult List(string value)
  48. {
  49. value = DesDecrypt(value);
  50. JsonData data = JsonMapper.ToObject(value);
  51. List<Dictionary<string, object>> dataList = ListDo(value);
  52. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  53. }
  54. public List<Dictionary<string, object>> ListDo(string value)
  55. {
  56. JsonData data = JsonMapper.ToObject(value);
  57. string UserId = data["UserId"].ToString(); //创客Id
  58. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  59. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  60. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  61. IQueryable<MsgPlacard> query = maindb.MsgPlacard.Where(m => m.Status > -1).OrderByDescending(m => m.Id);
  62. if (PageNum == 1)
  63. {
  64. query = query.Take(PageSize);
  65. }
  66. else
  67. {
  68. int skipNum = PageSize * (PageNum - 1);
  69. query = query.Skip(skipNum).Take(PageSize);
  70. }
  71. foreach (MsgPlacard subdata in query.ToList())
  72. {
  73. Dictionary<string, object> curData = new Dictionary<string, object>();
  74. curData.Add("Title", subdata.Title); //标题
  75. curData.Add("Summary", subdata.Summary); //简介
  76. curData.Add("BgPic", !string.IsNullOrEmpty(subdata.BgPic) ? DefaultPic(subdata.BgPic) : ""); //背景图片
  77. curData.Add("Url", subdata.Url); //跳转Url
  78. curData.Add("Id", subdata.Id); //Id
  79. curData.Add("MsgType", subdata.MsgType);
  80. if (subdata.Title.Contains("补录成功通知"))
  81. {
  82. curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate
  83. }
  84. else
  85. {
  86. curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd")); //CreateDate
  87. }
  88. curData.Add("ReadFlag", maindb.MsgPlacardRead.Any(m => m.PlacardId == subdata.Id && m.UserId == UserId) ? 1 : 0); //已读标记
  89. dataList.Add(curData);
  90. }
  91. return dataList;
  92. }
  93. #endregion
  94. #region 商户-系统消息已读
  95. [Authorize]
  96. public JsonResult Read(string value)
  97. {
  98. value = DesDecrypt(value);
  99. JsonData data = JsonMapper.ToObject(value);
  100. AppResultJson result = ReadDo(value);
  101. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  102. }
  103. public AppResultJson ReadDo(string value)
  104. {
  105. JsonData data = JsonMapper.ToObject(value);
  106. Dictionary<string, object> Obj = new Dictionary<string, object>();
  107. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  108. string UserId = data["UserId"].ToString();
  109. bool check = maindb.MsgPlacardRead.Any(m => m.PlacardId == Id && m.UserId == UserId);
  110. if(!check)
  111. {
  112. MsgPlacardRead read = maindb.MsgPlacardRead.Add(new MsgPlacardRead()
  113. {
  114. CreateDate = DateTime.Now,
  115. UserId = UserId,
  116. PlacardId = Id,
  117. ReadFlag = 1,
  118. }).Entity;
  119. maindb.SaveChanges();
  120. }
  121. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  122. }
  123. #endregion
  124. #region 商户-系统消息详情
  125. [Authorize]
  126. public JsonResult Detail(string value)
  127. {
  128. value = DesDecrypt(value);
  129. JsonData data = JsonMapper.ToObject(value);
  130. Dictionary<string, object> Obj = DetailDo(value);
  131. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  132. }
  133. public Dictionary<string, object> DetailDo(string value)
  134. {
  135. JsonData data = JsonMapper.ToObject(value);
  136. Dictionary<string, object> Obj = new Dictionary<string, object>();
  137. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  138. MsgPlacard query = maindb.MsgPlacard.FirstOrDefault(m => m.Id == Id) ?? new MsgPlacard();
  139. Obj.Add("Title", query.Title); //标题
  140. Obj.Add("Content", CheckContentImage(query.Content)); //内容
  141. return Obj;
  142. }
  143. #endregion
  144. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  145. /// <summary>
  146. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  147. /// </summary>
  148. /// <param name="value">请求的参数(json字符串)</param>
  149. /// <param name="signField">要签名的字段</param>
  150. /// <returns></returns>
  151. private string CheckSign(string value, string[] signField)
  152. {
  153. JsonData json = JsonMapper.ToObject(value);
  154. Dictionary<string, string> dic = new Dictionary<string, string>();
  155. for (int i = 0; i < signField.Length; i++)
  156. {
  157. dic.Add(signField[i], json[signField[i]].ToString());
  158. }
  159. string sign = json["sign"].ToString(); //客户端签名字符串
  160. return new Sign().sign(dic, sign);
  161. }
  162. #endregion
  163. }
  164. }