MerchantQrCodeController.cs 17 KB

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