ConsumersController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. using Aliyun.OSS;
  16. namespace MySystem.Areas.Api.Controllers.v1
  17. {
  18. [Area("Api")]
  19. [Route("/v1/QrCodePlateMain/[controller]/[action]")]
  20. public class ConsumersController : BaseController
  21. {
  22. public ConsumersController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  23. {
  24. }
  25. #region 首页-首页-个人信息-返现金额
  26. // [Authorize]
  27. public JsonResult AmountInfo(string value)
  28. {
  29. value = DesDecrypt(value);
  30. JsonData data = JsonMapper.ToObject(value);
  31. Dictionary<string, object> Obj = AmountInfoDo(value);
  32. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  33. }
  34. public Dictionary<string, object> AmountInfoDo(string value)
  35. {
  36. JsonData data = JsonMapper.ToObject(value);
  37. string OpenId = data["OpenId"].ToString(); //微信或支付宝Id
  38. int Kind = int.Parse(data["Kind"].ToString()); //类型(1 支付宝 2 微信)
  39. if (Kind == 1)
  40. {
  41. OpenId = AlipayMiniOpenIdDo(OpenId);
  42. }
  43. if (Kind == 2)
  44. {
  45. OpenId = WeChatMiniOpenIdDo(OpenId);
  46. }
  47. Dictionary<string, object> Obj = new Dictionary<string, object>();
  48. var ConsumerId = 0;
  49. var HeadPhoto = "";
  50. var NickName = "";
  51. var TotalAmount = 0.00M;
  52. var Amount = 0.00M;
  53. var Amount2 = 0.00M;
  54. var consumer = Services.Main.ConsumersService.Query(" and WechatOpenId='" + OpenId + "'");
  55. var consumer2 = Services.Main2.ConsumersService.Query(" and WechatOpenId='" + OpenId + "'");
  56. if (consumer.Id > 0 && !string.IsNullOrEmpty(consumer.WechatOpenId)) Amount = Services.Main.ConsumerProfitService.Sum(" and ConsumerId=" + consumer.Id + "", "GetMoney");
  57. if (consumer2.Id > 0 && !string.IsNullOrEmpty(consumer2.WechatOpenId)) Amount2 = Services.Main2.ConsumerProfitService.Sum(" and ConsumerId=" + consumer2.Id + "", "GetMoney");
  58. TotalAmount = Amount + Amount2;
  59. if (consumer.Id > 0)
  60. {
  61. ConsumerId = consumer.Id;
  62. HeadPhoto = consumer.HeadPhoto;
  63. NickName = consumer.NickName;
  64. }
  65. else
  66. {
  67. if (consumer2.Id > 0)
  68. {
  69. ConsumerId = consumer2.Id;
  70. HeadPhoto = consumer2.HeadPhoto;
  71. NickName = consumer2.NickName;
  72. }
  73. }
  74. var token = PublicFunction.AppToken(ConsumerId, JwtSecret, JwtIss);
  75. // Obj.Add("Amount", 0.00); //累计返现金额
  76. Obj.Add("Amount", TotalAmount); //累计返现金额
  77. Obj.Add("HeadPhoto", HeadPhoto); //头像
  78. Obj.Add("NickName", NickName); //昵称
  79. Obj.Add("Token", token); //token
  80. Obj.Add("OpenId", OpenId);
  81. Obj.Add("ConsumerId", ConsumerId); //用户Id
  82. return Obj;
  83. }
  84. #endregion
  85. #region 首页-首页-oss信息
  86. [Authorize]
  87. public JsonResult OssInfo(string value)
  88. {
  89. value = DesDecrypt(value);
  90. JsonData data = JsonMapper.ToObject(value);
  91. string dir = data["dir"].ToString(); //文件上传路径
  92. if (string.IsNullOrEmpty(data["dir"].ToString()))
  93. {
  94. return Json(new AppResultJson() { Status = "1", Info = "文件上传路径不能为空" });
  95. }
  96. Dictionary<string, object> Obj = new Dictionary<string, object>();
  97. var OssUrl = OssHost;
  98. var AccessKeyId = OssKey;
  99. var AccessKeySecret = OssSecret;
  100. var endpoint = "https://" + OssEndpoint;
  101. // 构造OssClient实例。 endpoint 格式:https://oss-cn-beijing.aliyuncs.com
  102. var ossClient = new OssClient(endpoint, AccessKeyId, AccessKeySecret);
  103. var config = new PolicyConditions();
  104. config.AddConditionItem(PolicyConditions.CondContentLengthRange, 1, 1024L * 1024 * 1024 * 5);// 文件大小范围:单位byte
  105. config.AddConditionItem(MatchMode.StartWith, PolicyConditions.CondKey, dir);
  106. var expire = DateTimeOffset.Now.AddMinutes(30);// 过期时间
  107. // 生成 Policy,并进行 Base64 编码
  108. var policy = ossClient.GeneratePostPolicy(expire.LocalDateTime, config);
  109. var policyBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(policy));
  110. // 计算签名
  111. var hmac = new HMACSHA1(Encoding.UTF8.GetBytes(AccessKeySecret));
  112. var bytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(policyBase64));
  113. var Signature = Convert.ToBase64String(bytes);
  114. Obj.Add("OssUrl", OssUrl);
  115. Obj.Add("AccessKeyId", AccessKeyId);
  116. // Obj.Add("AccessKeySecret", AccessKeySecret);
  117. Obj.Add("Policy", policyBase64);
  118. Obj.Add("Expiration", expire);
  119. Obj.Add("Signature", Signature);
  120. Obj.Add("dir", dir);
  121. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  122. }
  123. #endregion
  124. #region 首页-首页-个人信息-修改个人信息
  125. [Authorize]
  126. public JsonResult EditPersonalInfo(string value)
  127. {
  128. value = DesDecrypt(value);
  129. JsonData data = JsonMapper.ToObject(value);
  130. AppResultJson result = EditPersonalInfoDo(value);
  131. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  132. }
  133. private AppResultJson EditPersonalInfoDo(string value)
  134. {
  135. JsonData data = JsonMapper.ToObject(value);
  136. int ConsumerId = int.Parse(function.CheckInt(data["ConsumerId"].ToString())); //用户Id
  137. string HeadPhoto = data["HeadPhoto"].ToString(); //头像
  138. string NickName = data["NickName"].ToString(); //昵称
  139. Dictionary<string, object> Obj = new Dictionary<string, object>();
  140. var consumer = maindb.Consumers.FirstOrDefault(m => m.Id == ConsumerId) ?? new Consumers();
  141. var consumer2 = maindb2.Consumers.FirstOrDefault(m => m.Id == ConsumerId) ?? new MainModels2.Consumers();
  142. if (consumer.Id > 0)
  143. {
  144. if (!string.IsNullOrEmpty(HeadPhoto))
  145. {
  146. consumer.HeadPhoto = HeadPhoto;
  147. }
  148. if (!string.IsNullOrEmpty(NickName))
  149. {
  150. consumer.NickName = NickName;
  151. }
  152. }
  153. if (consumer2.Id > 0)
  154. {
  155. if (!string.IsNullOrEmpty(HeadPhoto))
  156. {
  157. consumer2.HeadPhoto = HeadPhoto;
  158. }
  159. if (!string.IsNullOrEmpty(NickName))
  160. {
  161. consumer2.NickName = NickName;
  162. }
  163. }
  164. maindb.SaveChanges();
  165. maindb2.SaveChanges();
  166. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  167. }
  168. #endregion
  169. #region 消费者-微信小程序通过code获取openid
  170. public string WeChatMiniOpenIdDo(string code)
  171. {
  172. string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + new WeChatFunction().AppId + "&secret=" + new WeChatFunction().AppSecret + "&js_code=" + code + "&grant_type=authorization_code";
  173. function.WriteLog(url, "微信小程序通过code获取openid");
  174. string result = function.GetWebRequest(url);
  175. function.WriteLog(result + "\n", "微信小程序通过code获取openid");
  176. JsonData jsonObj = JsonMapper.ToObject(result);
  177. string openid = jsonObj["openid"].ToString();
  178. return openid;
  179. }
  180. public Dictionary<string, string> getPhoneNumber(string encryptedData, string iv, string session_key)
  181. {
  182. Dictionary<string, string> Obj = new Dictionary<string, string>();
  183. try
  184. {
  185. byte[] encryData = Convert.FromBase64String(encryptedData);
  186. RijndaelManaged rijndaelCipher = new RijndaelManaged();
  187. rijndaelCipher.Key = Convert.FromBase64String(session_key);
  188. rijndaelCipher.IV = Convert.FromBase64String(iv);
  189. rijndaelCipher.Mode = CipherMode.CBC;
  190. rijndaelCipher.Padding = PaddingMode.PKCS7;
  191. ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
  192. byte[] plainText = transform.TransformFinalBlock(encryData, 0, encryData.Length);
  193. string result = Encoding.Default.GetString(plainText);
  194. dynamic model = Newtonsoft.Json.Linq.JToken.Parse(result) as dynamic;
  195. string phoneNumber = model.phoneNumber;
  196. //return model.phoneNumber;
  197. if (string.IsNullOrEmpty(phoneNumber))
  198. {
  199. phoneNumber = "";
  200. }
  201. Obj.Add("Mobile", phoneNumber);
  202. }
  203. catch (Exception ex)
  204. {
  205. Obj.Add("Mobile", "err");
  206. function.WriteLog(DateTime.Now.ToString(), "微信小程序获取手机号异常");
  207. function.WriteLog(encryptedData, "微信小程序获取手机号异常");
  208. function.WriteLog(iv, "微信小程序获取手机号异常");
  209. function.WriteLog(session_key, "微信小程序获取手机号异常");
  210. function.WriteLog(ex.ToString(), "微信小程序获取手机号异常");
  211. function.WriteLog("\r\n\r\n", "微信小程序获取手机号异常");
  212. }
  213. return Obj;
  214. }
  215. public string filterEmoji(string str)
  216. {
  217. string origin = str;
  218. try
  219. {
  220. //关键代码
  221. foreach (var a in str)
  222. {
  223. byte[] bts = System.Text.Encoding.UTF32.GetBytes(a.ToString());
  224. if (bts[0].ToString() == "253" && bts[1].ToString() == "255")
  225. {
  226. str = str.Replace(a.ToString(), "");
  227. }
  228. }
  229. }
  230. catch
  231. {
  232. str = origin;
  233. }
  234. return str;
  235. }
  236. #endregion
  237. #region 消费者-支付宝通过code获取openid
  238. public string AlipayMiniOpenIdDo(string code)
  239. {
  240. string openid = new AlipayFunction(_accessor.HttpContext).GetAlipayUserId(code);
  241. return openid;
  242. }
  243. #endregion
  244. }
  245. }