MerchantQrCodeController.cs 10 KB

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