ConsumerOrdersController.cs 17 KB

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