UserCardRecordController.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 UserCardRecordController : BaseController
  18. {
  19. public UserCardRecordController(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 MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
  35. string SearchKey = data["SearchKey"].ToString(); //查询关键词
  36. string Sort = data["Sort"].ToString(); //排序,0-默认,1-最近消费降序,2-最近加入会员降序,3-累计消费次数降序,4-累计消费金额降序,5-近7日消费金额降序
  37. ulong CardFlag = ulong.Parse(function.CheckInt(data["CardFlag"].ToString()));
  38. int ConsumerFlag = int.Parse(function.CheckInt(data["ConsumerFlag"].ToString()));
  39. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  40. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  41. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  42. string condition = "";
  43. if (!string.IsNullOrEmpty(data["SearchKey"].ToString()))
  44. {
  45. condition += " and NickName like '%" + SearchKey + "%'";
  46. }
  47. if (!string.IsNullOrEmpty(data["CardFlag"].ToString()))
  48. {
  49. condition += " and CardFlag=" + CardFlag;
  50. }
  51. if (!string.IsNullOrEmpty(data["ConsumerFlag"].ToString()))
  52. {
  53. string check = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd") + "00:00:00";
  54. if (ConsumerFlag == 1)
  55. {
  56. condition += " and LastConsumDate>='" + check + "'";
  57. }
  58. else if (ConsumerFlag == 0)
  59. {
  60. condition += " and LastConsumDate<'" + check + "'";
  61. }
  62. }
  63. string OrderBy = "Id desc";
  64. if (!string.IsNullOrEmpty(data["Sort"].ToString()))
  65. {
  66. if (Sort == "1")
  67. {
  68. OrderBy = "LastConsumDate desc";
  69. }
  70. else if (Sort == "2")
  71. {
  72. OrderBy = "Id desc";
  73. }
  74. else if (Sort == "3")
  75. {
  76. OrderBy = "TotalConsumCount desc";
  77. }
  78. else if (Sort == "4")
  79. {
  80. OrderBy = "TotalAmount desc";
  81. }
  82. }
  83. else
  84. {
  85. OrderBy = "Id desc";
  86. }
  87. List<Dictionary<string, object>> query = new ConsumersService().List(new List<FieldItem>() , condition, PageNum, PageSize, OrderBy);
  88. foreach (var ConsumersData in query)
  89. {
  90. Dictionary<string, object> curData = new Dictionary<string, object>();
  91. curData.Add("ConsumerId", RelationClass.GetConsumersInfo(int.Parse(ConsumersData["Id"].ToString()))); //消费者
  92. Dictionary<string, object> Consumer = new Dictionary<string, object>();
  93. Consumer.Add("NickName", ConsumersData["NickName"].ToString()); //昵称
  94. Consumer.Add("HeadPhoto", ConsumersData["HeadPhoto"].ToString()); //头像
  95. Consumer.Add("CurIntgegral", ConsumersData["CurIntgegral"].ToString()); //当前积分
  96. Consumer.Add("TotalAmount", ConsumersData["TotalAmount"].ToString()); //累计消费金额
  97. Consumer.Add("TotalConsumCount", ConsumersData["TotalConsumCount"].ToString()); //累计消费次数
  98. Consumer.Add("LastConsumDate", ConsumersData["LastConsumDate"].ToString()); //最近消费时间
  99. curData.Add("Consumer", Consumer);
  100. curData.Add("CreateDate", ConsumersData["CreateDate"].ToString()); //CreateDate
  101. int ConsumeCount = 0;
  102. for (int i = 0; i < 7; i++)
  103. {
  104. string Date = DateTime.Now.AddDays(-i).ToString("yyyyMMdd");
  105. }
  106. curData.Add("ConsumeCount", ConsumeCount); //近7日消费次数
  107. dataList.Add(curData);
  108. }
  109. return dataList;
  110. }
  111. #endregion
  112. #region 消费者-发放会员卡
  113. [Authorize]
  114. public JsonResult Add(string value)
  115. {
  116. value = DesDecrypt(value);
  117. JsonData data = JsonMapper.ToObject(value);
  118. AppResultJson result = AddDo(value);
  119. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  120. }
  121. public AppResultJson AddDo(string value)
  122. {
  123. JsonData data = JsonMapper.ToObject(value);
  124. int ConsumerId = int.Parse(function.CheckInt(data["ConsumerId"].ToString())); //消费者
  125. int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
  126. UserCardForWeChat card = new UserCardForWeChatService().Query(MerchantId);
  127. Dictionary<string, object> Obj = new Dictionary<string, object>();
  128. UserCardRecord query = new UserCardRecord()
  129. {
  130. CreateDate = DateTime.Now, //创建时间
  131. UpdateDate = DateTime.Now, //修改时间
  132. ConsumerId = ConsumerId, //消费者
  133. MerchantId = MerchantId, //商户
  134. CardId = card.Id,
  135. };
  136. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  137. }
  138. #endregion
  139. #region 消费者-获取会员卡
  140. [Authorize]
  141. public JsonResult GetCardList(string value)
  142. {
  143. value = DesDecrypt(value);
  144. JsonData data = JsonMapper.ToObject(value);
  145. AppResultJson result = GetCardListDo(value);
  146. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  147. }
  148. public AppResultJson GetCardListDo(string value)
  149. {
  150. JsonData data = JsonMapper.ToObject(value);
  151. int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
  152. UserCardForWeChat card = new UserCardForWeChatService().Query(MerchantId);
  153. Dictionary<string, object> Obj = new Dictionary<string, object>();
  154. Dictionary<string, object> cardExt = new WeChatFunction(_accessor.HttpContext).GetUserCard(MerchantId);
  155. Obj.Add("CardId", card.Id);
  156. Obj.Add("CardExt", cardExt);
  157. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  158. }
  159. #endregion
  160. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  161. /// <summary>
  162. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  163. /// </summary>
  164. /// <param name="value">请求的参数(json字符串)</param>
  165. /// <param name="signField">要签名的字段</param>
  166. /// <returns></returns>
  167. private string CheckSign(string value, string[] signField)
  168. {
  169. JsonData json = JsonMapper.ToObject(value);
  170. Dictionary<string, string> dic = new Dictionary<string, string>();
  171. for (int i = 0; i < signField.Length; i++)
  172. {
  173. dic.Add(signField[i], json[signField[i]].ToString());
  174. }
  175. string sign = json["sign"].ToString(); //客户端签名字符串
  176. return new Sign().sign(dic, sign);
  177. }
  178. #endregion
  179. }
  180. }