using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.AspNetCore.Authorization; using System.Web; using MySystem.MainModels; using LitJson; using Library; namespace MySystem.Areas.Api.Controllers.v1 { [Area("Api")] [Route("Api/v1/[controller]/[action]")] public class MsgPersonalController : BaseController { public MsgPersonalController(IHttpContextAccessor accessor, ILogger logger, IOptions setting) : base(accessor, logger, setting) { } #region 商户-个人消息列表 [Authorize] public JsonResult List(string value) { value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); List> dataList = ListDo(value); return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList }); } public List> ListDo(string value) { JsonData data = JsonMapper.ToObject(value); int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客 int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString())); int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString())); List> dataList = new List>(); IQueryable query = maindb.MsgPersonal.Where(m => m.Status > -1 && m.UserId == UserId).OrderByDescending(m => m.Id); if (PageNum == 1) { query = query.Take(PageSize); } else { int skipNum = PageSize * (PageNum - 1); query = query.Skip(skipNum).Take(PageSize); } foreach (MsgPersonal subdata in query.ToList()) { Dictionary curData = new Dictionary(); curData.Add("BgPic", !string.IsNullOrEmpty(subdata.BgPic) ? DefaultPic(subdata.BgPic) : ""); //背景图片 curData.Add("Title", subdata.Title); //标题 curData.Add("Url", subdata.Url); //跳转Url curData.Add("Summary", subdata.Summary); //简介 curData.Add("Id", subdata.Id); //Id curData.Add("MsgType", subdata.MsgType); curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd")); //CreateDate curData.Add("ReadFlag", subdata.ReadFlag); dataList.Add(curData); } return dataList; } #endregion #region 商户-个人消息已读 [Authorize] public JsonResult Read(string value) { value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); AppResultJson result = ReadDo(value); return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data }); } public AppResultJson ReadDo(string value) { JsonData data = JsonMapper.ToObject(value); Dictionary Obj = new Dictionary(); int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); int Id = int.Parse(function.CheckInt(data["Id"].ToString())); MsgPersonal query = maindb.MsgPersonal.FirstOrDefault(m=>m.Id == Id && m.UserId == UserId); if (query != null) { query.ReadFlag = 1; maindb.SaveChanges(); } return new AppResultJson() { Status = "1", Info = "", Data = Obj }; } #endregion #region 商户-个人消息详情 [Authorize] public JsonResult Detail(string value) { value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); Dictionary Obj = DetailDo(value); return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj }); } public Dictionary DetailDo(string value) { JsonData data = JsonMapper.ToObject(value); Dictionary Obj = new Dictionary(); int Id = int.Parse(function.CheckInt(data["Id"].ToString())); MsgPersonal query = maindb.MsgPersonal.FirstOrDefault(m => m.Id == Id) ?? new MsgPersonal(); Obj.Add("Title", query.Title); //标题 Obj.Add("Content", CheckContentImage(query.Content)); //内容 return Obj; } #endregion #region 检查签名是否合法,合法返回1,不合法返回提示信息 /// /// 检查签名是否合法,合法返回1,不合法返回提示信息 /// /// 请求的参数(json字符串) /// 要签名的字段 /// private string CheckSign(string value, string[] signField) { JsonData json = JsonMapper.ToObject(value); Dictionary dic = new Dictionary(); for (int i = 0; i < signField.Length; i++) { dic.Add(signField[i], json[signField[i]].ToString()); } string sign = json["sign"].ToString(); //客户端签名字符串 return new Sign().sign(dic, sign); } #endregion } }