MerchantQrCodeController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 MerchantQrCodeController : BaseController
  18. {
  19. public MerchantQrCodeController(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. 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. MerchantInfo merchant = MerchantInfoDbconn.Instance.Get(MerchantId) ?? new MerchantInfo();
  39. IQueryable<MerchantQrCode> query = maindb.MerchantQrCode.Where(m => m.MerchantId == MerchantId);
  40. if (PageNum == 1)
  41. {
  42. query = query.Take(PageSize);
  43. }
  44. else
  45. {
  46. int skipNum = PageSize * (PageNum - 1);
  47. query = query.Skip(skipNum).Take(PageSize);
  48. }
  49. foreach (MerchantQrCode subdata in query.ToList())
  50. {
  51. Dictionary<string, object> curData = new Dictionary<string, object>();
  52. curData.Add("SnNo", subdata.SnNo); //Sn编号
  53. curData.Add("Id", subdata.Id); //Id
  54. curData.Add("BindDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd")); //绑定时间
  55. curData.Add("MerchantName", merchant.Name); //商户名称
  56. PosMachines machine = PosMachinesDbconn.Instance.Get(subdata.MachineId) ?? new PosMachines();
  57. curData.Add("MachineSnNo", machine.PosSn); //音箱SN
  58. curData.Add("BindFlag", machine.BindingState); //绑定状态
  59. dataList.Add(curData);
  60. }
  61. return dataList;
  62. }
  63. #endregion
  64. #region 商户-绑定收款码
  65. [Authorize]
  66. public JsonResult Bind(string value)
  67. {
  68. value = DesDecrypt(value);
  69. JsonData data = JsonMapper.ToObject(value);
  70. AppResultJson result = BindDo(value);
  71. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  72. }
  73. public AppResultJson BindDo(string value)
  74. {
  75. function.WriteLog(value, "商户-绑定收款码");
  76. JsonData data = JsonMapper.ToObject(value);
  77. int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
  78. string SnNo = data["SnNo"].ToString(); //Sn编号
  79. string MachineNo = data["Machine"].ToString(); //音箱码
  80. Dictionary<string, object> Obj = new Dictionary<string, object>();
  81. MerchantQrCode query = new MerchantQrCodeService().Query(SnNo);
  82. if (query.Id > 0)
  83. {
  84. if (query.MerchantId > 0)
  85. {
  86. return new AppResultJson() { Status = "-1", Info = "此二维码已被绑定,请勿重复绑定" };
  87. }
  88. }
  89. query = maindb.MerchantQrCode.Add(new MerchantQrCode(){
  90. CreateDate = DateTime.Now, //绑定时间
  91. MerchantId = MerchantId, //商户
  92. SnNo = SnNo,
  93. }).Entity;
  94. maindb.SaveChanges();
  95. PosMachines machine = maindb.PosMachines.FirstOrDefault(m => m.PosSn == MachineNo);
  96. if (machine != null)
  97. {
  98. if (machine.BindMerchantId > 0)
  99. {
  100. return new AppResultJson() { Status = "-1", Info = "音箱码已被绑定,请勿重复绑定" };
  101. }
  102. return new AppResultJson() { Status = "1", Info = "绑定成功,收款码暂未关联音箱", Data = Obj };
  103. }
  104. else if (!string.IsNullOrEmpty(MachineNo))
  105. {
  106. machine = maindb.PosMachines.Add(new PosMachines(){
  107. PosSn = MachineNo,
  108. BindMerchantId = MerchantId,
  109. BindingState = 1,
  110. BindingTime = DateTime.Now,
  111. }).Entity;
  112. maindb.SaveChanges();
  113. string DataId = query.Id + "_" + machine.Id;
  114. MachineForQrCode forQrCode = maindb.MachineForQrCode.FirstOrDefault(m => m.DataId == DataId);
  115. if (forQrCode == null)
  116. {
  117. forQrCode = maindb.MachineForQrCode.Add(new MachineForQrCode(){
  118. DataId = DataId,
  119. SnNo = SnNo,
  120. MachineSnNo = MachineNo,
  121. BindDate = DateTime.Now,
  122. MerchantId = MerchantId,
  123. }).Entity;
  124. }
  125. else
  126. {
  127. forQrCode.SnNo = SnNo;
  128. forQrCode.MachineSnNo = MachineNo;
  129. forQrCode.BindDate = DateTime.Now;
  130. forQrCode.MerchantId = MerchantId;
  131. }
  132. maindb.SaveChanges();
  133. return new AppResultJson() { Status = "2", Info = "", Data = Obj };
  134. }
  135. return new AppResultJson() { Status = "-1", Info = "绑定失败" };
  136. }
  137. #endregion
  138. #region 商户-确认替换音箱码
  139. [Authorize]
  140. public JsonResult ConfirmReplace(string value)
  141. {
  142. value = DesDecrypt(value);
  143. JsonData data = JsonMapper.ToObject(value);
  144. AppResultJson result = ConfirmReplaceDo(value);
  145. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  146. }
  147. public AppResultJson ConfirmReplaceDo(string value)
  148. {
  149. JsonData data = JsonMapper.ToObject(value);
  150. int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
  151. int MachineId = int.Parse(function.CheckInt(data["MachineId"].ToString())); //设备
  152. int QrCodeId = int.Parse(function.CheckInt(data["QrCodeId"].ToString())); //收款码Id
  153. Dictionary<string, object> Obj = new Dictionary<string, object>();
  154. MerchantQrCode query = new MerchantQrCodeService().Query(QrCodeId) ?? new MerchantQrCode();
  155. PosMachines machine = PosMachinesDbconn.Instance.Get(MachineId) ?? new PosMachines();
  156. query.MerchantId = MerchantId; //商户
  157. query.MachineId = MachineId; //设备
  158. MachineForQrCode qrcode = new MachineForQrCode()
  159. {
  160. MerchantId = MerchantId,
  161. DataId = QrCodeId + "_" + MachineId,
  162. BindDate = DateTime.Now,
  163. SnNo = query.SnNo,
  164. MachineSnNo = machine.PosSn,
  165. };
  166. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  167. }
  168. #endregion
  169. #region 商户-已绑收款码解除关联
  170. [Authorize]
  171. public JsonResult Remove(string value)
  172. {
  173. value = DesDecrypt(value);
  174. JsonData data = JsonMapper.ToObject(value);
  175. AppResultJson result = RemoveDo(value);
  176. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  177. }
  178. public AppResultJson RemoveDo(string value)
  179. {
  180. JsonData data = JsonMapper.ToObject(value);
  181. int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
  182. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  183. Dictionary<string, object> Obj = new Dictionary<string, object>();
  184. string CheckKey = Id + "_";
  185. var list = maindb.MachineForQrCode.Select(m => new { m.MerchantId, m.DataId }).Where(m => m.MerchantId == MerchantId && m.DataId.StartsWith(CheckKey)).ToList();
  186. foreach (var sub in list)
  187. {
  188. MachineForQrCode edit = maindb.MachineForQrCode.FirstOrDefault(m => m.DataId == sub.DataId);
  189. if (edit != null)
  190. {
  191. maindb.MachineForQrCode.Remove(edit);
  192. int MachineId = int.Parse(function.CheckInt(edit.DataId.Split('_')[1]));
  193. MerchantQrCode qrCode = maindb.MerchantQrCode.FirstOrDefault(m => m.MerchantId == MachineId);
  194. if (qrCode != null)
  195. {
  196. qrCode.MerchantId = 0;
  197. }
  198. }
  199. }
  200. maindb.SaveChanges();
  201. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  202. }
  203. #endregion
  204. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  205. /// <summary>
  206. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  207. /// </summary>
  208. /// <param name="value">请求的参数(json字符串)</param>
  209. /// <param name="signField">要签名的字段</param>
  210. /// <returns></returns>
  211. private string CheckSign(string value, string[] signField)
  212. {
  213. JsonData json = JsonMapper.ToObject(value);
  214. Dictionary<string, string> dic = new Dictionary<string, string>();
  215. for (int i = 0; i < signField.Length; i++)
  216. {
  217. dic.Add(signField[i], json[signField[i]].ToString());
  218. }
  219. string sign = json["sign"].ToString(); //客户端签名字符串
  220. return new Sign().sign(dic, sign);
  221. }
  222. #endregion
  223. }
  224. }