MerchantQrCodeController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.Models.Main;
  11. using MySystem.Models.Main1;
  12. using LitJson;
  13. using Library;
  14. using MySystem.Service.Main;
  15. namespace MySystem.Areas.Api.Controllers.v1
  16. {
  17. [Area("Api")]
  18. [Route("Api/v1/[controller]/[action]")]
  19. public class MerchantQrCodeController : BaseController
  20. {
  21. public MerchantQrCodeController(IHttpContextAccessor accessor) : base(accessor)
  22. {
  23. }
  24. #region 商户-已绑二维码
  25. // [Authorize]
  26. public JsonResult List(string value)
  27. {
  28. value = DesDecrypt(value);
  29. JsonData data = JsonMapper.ToObject(value);
  30. List<Dictionary<string, object>> dataList = ListDo(value);
  31. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  32. }
  33. public List<Dictionary<string, object>> ListDo(string value)
  34. {
  35. JsonData data = JsonMapper.ToObject(value);
  36. int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
  37. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  38. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  39. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  40. Models.Main.MerchantInfo merchant = MerchantInfoDbconn.Instance.Get(MerchantId) ?? new Models.Main.MerchantInfo();
  41. IQueryable<Models.Main1.MerchantQrCode> query = main1db.MerchantQrCode.Where(m => m.MerchantId == MerchantId && m.QueryCount == 2);
  42. if (PageNum == 1)
  43. {
  44. query = query.Take(PageSize);
  45. }
  46. else
  47. {
  48. int skipNum = PageSize * (PageNum - 1);
  49. query = query.Skip(skipNum).Take(PageSize);
  50. }
  51. foreach (Models.Main1.MerchantQrCode subdata in query.ToList())
  52. {
  53. Dictionary<string, object> curData = new Dictionary<string, object>();
  54. curData.Add("SnNo", subdata.SnNo); //Sn编号
  55. curData.Add("Id", subdata.Id); //Id
  56. curData.Add("BindDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd")); //绑定时间
  57. curData.Add("MerchantName", merchant.Name); //商户名称
  58. Models.Main1.PosMachines machine = PosMachinesDbconn.Instance.Get(subdata.MachineId) ?? new Models.Main1.PosMachines();
  59. curData.Add("MachineSnNo", machine.PosSn); //音箱SN
  60. curData.Add("BindFlag", machine.BindingState); //绑定状态
  61. dataList.Add(curData);
  62. }
  63. return dataList;
  64. }
  65. #endregion
  66. #region 商户-绑定收款码
  67. // [Authorize]
  68. public JsonResult Bind(string value)
  69. {
  70. value = DesDecrypt(value);
  71. JsonData data = JsonMapper.ToObject(value);
  72. AppResultJson result = BindDo(value);
  73. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  74. }
  75. public AppResultJson BindDo(string value)
  76. {
  77. JsonData data = JsonMapper.ToObject(value);
  78. int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
  79. string SnNo = data["SnNo"].ToString(); //Sn编号
  80. string MachineNo = data["Machine"].ToString(); //音箱码
  81. Dictionary<string, object> Obj = new Dictionary<string, object>();
  82. Models.Main1.MerchantQrCode query = MerchantQrCodeService.Query(" SnNo='" + SnNo + "'");
  83. Models.Main1.PosMachinesTwo pos = PosMachinesTwoService.Query(" PosSn='" + SnNo + "'");
  84. Models.Main.MerchantInfo merchant = MerchantInfoService.Query(MerchantId);
  85. if (pos.Id == 0)
  86. {
  87. return new AppResultJson() { Status = "-1", Info = "绑定失败" };
  88. }
  89. if (pos.BuyUserId == 0)
  90. {
  91. return new AppResultJson() { Status = "-1", Info = "码牌未授权" };
  92. }
  93. if (query.Id > 0)
  94. {
  95. if (query.MerchantId > 0)
  96. {
  97. return new AppResultJson() { Status = "-1", Info = "此二维码已被绑定,请勿重复绑定" };
  98. }
  99. }
  100. Dictionary<string, object> fields = new Dictionary<string, object>();
  101. fields.Add("MerchantId", MerchantId);
  102. fields.Add("SnNo", SnNo);
  103. fields.Add("QueryCount", 2);
  104. MerchantQrCodeService.Add(fields);
  105. // query = maindb.MerchantQrCode.Add(new MerchantQrCode()
  106. // {
  107. // CreateDate = DateTime.Now, //绑定时间
  108. // MerchantId = MerchantId, //商户
  109. // SnNo = SnNo,
  110. // }).Entity;
  111. // maindb.SaveChanges();
  112. string DataId = query.Id + "_0";
  113. Models.Main1.MachineForQrCode forQrCode = main1db.MachineForQrCode.FirstOrDefault(m => m.DataId == DataId);
  114. if (forQrCode == null)
  115. {
  116. forQrCode = main1db.MachineForQrCode.Add(new Models.Main1.MachineForQrCode()
  117. {
  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. main1db.SaveChanges();
  133. fields = new Dictionary<string, object>();
  134. fields.Add("BuyUserId", merchant.UserId);
  135. fields.Add("UserId", merchant.UserId);
  136. fields.Add("BindingTime", DateTime.Now);
  137. fields.Add("BindingState", 1);
  138. fields.Add("BindMerchantId", MerchantId);
  139. fields.Add("OpId", 2);
  140. fields.Add("QueryCount", 2);
  141. PosMachinesTwoService.Edit(fields, pos.Id, false);
  142. // pos.BuyUserId = merchant.UserId;
  143. // pos.UserId = merchant.UserId;
  144. // pos.BindingTime = DateTime.Now;
  145. // pos.BindingState = 1;
  146. // pos.BindMerchantId = MerchantId;
  147. // maindb.SaveChanges();
  148. PublicFunction.BindUserMachineData(main1db, merchant.UserId, 0, 1, SnNo);
  149. return new AppResultJson() { Status = "1", Info = "绑定成功" };
  150. }
  151. #endregion
  152. #region 商户-确认替换音箱码
  153. // [Authorize]
  154. public JsonResult ConfirmReplace(string value)
  155. {
  156. value = DesDecrypt(value);
  157. JsonData data = JsonMapper.ToObject(value);
  158. AppResultJson result = ConfirmReplaceDo(value);
  159. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  160. }
  161. public AppResultJson ConfirmReplaceDo(string value)
  162. {
  163. JsonData data = JsonMapper.ToObject(value);
  164. int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
  165. int MachineId = int.Parse(function.CheckInt(data["MachineId"].ToString())); //设备
  166. int QrCodeId = int.Parse(function.CheckInt(data["QrCodeId"].ToString())); //收款码Id
  167. Dictionary<string, object> Obj = new Dictionary<string, object>();
  168. Models.Main1.MerchantQrCode query = MerchantQrCodeService.Query(QrCodeId) ?? new Models.Main1.MerchantQrCode();
  169. Models.Main1.PosMachines machine = PosMachinesDbconn.Instance.Get(MachineId) ?? new Models.Main1.PosMachines();
  170. query.MerchantId = MerchantId; //商户
  171. query.MachineId = MachineId; //设备
  172. Models.Main1.MachineForQrCode qrcode = new Models.Main1.MachineForQrCode()
  173. {
  174. MerchantId = MerchantId,
  175. DataId = QrCodeId + "_" + MachineId,
  176. BindDate = DateTime.Now,
  177. SnNo = query.SnNo,
  178. MachineSnNo = machine.PosSn,
  179. };
  180. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  181. }
  182. #endregion
  183. #region 商户-已绑收款码解除关联
  184. // [Authorize]
  185. public JsonResult Remove(string value)
  186. {
  187. value = DesDecrypt(value);
  188. JsonData data = JsonMapper.ToObject(value);
  189. AppResultJson result = RemoveDo(value);
  190. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  191. }
  192. public AppResultJson RemoveDo(string value)
  193. {
  194. JsonData data = JsonMapper.ToObject(value);
  195. int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
  196. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  197. Dictionary<string, object> Obj = new Dictionary<string, object>();
  198. string CheckKey = Id + "_";
  199. var list = main1db.MachineForQrCode.Select(m => new { m.MerchantId, m.DataId }).Where(m => m.MerchantId == MerchantId && m.DataId.StartsWith(CheckKey)).ToList();
  200. foreach (var sub in list)
  201. {
  202. Models.Main1.MachineForQrCode edit = main1db.MachineForQrCode.FirstOrDefault(m => m.DataId == sub.DataId);
  203. if (edit != null)
  204. {
  205. main1db.MachineForQrCode.Remove(edit);
  206. int MachineId = int.Parse(function.CheckInt(edit.DataId.Split('_')[1]));
  207. Models.Main1.MerchantQrCode qrCode = main1db.MerchantQrCode.FirstOrDefault(m => m.MerchantId == MachineId);
  208. if (qrCode != null)
  209. {
  210. qrCode.MerchantId = 0;
  211. }
  212. }
  213. }
  214. main1db.SaveChanges();
  215. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  216. }
  217. #endregion
  218. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  219. /// <summary>
  220. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  221. /// </summary>
  222. /// <param name="value">请求的参数(json字符串)</param>
  223. /// <param name="signField">要签名的字段</param>
  224. /// <returns></returns>
  225. private string CheckSign(string value, string[] signField)
  226. {
  227. JsonData json = JsonMapper.ToObject(value);
  228. Dictionary<string, string> dic = new Dictionary<string, string>();
  229. for (int i = 0; i < signField.Length; i++)
  230. {
  231. dic.Add(signField[i], json[signField[i]].ToString());
  232. }
  233. string sign = json["sign"].ToString(); //客户端签名字符串
  234. return new Sign().sign(dic, sign);
  235. }
  236. #endregion
  237. }
  238. }