ProductsController.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 ProductsController : BaseController
  18. {
  19. public ProductsController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  20. {
  21. }
  22. #region 创客-商城-商品详情
  23. [Authorize]
  24. public JsonResult Detail(string value)
  25. {
  26. value = DesDecrypt(value);
  27. JsonData data = JsonMapper.ToObject(value);
  28. Dictionary<string, object> Obj = DetailDo(value);
  29. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  30. }
  31. public Dictionary<string, object> DetailDo(string value)
  32. {
  33. JsonData data = JsonMapper.ToObject(value);
  34. Dictionary<string, object> Obj = new Dictionary<string, object>();
  35. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  36. Products query = ProductsDbconn.Instance.Get(Id) ?? new Products();
  37. KqProducts brand = maindb.KqProducts.FirstOrDefault(m => m.QueryCount == Id) ?? new KqProducts();
  38. Obj.Add("BrandId", brand.Id);
  39. Obj.Add("ProductName", query.ProductName); //商品名称
  40. Obj.Add("Details", query.Details); //简介
  41. Obj.Add("Contents", CheckContentImage(query.Contents)); //内容
  42. List<Dictionary<string, string>> DetailPicPath = new List<Dictionary<string, string>>();
  43. if (!string.IsNullOrEmpty(query.DetailPicPath))
  44. {
  45. string[] DetailPicPathList = query.DetailPicPath.Split(new string[] { "#cut#" }, StringSplitOptions.RemoveEmptyEntries)[0].Split('|');
  46. foreach (string sub in DetailPicPathList)
  47. {
  48. Dictionary<string, string> item = new Dictionary<string, string>();
  49. item.Add("Photo", DefaultPic(sub));
  50. DetailPicPath.Add(item);
  51. }
  52. }
  53. Obj.Add("DetailPicPath", DetailPicPath); //图集
  54. Obj.Add("Price", query.Price); //价格
  55. Dictionary<string, object> NormJson = new Dictionary<string, object>();
  56. int collection_id = 0;
  57. List<Dictionary<string, object>> tree = new List<Dictionary<string, object>>();
  58. List<ProductNormItem> productNormItems = ProductNormItemDbconn.Instance.GetList(Id);
  59. if (productNormItems.Count > 0)
  60. {
  61. int i = 0;
  62. foreach (ProductNormItem productNormItem in productNormItems.Where(m => m.ParentId == 0).ToList())
  63. {
  64. i += 1;
  65. Dictionary<string, object> treeitem = new Dictionary<string, object>();
  66. treeitem.Add("k", productNormItem.ColName);
  67. treeitem.Add("k_s", "s" + i.ToString());
  68. List<Dictionary<string, object>> subtreeitems = new List<Dictionary<string, object>>();
  69. foreach (ProductNormItem subProductNormItem in productNormItems.Where(m => m.ParentId == productNormItem.Id).ToList())
  70. {
  71. Dictionary<string, object> subtreeitem = new Dictionary<string, object>();
  72. subtreeitem.Add("id", subProductNormItem.Id.ToString());
  73. subtreeitem.Add("name", subProductNormItem.ColName);
  74. subtreeitems.Add(subtreeitem);
  75. }
  76. treeitem.Add("v", subtreeitems);
  77. treeitem.Add("largeImageMode", true);
  78. tree.Add(treeitem);
  79. }
  80. NormJson.Add("tree", tree);
  81. List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
  82. List<int> productNorms = ProductNormDbconn.Instance.GetList(Id);
  83. i = 0;
  84. foreach (int NormId in productNorms)
  85. {
  86. ProductNorm productNorm = ProductNormDbconn.Instance.Get(NormId) ?? new ProductNorm();
  87. i += 1;
  88. if (i == 1)
  89. {
  90. collection_id = productNorm.Id;
  91. }
  92. Dictionary<string, object> item = new Dictionary<string, object>();
  93. item.Add("id", productNorm.Id);
  94. string[] idlist = function.CheckNull(productNorm.IdList).Split(',');
  95. int j = 0;
  96. foreach (string sub in idlist)
  97. {
  98. j += 1;
  99. item.Add("s" + j, sub);
  100. }
  101. item.Add("price", productNorm.Price);
  102. item.Add("stock_num", productNorm.Stock);
  103. list.Add(item);
  104. }
  105. NormJson.Add("list", list);
  106. NormJson.Add("collection_id", collection_id);
  107. NormJson.Add("none_sku", false);
  108. }
  109. else
  110. {
  111. NormJson.Add("collection_id", query.Id);
  112. NormJson.Add("none_sku", true);
  113. }
  114. NormJson.Add("price", query.Price);
  115. NormJson.Add("stock_num", query.Stock);
  116. NormJson.Add("hide_stock", true);
  117. Obj.Add("NormJson", NormJson); //规格
  118. Obj.Add("MinBuyCount", 1); //最小购买数量
  119. return Obj;
  120. }
  121. #endregion
  122. #region 创客-商城-商城主界面列表
  123. [Authorize]
  124. public JsonResult List(string value)
  125. {
  126. value = DesDecrypt(value);
  127. JsonData data = JsonMapper.ToObject(value);
  128. List<Dictionary<string, object>> dataList = ListDo(value);
  129. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  130. }
  131. public List<Dictionary<string, object>> ListDo(string value)
  132. {
  133. JsonData data = JsonMapper.ToObject(value);
  134. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  135. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  136. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  137. List<int> query = ProductsDbconn.Instance.GetList(PageNum, PageSize);
  138. foreach (int ProductId in query)
  139. {
  140. Products subdata = ProductsDbconn.Instance.Get(ProductId) ?? new Products();
  141. Dictionary<string, object> curData = new Dictionary<string, object>();
  142. curData.Add("ProductName", subdata.ProductName); //商品名称
  143. curData.Add("Details", subdata.Details); //简介
  144. curData.Add("ListPicPath", DefaultPic(subdata.ListPicPath)); //列表图片
  145. curData.Add("Price", subdata.Price); //价格
  146. curData.Add("Id", subdata.Id); //Id
  147. dataList.Add(curData);
  148. }
  149. return dataList;
  150. }
  151. #endregion
  152. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  153. /// <summary>
  154. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  155. /// </summary>
  156. /// <param name="value">请求的参数(json字符串)</param>
  157. /// <param name="signField">要签名的字段</param>
  158. /// <returns></returns>
  159. private string CheckSign(string value, string[] signField)
  160. {
  161. JsonData json = JsonMapper.ToObject(value);
  162. Dictionary<string, string> dic = new Dictionary<string, string>();
  163. for (int i = 0; i < signField.Length; i++)
  164. {
  165. dic.Add(signField[i], json[signField[i]].ToString());
  166. }
  167. string sign = json["sign"].ToString(); //客户端签名字符串
  168. return new Sign().sign(dic, sign);
  169. }
  170. #endregion
  171. }
  172. }