512 lines
27 KiB
C#
512 lines
27 KiB
C#
using Api.Framework;
|
||
using Api.Framework.Cps;
|
||
using Api.Framework.Enums;
|
||
using Api.Framework.Events;
|
||
using Api.Framework.Model;
|
||
using Api.Framework.SDK;
|
||
using Api.Framework.Tools;
|
||
using Chat.Framework.WXSdk;
|
||
using Chat.Framework.WXSdk.Implement;
|
||
using CsharpHttpHelper;
|
||
using DYRebate.Entitys;
|
||
using DYRebate.Properties;
|
||
using System;
|
||
using System.Linq;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using System.Timers;
|
||
using Chat.Framework;
|
||
|
||
namespace DYRebate
|
||
{
|
||
public class Class1 : Plugin
|
||
{
|
||
public Class1()
|
||
{
|
||
this.Name = Resources.PluginName;
|
||
this.Note = Resources.PluginNote;
|
||
this.Logo = Resources.抖音;
|
||
}
|
||
|
||
#region 自定义变量
|
||
public static Config Config;
|
||
private MainForm mainForm = null;
|
||
|
||
#endregion
|
||
|
||
private System.Timers.Timer timersTimer = null;
|
||
|
||
public override void Start()
|
||
{
|
||
try
|
||
{
|
||
var session = ApiClient.GetSession();
|
||
#region 判断表是否存在,不存在创建表
|
||
|
||
if (session.TableExist<fl_plugin_dyrebate_dytgw>())
|
||
{
|
||
|
||
var dytgws = session.Find<fl_plugin_dyrebate_dytgw>("select * from fl_plugin_dyrebate_dytgw").ToList();
|
||
if (dytgws != null)
|
||
{
|
||
foreach (var item in dytgws)
|
||
{
|
||
//插入主推广位数据
|
||
session.Insertable(new fl_adzone_info()
|
||
{
|
||
adzone_name = item.pid_chief_name, //推广位名称
|
||
adzone_pid = item.pid_chief, //推广位pid
|
||
adzone_pid_cps_name = item.pid_chief_cps_name, //推广位cps名称
|
||
alliance_id = (int)CpsType.抖音联盟, //联盟id
|
||
robot_id = item.robot_id, //机器人id
|
||
group_id = string.Empty, //群id
|
||
is_download = false, //不下载
|
||
member_id = 0, //私人id
|
||
onoff = item.onoff, //不禁用
|
||
custom_type = Resources.SoftwareType, //自定义类型
|
||
extend = "chief"
|
||
}).ExecuteCommand();
|
||
//插入副推广位数据
|
||
session.Insertable(new fl_adzone_info()
|
||
{
|
||
adzone_name = item.pid_deputy_name, //推广位名称
|
||
adzone_pid = item.pid_deputy, //推广位pid
|
||
adzone_pid_cps_name = item.pid_deputy_cps_name, //推广位cps名称
|
||
alliance_id = (int)CpsType.抖音联盟, //联盟id
|
||
robot_id = item.robot_id, //机器人id
|
||
group_id = string.Empty, //群id
|
||
is_download = false, //不下载
|
||
member_id = 0, //私人id
|
||
onoff = item.onoff, //不禁用
|
||
custom_type = Resources.SoftwareType, //自定义类型
|
||
extend = "deputy"
|
||
}).ExecuteCommand();
|
||
}
|
||
}
|
||
session.DropTable<fl_plugin_dyrebate_dytgw>();
|
||
}
|
||
|
||
if (!session.TableExist<fl_plugin_dy_pidhist>())
|
||
{
|
||
session.CreateTable<fl_plugin_dy_pidhist>();
|
||
session.AddUnique<fl_plugin_dy_pidhist>("cpsname", "pid");
|
||
}
|
||
#endregion
|
||
|
||
//创建配置文件
|
||
Config = this.ReadConfig<Config>();
|
||
SDK.ReciveIMEvent += SDK_ReciveIMEvent;
|
||
SDK.OrderNoticeEvent += SDK_OrderNoticeEvent;
|
||
|
||
timersTimer = new System.Timers.Timer();
|
||
timersTimer.Enabled = true;
|
||
timersTimer.Interval = 5 * 60 * 1000;
|
||
timersTimer.Elapsed += new ElapsedEventHandler(timersTimer_Elapsed);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
this.OnLog(ex.Message);
|
||
}
|
||
}
|
||
|
||
#region 清空抖音商品缓存
|
||
private void timersTimer_Elapsed(object sender, ElapsedEventArgs e)
|
||
{
|
||
try
|
||
{
|
||
var cpss = CpsClient.Members.Where(f => f.cpstype == CpsType.抖音联盟).ToList();
|
||
if (cpss != null && cpss.Count != 0)
|
||
{
|
||
for (int i = 0; i < cpss.Count; i++)
|
||
{
|
||
var cps = cpss[i];
|
||
Task.Run(() =>
|
||
{
|
||
var api = CpsClient.CreateDouyinRequest(cps);
|
||
if (api != null)
|
||
{
|
||
int total = 0;
|
||
do
|
||
{
|
||
var list = api.FindGoodsWindowUrls(out total);
|
||
if (list != null && list.Count != 0)
|
||
{
|
||
if (!string.IsNullOrWhiteSpace(Class1.Config.PromotionWhiteList))
|
||
{
|
||
var whiteList = Config.PromotionWhiteList.Replace(",", ",").Split(',').Where(f => !string.IsNullOrWhiteSpace(f)).Select(f => f.Trim()).ToList();
|
||
for (int z = 0; z < whiteList.Count; z++)
|
||
{
|
||
if (list.Contains(whiteList[z]))
|
||
list.Remove(whiteList[z]);
|
||
}
|
||
}
|
||
api.DelGoodsWindowUrls(list);
|
||
}
|
||
Thread.Sleep(2000);
|
||
} while (total >= 20);
|
||
}
|
||
});
|
||
Thread.Sleep(10);
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{ }
|
||
}
|
||
#endregion
|
||
|
||
public override void ShowForm()
|
||
{
|
||
try
|
||
{
|
||
if (mainForm == null || mainForm.IsDisposed)
|
||
{
|
||
mainForm = new MainForm();
|
||
mainForm.Show();
|
||
}
|
||
mainForm.TopMost = true;
|
||
mainForm.TopMost = false;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
this.OnLog(ex.Message);
|
||
}
|
||
}
|
||
|
||
public override void Stop()
|
||
{
|
||
try
|
||
{
|
||
timersTimer.Close();
|
||
SessionExt.Clear();
|
||
if (mainForm != null)
|
||
{
|
||
mainForm.CloseForm();
|
||
mainForm = null;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
this.OnLog(ex.Message);
|
||
}
|
||
}
|
||
|
||
private void SDK_ReciveIMEvent(object sender, ReciveIMEvent e)
|
||
{
|
||
try
|
||
{
|
||
//过滤表情xml和卡片xml
|
||
if ((e.Message.Contains(@"<appmsg appid=") || e.Message.Contains(@"<msg><emoji") || e.Message.StartsWith("[声音=")) && !e.Message.Contains(@"点击链接,再选择浏览器咑閞")) return;
|
||
if (!OrderHelper.IsCurrentCpsMess(e.Message, CpsType.抖音联盟)) return;
|
||
var session = ApiClient.GetSession();
|
||
var _member = e.GetMemberinfo();
|
||
|
||
var mOper = new MessageOperation(sender, e, this);
|
||
|
||
//手动发送订单号绑定
|
||
if (mOper.BindOrderId()) return;
|
||
|
||
//返利消息判断
|
||
if (mOper.AnalyseFanLi()) return;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
this.OnLog($"抖音返利插件异常.:{ex.Message} - {ex.StackTrace}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 订单通知
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
public void SDK_OrderNoticeEvent(object sender, OrderNoticeEvent e)
|
||
{
|
||
try
|
||
{
|
||
if (e.ChatType == CpsType.抖音联盟)
|
||
{
|
||
//this.OnLog("淘宝订单:" + HttpHelper.ObjectToJson(e));
|
||
var session = ApiClient.GetSession();
|
||
//订单信息
|
||
var order_dy = e.Order as fl_order_douyin;
|
||
|
||
if (order_dy == null) return;
|
||
|
||
if (e.Member != null)
|
||
{
|
||
//是否黑名单
|
||
if (ApiClient.IsBlackFlMemberInfo(e.Member))
|
||
{
|
||
return;
|
||
}
|
||
|
||
#region 订单变化通知消息
|
||
var robot_info = session.FindRobotInfo(e.Member.robot_name.Trim(), e.Member.robot_type);
|
||
if (robot_info != null)
|
||
{
|
||
var mess = string.Empty;
|
||
var point = HttpHelper.JsonToObject<ItemPoint>(order_dy.db_point) as ItemPoint;
|
||
|
||
#region 下单用户订单检测(防撸)
|
||
if (e.Customer == null)
|
||
{
|
||
var prevent_theft_cache = session.FindSingle<fl_prevent_theft_cache>("select * from fl_prevent_theft_cache where order_id = @order_id", new { order_id = order_dy.order_id });//防止上级多次触发
|
||
if (prevent_theft_cache == null)
|
||
{
|
||
#region 收货时间验证时长
|
||
|
||
var status = Util.ConvertEnum<DouyinOrderStatus>(order_dy.order_status);
|
||
if (Class1.Config.ReceivingTimeCheck_Switch && (status == DouyinOrderStatus.CONFIRM || status == DouyinOrderStatus.SETTLE) && e.Member.status != MemberType.白名单)
|
||
{
|
||
if (order_dy.confirm_time != 0 && order_dy.confirm_time != 1)
|
||
{
|
||
var confirmTime = HttpExtend.GetDateTime(order_dy.confirm_time.ToString());
|
||
var payTime = HttpExtend.GetDateTime(order_dy.pay_time.ToString());
|
||
|
||
var timeLag = (int)Math.Floor((confirmTime - payTime).TotalHours);
|
||
if (timeLag <= Class1.Config.ReceivingTimeCheck_Hour)
|
||
{
|
||
if (Class1.Config.ReceivingTimeCheck_OperateType == OperateType.拉入黑名单)
|
||
{
|
||
if (!session.TemporaryBypassedBlack(e.Member))
|
||
{
|
||
e.Member.status = MemberType.黑名单; //拉入黑名单
|
||
session.SaveOrUpdate(e.Member);
|
||
session.FindBlacklistMemberInfos(true);//刷新黑名单缓存
|
||
|
||
ApiClient.SendMessage(robot_info, e.Member.username, new VariateReplace().CommonReplace(Config.Blocked_RestrictTip, order_dy, e.Member, point), order_dy.msg_groupid);
|
||
|
||
ApiClient.SendNoticeMessage($@"嫌疑用户拉黑
|
||
————
|
||
事件类型:订单检测
|
||
微信账号:{robot_info.name}
|
||
微信昵称:{robot_info.nick}
|
||
客户账号:{e.Member.username}
|
||
客户昵称:{e.Member.usernick}
|
||
拉黑原因:订单收货时间间隔{timeLag}小时", Config.notice_robotname);
|
||
e.Cancel = true;
|
||
{
|
||
prevent_theft_cache = new fl_prevent_theft_cache();
|
||
prevent_theft_cache.operate_type = Class1.Config.ReceivingTimeCheck_OperateType;
|
||
prevent_theft_cache.member_id = e.Member.id;
|
||
prevent_theft_cache.item_id = order_dy.product_id.ToString();
|
||
prevent_theft_cache.order_id = order_dy.order_id.ToString();
|
||
prevent_theft_cache.cps_type = CpsType.抖音联盟;
|
||
session.Insertable(prevent_theft_cache).ExecuteCommand();
|
||
}
|
||
|
||
if (Config.ReceivingTimeCheck_UserTop)
|
||
{
|
||
if (robot_info.type == ChatType.微信)
|
||
{
|
||
var wx = ChatClient.WXClient.Values.FirstOrDefault(f => f.WeixinHao == robot_info.name);
|
||
if (wx != null)
|
||
wx.EditContacts(e.Member.username, EditContactsType.置顶, e.Member.usernick);
|
||
}
|
||
}
|
||
return;
|
||
}
|
||
}
|
||
else if (Class1.Config.ReceivingTimeCheck_OperateType == OperateType.通知钉钉群)
|
||
{
|
||
ApiClient.SendNoticeMessage($@"嫌疑用户警告
|
||
————
|
||
事件类型:订单检测
|
||
微信账号:{robot_info.name}
|
||
微信昵称:{robot_info.nick}
|
||
客户账号:{e.Member.username}
|
||
客户昵称:{e.Member.usernick}
|
||
警告原因:订单收货时间间隔{timeLag}小时", Config.notice_robotname);
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
switch (e.OrderNoticeType)
|
||
{
|
||
case OrderNoticeType.客户订单:
|
||
{
|
||
if (Config.UserOrderChangeSwitch == SwitchType.开启 && !ApiClient.Setting.SystemConfig.message_warning_switch)
|
||
{
|
||
mess = _GetOrderStateMess(order_dy.db_status, SettleType.未结算提示, order_dy, e.Member, point);
|
||
|
||
if (!string.IsNullOrEmpty(e.Member.username) && !string.IsNullOrEmpty(mess))
|
||
ApiClient.SendMessage(robot_info, e.Member.username, mess, order_dy.msg_groupid);
|
||
}
|
||
}
|
||
return;
|
||
case OrderNoticeType.一级提成:
|
||
if (Config.AgentReceivedCommissionSwitch == SwitchType.开启)
|
||
{
|
||
if (e.Customer != null && point.AwardOne != 0)
|
||
{
|
||
if (order_dy.db_status == SystemOrderStatus.全额退款)
|
||
mess = Config.ClientOrderRefund_OneLevelTip;
|
||
else if (order_dy.db_status == SystemOrderStatus.订单付款)
|
||
mess = Config.OrderPaymentInform_OneLevelTip;
|
||
else if (order_dy.db_status == SystemOrderStatus.订单失效)
|
||
mess = Config.OrderCountermandInform_OneLevelTip;
|
||
else if (order_dy.db_status == SystemOrderStatus.订单结算)
|
||
mess = Config.OrderSettlement_OneLevelTip;
|
||
}
|
||
}
|
||
break;
|
||
case OrderNoticeType.二级提成:
|
||
if (Config.AgentReceivedCommissionSwitch == SwitchType.开启)
|
||
{
|
||
if (e.Customer != null)
|
||
{
|
||
if (order_dy.db_status == SystemOrderStatus.全额退款)
|
||
mess = Config.ClientOrderRefund_TwoLevelTip;
|
||
else if (order_dy.db_status == SystemOrderStatus.订单付款)
|
||
mess = Config.OrderPaymentInform_TwoLevelTip;
|
||
else if (order_dy.db_status == SystemOrderStatus.订单失效)
|
||
mess = Config.OrderCountermandInform_TwoLevelTip;
|
||
else if (order_dy.db_status == SystemOrderStatus.订单结算)
|
||
mess = Config.OrderSettlement_TwoLevelTip;
|
||
}
|
||
}
|
||
break;
|
||
case OrderNoticeType.三级提成:
|
||
if (Config.AgentReceivedCommissionSwitch == SwitchType.开启)
|
||
{
|
||
if (e.Customer != null)
|
||
{
|
||
if (order_dy.db_status == SystemOrderStatus.全额退款)
|
||
mess = Config.ClientOrderRefund_ThreeLevelTip;
|
||
else if (order_dy.db_status == SystemOrderStatus.订单付款)
|
||
mess = Config.OrderPaymentInform_ThreeLevelTip;
|
||
else if (order_dy.db_status == SystemOrderStatus.订单失效)
|
||
mess = Config.OrderCountermandInform_ThreeLevelTip;
|
||
else if (order_dy.db_status == SystemOrderStatus.订单结算)
|
||
mess = Config.OrderSettlement_ThreeLevelTip;
|
||
}
|
||
}
|
||
break;
|
||
case OrderNoticeType.群主分成:
|
||
if (Config.PrincipalReceivedCommissionSwitch == SwitchType.开启)
|
||
{
|
||
if (e.Customer != null)
|
||
{
|
||
if (order_dy.db_status == SystemOrderStatus.全额退款)
|
||
mess = Config.ClientOrderRefund_LeaderTip;
|
||
else if (order_dy.db_status == SystemOrderStatus.订单付款)
|
||
mess = Config.OrderPaymentInform_LeaderTip;
|
||
else if (order_dy.db_status == SystemOrderStatus.订单失效)
|
||
mess = Config.OrderCountermandInform_LeaderTip;
|
||
else if (order_dy.db_status == SystemOrderStatus.订单结算)
|
||
mess = Config.OrderSettlement_LeaderTip;
|
||
}
|
||
}
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
|
||
if (!ApiClient.Setting.SystemConfig.message_warning_switch && !string.IsNullOrEmpty(e.Member.username) && !string.IsNullOrWhiteSpace(mess))
|
||
//ApiClient.SendMessage(robot_info, e.Member.username, new VariateReplace().CommonReplace(mess, order_tb, e.Member, point).Replace("[下级昵称]", string.IsNullOrEmpty(e.Customer.realnick) ? e.Customer.usernick : e.Customer.realnick));
|
||
ApiClient.SendMessage(robot_info, e.Member.username, new VariateReplace().CommonReplace(mess, order_dy, e.Member, point).Replace("[下级昵称]", e.Customer.realnick ?? ""));
|
||
}
|
||
#endregion
|
||
}
|
||
else
|
||
{
|
||
#region 多人查询通知消息
|
||
var querys = session.Find<queryhist_temp>("select robot_name,type,userid from fl_query_hist where itemid=@itemid and adzoneid = @adzoneid and userid > 0 and crt_time>@time and is_multiple =@is_multiple group by userid,type,robot_name", new { itemid = order_dy.product_id, adzoneid = order_dy.db_cpsname, time = DateTime.Now.AddHours(-24).ToString("yyyy-MM-dd HH:mm:ss"), is_multiple = false });
|
||
if (querys.Count > 0)
|
||
{
|
||
for (int i = 0; i < querys.Count; i++)
|
||
{
|
||
try
|
||
{
|
||
if (querys[i].type != CpsType.阿里妈妈) continue;
|
||
//未绑定的数量
|
||
var unbound_count = int.Parse(session.FindTable($"select count(id) as num from fl_order_douyin where order_id = @order_id and db_userid != 0", new { order_id = order_dy.order_id }).Rows[0]["num"].ToString());
|
||
if (unbound_count != 0) break;//已经被绑定直接停止
|
||
|
||
var queryhist_temp = querys[i];
|
||
var userid = queryhist_temp.userid;
|
||
var robot_name = queryhist_temp.robot_name;
|
||
var robot_info = session.Find<fl_robot_info>("select * from fl_robot_info where name = @name", new { name = robot_name }).FirstOrDefault();//机器人类型应该是没有的,直接通过机器人name查询机器人信息
|
||
if (robot_info != null)
|
||
{
|
||
Thread.Sleep(5000);
|
||
var wxbase = Chat.Framework.ChatClient.WXClient.Values.ToList().FirstOrDefault(f => f.WeixinHao == robot_name);
|
||
if (wxbase != null /*&& wxbase.WeixinType == WeixinType.Grpc微信*/ && wxbase.Status == WxStatus.在线)
|
||
{
|
||
var member = session.FindMemberInfoById(userid);
|
||
if (member != null)
|
||
{
|
||
ApiClient.SendMessage(robot_info, member.username, Config.LotUserQueryBindTip.Replace("[商品标题]", order_dy.product_name));
|
||
|
||
session.ExcuteSQL("update fl_query_hist set is_multiple = @is_multiple where itemid = @itemid and adzoneid = @adzoneid and robot_name = @robotname and userid = @userid", new { is_multiple = true, itemid = order_dy.product_id, adzoneid = order_dy.db_cpsname, robotname = robot_name, userid = member.id });
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{ }
|
||
}
|
||
}
|
||
#endregion
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
this.OnLog(ex.Message);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 订单状态 提示语替换
|
||
/// </summary>
|
||
/// <param name="order"></param>
|
||
/// <param name="objs"></param>
|
||
/// <returns></returns>
|
||
public static string _GetOrderStateMess(SystemOrderStatus order, SettleType settle, params object[] objs)
|
||
{
|
||
var mess = string.Empty;
|
||
switch (order)
|
||
{
|
||
case SystemOrderStatus.订单付款:
|
||
mess = new VariateReplace().CommonReplace(Config.OrderPaymentTip, objs);
|
||
break;
|
||
case SystemOrderStatus.订单失效:
|
||
mess = new VariateReplace().CommonReplace(Config.OrderFailureTip, objs);
|
||
break;
|
||
case SystemOrderStatus.订单维权中:
|
||
//mess = new VariateReplace().CommonReplace(Config.OrderRefundTip, objs);
|
||
break;
|
||
case SystemOrderStatus.订单结算:
|
||
mess = new VariateReplace().CommonReplace(settle == SettleType.未结算提示 ? Config.OrderSettlementTip : Config.OrderAlreadySettlementTip, objs);
|
||
break;
|
||
case SystemOrderStatus.订单冻结:
|
||
mess = new VariateReplace().CommonReplace(Config.OrderFreezeTip, objs);
|
||
break;
|
||
case SystemOrderStatus.全额退款:
|
||
mess = new VariateReplace().CommonReplace(Config.OrderRefundTip, objs);
|
||
break;
|
||
}
|
||
return mess;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 结账提示类型
|
||
/// </summary>
|
||
public enum SettleType
|
||
{
|
||
已结算提示 = 0,
|
||
未结算提示 = 1
|
||
}
|
||
|
||
}
|
||
}
|