ConsumerOrdersController.cs 18 KB

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