Преглед на файлове

搜同款增加超时重试时间、指定时间内不断轮询结果;

dodo hold преди 2 години
родител
ревизия
59e433cc17

+ 7 - 5
molilian.api/Controllers/public/RootController.cs → molilian.api/Controllers/public/ApiController.cs

@@ -16,13 +16,15 @@ namespace molilian.api.Controllers
 {
     [ApiController]
     [Route("api/[action]")]
-    public class RootController : ControllerBase
+    public class ApiController : ControllerBase
     {
 
         protected IHttpContextAccessor _accessor;
-        public RootController(IHttpContextAccessor accessor)
+        TaokeOpenCore core;
+        public ApiController(IHttpContextAccessor accessor)
         {
             _accessor = accessor;
+            core = new TaokeOpenCore();
         }
 
 
@@ -62,7 +64,7 @@ namespace molilian.api.Controllers
                 var bytes = new WebClientUtility().Request(url).ResponseBody;
                 img = Convert.ToBase64String(bytes);
             }
-            return await TaokeOpenCore.PromotionQueryAsync(img, oaid, ip);
+            return await core.PromotionQueryAsync(img, oaid, ip);
         }
 
 
@@ -79,9 +81,9 @@ namespace molilian.api.Controllers
                 var bytes = new WebClientUtility().Request(url).ResponseBody;
                 img = Convert.ToBase64String(bytes);
             }
-            return await TaokeOpenCore.PromotionQueryAsync(img, oaid, ip);
+            return await core.PromotionQueryAsync(img, oaid, ip);
         }
 
-         
+
     }
 }

Файловите разлики са ограничени, защото са твърде много
+ 0 - 0
molilian.api/Properties/PublishProfiles/https___ccr.ccs.tencentyun.com_shaobin.pubxml.user


+ 58 - 24
molilian.core/Core/taoke/TaokeOpenCore.cs

@@ -1,22 +1,4 @@
-using Microsoft.AspNetCore.Http;
-using Microsoft.AspNetCore.Mvc.Controllers;
-using Microsoft.AspNetCore.Mvc.Filters;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using dodohold.core;
-using Spire.Pdf.Exporting.XPS.Schema;
-using System.Xml.Linq;
-using System.Net;
-using TencentCloud.Mrs.V20200910.Models;
-using Microsoft.AspNetCore.Mvc;
-using Microsoft.AspNetCore.Authentication;
-using Spire.Pdf.Graphics;
-using Google.Protobuf.WellKnownTypes;
-using System.Threading.Channels;
-using TencentCloud.Weilingwith.V20230427.Models;
-using System.Threading;
+using dodohold.core;
 using System.Text.Json;
 
 
@@ -25,6 +7,7 @@ namespace molilian.core
     public partial class TaokeOpenCore
     {
         static string _test_base64String = string.Empty;
+        public TkConfigDTO _config;
 
         private static string _end_point;
         static TaokeOpenCore()
@@ -32,8 +15,13 @@ namespace molilian.core
             _end_point = Environment.GetEnvironmentVariable("EndPoint");
         }
 
+        public TaokeOpenCore()
+        {
+            _config = TkConfigCore.Get();
+        }
 
-        public static async Task<APIResult> PromotionQueryAsync(string img, string oaid = "", string ip = "")
+
+        public async Task<APIResult> PromotionQueryAsync(string img, string oaid = "", string ip = "")
         {
             PromotionQueryDTO result = new()
             {
@@ -118,10 +106,56 @@ namespace molilian.core
                 oaid = data.Url.GetContentPart("oaid=", "&");
                 string cookies = string.Empty;
 
-                (result.SimilarPromotion, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies);
-                (result.PromotionImg, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies);
-                result.similarPromotion = result.SimilarPromotion.Convert2Json();
-                result.promotionImg = result.PromotionImg.Convert2Json();
+
+
+                int timeout = _config.similar_timeout;
+#if DEBUG
+                timeout = 5000;
+#endif
+                using var cts = new CancellationTokenSource();
+                cts.CancelAfter(timeout);
+
+
+                var requestTask = Task.Run(async () =>
+                {
+                    (result.SimilarPromotion, cookies) = await taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies, cts.Token);
+                    (result.PromotionImg, cookies) = await taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies, cts.Token);
+                    result.similarPromotion = result.SimilarPromotion.Convert2Json();
+                    result.promotionImg = result.PromotionImg.Convert2Json();
+                    return result;
+
+                }, cts.Token);
+
+                var delayTask = Task.Delay(timeout, cts.Token);
+                var completedTask = await Task.WhenAny(requestTask, delayTask);
+                if (completedTask == requestTask)
+                {
+                    result = await requestTask;
+                }
+                else
+                {
+                    //============================== 放弃转链-请求超时 ==============================
+                    cts.Cancel();
+                    result.success = false;
+                    result.message = "调用失败";
+                    result.reason = "请求超时";
+                    _ = TkLogCore.PromotionImgLogAsync(result);
+                    return new APIResult(new
+                    {
+                        result.success,
+                        result.message,
+                        result.reason,
+                    }, APIResultCodeEnum.OK, new JsonSerializerOptions
+                    {
+                        Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
+                    });
+                }
+
+
+                //(result.SimilarPromotion, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies);
+                //(result.PromotionImg, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies);
+                //result.similarPromotion = result.SimilarPromotion.Convert2Json();
+                //result.promotionImg = result.PromotionImg.Convert2Json();
 
                 result.success = true;
                 result.message = "调用成功";

