ConsumersController.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.Extensions.Logging;
  8. using Microsoft.Extensions.Options;
  9. using Microsoft.AspNetCore.Authorization;
  10. using MySystem.MainModels;
  11. using LitJson;
  12. using Library;
  13. using System.Security.Cryptography;
  14. using System.Text;
  15. namespace MySystem.Areas.Api.Controllers.v1
  16. {
  17. [Area("Api")]
  18. [Route("/v1/QrCodePlateMain/[controller]/[action]")]
  19. public class ConsumersController : BaseController
  20. {
  21. public ConsumersController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  22. {
  23. }
  24. #region 首页-首页-个人信息-返现金额
  25. // [Authorize]
  26. public JsonResult AmountInfo(string value)
  27. {
  28. value = DesDecrypt(value);
  29. JsonData data = JsonMapper.ToObject(value);
  30. Dictionary<string, object> Obj = AmountInfoDo(value);
  31. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  32. }
  33. public Dictionary<string, object> AmountInfoDo(string value)
  34. {
  35. JsonData data = JsonMapper.ToObject(value);
  36. string OpenId = data["OpenId"].ToString(); //微信或支付宝Id
  37. int Kind = int.Parse(data["Kind"].ToString()); //类型(1 支付宝 2 微信)
  38. if (Kind == 1)
  39. {
  40. OpenId = AlipayMiniOpenIdDo(OpenId);
  41. }
  42. if (Kind == 2)
  43. {
  44. OpenId = WeChatMiniOpenIdDo(OpenId);
  45. }
  46. Dictionary<string, object> Obj = new Dictionary<string, object>();
  47. var HeadPhoto = "";
  48. var NickName = "";
  49. var TotalAmount = 0.00M;
  50. var Amount = 0.00M;
  51. var Amount2 = 0.00M;
  52. var consumer = Services.Main.ConsumersService.Query(" and WechatOpenId='" + OpenId + "'");
  53. Amount = Services.Main.ConsumerProfitService.Sum(" and ConsumerId=" + consumer.Id + "", "GetMoney");
  54. Amount2 = Services.Main2.ConsumerProfitService.Sum(" and ConsumerId=" + consumer.Id + "", "GetMoney");
  55. TotalAmount = Amount + Amount2;
  56. if (consumer.Id > 0)
  57. {
  58. HeadPhoto = consumer.HeadPhoto;
  59. NickName = consumer.NickName;
  60. }
  61. var token = PublicFunction.AppToken(consumer.Id,JwtSecret,JwtIss);
  62. Obj.Add("Amount", TotalAmount); //累计返现金额
  63. Obj.Add("HeadPhoto", HeadPhoto); //头像
  64. Obj.Add("NickName", NickName); //昵称
  65. Obj.Add("Token", token); //token
  66. Obj.Add("ConsumerId", consumer.Id); //用户Id
  67. return Obj;
  68. }
  69. #endregion
  70. #region 首页-首页-个人信息-修改个人信息
  71. [Authorize]
  72. public JsonResult EditPersonalInfo(string value)
  73. {
  74. value = DesDecrypt(value);
  75. JsonData data = JsonMapper.ToObject(value);
  76. AppResultJson result = EditPersonalInfoDo(value);
  77. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  78. }
  79. private AppResultJson EditPersonalInfoDo(string value)
  80. {
  81. JsonData data = JsonMapper.ToObject(value);
  82. int ConsumerId = int.Parse(function.CheckInt(data["ConsumerId"].ToString())); //用户Id
  83. string HeadPhoto = data["HeadPhoto"].ToString(); //头像
  84. string NickName = data["NickName"].ToString(); //昵称
  85. Dictionary<string, object> Obj = new Dictionary<string, object>();
  86. var consumer = maindb.Consumers.FirstOrDefault(m => m.Id == ConsumerId) ?? new Consumers();
  87. if (consumer.Id > 0)
  88. {
  89. if (!string.IsNullOrEmpty(HeadPhoto))
  90. {
  91. consumer.HeadPhoto = HeadPhoto;
  92. }
  93. if (!string.IsNullOrEmpty(NickName))
  94. {
  95. consumer.NickName = NickName;
  96. }
  97. }
  98. maindb.SaveChanges();
  99. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  100. }
  101. #endregion
  102. #region 消费者-微信小程序通过code获取openid
  103. public string WeChatMiniOpenIdDo(string code)
  104. {
  105. string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + new WeChatFunction().AppId + "&secret=" + new WeChatFunction().AppSecret + "&js_code=" + code + "&grant_type=authorization_code";
  106. function.WriteLog(url, "微信小程序通过code获取openid");
  107. string result = function.GetWebRequest(url);
  108. function.WriteLog(result + "\n", "微信小程序通过code获取openid");
  109. JsonData jsonObj = JsonMapper.ToObject(result);
  110. string openid = jsonObj["openid"].ToString();
  111. return openid;
  112. }
  113. public Dictionary<string, string> getPhoneNumber(string encryptedData, string iv, string session_key)
  114. {
  115. Dictionary<string, string> Obj = new Dictionary<string, string>();
  116. try
  117. {
  118. byte[] encryData = Convert.FromBase64String(encryptedData);
  119. RijndaelManaged rijndaelCipher = new RijndaelManaged();
  120. rijndaelCipher.Key = Convert.FromBase64String(session_key);
  121. rijndaelCipher.IV = Convert.FromBase64String(iv);
  122. rijndaelCipher.Mode = CipherMode.CBC;
  123. rijndaelCipher.Padding = PaddingMode.PKCS7;
  124. ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
  125. byte[] plainText = transform.TransformFinalBlock(encryData, 0, encryData.Length);
  126. string result = Encoding.Default.GetString(plainText);
  127. dynamic model = Newtonsoft.Json.Linq.JToken.Parse(result) as dynamic;
  128. string phoneNumber = model.phoneNumber;
  129. //return model.phoneNumber;
  130. if (string.IsNullOrEmpty(phoneNumber))
  131. {
  132. phoneNumber = "";
  133. }
  134. Obj.Add("Mobile", phoneNumber);
  135. }
  136. catch (Exception ex)
  137. {
  138. Obj.Add("Mobile", "err");
  139. function.WriteLog(DateTime.Now.ToString(), "微信小程序获取手机号异常");
  140. function.WriteLog(encryptedData, "微信小程序获取手机号异常");
  141. function.WriteLog(iv, "微信小程序获取手机号异常");
  142. function.WriteLog(session_key, "微信小程序获取手机号异常");
  143. function.WriteLog(ex.ToString(), "微信小程序获取手机号异常");
  144. function.WriteLog("\r\n\r\n", "微信小程序获取手机号异常");
  145. }
  146. return Obj;
  147. }
  148. public string filterEmoji(string str)
  149. {
  150. string origin = str;
  151. try
  152. {
  153. //关键代码
  154. foreach (var a in str)
  155. {
  156. byte[] bts = System.Text.Encoding.UTF32.GetBytes(a.ToString());
  157. if (bts[0].ToString() == "253" && bts[1].ToString() == "255")
  158. {
  159. str = str.Replace(a.ToString(), "");
  160. }
  161. }
  162. }
  163. catch
  164. {
  165. str = origin;
  166. }
  167. return str;
  168. }
  169. #endregion
  170. #region 消费者-支付宝通过code获取openid
  171. public string AlipayMiniOpenIdDo(string code)
  172. {
  173. string openid = new AlipayFunction(_accessor.HttpContext).GetAlipayUserId(code);
  174. return openid;
  175. }
  176. #endregion
  177. }
  178. }