MerchantQrCodeController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 LitJson;
  12. using Library;
  13. using MySystem.Service.Main;
  14. namespace MySystem.Areas.Api.Controllers.v1
  15. {
  16. [Area("Api")]
  17. [Route("Api/v1/[controller]/[action]")]
  18. public class MerchantQrCodeController : BaseController
  19. {
  20. public MerchantQrCodeController(IHttpContextAccessor accessor) : base(accessor)
  21. {
  22. }
  23. #region 商户-已绑二维码
  24. // [Authorize]
  25. public JsonResult List(string value)
  26. {
  27. value = DesDecrypt(value);
  28. value = value.Replace("null", "\"\"");
  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.getItem("MerchantId").ToString())); //商户
  37. int PageSize = int.Parse(function.CheckInt(data.getItem("PageSize").ToString()));
  38. int PageNum = int.Parse(function.CheckInt(data.getItem("PageNum").ToString()));
  39. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  40. MerchantInfo merchant = MerchantInfoDbconn.Instance.Get(MerchantId) ?? new MerchantInfo();
  41. IQueryable<MerchantQrCode> query = maindb.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 (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. value = value.Replace("null", "\"\"");
  72. JsonData data = JsonMapper.ToObject(value);
  73. AppResultJson result = BindDo(value);
  74. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  75. }
  76. public AppResultJson BindDo(string value)
  77. {
  78. JsonData data = JsonMapper.ToObject(value);
  79. int MerchantId = int.Parse(function.CheckInt(data.getItem("MerchantId").ToString())); //商户
  80. string SnNo = data.getItem("SnNo").ToString(); //Sn编号
  81. string MachineNo = data.getItem("Machine").ToString(); //音箱码
  82. if (SnNo.Length > 20)
  83. {
  84. SnNo = System.Web.HttpUtility.UrlDecode(SnNo);
  85. if (!SnNo.EndsWith("="))
  86. {
  87. SnNo += "=";
  88. }
  89. SnNo = dbconn.Decrypt3DES(SnNo, "l2k0b2#3");
  90. SnNo = SnNo.TrimEnd('\0');
  91. SnNo = SnNo.Substring(0, 20);
  92. }
  93. if (!string.IsNullOrEmpty(MachineNo))
  94. {
  95. if (MachineNo.Length > 20)
  96. {
  97. MachineNo = System.Web.HttpUtility.UrlDecode(MachineNo);
  98. if (!MachineNo.EndsWith("="))
  99. {
  100. MachineNo += "=";
  101. }
  102. MachineNo = dbconn.Decrypt3DES(MachineNo, "l2k0b2#3");
  103. MachineNo = MachineNo.TrimEnd('\0');
  104. MachineNo = MachineNo.Substring(0, 20);
  105. }
  106. }
  107. Dictionary<string, object> Obj = new Dictionary<string, object>();
  108. MerchantQrCode query = MerchantQrCodeService.Query(" SnNo='" + SnNo + "'");
  109. Models.Main1.PosMachinesTwo pos = PosMachinesTwoService.Query(" PosSn='" + SnNo + "'");
  110. Models.Main1.PosMachines machines = PosMachinesService.Query(" PosSn='" + SnNo + "'");
  111. MerchantInfo merchant = MerchantInfoService.Query(MerchantId);
  112. if (pos.BindingState == 1)
  113. {
  114. return new AppResultJson() { Status = "-1", Info = "此码已被绑定" };
  115. }
  116. if (machines.BindingState == 1)
  117. {
  118. return new AppResultJson() { Status = "-1", Info = "此码已被绑定" };
  119. }
  120. int PosId = 0;
  121. if (SnNo == MachineNo || string.IsNullOrEmpty(MachineNo))
  122. {
  123. if (pos.Id == 0)
  124. {
  125. return new AppResultJson() { Status = "-1", Info = "绑定失败" };
  126. }
  127. PosId = pos.Id;
  128. }
  129. else if (SnNo != MachineNo)
  130. {
  131. if (machines.Id == 0)
  132. {
  133. return new AppResultJson() { Status = "-1", Info = "绑定失败" };
  134. }
  135. PosId = machines.Id;
  136. }
  137. if (query.Id > 0)
  138. {
  139. if (query.MerchantId > 0)
  140. {
  141. return new AppResultJson() { Status = "-1", Info = "此二维码已被绑定,请勿重复绑定" };
  142. }
  143. }
  144. Dictionary<string, object> fields = new Dictionary<string, object>();
  145. if (SnNo == MachineNo || string.IsNullOrEmpty(MachineNo))
  146. {
  147. fields.Add("MerchantId", MerchantId);
  148. fields.Add("SnNo", SnNo);
  149. fields.Add("QueryCount", 2);
  150. MerchantQrCodeService.Add(fields);
  151. }
  152. else
  153. {
  154. string DataId = PosId + "_2";
  155. MachineForQrCode forQrCode = maindb.MachineForQrCode.FirstOrDefault(m => m.DataId == DataId);
  156. if (forQrCode == null)
  157. {
  158. forQrCode = maindb.MachineForQrCode.Add(new MachineForQrCode()
  159. {
  160. DataId = DataId,
  161. SnNo = SnNo,
  162. MachineSnNo = MachineNo,
  163. BindDate = DateTime.Now,
  164. MerchantId = MerchantId,
  165. }).Entity;
  166. }
  167. else
  168. {
  169. forQrCode.SnNo = SnNo;
  170. forQrCode.MachineSnNo = MachineNo;
  171. forQrCode.BindDate = DateTime.Now;
  172. forQrCode.MerchantId = MerchantId;
  173. }
  174. }
  175. maindb.SaveChanges();
  176. if (SnNo == MachineNo || string.IsNullOrEmpty(MachineNo))
  177. {
  178. fields = new Dictionary<string, object>();
  179. fields.Add("BuyUserId", merchant.UserId);
  180. fields.Add("UserId", merchant.UserId);
  181. fields.Add("BindingTime", DateTime.Now);
  182. fields.Add("BindingState", 1);
  183. fields.Add("BindMerchantId", MerchantId);
  184. fields.Add("OpId", 2);
  185. fields.Add("QueryCount", 2);
  186. PosMachinesTwoService.Edit(fields, pos.Id, false);
  187. }
  188. else if (SnNo != MachineNo && !string.IsNullOrEmpty(MachineNo))
  189. {
  190. //通过sn获取设备号
  191. // string result = AliIotFunction.Instance.IotDeviceQuery(MachineNo);
  192. // JsonData jsonObj = JsonMapper.ToObject(result);
  193. // if(jsonObj["alipay_commerce_iot_device_baseinfo_query_response"]["code"].ToString() == "10000")
  194. // {
  195. // string deviceId = jsonObj["alipay_commerce_iot_device_baseinfo_query_response"]["device_id"].ToString();
  196. MerchantAddInfo addinfo = MerchantAddInfoService.Query(MerchantId);
  197. if (machines.BrandId == 1)
  198. {
  199. //通过商户smid(好哒认证成功后台提供)绑定支付宝设备
  200. var result = AliIotFunction.Instance.IotBind(addinfo.AliMerchantId, MachineNo);
  201. JsonData jsonObj = JsonMapper.ToObject(result);
  202. if (jsonObj["alipay_merchant_indirect_iot_bind_response"]["code"].ToString() == "10000")
  203. {
  204. fields = new Dictionary<string, object>();
  205. fields.Add("BindingTime", DateTime.Now);
  206. fields.Add("BuyUserId", merchant.UserId);
  207. fields.Add("UserId", merchant.UserId);
  208. fields.Add("BindingState", 1);
  209. fields.Add("BindMerchantId", MerchantId);
  210. fields.Add("DeviceKind", "2");
  211. PosMachinesService.Edit(fields, machines.Id, false);
  212. //推送绑定数据到java
  213. // PosPushDataNewHelper.Bind(addinfo, pos);
  214. }
  215. }
  216. else
  217. {
  218. fields = new Dictionary<string, object>();
  219. fields.Add("BindingTime", DateTime.Now);
  220. fields.Add("BuyUserId", merchant.UserId);
  221. fields.Add("UserId", merchant.UserId);
  222. fields.Add("BindingState", 1);
  223. fields.Add("BindMerchantId", MerchantId);
  224. fields.Add("DeviceKind", "2");
  225. PosMachinesService.Edit(fields, machines.Id, false);
  226. //推送绑定数据到java
  227. // PosPushDataNewHelper.Bind(addinfo, pos);
  228. }
  229. // }
  230. }
  231. //码牌
  232. if (SnNo == MachineNo || string.IsNullOrEmpty(MachineNo))
  233. {
  234. PublicFunction.BindUserMachineData(maindb, merchant.UserId, 0, 1, SnNo);
  235. }
  236. //音响
  237. else
  238. {
  239. PublicFunction.BindUserMachineData(maindb, merchant.UserId, 1, 1, SnNo);
  240. }
  241. return new AppResultJson() { Status = "1", Info = "绑定成功" };
  242. }
  243. #endregion
  244. #region 商户-确认替换音箱码
  245. // [Authorize]
  246. public JsonResult ConfirmReplace(string value)
  247. {
  248. value = DesDecrypt(value);
  249. value = value.Replace("null", "\"\"");
  250. JsonData data = JsonMapper.ToObject(value);
  251. AppResultJson result = ConfirmReplaceDo(value);
  252. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  253. }
  254. public AppResultJson ConfirmReplaceDo(string value)
  255. {
  256. JsonData data = JsonMapper.ToObject(value);
  257. int MerchantId = int.Parse(function.CheckInt(data.getItem("MerchantId").ToString())); //商户
  258. int MachineId = int.Parse(function.CheckInt(data.getItem("MachineId").ToString())); //设备
  259. int QrCodeId = int.Parse(function.CheckInt(data.getItem("QrCodeId").ToString())); //收款码Id
  260. Dictionary<string, object> Obj = new Dictionary<string, object>();
  261. MerchantQrCode query = MerchantQrCodeService.Query(QrCodeId) ?? new MerchantQrCode();
  262. Models.Main1.PosMachines machine = PosMachinesDbconn.Instance.Get(MachineId) ?? new Models.Main1.PosMachines();
  263. query.MerchantId = MerchantId; //商户
  264. query.MachineId = MachineId; //设备
  265. MachineForQrCode qrcode = new MachineForQrCode()
  266. {
  267. MerchantId = MerchantId,
  268. DataId = QrCodeId + "_" + MachineId,
  269. BindDate = DateTime.Now,
  270. SnNo = query.SnNo,
  271. MachineSnNo = machine.PosSn,
  272. };
  273. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  274. }
  275. #endregion
  276. #region 商户-已绑收款码解除关联
  277. // [Authorize]
  278. public JsonResult Remove(string value)
  279. {
  280. value = DesDecrypt(value);
  281. value = value.Replace("null", "\"\"");
  282. JsonData data = JsonMapper.ToObject(value);
  283. AppResultJson result = RemoveDo(value);
  284. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  285. }
  286. public AppResultJson RemoveDo(string value)
  287. {
  288. JsonData data = JsonMapper.ToObject(value);
  289. int MerchantId = int.Parse(function.CheckInt(data.getItem("MerchantId").ToString())); //商户
  290. int Id = int.Parse(function.CheckInt(data.getItem("Id").ToString()));
  291. Dictionary<string, object> Obj = new Dictionary<string, object>();
  292. string CheckKey = Id + "_";
  293. var list = maindb.MachineForQrCode.Select(m => new { m.MerchantId, m.DataId }).Where(m => m.MerchantId == MerchantId && m.DataId.StartsWith(CheckKey)).ToList();
  294. foreach (var sub in list)
  295. {
  296. MachineForQrCode edit = maindb.MachineForQrCode.FirstOrDefault(m => m.DataId == sub.DataId);
  297. if (edit != null)
  298. {
  299. if (edit.SnNo != edit.MachineSnNo)
  300. {
  301. MerchantAddInfo addinfo = MerchantAddInfoService.Query(edit.MerchantId);
  302. Models.Main1.PosMachines machine = PosMachinesService.Query(" PosSn='" + edit.MachineSnNo + "'");
  303. string result = AliIotFunction.Instance.IotUnBind(addinfo.AliMerchantId, machine.PosSn);
  304. JsonData jsonObj = JsonMapper.ToObject(result);
  305. if (jsonObj["alipay_merchant_indirect_iot_unbind_response"]["code"].ToString() == "10000")
  306. {
  307. Dictionary<string, object> fields = new Dictionary<string, object>();
  308. fields.Add("BuyUserId", 0);
  309. fields.Add("UserId", 0);
  310. fields.Add("BindingTime", DateTime.Parse("1900-01-01"));
  311. fields.Add("BindingState", 0);
  312. fields.Add("BindMerchantId", 0);
  313. PosMachinesService.Edit(fields, machine.Id, false);
  314. }
  315. }
  316. maindb.MachineForQrCode.Remove(edit);
  317. int MachineId = int.Parse(function.CheckInt(edit.DataId.Split('_')[1]));
  318. MerchantQrCode qrCode = maindb.MerchantQrCode.FirstOrDefault(m => m.MerchantId == MachineId);
  319. if (qrCode != null)
  320. {
  321. qrCode.MerchantId = 0;
  322. }
  323. }
  324. }
  325. maindb.SaveChanges();
  326. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  327. }
  328. #endregion
  329. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  330. /// <summary>
  331. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  332. /// </summary>
  333. /// <param name="value">请求的参数(json字符串)</param>
  334. /// <param name="signField">要签名的字段</param>
  335. /// <returns></returns>
  336. private string CheckSign(string value, string[] signField)
  337. {
  338. JsonData json = JsonMapper.ToObject(value);
  339. Dictionary<string, string> dic = new Dictionary<string, string>();
  340. for (int i = 0; i < signField.Length; i++)
  341. {
  342. dic.Add(signField[i], json[signField[i]].ToString());
  343. }
  344. string sign = json["sign"].ToString(); //客户端签名字符串
  345. return new Sign().sign(dic, sign);
  346. }
  347. #endregion
  348. }
  349. }