123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using Microsoft.AspNetCore.Authorization;
- using System.Web;
- using MySystem.Models.Main;
- using MySystem.Models.Main1;
- using LitJson;
- using Library;
- using MySystem.Service.Main;
- namespace MySystem.Areas.Api.Controllers.v1
- {
- [Area("Api")]
- [Route("Api/v1/[controller]/[action]")]
- public class MerchantQrCodeController : BaseController
- {
- public MerchantQrCodeController(IHttpContextAccessor accessor) : base(accessor)
- {
- }
- #region 商户-已绑二维码
- // [Authorize]
- public JsonResult List(string value)
- {
- value = DesDecrypt(value);
- JsonData data = JsonMapper.ToObject(value);
- List<Dictionary<string, object>> dataList = ListDo(value);
- return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
- }
- public List<Dictionary<string, object>> ListDo(string value)
- {
- JsonData data = JsonMapper.ToObject(value);
- int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
- int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
- int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
- List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
- Models.Main.MerchantInfo merchant = MerchantInfoDbconn.Instance.Get(MerchantId) ?? new Models.Main.MerchantInfo();
- IQueryable<Models.Main1.MerchantQrCode> query = main1db.MerchantQrCode.Where(m => m.MerchantId == MerchantId && m.QueryCount == 2);
- if (PageNum == 1)
- {
- query = query.Take(PageSize);
- }
- else
- {
- int skipNum = PageSize * (PageNum - 1);
- query = query.Skip(skipNum).Take(PageSize);
- }
- foreach (Models.Main1.MerchantQrCode subdata in query.ToList())
- {
- Dictionary<string, object> curData = new Dictionary<string, object>();
- curData.Add("SnNo", subdata.SnNo); //Sn编号
- curData.Add("Id", subdata.Id); //Id
- curData.Add("BindDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd")); //绑定时间
- curData.Add("MerchantName", merchant.Name); //商户名称
- Models.Main1.PosMachines machine = PosMachinesDbconn.Instance.Get(subdata.MachineId) ?? new Models.Main1.PosMachines();
- curData.Add("MachineSnNo", machine.PosSn); //音箱SN
- curData.Add("BindFlag", machine.BindingState); //绑定状态
- dataList.Add(curData);
- }
- return dataList;
- }
- #endregion
- #region 商户-绑定收款码
- // [Authorize]
- public JsonResult Bind(string value)
- {
- value = DesDecrypt(value);
- JsonData data = JsonMapper.ToObject(value);
- AppResultJson result = BindDo(value);
- return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
- }
- public AppResultJson BindDo(string value)
- {
- JsonData data = JsonMapper.ToObject(value);
- int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
- string SnNo = data["SnNo"].ToString(); //Sn编号
- string MachineNo = data["Machine"].ToString(); //音箱码
- if (SnNo.Length > 20)
- {
- SnNo = System.Web.HttpUtility.UrlDecode(SnNo);
- if (!SnNo.EndsWith("="))
- {
- SnNo += "=";
- }
- SnNo = dbconn.Decrypt3DES(SnNo, "l2k0b2#3");
- SnNo = SnNo.TrimEnd('\0');
- SnNo = SnNo.Substring(0, SnNo.Length - 8);
- }
- Dictionary<string, object> Obj = new Dictionary<string, object>();
- Models.Main1.MerchantQrCode query = MerchantQrCodeService.Query(" SnNo='" + SnNo + "'");
- Models.Main1.PosMachinesTwo pos = PosMachinesTwoService.Query(" PosSn='" + SnNo + "'");
- Models.Main.MerchantInfo merchant = MerchantInfoService.Query(MerchantId);
- if (pos.Id == 0)
- {
- return new AppResultJson() { Status = "-1", Info = "绑定失败" };
- }
- if (pos.BuyUserId == 0)
- {
- return new AppResultJson() { Status = "-1", Info = "码牌未授权" };
- }
- if (query.Id > 0)
- {
- if (query.MerchantId > 0)
- {
- return new AppResultJson() { Status = "-1", Info = "此二维码已被绑定,请勿重复绑定" };
- }
- }
- Dictionary<string, object> fields = new Dictionary<string, object>();
- fields.Add("MerchantId", MerchantId);
- fields.Add("SnNo", SnNo);
- fields.Add("QueryCount", 2);
- MerchantQrCodeService.Add(fields);
- string DataId = query.Id + "_0";
- Models.Main1.MachineForQrCode forQrCode = main1db.MachineForQrCode.FirstOrDefault(m => m.DataId == DataId);
- if (forQrCode == null)
- {
- forQrCode = main1db.MachineForQrCode.Add(new Models.Main1.MachineForQrCode()
- {
- DataId = DataId,
- SnNo = SnNo,
- MachineSnNo = MachineNo,
- BindDate = DateTime.Now,
- MerchantId = MerchantId,
- }).Entity;
- }
- else
- {
- forQrCode.SnNo = SnNo;
- forQrCode.MachineSnNo = MachineNo;
- forQrCode.BindDate = DateTime.Now;
- forQrCode.MerchantId = MerchantId;
- }
- main1db.SaveChanges();
- fields = new Dictionary<string, object>();
- fields.Add("BuyUserId", merchant.UserId);
- fields.Add("UserId", merchant.UserId);
- fields.Add("BindingTime", DateTime.Now);
- fields.Add("BindingState", 1);
- fields.Add("BindMerchantId", MerchantId);
- fields.Add("OpId", 2);
- fields.Add("QueryCount", 2);
- PosMachinesTwoService.Edit(fields, pos.Id, false);
- if(SnNo != MachineNo && !string.IsNullOrEmpty(MachineNo))
- {
- //通过sn获取设备号
- string result = AliIotFunction.Instance.IotDeviceQuery(MachineNo);
- JsonData jsonObj = JsonMapper.ToObject(result);
- if(jsonObj["alipay_commerce_iot_device_baseinfo_query_response"]["code"].ToString() == "10000")
- {
- string deviceId = jsonObj["alipay_commerce_iot_device_baseinfo_query_response"]["device_id"].ToString();
- Models.Main.MerchantAddInfo addinfo = MerchantAddInfoService.Query(MerchantId);
- //通过商户smid(好哒认证成功后台提供)绑定支付宝设备
- result = AliIotFunction.Instance.IotBind(addinfo.AliMerchantId, deviceId);
- jsonObj = JsonMapper.ToObject(result);
- if(jsonObj["alipay_merchant_indirect_iot_bind_response"]["code"].ToString() == "10000")
- {
- Models.Main1.PosMachines machine = PosMachinesService.Query(" PosSn='" + MachineNo + "'");
- fields = new Dictionary<string, object>();
- fields.Add("BuyUserId", merchant.UserId);
- fields.Add("UserId", merchant.UserId);
- fields.Add("BindingTime", DateTime.Now);
- fields.Add("BindingState", 1);
- fields.Add("BindMerchantId", MerchantId);
- fields.Add("DeviceName", deviceId);
- PosMachinesService.Edit(fields, machine.Id, false);
- }
- }
- }
- PublicFunction.BindUserMachineData(main1db, merchant.UserId, 0, 1, SnNo);
- return new AppResultJson() { Status = "1", Info = "绑定成功" };
- }
- #endregion
- #region 商户-确认替换音箱码
- // [Authorize]
- public JsonResult ConfirmReplace(string value)
- {
- value = DesDecrypt(value);
- JsonData data = JsonMapper.ToObject(value);
- AppResultJson result = ConfirmReplaceDo(value);
- return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
- }
- public AppResultJson ConfirmReplaceDo(string value)
- {
- JsonData data = JsonMapper.ToObject(value);
- int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
- int MachineId = int.Parse(function.CheckInt(data["MachineId"].ToString())); //设备
- int QrCodeId = int.Parse(function.CheckInt(data["QrCodeId"].ToString())); //收款码Id
- Dictionary<string, object> Obj = new Dictionary<string, object>();
- Models.Main1.MerchantQrCode query = MerchantQrCodeService.Query(QrCodeId) ?? new Models.Main1.MerchantQrCode();
- Models.Main1.PosMachines machine = PosMachinesDbconn.Instance.Get(MachineId) ?? new Models.Main1.PosMachines();
- query.MerchantId = MerchantId; //商户
- query.MachineId = MachineId; //设备
- Models.Main1.MachineForQrCode qrcode = new Models.Main1.MachineForQrCode()
- {
- MerchantId = MerchantId,
- DataId = QrCodeId + "_" + MachineId,
- BindDate = DateTime.Now,
- SnNo = query.SnNo,
- MachineSnNo = machine.PosSn,
- };
- return new AppResultJson() { Status = "1", Info = "", Data = Obj };
- }
- #endregion
- #region 商户-已绑收款码解除关联
- // [Authorize]
- public JsonResult Remove(string value)
- {
- value = DesDecrypt(value);
- JsonData data = JsonMapper.ToObject(value);
- AppResultJson result = RemoveDo(value);
- return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
- }
- public AppResultJson RemoveDo(string value)
- {
- JsonData data = JsonMapper.ToObject(value);
- int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
- int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
- Dictionary<string, object> Obj = new Dictionary<string, object>();
- string CheckKey = Id + "_";
- var list = main1db.MachineForQrCode.Select(m => new { m.MerchantId, m.DataId }).Where(m => m.MerchantId == MerchantId && m.DataId.StartsWith(CheckKey)).ToList();
- foreach (var sub in list)
- {
- Models.Main1.MachineForQrCode edit = main1db.MachineForQrCode.FirstOrDefault(m => m.DataId == sub.DataId);
- if (edit != null)
- {
- if(edit.SnNo != edit.MachineSnNo)
- {
- Models.Main.MerchantAddInfo addinfo = MerchantAddInfoService.Query(edit.MerchantId);
- Models.Main1.PosMachines machine = PosMachinesService.Query(" PosSn='" + edit.MachineSnNo + "'");
- string result = AliIotFunction.Instance.IotUnBind(addinfo.AliMerchantId, machine.DeviceName);
- JsonData jsonObj = JsonMapper.ToObject(result);
- if(jsonObj["alipay_merchant_indirect_iot_unbind_response"]["code"].ToString() == "10000")
- {
- Dictionary<string, object> fields = new Dictionary<string, object>();
- fields.Add("BuyUserId", 0);
- fields.Add("UserId", 0);
- fields.Add("BindingTime", DateTime.Parse("1900-01-01"));
- fields.Add("BindingState", 0);
- fields.Add("BindMerchantId", 0);
- PosMachinesService.Edit(fields, machine.Id, false);
- }
- }
- main1db.MachineForQrCode.Remove(edit);
- int MachineId = int.Parse(function.CheckInt(edit.DataId.Split('_')[1]));
- Models.Main1.MerchantQrCode qrCode = main1db.MerchantQrCode.FirstOrDefault(m => m.MerchantId == MachineId);
- if (qrCode != null)
- {
- qrCode.MerchantId = 0;
- }
- }
- }
- main1db.SaveChanges();
- return new AppResultJson() { Status = "1", Info = "", Data = Obj };
- }
- #endregion
- #region 检查签名是否合法,合法返回1,不合法返回提示信息
- /// <summary>
- /// 检查签名是否合法,合法返回1,不合法返回提示信息
- /// </summary>
- /// <param name="value">请求的参数(json字符串)</param>
- /// <param name="signField">要签名的字段</param>
- /// <returns></returns>
- private string CheckSign(string value, string[] signField)
- {
- JsonData json = JsonMapper.ToObject(value);
- Dictionary<string, string> dic = new Dictionary<string, string>();
- for (int i = 0; i < signField.Length; i++)
- {
- dic.Add(signField[i], json[signField[i]].ToString());
- }
- string sign = json["sign"].ToString(); //客户端签名字符串
- return new Sign().sign(dic, sign);
- }
- #endregion
- }
- }
|