MerchantQrCodeController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 && m.QueryCount == 1);
  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. JsonData data = JsonMapper.ToObject(value);
  76. int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
  77. string SnNo = data["SnNo"].ToString(); //Sn编号
  78. string MachineNo = data["Machine"].ToString(); //音箱码
  79. if (SnNo.Length > 20)
  80. {
  81. SnNo = System.Web.HttpUtility.UrlDecode(SnNo);
  82. if (!SnNo.EndsWith("="))
  83. {
  84. SnNo += "=";
  85. }
  86. SnNo = dbconn.Decrypt3DES(SnNo, "l2k0b2#3");
  87. SnNo = SnNo.TrimEnd('\0');
  88. SnNo = SnNo.Substring(0, 20);
  89. }
  90. if(!string.IsNullOrEmpty(MachineNo))
  91. {
  92. if (MachineNo.Length > 20)
  93. {
  94. MachineNo = System.Web.HttpUtility.UrlDecode(MachineNo);
  95. if (!MachineNo.EndsWith("="))
  96. {
  97. MachineNo += "=";
  98. }
  99. MachineNo = dbconn.Decrypt3DES(MachineNo, "l2k0b2#3");
  100. MachineNo = MachineNo.TrimEnd('\0');
  101. MachineNo = MachineNo.Substring(0, 20);
  102. }
  103. }
  104. Dictionary<string, object> Obj = new Dictionary<string, object>();
  105. MerchantQrCode query = new MerchantQrCodeService().Query(SnNo);
  106. PosMachinesTwo pos = new PosMachinesTwoService().Query(SnNo);
  107. PosMachines machines = new PosMachinesService().Query(SnNo);
  108. MerchantInfo merchant = new MerchantInfoService().Query(MerchantId);
  109. MerchantAddInfo addMer = new MerchantAddInfoService().Query(MerchantId);
  110. if (pos.BindingState == 1)
  111. {
  112. return new AppResultJson() { Status = "-1", Info = "此码已被绑定" };
  113. }
  114. if (machines.BindingState == 1)
  115. {
  116. return new AppResultJson() { Status = "-1", Info = "此码已被绑定" };
  117. }
  118. int PosId = 0;
  119. if (SnNo == MachineNo || string.IsNullOrEmpty(MachineNo))
  120. {
  121. if (pos.Id == 0)
  122. {
  123. return new AppResultJson() { Status = "-1", Info = "绑定失败" };
  124. }
  125. PosId = pos.Id;
  126. }
  127. else if (SnNo != MachineNo)
  128. {
  129. if (machines.Id == 0)
  130. {
  131. return new AppResultJson() { Status = "-1", Info = "绑定失败" };
  132. }
  133. PosId = machines.Id;
  134. }
  135. if (query.Id > 0)
  136. {
  137. if (query.MerchantId > 0)
  138. {
  139. return new AppResultJson() { Status = "-1", Info = "此二维码已被绑定,请勿重复绑定" };
  140. }
  141. }
  142. Dictionary<string, object> fields = new Dictionary<string, object>();
  143. if (SnNo == MachineNo || string.IsNullOrEmpty(MachineNo))
  144. {
  145. fields.Add("MerchantId", MerchantId);
  146. fields.Add("SnNo", SnNo);
  147. fields.Add("QueryCount", 1);
  148. new MerchantQrCodeService().Add(fields);
  149. }
  150. else
  151. {
  152. string DataId = PosId + "_1";
  153. MachineForQrCode forQrCode = maindb.MachineForQrCode.FirstOrDefault(m => m.DataId == DataId);
  154. if (forQrCode == null)
  155. {
  156. forQrCode = maindb.MachineForQrCode.Add(new MachineForQrCode(){
  157. DataId = DataId,
  158. SnNo = SnNo,
  159. MachineSnNo = MachineNo,
  160. BindDate = DateTime.Now,
  161. MerchantId = MerchantId,
  162. }).Entity;
  163. }
  164. else
  165. {
  166. forQrCode.SnNo = SnNo;
  167. forQrCode.MachineSnNo = MachineNo;
  168. forQrCode.BindDate = DateTime.Now;
  169. forQrCode.MerchantId = MerchantId;
  170. }
  171. }
  172. maindb.SaveChanges();
  173. if (SnNo == MachineNo || string.IsNullOrEmpty(MachineNo))
  174. {
  175. fields = new Dictionary<string, object>();
  176. fields.Add("BuyUserId", merchant.UserId);
  177. fields.Add("UserId", merchant.UserId);
  178. fields.Add("BindingTime", DateTime.Now);
  179. fields.Add("BindingState", 1);
  180. fields.Add("BindMerchantId", MerchantId);
  181. fields.Add("OpId", 1);
  182. new PosMachinesTwoService().Edit(fields, pos.Id);
  183. //推送绑定数据到java
  184. // PosPushDataNewHelper.Bind(addMer, pos);
  185. }
  186. else if (SnNo != MachineNo && !string.IsNullOrEmpty(MachineNo))
  187. {
  188. fields = new Dictionary<string, object>();
  189. fields.Add("BindMerchantId", MerchantId);
  190. fields.Add("DeviceKind", "1");
  191. new PosMachinesService().Edit(fields, machines.Id);
  192. //推送绑定数据到java
  193. // PosPushDataNewHelper.Bind(addMer, pos);
  194. }
  195. if (SnNo == MachineNo || string.IsNullOrEmpty(MachineNo))
  196. {
  197. PublicFunction.BindUserMachineData(maindb, merchant.UserId, 0, 1, SnNo);
  198. }
  199. else
  200. {
  201. PublicFunction.BindUserMachineData(maindb, merchant.UserId, 1, 1, SnNo);
  202. }
  203. return new AppResultJson() { Status = "1", Info = "绑定成功" };
  204. }
  205. #endregion
  206. #region 商户-确认替换音箱码
  207. [Authorize]
  208. public JsonResult ConfirmReplace(string value)
  209. {
  210. value = DesDecrypt(value);
  211. JsonData data = JsonMapper.ToObject(value);
  212. AppResultJson result = ConfirmReplaceDo(value);
  213. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  214. }
  215. public AppResultJson ConfirmReplaceDo(string value)
  216. {
  217. JsonData data = JsonMapper.ToObject(value);
  218. int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
  219. int MachineId = int.Parse(function.CheckInt(data["MachineId"].ToString())); //设备
  220. int QrCodeId = int.Parse(function.CheckInt(data["QrCodeId"].ToString())); //收款码Id
  221. Dictionary<string, object> Obj = new Dictionary<string, object>();
  222. MerchantQrCode query = new MerchantQrCodeService().Query(QrCodeId) ?? new MerchantQrCode();
  223. PosMachines machine = PosMachinesDbconn.Instance.Get(MachineId) ?? new PosMachines();
  224. query.MerchantId = MerchantId; //商户
  225. query.MachineId = MachineId; //设备
  226. MachineForQrCode qrcode = new MachineForQrCode()
  227. {
  228. MerchantId = MerchantId,
  229. DataId = QrCodeId + "_" + MachineId,
  230. BindDate = DateTime.Now,
  231. SnNo = query.SnNo,
  232. MachineSnNo = machine.PosSn,
  233. };
  234. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  235. }
  236. #endregion
  237. #region 商户-已绑收款码解除关联
  238. [Authorize]
  239. public JsonResult Remove(string value)
  240. {
  241. value = DesDecrypt(value);
  242. JsonData data = JsonMapper.ToObject(value);
  243. AppResultJson result = RemoveDo(value);
  244. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  245. }
  246. public AppResultJson RemoveDo(string value)
  247. {
  248. JsonData data = JsonMapper.ToObject(value);
  249. int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
  250. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  251. Dictionary<string, object> Obj = new Dictionary<string, object>();
  252. string CheckKey = Id + "_";
  253. var list = maindb.MachineForQrCode.Select(m => new { m.MerchantId, m.DataId }).Where(m => m.MerchantId == MerchantId && m.DataId.StartsWith(CheckKey)).ToList();
  254. foreach (var sub in list)
  255. {
  256. MachineForQrCode edit = maindb.MachineForQrCode.FirstOrDefault(m => m.DataId == sub.DataId);
  257. if (edit != null)
  258. {
  259. maindb.MachineForQrCode.Remove(edit);
  260. int MachineId = int.Parse(function.CheckInt(edit.DataId.Split('_')[1]));
  261. MerchantQrCode qrCode = maindb.MerchantQrCode.FirstOrDefault(m => m.MerchantId == MachineId);
  262. if (qrCode != null)
  263. {
  264. qrCode.MerchantId = 0;
  265. }
  266. }
  267. }
  268. maindb.SaveChanges();
  269. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  270. }
  271. #endregion
  272. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  273. /// <summary>
  274. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  275. /// </summary>
  276. /// <param name="value">请求的参数(json字符串)</param>
  277. /// <param name="signField">要签名的字段</param>
  278. /// <returns></returns>
  279. private string CheckSign(string value, string[] signField)
  280. {
  281. JsonData json = JsonMapper.ToObject(value);
  282. Dictionary<string, string> dic = new Dictionary<string, string>();
  283. for (int i = 0; i < signField.Length; i++)
  284. {
  285. dic.Add(signField[i], json[signField[i]].ToString());
  286. }
  287. string sign = json["sign"].ToString(); //客户端签名字符串
  288. return new Sign().sign(dic, sign);
  289. }
  290. #endregion
  291. }
  292. }