ConsumersController.cs 10 KB

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