ConsumerOrdersController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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.MainModels;
  11. using LitJson;
  12. using Library;
  13. namespace MySystem.Areas.Api.Controllers.v1
  14. {
  15. [Area("Api")]
  16. [Route("Api/v1/[controller]/[action]")]
  17. public class ConsumerOrdersController : BaseController
  18. {
  19. public ConsumerOrdersController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  20. {
  21. }
  22. #region 商户-经营数据(日)
  23. [Authorize]
  24. public JsonResult ByDate(string value)
  25. {
  26. value = DesDecrypt(value);
  27. JsonData data = JsonMapper.ToObject(value);
  28. List<Dictionary<string, object>> dataList = ByDateDo(value);
  29. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  30. }
  31. public List<Dictionary<string, object>> ByDateDo(string value)
  32. {
  33. JsonData data = JsonMapper.ToObject(value);
  34. int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
  35. string Month = data["Month"].ToString(); //月份
  36. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  37. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  38. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  39. List<string> Dates = RedisDbconn.Instance.GetList<string>("ConsumerStat:" + MerchantId + ":" + Month, PageNum, PageSize);
  40. foreach (string Date in Dates)
  41. {
  42. string DateString = Date.Replace("-", "");
  43. Dictionary<string, object> curData = new Dictionary<string, object>();
  44. curData.Add("Date", Date); //日期
  45. MerchantIndexStat MonthIndexStat = RedisDbconn.Instance.Get<MerchantIndexStat>("MerchantIndexStat:" + MerchantId + ":" + DateString) ?? new MerchantIndexStat();
  46. curData.Add("Amount", MonthIndexStat.TotalActual); //实收金额
  47. curData.Add("WeChatAmount", MonthIndexStat.WeChatTotalActual); //微信实收
  48. curData.Add("AlipayAmount", MonthIndexStat.AlipayTotalActual); //微信实收
  49. curData.Add("OrderCount", MonthIndexStat.TotalOrder); //订单数
  50. curData.Add("AddCount", MonthIndexStat.TotalUser); //新增会员
  51. dataList.Add(curData);
  52. }
  53. return dataList;
  54. }
  55. #endregion
  56. #region 商户-经营数据(月)
  57. [Authorize]
  58. public JsonResult ByMonth(string value)
  59. {
  60. value = DesDecrypt(value);
  61. JsonData data = JsonMapper.ToObject(value);
  62. List<Dictionary<string, object>> dataList = ByMonthDo(value);
  63. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  64. }
  65. public List<Dictionary<string, object>> ByMonthDo(string value)
  66. {
  67. JsonData data = JsonMapper.ToObject(value);
  68. int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
  69. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  70. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  71. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  72. List<string> Months = RedisDbconn.Instance.GetList<string>("ConsumerStat:" + MerchantId, PageNum, PageSize);
  73. foreach (string Month in Months)
  74. {
  75. string MonthString = Month.Replace("-", "");
  76. Dictionary<string, object> curData = new Dictionary<string, object>();
  77. curData.Add("Month", Month); //日期
  78. MerchantIndexStat MonthIndexStat = RedisDbconn.Instance.Get<MerchantIndexStat>("MerchantIndexStat:" + MerchantId + ":" + MonthString) ?? new MerchantIndexStat();
  79. curData.Add("Amount", MonthIndexStat.TotalActual); //实收金额
  80. curData.Add("WeChatAmount", MonthIndexStat.WeChatTotalActual); //微信实收
  81. curData.Add("AlipayAmount", MonthIndexStat.AlipayTotalActual); //微信实收
  82. curData.Add("OrderCount", MonthIndexStat.TotalOrder); //订单数
  83. curData.Add("AddCount", MonthIndexStat.TotalUser); //新增会员
  84. dataList.Add(curData);
  85. }
  86. return dataList;
  87. }
  88. #endregion
  89. #region 商户-订单列表
  90. [Authorize]
  91. public JsonResult ForMer(string value)
  92. {
  93. value = DesDecrypt(value);
  94. JsonData data = JsonMapper.ToObject(value);
  95. List<Dictionary<string, object>> dataList = ForMerDo(value);
  96. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  97. }
  98. public List<Dictionary<string, object>> ForMerDo(string value)
  99. {
  100. JsonData data = JsonMapper.ToObject(value);
  101. int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户
  102. string OrderNo = data["OrderNo"].ToString(); //订单号
  103. string Date = data["Date"].ToString(); //日期
  104. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  105. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  106. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  107. string condition = "";
  108. if (!string.IsNullOrEmpty(data["MerchantId"].ToString()))
  109. {
  110. condition += " and MerchantId=" + MerchantId;
  111. }
  112. if (!string.IsNullOrEmpty(data["OrderNo"].ToString()))
  113. {
  114. condition += " and OrderNo='" + OrderNo + "'";
  115. }
  116. List<ConsumerOrders> query = new ConsumerOrdersService().List(new List<FieldItem>(), condition, PageNum, PageSize);
  117. foreach (var subdata in query)
  118. {
  119. Dictionary<string, object> curData = new Dictionary<string, object>();
  120. curData.Add("OrderNo", subdata.OrderNo); //订单号
  121. curData.Add("PayMode", subdata.PayMode); //支付方式
  122. curData.Add("PayMoney", subdata.PayMoney); //支付金额
  123. curData.Add("ReturnMoney", subdata.ReturnMoney); //已返金额
  124. curData.Add("SnNo", subdata.SnNo); //SN号
  125. curData.Add("Id", subdata.Id); //Id
  126. curData.Add("Status", subdata.Status); //Status
  127. curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate
  128. curData.Add("MerchantName", ""); //门店信息
  129. dataList.Add(curData);
  130. }
  131. return dataList;
  132. }
  133. #endregion
  134. #region 消费者-我的订单
  135. [Authorize]
  136. public JsonResult List(string value)
  137. {
  138. value = DesDecrypt(value);
  139. JsonData data = JsonMapper.ToObject(value);
  140. List<Dictionary<string, object>> dataList = ListDo(value);
  141. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  142. }
  143. public List<Dictionary<string, object>> ListDo(string value)
  144. {
  145. JsonData data = JsonMapper.ToObject(value);
  146. int ConsumerId = int.Parse(function.CheckInt(data["ConsumerId"].ToString())); //消费者
  147. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  148. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  149. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  150. List<ConsumerOrders> query = new ConsumerOrdersService().List(new List<FieldItem>(), " and ConsumerId=" + ConsumerId, PageNum, PageSize);
  151. foreach (ConsumerOrders subdata in query)
  152. {
  153. MerchantInfo merchant = MerchantInfoDbconn.Instance.Get(subdata.MerchantId) ?? new MerchantInfo();
  154. Dictionary<string, object> curData = new Dictionary<string, object>();
  155. curData.Add("OrderNo", subdata.OrderNo); //订单号
  156. curData.Add("PayMoney", subdata.PayMoney); //支付金额
  157. curData.Add("ReturnMoney", subdata.ReturnMoney); //已返金额
  158. curData.Add("Id", subdata.Id); //Id
  159. curData.Add("Status", subdata.Status); //Status
  160. curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate
  161. curData.Add("MerchantName", merchant.Name); //商户名称
  162. dataList.Add(curData);
  163. }
  164. return dataList;
  165. }
  166. #endregion
  167. #region 消费者-支付接口
  168. [Authorize]
  169. public JsonResult Pay(string value)
  170. {
  171. if (string.IsNullOrEmpty(value))
  172. {
  173. System.IO.StreamReader sr = new System.IO.StreamReader(Request.Body);
  174. value = sr.ReadToEnd();
  175. value = value.Split('=')[1];
  176. }
  177. value = DesDecrypt(value);
  178. JsonData data = JsonMapper.ToObject(value);
  179. AppResultJson result = PayDo(value);
  180. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  181. }
  182. public AppResultJson PayDo(string value)
  183. {
  184. JsonData data = JsonMapper.ToObject(value);
  185. string SnNo = data["Sn"].ToString(); //商户
  186. string Machine = data["Machine"].ToString();
  187. int PayMode = int.Parse(function.CheckInt(data["PayMode"].ToString())); //支付方式
  188. decimal PayMoney = decimal.Parse(function.CheckNum(data["PayMoney"].ToString())); //支付金额
  189. string Code = data["Code"].ToString();
  190. if (string.IsNullOrEmpty(data["PayMode"].ToString()))
  191. {
  192. return new AppResultJson() { Status = "-1", Info = "请填写支付方式" };
  193. }
  194. if (string.IsNullOrEmpty(data["PayMoney"].ToString()))
  195. {
  196. return new AppResultJson() { Status = "-1", Info = "请填写支付金额" };
  197. }
  198. if (!function.IsNum(data["PayMoney"].ToString()))
  199. {
  200. return new AppResultJson() { Status = "-1", Info = "请填写正确的支付金额" };
  201. }
  202. // PublicAccountSet set = RedisDbconn.Instance.Get<PublicAccountSet>("PublicAccountSet") ?? new PublicAccountSet();
  203. string openid = "";
  204. if (PayMode == 1)
  205. {
  206. openid = new AlipayFunction(_accessor.HttpContext).GetAlipayUserId(Code);
  207. if (openid.Contains("|"))
  208. {
  209. openid = openid.Split('|')[0];
  210. }
  211. }
  212. else
  213. {
  214. PublicAccountSet set = new PublicAccountSet()
  215. {
  216. WeChatMiniAppId = "wx5417e0770bb19c4e",
  217. WeChatMiniAppSecret = "b853caabd367e1f3fd729c259ac8bee6",
  218. };
  219. string result = function.GetWebRequest("https://api.weixin.qq.com/sns/jscode2session?appid=" + set.WeChatMiniAppId + "&secret=" + set.WeChatMiniAppSecret + "&js_code=" + Code + "&grant_type=authorization_code");
  220. JsonData jsonObj = JsonMapper.ToObject(result);
  221. openid = jsonObj["openid"].ToString();
  222. }
  223. int ConsumerId = 0;
  224. ConsumerOpenIds check = ConsumerOpenIdsDbconn.Instance.Get(openid);
  225. if (check == null)
  226. {
  227. // ConsumerId = PublicFunction.MakeConsumerId();
  228. Consumers consumer = new Consumers()
  229. {
  230. Id = ConsumerId,
  231. CreateDate = DateTime.Now,
  232. WechatOpenId = openid,
  233. };
  234. check = new ConsumerOpenIds()
  235. {
  236. OpenId = openid,
  237. ConsumerId = ConsumerId,
  238. };
  239. }
  240. else
  241. {
  242. ConsumerId = check.ConsumerId;
  243. }
  244. MerchantQrCode qrcode = MerchantQrCodeDbconn.Instance.Get(SnNo) ?? new MerchantQrCode();
  245. // PosMachines machine = PosMachinesDbconn.Instance.Get(qrcode.SnId) ?? new PosMachines();
  246. // if (machine.BindMerchantId == 0) machine.BindMerchantId = 1; // TODO: 需要绑定二维码
  247. MerchantInfo merchant = MerchantInfoDbconn.Instance.Get(qrcode.MerchantId) ?? new MerchantInfo();
  248. MerchantAddInfo merchantAdd = MerchantAddInfoDbconn.Instance.Get(qrcode.MerchantId) ?? new MerchantAddInfo();
  249. MerchantParamSet merchantset = MerchantParamSetDbconn.Instance.Get(qrcode.MerchantId) ?? new MerchantParamSet();
  250. Dictionary<string, object> Obj = new Dictionary<string, object>();
  251. string OrderNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  252. ConsumerOrders query = new ConsumerOrders()
  253. {
  254. // Id = PublicFunction.MakeConsumerOrderId(),
  255. CreateDate = DateTime.Now, //创建时间
  256. MerchantId = qrcode.MerchantId, //商户
  257. ConsumerId = ConsumerId, //消费者
  258. PayMode = PayMode, //支付方式
  259. PayMoney = PayMoney, //支付金额
  260. SnNo = SnNo, //SN号
  261. OrderNo = OrderNo,
  262. MaxDivi = PayMoney * merchantset.DiviPercent / 100,
  263. IsAct = merchantset.IsAll == 1 ? 0u : 1u,
  264. SeoDescription = Newtonsoft.Json.JsonConvert.SerializeObject(merchantset),
  265. };
  266. if (PayMode == 1)
  267. {
  268. string backString = new AlipayFunction(_accessor.HttpContext).CreateTrade(OrderNo, merchant.Name, PayMoney.ToString("f2"), openid, SourceHost + "/api/alipay/notice", "202203BB9929042f42af4f0395c0ad8c6b056A03");
  269. JsonData obj = JsonMapper.ToObject(backString);
  270. if (obj["alipay_trade_create_response"]["code"].ToString() == "10000")
  271. {
  272. string tradeNo = obj["alipay_trade_create_response"]["trade_no"].ToString();
  273. Obj.Add("respCode", obj["alipay_trade_create_response"]["code"].ToString());
  274. Obj.Add("tradeNo", tradeNo);
  275. }
  276. else
  277. {
  278. Obj.Add("respCode", obj["alipay_trade_create_response"]["code"].ToString());
  279. Obj.Add("tradeNo", "");
  280. }
  281. }
  282. else if (PayMode == 2)
  283. {
  284. string AppId = "wx5417e0770bb19c4e";
  285. string MchId = "1611167423";
  286. string SubMchId = merchantAdd.SubMchid;
  287. string Description = merchant.Name;
  288. string OpenId = openid;
  289. string Key = "VyULlqfAW2gBfCNfLdupcL7zlha7d93F";
  290. string NotifyUrl = SourceHost + "/api/wechat/notice";
  291. Dictionary<string, string> dic = new WeChatFunction(_accessor.HttpContext).Pay(AppId, MchId, SubMchId, PayMoney, OrderNo, Description, OpenId, Key, NotifyUrl);
  292. Obj.Add("appId", dic["appId"]); //微信小程序appid
  293. Obj.Add("timeStamp", dic["timeStamp"]); //时间戳
  294. Obj.Add("nonceStr", dic["nonceStr"]); //随机字符串
  295. Obj.Add("package", dic["package"]); //统一支付接口返回的prepayid参数值
  296. Obj.Add("paySign", dic["paySign"]); //支付签名
  297. Obj.Add("ConsumerId", ConsumerId);
  298. }
  299. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  300. }
  301. #endregion
  302. public string test()
  303. {
  304. return new WeChatFunction(_accessor.HttpContext).GetPlatCert();
  305. }
  306. public string test2()
  307. {
  308. return new WeChatFunction(_accessor.HttpContext).GetPublicKey();
  309. }
  310. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  311. /// <summary>
  312. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  313. /// </summary>
  314. /// <param name="value">请求的参数(json字符串)</param>
  315. /// <param name="signField">要签名的字段</param>
  316. /// <returns></returns>
  317. private string CheckSign(string value, string[] signField)
  318. {
  319. JsonData json = JsonMapper.ToObject(value);
  320. Dictionary<string, string> dic = new Dictionary<string, string>();
  321. for (int i = 0; i < signField.Length; i++)
  322. {
  323. dic.Add(signField[i], json[signField[i]].ToString());
  324. }
  325. string sign = json["sign"].ToString(); //客户端签名字符串
  326. return new Sign().sign(dic, sign);
  327. }
  328. #endregion
  329. }
  330. }