MerchantQrCodeController.cs 14 KB

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