using System; using System.Collections.Generic; using System.Linq; using System.Data; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.AspNetCore.Authorization; using MySystem.MainModels; using LitJson; using Library; using System.Security.Cryptography; using System.Text; namespace MySystem.Areas.Api.Controllers.v1 { [Area("Api")] [Route("/v1/QrCodePlateMain/[controller]/[action]")] public class ConsumersController : BaseController { public ConsumersController(IHttpContextAccessor accessor, ILogger logger, IOptions setting) : base(accessor, logger, setting) { } #region 首页-首页-个人信息-返现金额 // [Authorize] public JsonResult AmountInfo(string value) { value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); Dictionary Obj = AmountInfoDo(value); return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj }); } public Dictionary AmountInfoDo(string value) { JsonData data = JsonMapper.ToObject(value); string OpenId = data["OpenId"].ToString(); //微信或支付宝Id int Kind = int.Parse(data["Kind"].ToString()); //类型(1 支付宝 2 微信) if (Kind == 1) { OpenId = AlipayMiniOpenIdDo(OpenId); // OpenId = "okuWn5fKbcHEiYJRtKZQT8iF5W1Y"; } if (Kind == 2) { OpenId = AlipayMiniOpenIdDo(OpenId); // OpenId = "okuWn5fKbcHEiYJRtKZQT8iF5W1Y"; } Dictionary Obj = new Dictionary(); var HeadPhoto = ""; var NickName = ""; var TotalAmount = 0.00M; var Amount = 0.00M; var Amount2 = 0.00M; var consumer = Services.Main.ConsumersService.Query(" and WechatOpenId='" + OpenId + "'"); var consumer2 = Services.Main2.ConsumersService.Query(" and WechatOpenId='" + OpenId + "'"); Amount = Services.Main.ConsumerProfitService.Sum(" and ConsumerId=" + consumer.Id + "", "GetMoney"); Amount2 = Services.Main.ConsumerProfitService.Sum(" and ConsumerId=" + consumer.Id + "", "GetMoney"); TotalAmount = Amount + Amount2; if (consumer.Id > 0) { HeadPhoto = consumer.HeadPhoto; NickName = consumer.NickName; } if (consumer2.Id > 0) { HeadPhoto = consumer2.HeadPhoto; NickName = consumer2.NickName; } Obj.Add("Amount", TotalAmount); //累计返现金额 Obj.Add("HeadPhoto", HeadPhoto); //头像 Obj.Add("NickName", NickName); //昵称 return Obj; } #endregion #region 首页-首页-个人信息-修改个人信息 // [Authorize] public JsonResult EditPersonalInfo(string value) { value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); AppResultJson result = EditPersonalInfoDo(value); return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data }); } private AppResultJson EditPersonalInfoDo(string value) { JsonData data = JsonMapper.ToObject(value); string OpenId = data["OpenId"].ToString(); //微信或支付宝Id int Kind = int.Parse(data["Kind"].ToString()); //类型(1 支付宝 2 微信) string HeadPhoto = data["HeadPhoto"].ToString(); //头像 string NickName = data["NickName"].ToString(); //昵称 if (Kind == 1) { OpenId = AlipayMiniOpenIdDo(OpenId); // OpenId = "okuWn5fKbcHEiYJRtKZQT8iF5W1Y"; } if (Kind == 2) { OpenId = AlipayMiniOpenIdDo(OpenId); // OpenId = "okuWn5fKbcHEiYJRtKZQT8iF5W1Y"; } Dictionary Obj = new Dictionary(); var consumer = maindb.Consumers.FirstOrDefault(m => m.WechatOpenId == OpenId) ?? new Consumers(); var consumer2 = maindb2.Consumers.FirstOrDefault(m => m.WechatOpenId == OpenId) ?? new MainModels2.Consumers(); // var consumer = Services.Main.ConsumersService.Query(" and WechatOpenId=" + OpenId + ""); // var consumer2 = Services.Main2.ConsumersService.Query(" and WechatOpenId=" + OpenId + ""); if (string.IsNullOrEmpty(OpenId)) { return new AppResultJson() { Status = "-1", Info = "微信或支付宝Id为空" }; } if (consumer.Id > 0) { if (!string.IsNullOrEmpty(HeadPhoto)) { consumer.HeadPhoto = HeadPhoto; } if (!string.IsNullOrEmpty(NickName)) { consumer.NickName = NickName; } } if (consumer2.Id > 0) { if (!string.IsNullOrEmpty(HeadPhoto)) { consumer2.HeadPhoto = HeadPhoto; } if (!string.IsNullOrEmpty(NickName)) { consumer2.NickName = NickName; } } maindb.SaveChanges(); maindb2.SaveChanges(); return new AppResultJson() { Status = "1", Info = "", Data = Obj }; } #endregion #region 消费者-微信小程序通过code获取openid public string WeChatMiniOpenIdDo(string code) { string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + new WeChatFunction().AppId + "&secret=" + new WeChatFunction().AppSecret + "&js_code=" + code + "&grant_type=authorization_code"; function.WriteLog(url, "微信小程序通过code获取openid"); string result = function.GetWebRequest(url); function.WriteLog(result + "\n", "微信小程序通过code获取openid"); JsonData jsonObj = JsonMapper.ToObject(result); string openid = jsonObj["openid"].ToString(); return openid; } public Dictionary getPhoneNumber(string encryptedData, string iv, string session_key) { Dictionary Obj = new Dictionary(); try { byte[] encryData = Convert.FromBase64String(encryptedData); RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Key = Convert.FromBase64String(session_key); rijndaelCipher.IV = Convert.FromBase64String(iv); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; ICryptoTransform transform = rijndaelCipher.CreateDecryptor(); byte[] plainText = transform.TransformFinalBlock(encryData, 0, encryData.Length); string result = Encoding.Default.GetString(plainText); dynamic model = Newtonsoft.Json.Linq.JToken.Parse(result) as dynamic; string phoneNumber = model.phoneNumber; //return model.phoneNumber; if (string.IsNullOrEmpty(phoneNumber)) { phoneNumber = ""; } Obj.Add("Mobile", phoneNumber); } catch (Exception ex) { Obj.Add("Mobile", "err"); function.WriteLog(DateTime.Now.ToString(), "微信小程序获取手机号异常"); function.WriteLog(encryptedData, "微信小程序获取手机号异常"); function.WriteLog(iv, "微信小程序获取手机号异常"); function.WriteLog(session_key, "微信小程序获取手机号异常"); function.WriteLog(ex.ToString(), "微信小程序获取手机号异常"); function.WriteLog("\r\n\r\n", "微信小程序获取手机号异常"); } return Obj; } public string filterEmoji(string str) { string origin = str; try { //关键代码 foreach (var a in str) { byte[] bts = System.Text.Encoding.UTF32.GetBytes(a.ToString()); if (bts[0].ToString() == "253" && bts[1].ToString() == "255") { str = str.Replace(a.ToString(), ""); } } } catch { str = origin; } return str; } #endregion #region 消费者-支付宝通过code获取openid public string AlipayMiniOpenIdDo(string code) { string openid = new AlipayFunction(_accessor.HttpContext).GetAlipayUserId(code); return openid; } #endregion } }