old_flsystem/应用/TBRebate/Tools.cs

284 lines
12 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Api.Framework;
using Api.Framework.Cps;
using Api.Framework.Enums;
using Api.Framework.Model;
using Api.Framework.Tools;
using CsharpHttpHelper;
using CsharpHttpHelper.Enum;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using TBRebate.Entitys;
namespace TBRebate
{
public static class Tools
{
public const string Reg_QRCode_Goods = @"\[图片=(?<二维码图片>.+?)\]";
public static List<string> Regex_Items = new List<string>() { @",\\""price\\"":{\\""priceText\\"":\\""(?<price>.+?)\\"",", @",\\""price\\"":\\""(?<price>.+?)\\""", @",\\""priceText\\"":\\""(?<price>.+?)\\""," };
/// <summary>
/// 获取中英文混排字符串的实际长度(字节数)
/// </summary>
/// <param name="str">要获取长度的字符串</param>
/// <returns>字符串的实际长度值(字节数)</returns>
public static int GetStringLength(string str)
{
if (str.Equals(string.Empty))
return 0;
int strlen = 0;
ASCIIEncoding strData = new ASCIIEncoding();
//将字符串转换为ASCII编码的字节数字
byte[] strBytes = strData.GetBytes(str);
for (int i = 0; i <= strBytes.Length - 1; i++)
{
if (strBytes[i] == 63) //中文都将编码为ASCII编码63,即"?"号
strlen++;
strlen++;
}
return strlen;
}
private static int chief_group_count = 0;
private static int chief_group_index = -1;
private static int deputy_group_count = 0;
private static int deputy_group_index = -1;
/// <summary>
/// 累加推广位组调用次数
/// </summary>
/// <param name="flag"></param>
public static void GroupAdzoneCount(fl_member_info member, tbinfo_temp tbInfoTemp)
{
if (member.buy_point >= Class1.Config.Point && member.finish_order >= Class1.Config.OrderNum)
{
if (tbInfoTemp.is_chief_group)
chief_group_count++;
}
else
{
if (tbInfoTemp.is_deputy_group)
deputy_group_count++;
}
}
/// <summary>
/// 获取推广位
/// </summary>
/// <param name="member"></param>
/// <param name="tbInfoTemp"></param>
/// <returns></returns>
public static Tuiguangwei GetTuiguangwei(string robotName, fl_member_info member, tbinfo_temp tbInfoTemp)
{
try
{
#region
var choose = (member.buy_point >= Class1.Config.Point && member.finish_order >= Class1.Config.OrderNum);//判断是主还是副
if (choose)
{
if (tbInfoTemp.is_chief_group)
{
string key_chief = "fl_tbrebate_key_chief_" + robotName;
Tuiguangwei tgw = ApiClient.Cache.Get<Tuiguangwei>(key_chief);
if (chief_group_count >= ApiClient.Setting.SystemConfig.TGWRandomCount || tgw == null)
{
chief_group_count = 0;
chief_group_index++;
var list = HttpHelper.JsonToObject<List<Tuiguangwei>>(tbInfoTemp.pid_chief) as List<Tuiguangwei>;
if (list != null && list.Count != 0)
{
list = list.Where(o => CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType. && f.username == o.Member.username) != null).ToList();
if (list == null || list.Count == 0) return null;
if (chief_group_index < list.Count)
tgw = list[chief_group_index];
else
{
tgw = list[0];
chief_group_index = 0;
}
}
ApiClient.Cache.Set(key_chief, tgw, 60);
}
return tgw;
}
else
return new Tuiguangwei() { Pid = tbInfoTemp.pid_chief, Name = tbInfoTemp.pid_chief_name, Member = new fl_cps_member() { username = tbInfoTemp.pid_chief_cps_name } };
}
else
{
if (tbInfoTemp.is_deputy_group)
{
string key_deputy = "fl_tbrebate_key_deputy_" + robotName;
var tgw = ApiClient.Cache.Get<Tuiguangwei>(key_deputy);
if (deputy_group_count >= ApiClient.Setting.SystemConfig.TGWRandomCount || tgw == null)
{
deputy_group_count = 0;
deputy_group_index++;
var list = HttpHelper.JsonToObject<List<Tuiguangwei>>(tbInfoTemp.pid_deputy) as List<Tuiguangwei>;
if (list != null && list.Count != 0)
{
list = list.Where(o => CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType. && f.username == o.Member.username) != null).ToList();
if (list == null || list.Count == 0) return null;
if (deputy_group_index < list.Count)
tgw = list[deputy_group_index];
else
{
tgw = list[0];
deputy_group_index = 0;
}
}
ApiClient.Cache.Set(key_deputy, tgw, 60);
}
return tgw;
}
else
return new Tuiguangwei() { Pid = tbInfoTemp.pid_deputy, Name = tbInfoTemp.pid_deputy_name, Member = new fl_cps_member() { username = tbInfoTemp.pid_deputy_cps_name } };
}
#endregion
}
catch (System.Exception ex)
{
EventClient.OnEvent(null, $"选择使用的推广位异常:{ex.Message} - {ex.StackTrace}");
}
return null;
}
private static int activity_group_count = 0;
private static int activity_group_index = -1;
/// <summary>
/// 强制推广位组
/// </summary>
/// <param name="member"></param>
/// <param name="tbInfoTemp"></param>
/// <returns></returns>
public static TuiguangweiActivity GetActivityTuiguangwei(string robotName, tbinfo_temp tbInfoTemp)
{
try
{
string key_activity = "fl_tbrebate_key_activity_" + robotName;
var tgw = ApiClient.Cache.Get<TuiguangweiActivity>(key_activity);
if (activity_group_count >= ApiClient.Setting.SystemConfig.TGWRandomCount || tgw == null)
{
activity_group_count = 0;
activity_group_index++;
var list = HttpHelper.JsonToObject<List<TuiguangweiActivity>>(tbInfoTemp.adzone_activity_pid) as List<TuiguangweiActivity>;
if (list != null && list.Count != 0)
{
list = list.Where(o => CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType. && f.username == o.Member.username) != null).ToList();
if (list == null || list.Count == 0) return null;
if (activity_group_index < list.Count)
{
tgw = list[activity_group_index];
}
else
{
tgw = list[0];
activity_group_index = 0;
}
}
ApiClient.Cache.Set(key_activity, tgw, 60);
}
activity_group_count++;
return tgw;
}
catch (Exception ex)
{
EventClient.OnEvent(null, $"选择使用的强返推广位异常:{ex.Message} - {ex.StackTrace}");
}
return null;
}
/// <summary>
/// 清除强制推广位组缓存
/// </summary>
public static void ClearActivityTuiguangwei(string robotName)
{
string key_activity = "fl_tbrebate_key_activity_" + robotName;
ApiClient.Cache.Remove(key_activity);
}
/// <summary>
/// 获取商品标题缓存
/// </summary>
public static Dictionary<string, string> GoodTitleCacheDic = new Dictionary<string, string>();
/// <summary>
/// 获取商品的标题,这个会频繁
/// </summary>
/// <param name="mess"></param>
/// <returns></returns>
public static Task<string> GetGoodTitle(string mess)
{
return Task.Run<string>(() =>
{
var md5 = Util.GetMD5(mess);
if (GoodTitleCacheDic.ContainsKey(md5))
return GoodTitleCacheDic[md5];
for (int i = 0; i < 2; i++)
{
try
{
//{"tkl":"6.0 hi:/哈LpUPXfo8wU0嘻 【移动用户立减1000元/咨询客服享惊喜好礼】Apple/苹果 iPhone 12 5G官网旗舰iphone12苹果12官方正品手机"}
var postData = $"{{\"tkl\":\"{mess}\"}}";
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = "http://api.chaozhi.hk/tb/tklParse",
Method = "POST",
Timeout = 5000,
ReadWriteTimeout = 2000,
IsToLower = false,
Cookie = "",
UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",
Accept = "text/html, application/xhtml+xml, */*",
ContentType = "application/json",
Referer = "http://tool.chaozhi.hk/",
Allowautoredirect = false,
AutoRedirectCookie = false,
Postdata = postData,
ResultType = ResultType.String
};
HttpResult result = http.GetHtml(item);
string html = result.Html;
var jOjb = JObject.Parse(html);
var error_code = jOjb["error_code"].ToString();
if (error_code == "0")
{
var keyword = jOjb["data"]["content"].ToString();
if (!string.IsNullOrWhiteSpace(keyword))
{
if (!GoodTitleCacheDic.ContainsKey(md5))
GoodTitleCacheDic.Add(md5, keyword);
return keyword;
}
}
else if (error_code == "113")//{"error_code":113,"msg":"频率太快了","data":{}}
{
Thread.Sleep(1000);
continue;
}
}
catch (Exception ex)
{
return string.Empty;
}
}
return string.Empty;
});
}
}
}