ConsumersController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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()
  88. {
  89. Dictionary<string, object> Obj = new Dictionary<string, object>();
  90. var time = DateTime.Now.ToString("yyyyMMdd");
  91. var dir = "/miniappHeadPhoto/" + time.Substring(0, 4) + "/" + time.Substring(4, 2) + "/" + time.Substring(6, 2) + "/";
  92. var OssUrl = "https://laikeba.oss-cn-chengdu.aliyuncs.com";
  93. // https://laikeba.oss-cn-chengdu.aliyuncs.com/miniappHeadPhoto/2024/01/17/lkb-1705458639623.jpg
  94. var AccessKeyId = "LTAI5tJsPaNzqCSMCPwb8zfz";
  95. var AccessKeySecret = "efM31Up75fQcgZ32U6xvAciagceQae";
  96. // 构造OssClient实例。 endpoint 格式:https://oss-cn-beijing.aliyuncs.com
  97. var ossClient = new OssClient(OssUrl, AccessKeyId, AccessKeySecret);
  98. var config = new PolicyConditions();
  99. config.AddConditionItem(PolicyConditions.CondContentLengthRange, 1, 1024L * 1024 * 1024 * 5);// 文件大小范围:单位byte
  100. config.AddConditionItem(MatchMode.StartWith, PolicyConditions.CondKey, dir);
  101. // config.AddConditionItem(PolicyConditions.CondSuccessActionStatus, "200");
  102. var expire = DateTimeOffset.Now.AddMinutes(30);// 过期时间
  103. // 生成 Policy,并进行 Base64 编码
  104. var policy = ossClient.GeneratePostPolicy(expire.LocalDateTime, config);
  105. var policyBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(policy));
  106. // 计算签名
  107. var hmac = new HMACSHA1(Encoding.UTF8.GetBytes(AccessKeySecret));
  108. var bytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(policyBase64));
  109. var Signature = Convert.ToBase64String(bytes);
  110. Obj.Add("OssUrl", OssUrl + dir);
  111. Obj.Add("AccessKeyId", AccessKeyId);
  112. Obj.Add("AccessKeySecret", AccessKeySecret);
  113. Obj.Add("Policy", policyBase64);
  114. Obj.Add("Expiration", expire);
  115. Obj.Add("Signature", Signature);
  116. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  117. }
  118. #endregion
  119. #region 首页-首页-个人信息-修改个人信息
  120. [Authorize]
  121. public JsonResult EditPersonalInfo(string value)
  122. {
  123. value = DesDecrypt(value);
  124. JsonData data = JsonMapper.ToObject(value);
  125. AppResultJson result = EditPersonalInfoDo(value);
  126. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  127. }
  128. private AppResultJson EditPersonalInfoDo(string value)
  129. {
  130. JsonData data = JsonMapper.ToObject(value);
  131. int ConsumerId = int.Parse(function.CheckInt(data["ConsumerId"].ToString())); //用户Id
  132. string HeadPhoto = data["HeadPhoto"].ToString(); //头像
  133. string NickName = data["NickName"].ToString(); //昵称
  134. Dictionary<string, object> Obj = new Dictionary<string, object>();
  135. var consumer = maindb.Consumers.FirstOrDefault(m => m.Id == ConsumerId) ?? new Consumers();
  136. var consumer2 = maindb2.Consumers.FirstOrDefault(m => m.Id == ConsumerId) ?? new MainModels2.Consumers();
  137. if (consumer.Id > 0)
  138. {
  139. if (!string.IsNullOrEmpty(HeadPhoto))
  140. {
  141. consumer.HeadPhoto = HeadPhoto;
  142. }
  143. if (!string.IsNullOrEmpty(NickName))
  144. {
  145. consumer.NickName = NickName;
  146. }
  147. }
  148. if (consumer2.Id > 0)
  149. {
  150. if (!string.IsNullOrEmpty(HeadPhoto))
  151. {
  152. consumer2.HeadPhoto = HeadPhoto;
  153. }
  154. if (!string.IsNullOrEmpty(NickName))
  155. {
  156. consumer2.NickName = NickName;
  157. }
  158. }
  159. maindb.SaveChanges();
  160. maindb2.SaveChanges();
  161. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  162. }
  163. #endregion
  164. #region 消费者-微信小程序通过code获取openid
  165. public string WeChatMiniOpenIdDo(string code)
  166. {
  167. string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + new WeChatFunction().AppId + "&secret=" + new WeChatFunction().AppSecret + "&js_code=" + code + "&grant_type=authorization_code";
  168. function.WriteLog(url, "微信小程序通过code获取openid");
  169. string result = function.GetWebRequest(url);
  170. function.WriteLog(result + "\n", "微信小程序通过code获取openid");
  171. JsonData jsonObj = JsonMapper.ToObject(result);
  172. string openid = jsonObj["openid"].ToString();
  173. return openid;
  174. }
  175. public Dictionary<string, string> getPhoneNumber(string encryptedData, string iv, string session_key)
  176. {
  177. Dictionary<string, string> Obj = new Dictionary<string, string>();
  178. try
  179. {
  180. byte[] encryData = Convert.FromBase64String(encryptedData);
  181. RijndaelManaged rijndaelCipher = new RijndaelManaged();
  182. rijndaelCipher.Key = Convert.FromBase64String(session_key);
  183. rijndaelCipher.IV = Convert.FromBase64String(iv);
  184. rijndaelCipher.Mode = CipherMode.CBC;
  185. rijndaelCipher.Padding = PaddingMode.PKCS7;
  186. ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
  187. byte[] plainText = transform.TransformFinalBlock(encryData, 0, encryData.Length);
  188. string result = Encoding.Default.GetString(plainText);
  189. dynamic model = Newtonsoft.Json.Linq.JToken.Parse(result) as dynamic;
  190. string phoneNumber = model.phoneNumber;
  191. //return model.phoneNumber;
  192. if (string.IsNullOrEmpty(phoneNumber))
  193. {
  194. phoneNumber = "";
  195. }
  196. Obj.Add("Mobile", phoneNumber);
  197. }
  198. catch (Exception ex)
  199. {
  200. Obj.Add("Mobile", "err");
  201. function.WriteLog(DateTime.Now.ToString(), "微信小程序获取手机号异常");
  202. function.WriteLog(encryptedData, "微信小程序获取手机号异常");
  203. function.WriteLog(iv, "微信小程序获取手机号异常");
  204. function.WriteLog(session_key, "微信小程序获取手机号异常");
  205. function.WriteLog(ex.ToString(), "微信小程序获取手机号异常");
  206. function.WriteLog("\r\n\r\n", "微信小程序获取手机号异常");
  207. }
  208. return Obj;
  209. }
  210. public string filterEmoji(string str)
  211. {
  212. string origin = str;
  213. try
  214. {
  215. //关键代码
  216. foreach (var a in str)
  217. {
  218. byte[] bts = System.Text.Encoding.UTF32.GetBytes(a.ToString());
  219. if (bts[0].ToString() == "253" && bts[1].ToString() == "255")
  220. {
  221. str = str.Replace(a.ToString(), "");
  222. }
  223. }
  224. }
  225. catch
  226. {
  227. str = origin;
  228. }
  229. return str;
  230. }
  231. #endregion
  232. #region 消费者-支付宝通过code获取openid
  233. public string AlipayMiniOpenIdDo(string code)
  234. {
  235. string openid = new AlipayFunction(_accessor.HttpContext).GetAlipayUserId(code);
  236. return openid;
  237. }
  238. #endregion
  239. }
  240. }