+ 5 - 0
molilian.core/DTO/alimama/TkConfigDTO.cs

@@ -17,6 +17,11 @@ namespace molilian.core
         public int fake_click_max { get; set; }
         public int fake_click_ttl { get; set; } = 86400;
         public int rt_max { get; set; } = 0;
+
+        /// <summary>
+        /// 搜同款超时时间
+        /// </summary>
+        public int similar_timeout { get; set; } = 0;
         public string tk_whitelist_regular { get; set; } = string.Empty;
         public string tk_blacklist_regular { get; set; } = string.Empty;
         public string multi_token_regular { get; set; } = string.Empty;

+ 2 - 1
molilian.core/Plus/Alimama/parse.cs

@@ -224,7 +224,8 @@ namespace molilian.core
         public static string GetDeeplink(string url)
         {
             if (string.IsNullOrEmpty(url)) return "tbopen://m.taobao.com/tbopen/index.html";
-            string result = $"tbopen://m.taobao.com/tbopen/index.html?action=ali.open.nav&module=h5&h5Url={url.UrlEncode()}&backURL=__back_url__";
+            string result = $"tbopen://m.taobao.com/tbopen/index.html?action=ali.open.nav&module=h5&h5Url={url.UrlEncode()}";
+            //string result = $"tbopen://m.taobao.com/tbopen/index.html?action=ali.open.nav&module=h5&bootImage=0&afcPromotionOpen=false&bc_fl_src=h5_huanduan&source=slk_dp&h5Url={url.UrlEncode()}";
             return result;
         }
 

+ 32 - 3
molilian.core/Plus/TaobaoOpen/TaobaoOpenPlus.cs

@@ -70,8 +70,35 @@ namespace molilian.core
         }
 
 
+
+        public async Task<(List<PromotionQueryItemDTO>, string)> GetRecommendDataWithRetry(RecommendRequestType type, string oaid,
+           string cookies, CancellationToken cancellationToken = default)
+        {
+            // 假设传入了一个 CancellationToken cancellationToken 和 int maxRetries 表示最大重试次数
+            int retryCount = 0;
+            int maxRetries = 10;
+            while (true)
+            {
+                try
+                {
+                    return await GetRecommendData(type, oaid, cookies, cancellationToken);
+                }
+                catch (Exception ex)
+                {
+                    if (retryCount >= maxRetries || cancellationToken.IsCancellationRequested)
+                    {
+                        throw;
+                    }
+                    retryCount++;
+                    Thread.Sleep(400);
+                }
+            }
+
+        }
+
+
         public async Task<(List<PromotionQueryItemDTO>, string)> GetRecommendData(RecommendRequestType type, string oaid,
-           string cookies)
+           string cookies, CancellationToken cancellationToken = default)
         {
             string token = "undefined";
             if (!string.IsNullOrEmpty(cookies)) token = cookies.GetContentPart("_m_h5_tk=", "_");
@@ -80,7 +107,9 @@ namespace molilian.core
 
             if (string.IsNullOrEmpty(cookies))
             {
-                var res = await new WebClientUtility().RequestAsync(url);
+                var res = await new WebClientUtility().RequestAsync(url, "GET", cancellationToken);
+
+                if (!res.Successed) { throw res.ResponseException; }
                 body = res.Body();
                 cookies = res.ResponseMessage.GetCookiesString();
                 token = cookies.GetContentPart("_m_h5_tk=", "_");
@@ -89,7 +118,7 @@ namespace molilian.core
 
             WebClientUtility client = new WebClientUtility();
             if (!string.IsNullOrEmpty(cookies)) client.SetCookies(cookies);
-            var response = await client.RequestAsync(url);
+            var response = await client.RequestAsync(url, "GET", cancellationToken);
             body = response.Body();
             body = body.GetContentPart("mtopjsonp1(", ")");
 

Някои файлове не бяха показани, защото твърде много файлове са промени