ConsumersController.cs 9.1 KB

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