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; using System.Data; namespace MySystem.Areas.Api.Controllers.v1 { [Area("Api")] [Route("Api/v1/[controller]/[action]")] public class ConsumerOrdersController : BaseController { public ConsumerOrdersController(IHttpContextAccessor accessor, ILogger logger, IOptions setting) : base(accessor, logger, setting) { } #region 商户-经营数据(日) [Authorize] public JsonResult ByDate(string value) { value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); List> dataList = ByDateDo(value); return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList }); } public List> ByDateDo(string value) { JsonData data = JsonMapper.ToObject(value); int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户 string Month = data["Month"].ToString(); //月份 int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString())); int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString())); List> dataList = new List>(); DataTable dt = MerchantAmountSummary.Instance.GetTradeListByDate(MerchantId, Month, PageNum, PageSize); foreach (DataRow dr in dt.Rows) { Dictionary curData = new Dictionary(); curData.Add("Date", dr["TradeDate"].ToString()); //日期 curData.Add("Amount", dr[2].ToString()); //实收金额 curData.Add("WeChatAmount", dr[0].ToString()); //微信实收 curData.Add("AlipayAmount", dr[1].ToString()); //微信实收 curData.Add("OrderCount", dr[3].ToString()); //订单数 curData.Add("AddCount", 0); //新增会员 dataList.Add(curData); } return dataList; } #endregion #region 商户-经营数据(月) [Authorize] public JsonResult ByMonth(string value) { value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); List> dataList = ByMonthDo(value); return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList }); } public List> ByMonthDo(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>(); DataTable dt = MerchantAmountSummary.Instance.GetTradeListByMonth(MerchantId, PageNum, PageSize); foreach (DataRow dr in dt.Rows) { Dictionary curData = new Dictionary(); curData.Add("Date", dr["TradeMonth"].ToString()); //日期 curData.Add("Amount", dr[2].ToString()); //实收金额 curData.Add("WeChatAmount", dr[0].ToString()); //微信实收 curData.Add("AlipayAmount", dr[1].ToString()); //微信实收 curData.Add("OrderCount", dr[3].ToString()); //订单数 curData.Add("AddCount", 0); //新增会员 dataList.Add(curData); } return dataList; } #endregion #region 商户-订单列表 [Authorize] public JsonResult ForMer(string value) { value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); List> dataList = ForMerDo(value); return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList }); } public List> ForMerDo(string value) { JsonData data = JsonMapper.ToObject(value); int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户 string OrderNo = data["OrderNo"].ToString(); //订单号 string Date = data["Date"].ToString(); //日期 int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString())); int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString())); List> dataList = new List>(); string condition = ""; if (!string.IsNullOrEmpty(data["MerchantId"].ToString())) { condition += " and MerchantId=" + MerchantId; } if (!string.IsNullOrEmpty(data["OrderNo"].ToString())) { condition += " and OrderNo='" + OrderNo + "'"; } List query = new ConsumerOrdersService().List(new List(), condition, PageNum, PageSize); foreach (var subdata in query) { Dictionary curData = new Dictionary(); curData.Add("OrderNo", subdata.OrderNo); //订单号 curData.Add("PayMode", subdata.PayMode); //支付方式 curData.Add("PayMoney", subdata.PayMoney); //支付金额 curData.Add("ReturnMoney", subdata.ReturnMoney); //已返金额 curData.Add("SnNo", subdata.SnNo); //SN号 curData.Add("Id", subdata.Id); //Id curData.Add("Status", subdata.Status); //Status curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate curData.Add("MerchantName", ""); //门店信息 dataList.Add(curData); } return dataList; } #endregion #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 ConsumerId = int.Parse(function.CheckInt(data["ConsumerId"].ToString())); //消费者 int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString())); int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString())); List> dataList = new List>(); List query = new ConsumerOrdersService().List(new List(), " and ConsumerId=" + ConsumerId, PageNum, PageSize); foreach (ConsumerOrders subdata in query) { MerchantInfo merchant = MerchantInfoDbconn.Instance.Get(subdata.MerchantId) ?? new MerchantInfo(); Dictionary curData = new Dictionary(); curData.Add("OrderNo", subdata.OrderNo); //订单号 curData.Add("PayMoney", subdata.PayMoney); //支付金额 curData.Add("ReturnMoney", subdata.ReturnMoney); //已返金额 curData.Add("Id", subdata.Id); //Id curData.Add("Status", subdata.Status); //Status curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate curData.Add("MerchantName", merchant.Name); //商户名称 dataList.Add(curData); } return dataList; } #endregion #region 消费者-支付接口 [Authorize] public JsonResult Pay(string value) { if (string.IsNullOrEmpty(value)) { System.IO.StreamReader sr = new System.IO.StreamReader(Request.Body); value = sr.ReadToEnd(); value = value.Split('=')[1]; } value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); AppResultJson result = PayDo(value); return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data }); } public AppResultJson PayDo(string value) { JsonData data = JsonMapper.ToObject(value); string SnNo = data["Sn"].ToString(); //商户 string Machine = data["Machine"].ToString(); int PayMode = int.Parse(function.CheckInt(data["PayMode"].ToString())); //支付方式 decimal PayMoney = decimal.Parse(function.CheckNum(data["PayMoney"].ToString())); //支付金额 string Code = data["Code"].ToString(); if (string.IsNullOrEmpty(data["PayMode"].ToString())) { return new AppResultJson() { Status = "-1", Info = "请填写支付方式" }; } if (string.IsNullOrEmpty(data["PayMoney"].ToString())) { return new AppResultJson() { Status = "-1", Info = "请填写支付金额" }; } if (!function.IsNum(data["PayMoney"].ToString())) { return new AppResultJson() { Status = "-1", Info = "请填写正确的支付金额" }; } // PublicAccountSet set = RedisDbconn.Instance.Get("PublicAccountSet") ?? new PublicAccountSet(); string openid = ""; if (PayMode == 1) { openid = new AlipayFunction(_accessor.HttpContext).GetAlipayUserId(Code); if (openid.Contains("|")) { openid = openid.Split('|')[0]; } } else { PublicAccountSet set = new PublicAccountSet() { WeChatMiniAppId = "wx5417e0770bb19c4e", WeChatMiniAppSecret = "b853caabd367e1f3fd729c259ac8bee6", }; string result = function.GetWebRequest("https://api.weixin.qq.com/sns/jscode2session?appid=" + set.WeChatMiniAppId + "&secret=" + set.WeChatMiniAppSecret + "&js_code=" + Code + "&grant_type=authorization_code"); JsonData jsonObj = JsonMapper.ToObject(result); openid = jsonObj["openid"].ToString(); } int ConsumerId = 0; ConsumerOpenIds check = ConsumerOpenIdsDbconn.Instance.Get(openid); if (check == null) { // ConsumerId = PublicFunction.MakeConsumerId(); Consumers consumer = new Consumers() { Id = ConsumerId, CreateDate = DateTime.Now, WechatOpenId = openid, }; check = new ConsumerOpenIds() { OpenId = openid, ConsumerId = ConsumerId, }; } else { ConsumerId = check.ConsumerId; } MerchantQrCode qrcode = MerchantQrCodeDbconn.Instance.Get(SnNo) ?? new MerchantQrCode(); // PosMachines machine = PosMachinesDbconn.Instance.Get(qrcode.SnId) ?? new PosMachines(); // if (machine.BindMerchantId == 0) machine.BindMerchantId = 1; // TODO: 需要绑定二维码 MerchantInfo merchant = MerchantInfoDbconn.Instance.Get(qrcode.MerchantId) ?? new MerchantInfo(); MerchantAddInfo merchantAdd = MerchantAddInfoDbconn.Instance.Get(qrcode.MerchantId) ?? new MerchantAddInfo(); MerchantParamSet merchantset = MerchantParamSetDbconn.Instance.Get(qrcode.MerchantId) ?? new MerchantParamSet(); Dictionary Obj = new Dictionary(); string OrderNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8); ConsumerOrders query = new ConsumerOrders() { // Id = PublicFunction.MakeConsumerOrderId(), CreateDate = DateTime.Now, //创建时间 MerchantId = qrcode.MerchantId, //商户 ConsumerId = ConsumerId, //消费者 PayMode = PayMode, //支付方式 PayMoney = PayMoney, //支付金额 SnNo = SnNo, //SN号 OrderNo = OrderNo, MaxDivi = PayMoney * merchantset.DiviPercent / 100, IsAct = merchantset.IsAll == 1 ? 0u : 1u, SeoDescription = Newtonsoft.Json.JsonConvert.SerializeObject(merchantset), }; if (PayMode == 1) { string backString = new AlipayFunction(_accessor.HttpContext).CreateTrade(OrderNo, merchant.Name, PayMoney.ToString("f2"), openid, SourceHost + "/api/alipay/notice", "202203BB9929042f42af4f0395c0ad8c6b056A03"); JsonData obj = JsonMapper.ToObject(backString); if (obj["alipay_trade_create_response"]["code"].ToString() == "10000") { string tradeNo = obj["alipay_trade_create_response"]["trade_no"].ToString(); Obj.Add("respCode", obj["alipay_trade_create_response"]["code"].ToString()); Obj.Add("tradeNo", tradeNo); } else { Obj.Add("respCode", obj["alipay_trade_create_response"]["code"].ToString()); Obj.Add("tradeNo", ""); } } else if (PayMode == 2) { string AppId = "wx5417e0770bb19c4e"; string MchId = "1611167423"; string SubMchId = merchantAdd.SubMchid; string Description = merchant.Name; string OpenId = openid; string Key = "VyULlqfAW2gBfCNfLdupcL7zlha7d93F"; string NotifyUrl = SourceHost + "/api/wechat/notice"; Dictionary dic = new WeChatFunction(_accessor.HttpContext).Pay(AppId, MchId, SubMchId, PayMoney, OrderNo, Description, OpenId, Key, NotifyUrl); Obj.Add("appId", dic["appId"]); //微信小程序appid Obj.Add("timeStamp", dic["timeStamp"]); //时间戳 Obj.Add("nonceStr", dic["nonceStr"]); //随机字符串 Obj.Add("package", dic["package"]); //统一支付接口返回的prepayid参数值 Obj.Add("paySign", dic["paySign"]); //支付签名 Obj.Add("ConsumerId", ConsumerId); } return new AppResultJson() { Status = "1", Info = "", Data = Obj }; } #endregion public string test() { return new WeChatFunction(_accessor.HttpContext).GetPlatCert(); } public string test2() { return new WeChatFunction(_accessor.HttpContext).GetPublicKey(); } #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 } }