MsgPersonalController.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 MsgPersonalController : BaseController
  18. {
  19. public MsgPersonalController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  20. {
  21. }
  22. #region 商户-个人消息列表
  23. [Authorize]
  24. public JsonResult List(string value)
  25. {
  26. value = DesDecrypt(value);
  27. JsonData data = JsonMapper.ToObject(value);
  28. List<Dictionary<string, object>> dataList = ListDo(value);
  29. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  30. }
  31. public List<Dictionary<string, object>> ListDo(string value)
  32. {
  33. JsonData data = JsonMapper.ToObject(value);
  34. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  35. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  36. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  37. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  38. IQueryable<MsgPersonal> query = maindb.MsgPersonal.Where(m => m.Status > -1 && m.UserId == UserId).OrderByDescending(m => m.Id);
  39. if (PageNum == 1)
  40. {
  41. query = query.Take(PageSize);
  42. }
  43. else
  44. {
  45. int skipNum = PageSize * (PageNum - 1);
  46. query = query.Skip(skipNum).Take(PageSize);
  47. }
  48. foreach (MsgPersonal subdata in query.ToList())
  49. {
  50. Dictionary<string, object> curData = new Dictionary<string, object>();
  51. curData.Add("BgPic", !string.IsNullOrEmpty(subdata.BgPic) ? DefaultPic(subdata.BgPic) : ""); //背景图片
  52. curData.Add("Title", subdata.Title); //标题
  53. curData.Add("Url", subdata.Url); //跳转Url
  54. curData.Add("Summary", subdata.Summary); //简介
  55. curData.Add("Id", subdata.Id); //Id
  56. curData.Add("MsgType", subdata.MsgType);
  57. curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd")); //CreateDate
  58. curData.Add("ReadFlag", subdata.ReadFlag);
  59. dataList.Add(curData);
  60. }
  61. return dataList;
  62. }
  63. #endregion
  64. #region 商户-个人消息已读
  65. [Authorize]
  66. public JsonResult Read(string value)
  67. {
  68. value = DesDecrypt(value);
  69. JsonData data = JsonMapper.ToObject(value);
  70. AppResultJson result = ReadDo(value);
  71. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  72. }
  73. public AppResultJson ReadDo(string value)
  74. {
  75. JsonData data = JsonMapper.ToObject(value);
  76. Dictionary<string, object> Obj = new Dictionary<string, object>();
  77. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString()));
  78. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  79. MsgPersonal query = maindb.MsgPersonal.FirstOrDefault(m=>m.Id == Id && m.UserId == UserId);
  80. if (query != null)
  81. {
  82. query.ReadFlag = 1;
  83. maindb.SaveChanges();
  84. }
  85. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  86. }
  87. #endregion
  88. #region 商户-个人消息详情
  89. [Authorize]
  90. public JsonResult Detail(string value)
  91. {
  92. value = DesDecrypt(value);
  93. JsonData data = JsonMapper.ToObject(value);
  94. Dictionary<string, object> Obj = DetailDo(value);
  95. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  96. }
  97. public Dictionary<string, object> DetailDo(string value)
  98. {
  99. JsonData data = JsonMapper.ToObject(value);
  100. Dictionary<string, object> Obj = new Dictionary<string, object>();
  101. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  102. MsgPersonal query = maindb.MsgPersonal.FirstOrDefault(m => m.Id == Id) ?? new MsgPersonal();
  103. Obj.Add("Title", query.Title); //标题
  104. Obj.Add("Content", CheckContentImage(query.Content)); //内容
  105. return Obj;
  106. }
  107. #endregion
  108. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  109. /// <summary>
  110. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  111. /// </summary>
  112. /// <param name="value">请求的参数(json字符串)</param>
  113. /// <param name="signField">要签名的字段</param>
  114. /// <returns></returns>
  115. private string CheckSign(string value, string[] signField)
  116. {
  117. JsonData json = JsonMapper.ToObject(value);
  118. Dictionary<string, string> dic = new Dictionary<string, string>();
  119. for (int i = 0; i < signField.Length; i++)
  120. {
  121. dic.Add(signField[i], json[signField[i]].ToString());
  122. }
  123. string sign = json["sign"].ToString(); //客户端签名字符串
  124. return new Sign().sign(dic, sign);
  125. }
  126. #endregion
  127. }
  128. }