public async Task <IActionResult> AuthorizeAsync(string appId, string code, string returnUrl) { if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(returnUrl)) { return(NotFound()); } string secret = ConfigCenterClient.GetSection("WeChatAccount").GetValue <string>(appId, null); if (secret == null) { return(NotFound()); } OAuthAccessToken accessTokenResponse = await GetAccessTokenAsync(appId, secret, code); OAuthUserInfo userInfo = new OAuthUserInfo { OpenId = accessTokenResponse?.OpenId }; if (accessTokenResponse != null && accessTokenResponse.Scope.Contains("snsapi_userinfo")) { userInfo = await GetUserInfoAsync(accessTokenResponse.AccessToken, accessTokenResponse.AccessToken); } returnUrl = HttpUtility.UrlDecode(returnUrl); string userInfoParams = accessTokenResponse.Scope.Contains("snsapi_userinfo") ? $"nickname={ HttpUtility.UrlEncode(userInfo.Nickname) }&headimgurl={ HttpUtility.UrlEncode(userInfo.HeadImgUrl) }" : string.Empty; return(Redirect($"{ returnUrl }{ (returnUrl.Contains("?") ? "&" : "?") }openId={ userInfo.OpenId }&{ userInfoParams }")); }
public async Task <JSSDK_ConfigResponse> ConfigAsync(string appId) { string secret = ConfigCenterClient.GetSection("WeChatAccount").GetValue <string>(appId, null); if (secret == null) { return(new JSSDK_ConfigResponse { IsSuccess = false, Code = CodeConstant.ClientError_NotFound_AppId }); } if (!_memoryCache.TryGetValue("Ticket_" + appId, out string ticket)) { string accessToken = await _jsSDKService.GetAccessTokenAsync(appId, secret); ticket = await _jsSDKService.GetTicketAsync(accessToken); if (ticket == null) { return(new JSSDK_ConfigResponse { IsSuccess = false, Code = 402 }); } _memoryCache.Set("Ticket_" + appId, ticket, DateTimeOffset.Now.AddHours(1.5)); } JSSDK_ConfigResponse config = new JSSDK_ConfigResponse { IsSuccess = true, Code = CodeConstant.Success, AppId = appId }; if (Request.Headers.TryGetValue("Referer", out StringValues value) && value.Count > 0) { config.Signature = _jsSDKService.Sign(ticket, value[0], config); return(config); } else { return(new JSSDK_ConfigResponse { IsSuccess = false, Code = 403 }); } }
public ConnStringManager() { if (this.cache == null) { string xml = ConfigCenterClient.Get("TCBase.Data"); try { this.cache = ConfigConvert.XmlToConfig(xml); } catch (Exception ex) { Log.LogMessage("TCBase.Data", "数据库配置文件转换失败", LogType.Error, true, ex, (object)new { configInfo = xml }); } } ConfigCenterClient.ConfigChanged += new EventHandler <ConfigDataEventArgs>(this.ConfigCenterClient_ConfigChanged); }
private void ConfigCenterClient_ConfigChanged(object sender, ConfigDataEventArgs e) { if (!e.ConfigChangeDictionary.ContainsKey(AppProfile.AppName)) { return; } List <ConfigData> addList = e.ConfigChangeDictionary[AppProfile.AppName].AddList; Predicate <ConfigData> predicate1 = (Predicate <ConfigData>)(p => p.Key == "TCBase.Data"); Predicate <ConfigData> match1; if (addList.Exists(match1)) { this.UploadCache(ConfigCenterClient.Get("TCBase.Data")); } List <ConfigData> updateList = e.ConfigChangeDictionary[AppProfile.AppName].UpdateList; Predicate <ConfigData> predicate2 = (Predicate <ConfigData>)(p => p.Key == "TCBase.Data"); Predicate <ConfigData> match2; if (updateList.Exists(match2)) { this.UploadCache(ConfigCenterClient.Get("TCBase.Data")); } }
public async Task <string> ShortUrlAsync(string appId, string longUrl) { string secret = ConfigCenterClient.GetSection("WeChatAccount").GetValue <string>(appId, null); if (secret == null) { return(null); } string accessToken = await GetAccessTokenAsync(appId, secret); HttpWebRequest httpRequest = WebRequest.CreateHttp($"https://api.weixin.qq.com/cgi-bin/shorturl?access_token={ accessToken }"); httpRequest.Method = "POST"; httpRequest.ContentType = "application/json"; byte[] buffer = Encoding.UTF8.GetBytes($"{{\"action\":\"long2short\",\"long_url\":\"{ longUrl }\"}}"); httpRequest.ContentLength = buffer.LongLength; using (Stream stream = httpRequest.GetRequestStream()) { stream.Write(buffer, 0, buffer.Length); } string shortUrl = null; using (WebResponse response = httpRequest.GetResponse()) using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { string json = reader.ReadToEnd(); if (JObject.Parse(json).TryGetValue("short_url", out JToken value)) { shortUrl = value.ToString(); } } return(shortUrl); }
public async Task <GoodsDetails> GetDetailsAsync(long rawId) { HttpWebRequest request = WebRequest.CreateHttp($"https://item.m.jd.com/product/{ rawId }.html"); request.UserAgent = ConfigCenterClient.GetSection("UserAgent").GetValue <string>("Chrome"); using (WebResponse response = await request.GetResponseAsync()) using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { string html = reader.ReadToEnd(); HtmlDocument document = new HtmlDocument(); document.LoadHtml(html); // 商品名称 HtmlNode goodNameHtmlNode = document.DocumentNode.SelectSingleNode("//*[@id=\"goodName\"]"); string goodsName = goodNameHtmlNode.GetAttributeValue("value", string.Empty); if (string.IsNullOrWhiteSpace(goodsName)) { return(null); } // 京东售卖价格 HtmlNode priceHtmlNode = document.DocumentNode.SelectSingleNode("//*[@id=\"jdPrice\"]"); string priceText = priceHtmlNode.GetAttributeValue("value", string.Empty); bool stopSelling = false; if (priceText == "暂无报价") { stopSelling = true; } if (!decimal.TryParse(priceText, out decimal price)) { // TODO: add logs. } // 商品分类 HtmlNode categoryIdHtmlNode = document.DocumentNode.SelectSingleNode("//*[@id=\"categoryId\"]"); string categoryId = categoryIdHtmlNode.GetAttributeValue("value", string.Empty); // 图片 HtmlNode specImageHtmlNode = document.DocumentNode.SelectSingleNode("//*[@id=\"spec_image\"]"); string specImage = specImageHtmlNode.GetAttributeValue("src", string.Empty); return(new GoodsDetails { Id = 1, RawId = rawId, Channel = ChannelType.JD, Name = goodsName, SalePrice = price, StopSelling = stopSelling, Image = specImage, JDCategoryIds = categoryId?.Split('_').Select(ci => { if (long.TryParse(ci, out long result)) { return result; } return 0; }).ToArray() });