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.MainModels; using LitJson; using Library; namespace MySystem.Areas.Api.Controllers.v1 { [Area("Api")] [Route("Api/v1/[controller]/[action]")] public class MerchantQrCodeController : BaseController { public MerchantQrCodeController(IHttpContextAccessor accessor, ILogger logger, IOptions setting) : base(accessor, logger, setting) { } #region 商户-已绑二维码 [Authorize] public JsonResult List(string value) { value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); List> dataList = ListDo(value); return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList }); } public List> 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> dataList = new List>(); MerchantInfo merchant = MerchantInfoDbconn.Instance.Get(MerchantId) ?? new MerchantInfo(); List query = MerchantQrCodeDbconn.Instance.GetList(MerchantId, PageNum, PageSize); foreach (MerchantQrCode subdata in query) { Dictionary curData = new Dictionary(); 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); //商户名称 PosMachines machine = PosMachinesDbconn.Instance.Get(subdata.MachineId) ?? new 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) { function.WriteLog(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(); //音箱码 Dictionary Obj = new Dictionary(); MerchantQrCode query = new MerchantQrCodeService().Query(SnNo); if (query != null) { if (query.MerchantId > 0) { return new AppResultJson() { Status = "-1", Info = "此二维码已被绑定,请勿重复绑定" }; } } query = new MerchantQrCode(); // query.Id = PublicFunction.MakeMerchantQrCodeId(); query.CreateDate = DateTime.Now; //绑定时间 query.MerchantId = MerchantId; //商户 query.SnNo = SnNo; PosMachines machine = PosMachinesDbconn.Instance.Get(MachineNo); if (machine != null) { if (machine.BindMerchantId > 0) { return new AppResultJson() { Status = "-1", Info = "音箱码已被绑定,请勿重复绑定" }; } return new AppResultJson() { Status = "1", Info = "绑定成功,收款码暂未关联音箱", Data = Obj }; } else if (!string.IsNullOrEmpty(MachineNo)) { machine = new PosMachines(); // machine.Id = PublicFunction.MakeMachineId(); machine.PosSn = MachineNo; machine.BindMerchantId = MerchantId; machine.BindingState = 1; machine.BindingTime = DateTime.Now; string DataId = query.Id + "_" + machine.Id; MachineForQrCode forQrCode = MachineForQrCodeDbconn.Instance.Get(DataId); if (forQrCode == null) { forQrCode = new MachineForQrCode(); forQrCode.DataId = DataId; forQrCode.SnNo = SnNo; forQrCode.MachineSnNo = MachineNo; forQrCode.BindDate = DateTime.Now; forQrCode.MerchantId = MerchantId; } else { forQrCode.SnNo = SnNo; forQrCode.MachineSnNo = MachineNo; forQrCode.BindDate = DateTime.Now; forQrCode.MerchantId = MerchantId; } } return new AppResultJson() { Status = "2", Info = "", Data = Obj }; } #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 Obj = new Dictionary(); MerchantQrCode query = new MerchantQrCodeService().Query(QrCodeId) ?? new MerchantQrCode(); PosMachines machine = PosMachinesDbconn.Instance.Get(MachineId) ?? new PosMachines(); query.MerchantId = MerchantId; //商户 query.MachineId = MachineId; //设备 MachineForQrCode qrcode = new 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 Obj = new Dictionary(); string CheckKey = Id + "_"; var list = maindb.MachineForQrCode.Select(m => new { m.MerchantId, m.DataId }).Where(m => m.MerchantId == MerchantId && m.DataId.StartsWith(CheckKey)).ToList(); foreach (var sub in list) { MachineForQrCode edit = maindb.MachineForQrCode.FirstOrDefault(m => m.DataId == sub.DataId); if (edit != null) { maindb.MachineForQrCode.Remove(edit); int MachineId = int.Parse(function.CheckInt(edit.DataId.Split('_')[1])); MerchantQrCode qrCode = maindb.MerchantQrCode.FirstOrDefault(m => m.MerchantId == MachineId); if (qrCode != null) { qrCode.MerchantId = 0; } } } maindb.SaveChanges(); return new AppResultJson() { Status = "1", Info = "", Data = Obj }; } #endregion #region 检查签名是否合法,合法返回1,不合法返回提示信息 /// /// 检查签名是否合法,合法返回1,不合法返回提示信息 /// /// 请求的参数(json字符串) /// 要签名的字段 /// private string CheckSign(string value, string[] signField) { JsonData json = JsonMapper.ToObject(value); Dictionary dic = new Dictionary(); 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 } }