ConsumerOrdersController.cs 18 KB

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