UserAddressController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 UserAddressController : BaseController
  18. {
  19. public UserAddressController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  20. {
  21. }
  22. #region 创客-商城-修改收货地址
  23. [Authorize]
  24. public JsonResult Update(string value)
  25. {
  26. value = DesDecrypt(value);
  27. JsonData data = JsonMapper.ToObject(value);
  28. AppResultJson result = UpdateDo(value);
  29. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  30. }
  31. public AppResultJson UpdateDo(string value)
  32. {
  33. JsonData data = JsonMapper.ToObject(value);
  34. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  35. string Address = data["Address"].ToString(); //详细地址
  36. string RealName = data["RealName"].ToString(); //收货人姓名
  37. string Mobile = data["Mobile"].ToString(); //手机号码
  38. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //所属创客Id
  39. string Areas = data["Areas"].ToString(); //所在地区
  40. ulong IsDefault = ulong.Parse(data["IsDefault"].ToString()); //是否默认
  41. if (string.IsNullOrEmpty(data["Areas"].ToString()))
  42. {
  43. return new AppResultJson() { Status = "-1", Info = "请填写所在地区" };
  44. }
  45. if (data["Areas"].ToString().Length > 30)
  46. {
  47. return new AppResultJson() { Status = "-1", Info = "所在地区最多30个字符" };
  48. }
  49. Dictionary<string, object> Obj = new Dictionary<string, object>();
  50. UserAddress query = maindb.UserAddress.FirstOrDefault(m => m.Id == Id);
  51. if(query != null)
  52. {
  53. query.UpdateDate = DateTime.Now; //修改时间
  54. query.Address = Address; //详细地址
  55. query.RealName = RealName; //收货人姓名
  56. query.Mobile = Mobile; //手机号码
  57. query.UserId = UserId; //所属创客Id
  58. query.Areas = Areas; //所在地区
  59. query.IsDefault = IsDefault; //是否默认
  60. maindb.SaveChanges();
  61. if(IsDefault == 1)
  62. {
  63. SetDefaultDo("{\"Id\":\"" + Id + "\",\"UserId\":\"" + UserId + "\"}");
  64. }
  65. }
  66. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  67. }
  68. #endregion
  69. #region 创客-商城-删除收货地址
  70. [Authorize]
  71. public JsonResult Delete(string value)
  72. {
  73. value = DesDecrypt(value);
  74. JsonData data = JsonMapper.ToObject(value);
  75. AppResultJson result = DeleteDo(value);
  76. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  77. }
  78. public AppResultJson DeleteDo(string value)
  79. {
  80. JsonData data = JsonMapper.ToObject(value);
  81. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  82. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //所属创客Id
  83. Dictionary<string, object> Obj = new Dictionary<string, object>();
  84. UserAddress query = maindb.UserAddress.FirstOrDefault(m => m.Id == Id && m.UserId == UserId);
  85. if(query != null)
  86. {
  87. maindb.UserAddress.Remove(query);
  88. maindb.SaveChanges();
  89. }
  90. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  91. }
  92. #endregion
  93. #region 创客-商城-收货地址列表
  94. [Authorize]
  95. public JsonResult List(string value)
  96. {
  97. value = DesDecrypt(value);
  98. JsonData data = JsonMapper.ToObject(value);
  99. List<Dictionary<string, object>> dataList = ListDo(value);
  100. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  101. }
  102. public List<Dictionary<string, object>> ListDo(string value)
  103. {
  104. JsonData data = JsonMapper.ToObject(value);
  105. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //所属创客Id
  106. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  107. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  108. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  109. IQueryable<UserAddress> query = maindb.UserAddress.Where(m => m.Status > -1 && m.UserId == UserId);
  110. query = query.OrderByDescending(m => m.Id);
  111. if (PageNum == 1)
  112. {
  113. query = query.Take(PageSize);
  114. }
  115. else
  116. {
  117. int skipNum = PageSize * (PageNum - 1);
  118. query = query.Skip(skipNum).Take(PageSize);
  119. }
  120. foreach (var subdata in query.ToList())
  121. {
  122. Dictionary<string, object> curData = new Dictionary<string, object>();
  123. curData.Add("Address", subdata.Address); //详细地址
  124. curData.Add("RealName", subdata.RealName); //收货人姓名
  125. curData.Add("Mobile", subdata.Mobile); //手机号码
  126. curData.Add("Areas", function.CheckNull(subdata.Areas).Replace(",", "")); //所在地区
  127. curData.Add("IsDefault", subdata.IsDefault); //是否默认
  128. curData.Add("Id", subdata.Id); //Id
  129. dataList.Add(curData);
  130. }
  131. return dataList;
  132. }
  133. #endregion
  134. #region 创客-商城-添加收货地址
  135. [Authorize]
  136. public JsonResult Add(string value)
  137. {
  138. value = DesDecrypt(value);
  139. JsonData data = JsonMapper.ToObject(value);
  140. AppResultJson result = AddDo(value);
  141. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  142. }
  143. public AppResultJson AddDo(string value)
  144. {
  145. JsonData data = JsonMapper.ToObject(value);
  146. string Address = data["Address"].ToString(); //详细地址
  147. string RealName = data["RealName"].ToString(); //收货人姓名
  148. string Mobile = data["Mobile"].ToString(); //手机号码
  149. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //所属创客Id
  150. string Areas = data["Areas"].ToString(); //所在地区
  151. ulong IsDefault = ulong.Parse(data["IsDefault"].ToString()); //是否默认
  152. if (string.IsNullOrEmpty(data["Areas"].ToString()))
  153. {
  154. return new AppResultJson() { Status = "-1", Info = "请填写所在地区" };
  155. }
  156. if (data["Areas"].ToString().Length > 30)
  157. {
  158. return new AppResultJson() { Status = "-1", Info = "所在地区最多30个字符" };
  159. }
  160. Dictionary<string, object> Obj = new Dictionary<string, object>();
  161. UserAddress query = maindb.UserAddress.Add(new UserAddress()
  162. {
  163. CreateDate = DateTime.Now, //创建时间
  164. UpdateDate = DateTime.Now, //修改时间
  165. Address = Address, //详细地址
  166. RealName = RealName, //收货人姓名
  167. Mobile = Mobile, //手机号码
  168. UserId = UserId, //所属创客Id
  169. Areas = Areas, //所在地区
  170. IsDefault = IsDefault, //是否默认
  171. }).Entity;
  172. maindb.SaveChanges();
  173. if(IsDefault == 1)
  174. {
  175. SetDefaultDo("{\"Id\":\"" + query.Id + "\",\"UserId\":\"" + UserId + "\"}");
  176. }
  177. Obj.Add("Id", query.Id); //Id
  178. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  179. }
  180. #endregion
  181. #region 创客-商城-获取收货地址
  182. [Authorize]
  183. public JsonResult Detail(string value)
  184. {
  185. value = DesDecrypt(value);
  186. JsonData data = JsonMapper.ToObject(value);
  187. Dictionary<string, object> Obj = DetailDo(value);
  188. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  189. }
  190. public Dictionary<string, object> DetailDo(string value)
  191. {
  192. JsonData data = JsonMapper.ToObject(value);
  193. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //所属创客Id
  194. Dictionary<string, object> Obj = new Dictionary<string, object>();
  195. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  196. UserAddress query = UserAddressDbconn.Instance.Get(Id) ?? new UserAddress();
  197. Obj.Add("Address", query.Address); //详细地址
  198. Obj.Add("RealName", query.RealName); //收货人姓名
  199. Obj.Add("Mobile", query.Mobile); //手机号码
  200. Obj.Add("Areas", query.Areas); //所在地区
  201. Obj.Add("IsDefault", query.IsDefault); //是否默认
  202. return Obj;
  203. }
  204. #endregion
  205. #region 创客-商城-设置默认收货地址
  206. [Authorize]
  207. public JsonResult SetDefault(string value)
  208. {
  209. value = DesDecrypt(value);
  210. JsonData data = JsonMapper.ToObject(value);
  211. AppResultJson result = SetDefaultDo(value);
  212. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  213. }
  214. public AppResultJson SetDefaultDo(string value)
  215. {
  216. JsonData data = JsonMapper.ToObject(value);
  217. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //所属创客Id
  218. Dictionary<string, object> Obj = new Dictionary<string, object>();
  219. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  220. List<int> query = UserAddressDbconn.Instance.GetList(UserId);
  221. foreach (int AddressId in query)
  222. {
  223. UserAddress subdata = maindb.UserAddress.FirstOrDefault(m => m.Id == AddressId);
  224. if (subdata != null)
  225. {
  226. if (Id == AddressId)
  227. {
  228. subdata.IsDefault = 1;
  229. }
  230. else
  231. {
  232. subdata.IsDefault = 0;
  233. }
  234. }
  235. }
  236. maindb.SaveChanges();
  237. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  238. }
  239. #endregion
  240. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  241. /// <summary>
  242. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  243. /// </summary>
  244. /// <param name="value">请求的参数(json字符串)</param>
  245. /// <param name="signField">要签名的字段</param>
  246. /// <returns></returns>
  247. private string CheckSign(string value, string[] signField)
  248. {
  249. JsonData json = JsonMapper.ToObject(value);
  250. Dictionary<string, string> dic = new Dictionary<string, string>();
  251. for (int i = 0; i < signField.Length; i++)
  252. {
  253. dic.Add(signField[i], json[signField[i]].ToString());
  254. }
  255. string sign = json["sign"].ToString(); //客户端签名字符串
  256. return new Sign().sign(dic, sign);
  257. }
  258. #endregion
  259. }
  260. }