ConsumersController.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. return Obj;
  67. }
  68. #endregion
  69. #region 首页-首页-个人信息-修改个人信息
  70. [Authorize]
  71. public JsonResult EditPersonalInfo(string value)
  72. {
  73. value = DesDecrypt(value);
  74. JsonData data = JsonMapper.ToObject(value);
  75. AppResultJson result = EditPersonalInfoDo(value);
  76. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  77. }
  78. private AppResultJson EditPersonalInfoDo(string value)
  79. {
  80. JsonData data = JsonMapper.ToObject(value);
  81. string OpenId = data["OpenId"].ToString(); //微信或支付宝Id
  82. int Kind = int.Parse(data["Kind"].ToString()); //类型(1 支付宝 2 微信)
  83. string HeadPhoto = data["HeadPhoto"].ToString(); //头像
  84. string NickName = data["NickName"].ToString(); //昵称
  85. if (Kind == 1)
  86. {
  87. OpenId = AlipayMiniOpenIdDo(OpenId);
  88. }
  89. if (Kind == 2)
  90. {
  91. OpenId = AlipayMiniOpenIdDo(OpenId);
  92. }
  93. Dictionary<string, object> Obj = new Dictionary<string, object>();
  94. var consumer = maindb.Consumers.FirstOrDefault(m => m.WechatOpenId == OpenId) ?? new Consumers();
  95. if (string.IsNullOrEmpty(OpenId))
  96. {
  97. return new AppResultJson() { Status = "-1", Info = "微信或支付宝Id为空" };
  98. }
  99. if (consumer.Id > 0)
  100. {
  101. if (!string.IsNullOrEmpty(HeadPhoto))
  102. {
  103. consumer.HeadPhoto = HeadPhoto;
  104. }
  105. if (!string.IsNullOrEmpty(NickName))
  106. {
  107. consumer.NickName = NickName;
  108. }
  109. }
  110. maindb.SaveChanges();
  111. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  112. }
  113. #endregion
  114. #region 消费者-微信小程序通过code获取openid
  115. public string WeChatMiniOpenIdDo(string code)
  116. {
  117. string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + new WeChatFunction().AppId + "&secret=" + new WeChatFunction().AppSecret + "&js_code=" + code + "&grant_type=authorization_code";
  118. function.WriteLog(url, "微信小程序通过code获取openid");
  119. string result = function.GetWebRequest(url);
  120. function.WriteLog(result + "\n", "微信小程序通过code获取openid");
  121. JsonData jsonObj = JsonMapper.ToObject(result);
  122. string openid = jsonObj["openid"].ToString();
  123. return openid;
  124. }
  125. public Dictionary<string, string> getPhoneNumber(string encryptedData, string iv, string session_key)
  126. {
  127. Dictionary<string, string> Obj = new Dictionary<string, string>();
  128. try
  129. {
  130. byte[] encryData = Convert.FromBase64String(encryptedData);
  131. RijndaelManaged rijndaelCipher = new RijndaelManaged();
  132. rijndaelCipher.Key = Convert.FromBase64String(session_key);
  133. rijndaelCipher.IV = Convert.FromBase64String(iv);
  134. rijndaelCipher.Mode = CipherMode.CBC;
  135. rijndaelCipher.Padding = PaddingMode.PKCS7;
  136. ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
  137. byte[] plainText = transform.TransformFinalBlock(encryData, 0, encryData.Length);
  138. string result = Encoding.Default.GetString(plainText);
  139. dynamic model = Newtonsoft.Json.Linq.JToken.Parse(result) as dynamic;
  140. string phoneNumber = model.phoneNumber;
  141. //return model.phoneNumber;
  142. if (string.IsNullOrEmpty(phoneNumber))
  143. {
  144. phoneNumber = "";
  145. }
  146. Obj.Add("Mobile", phoneNumber);
  147. }
  148. catch (Exception ex)
  149. {
  150. Obj.Add("Mobile", "err");
  151. function.WriteLog(DateTime.Now.ToString(), "微信小程序获取手机号异常");
  152. function.WriteLog(encryptedData, "微信小程序获取手机号异常");
  153. function.WriteLog(iv, "微信小程序获取手机号异常");
  154. function.WriteLog(session_key, "微信小程序获取手机号异常");
  155. function.WriteLog(ex.ToString(), "微信小程序获取手机号异常");
  156. function.WriteLog("\r\n\r\n", "微信小程序获取手机号异常");
  157. }
  158. return Obj;
  159. }
  160. public string filterEmoji(string str)
  161. {
  162. string origin = str;
  163. try
  164. {
  165. //关键代码
  166. foreach (var a in str)
  167. {
  168. byte[] bts = System.Text.Encoding.UTF32.GetBytes(a.ToString());
  169. if (bts[0].ToString() == "253" && bts[1].ToString() == "255")
  170. {
  171. str = str.Replace(a.ToString(), "");
  172. }
  173. }
  174. }
  175. catch
  176. {
  177. str = origin;
  178. }
  179. return str;
  180. }
  181. #endregion
  182. #region 消费者-支付宝通过code获取openid
  183. public string AlipayMiniOpenIdDo(string code)
  184. {
  185. string openid = new AlipayFunction(_accessor.HttpContext).GetAlipayUserId(code);
  186. return openid;
  187. }
  188. #endregion
  189. }
  190. }