MerchantQrCodeController.cs 14 KB

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