MerchantQrCodeController.cs 14 KB

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