Files
KJD_WebService/TEMUKJD/KJDForms/UploadForms/CleanDataForm.cs
T
2025-06-29 19:38:31 +08:00

443 lines
17 KiB
C#

using AllBase;
using AllBase.Helper;
using AllBase.Model.Param.FissionManage;
using AllBase.Model.Param.ProductManage;
using AllBase.Model.Result;
using LittleRabbit.LittleRabbitForms.BaseForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Work.Api;
using Work.BLL.Upload;
using Work.CollectClass;
namespace LittleRabbit.LittleRabbitForms.UploadForms
{
public partial class CleanDataForm : Form
{
private string cleaningData = "";
private string sFromUrl = "";
private string path { get; set; } = "temp/FissionBackup.txt";
private delegate ProductCollectBase deleTaoBaoCollect(ProductCollectBase pcb);
private delegate ProductCollectBase delePingDuoDuoCollect(TaobaoProductBase pcb);
private delegate void deleChangePicVisible();
public static List<Fission> LstFissionForm = new List<Fission>();
public CleanDataForm()
{
InitializeComponent();
}
private void CleanDataForm_Load(object sender, EventArgs e)
{
}
private void btnSearch_Click(object sender, EventArgs e)
{
sFromUrl = cleaningData.Trim();
// 读取本地看是否有备份文件 提示存在备用数据,是否载入,点是载入,点否丢弃并删除备用数据,
if (FileBase.Exists(path))
{
if (BaseMsgForm.ShowConfirm("是否需要载入备份数据", "提示").DialogResult == DialogResult.OK)
{
//载入
using (StreamReader str = new StreamReader(path, System.Text.Encoding.Default))
{
sFromUrl += "\r\n" + str.ReadToEnd().Trim();
}
}
else
{
//FileBase.Delete(path);
}
}
try
{
CollectProductFromUrl();
}
catch (Exception ex)
{
BaseMsgForm.ShowError("未正确获取到商品信息!(请确认商品ID是否正确以及网络)");
}
}
private void btnImport_Click(object sender, EventArgs e)
{
BaseMsgForm.ShowInfo("格式1:商品地址\r\n" +
"格式2:关键词-----商品地址\r\n", "导入格式");
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "文本文件(*.txt)|*.txt";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string fName = openFileDialog.FileName;
string line = string.Empty;
using (StreamReader sr = new StreamReader(openFileDialog.FileName, System.Text.Encoding.Default))
{
txtProduct.Text = string.Empty;
txtProduct.Text = sr.ReadToEnd();
}
}
}
private void pbPaste_Click(object sender, EventArgs e)
{
IDataObject iData = Clipboard.GetDataObject();
if (iData != null && iData.GetDataPresent(DataFormats.Text))
{
txtProduct.Text = (string)iData.GetData(DataFormats.Text);
}
}
private void CollectProductFromUrl(bool showLoading = true)
{
string url = sFromUrl.Split(new string[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
if (url == null)
{
BaseMsgForm.ShowError("无效网址!");
return;
}
if (url.Contains("-----"))
{
url = url.Substring(url.IndexOf("-----") + 5);
}
ProductCollectBase proInfo = new ProductCollectBase { from_url = url };
// 拼多多商品采集
if (proInfo.from_url.Contains(@"yangkeduo.com"))
{
TaobaoProductBase tp = new TaobaoProductBase();
tp.GoodsId = tp.FromId = GetIdFromURLByRegex(proInfo.from_url);
tp.FromType = "拼多多";
if (tp.GoodsId.Equals(0))
{
BaseMsgForm.ShowError("无效网址!");
return;
}
if (showLoading) this.pictureBox1.Visible = true;
delePingDuoDuoCollect del_pddc = PddProductCollect;
del_pddc.BeginInvoke(tp, ProductCollectCompleted, del_pddc);
}
// 其他平台商品采集
else
{
if (!AllBase.BaseClass.IsCloudBuyOrder)
{
// 非公司用户暂时关闭除拼多多以外所有商品上传
BaseMsgForm.ShowError("无效网址!");
return;
}
proInfo.from_id = GetIdFromURLByRegex(proInfo.from_url);
if (proInfo.from_id.Equals(0))
{
BaseMsgForm.ShowError("无效网址!");
return;
}
if (showLoading) this.pictureBox1.Visible = true;
deleTaoBaoCollect del_tbc = ProductCollect;
del_tbc.BeginInvoke(proInfo, ProductCollectCompleted, del_tbc);
}
}
private void ProductCollectCompleted(IAsyncResult ar)
{
if (ar == null) throw new ArgumentNullException("ar");
deleTaoBaoCollect delTaoBaoCollect = ar.AsyncState as deleTaoBaoCollect;
delePingDuoDuoCollect delPinDuoDuo = ar.AsyncState as delePingDuoDuoCollect;
if (delTaoBaoCollect != null || delPinDuoDuo != null)
{
ProductCollectBase res;
if (delPinDuoDuo != null)
{
res = delPinDuoDuo.EndInvoke(ar);
}
else
{
res = delTaoBaoCollect.EndInvoke(ar);
}
// 先判断是否存在page.html临时文件,存在就先删除。
if (System.IO.File.Exists(System.IO.Directory.GetCurrentDirectory() + @"\page.html"))
{
System.IO.File.Delete(System.IO.Directory.GetCurrentDirectory() + @"\page.html");
}
// 调用php后台
string createHtmlPath = Fission.creatFissionPage(res);
ShowFormInUiThread(createHtmlPath, res);
}
}
internal ProductCollectBase ProductCollect(ProductCollectBase pcb)
{
if (pcb.from_url.Contains("detail.1688.com"))
{
AlibabaProductGet proInfo = new AlibabaProductGet(pcb);
proInfo.Get();
pcb.cat_id = FromTitleToCatTaobao.Get(pcb.name);
if (proInfo.Product.Images.Count == 0)
{
return null;
}
proInfo.Product.from = "阿里巴巴";
return proInfo.Product;
}
if (pcb.from_url.Contains("jinritemai.com"))
{
CollectDoyin cd = new CollectDoyin(pcb);
cd.GetProduct();
if (!cd.GetSuccess)
{
BaseMsgForm.ShowInfo("采集失败,跳过:" + pcb.from_id + ":" + pcb.message);
return null;
}
if (cd.Product?.IsOnSale == false)
{
BaseMsgForm.ShowInfo("商品已下架:" + pcb.from_id);
return null;
}
cd.Product.from = "抖音";
cd.Product.from_url = pcb.from_url;
return cd.Product;
}
if (pcb.from_url.Contains("jd.com"))
{
bool isPassVerify = false;
retry:
ProductCollectBase proInfo = new ProductCollectBase
{
from_url = "https://item.m.jd.com/product/" + pcb.from_id + ".html",
from_id = Convert.ToInt64(pcb.from_id),
};
JingXiCollect tm = new JingXiCollect(proInfo);
tm.IsUseProxy = GlobalConfig.FissionSettingParam.IsUseProxyIp;
tm.Get();
if (!string.IsNullOrEmpty(JingXiCollect.VerifyCodeUrl))
{
if (!isPassVerify && JingXiCollect.VerifyCodeUrl.Contains("wqs.jd.com/item/spider_check.shtml")) // 京喜采集验证码校验 by ttzhao 2020.6.29
{
//Fission.JingXiVerify(JingXiCollect.VerifyCodeUrl);
//if (string.IsNullOrEmpty(JingXiCollect.VerifyCodeUrl))
//{
// isPassVerify = true;
// goto retry;
//}
}
BaseMsgForm.ShowInfo("获取失败,京东商品采集需要验证");
return null;
}
else if ((tm.Product != null && tm.Product.Images.Count == 0) || tm.IsOnSale == false)
{
Invoke(new Action(() =>
{
BaseMsgForm.ShowInfo("获取失败,商品不存在或不可展示");
}));
return null;
}
tm.Product.from = "京喜";
return tm.Product;
}
TaobaoMobileCollect goodsFormInfo = new TaobaoMobileCollect(pcb);
goodsFormInfo.IsPurifyData = true;
try
{
goodsFormInfo.Get();
}
catch (Exception ex)
{
LogSave.Add("淘宝商品采集出现异常:" + ex.Message + "-" + ex.StackTrace);
}
goodsFormInfo.Product.from = "淘宝";
return goodsFormInfo.Product;
}
internal ProductCollectBase PddProductCollect(TaobaoProductBase tp)
{
int retryTime = 0;
ProductCollectBase proInfo;
Retry:
PinduoduoProductCollect pdd = new PinduoduoProductCollect(Name, tp);
PinduoduoProductCollect.TempIds.Clear();
pdd.IsUseProxy = GlobalConfig.FissionSettingParam.IsUseProxyIp;
pdd.Get();
if (GlobalConfig.FissionSettingParam.IsUseProxyIp && pdd.IsOverdue)
{
BaseMsgForm.ShowInfo("代理ip套餐已过期", "提示");
}
bool isResourceEnable = true;
if (GlobalConfig.FissionSettingParam.IsUseResourcePack)
{
//Func<bool> canContinue = PinDuoDuoHelperTemp.CanContinueCollectFunc((token) => { return token.ProductDetailStatus == TokenStatus.Valid; }, null, GlobalConfig.FissionSettingParam.IsUseResourcePack, null);
//if (!canContinue())
//{
// isResourceEnable = false;
// BaseMsgForm.ShowInfo("资源包打码不可用!", "提示");
//}
}
if (pdd.IsNoToken)
{
if (GlobalConfig.FissionSettingParam.IsUseResourcePack && isResourceEnable == true && retryTime < 2)
{
retryTime++;
goto Retry;
}
else
BaseMsgForm.ShowInfo("请先登录拼多多或输入Token!", "提示");
}
proInfo = pdd.pb;
proInfo.from_url = proInfo.from_url.Contains(@"mobile.yangkeduo.com") ? "http://mobile.yangkeduo.com/goods.html?goods_id=" + tp.GoodsId : "http://yangkeduo.com/goods.html?goods_id=" + tp.GoodsId;
return proInfo;
}
internal long GetIdFromURLByRegex(string url)
{
try
{
if (url.Contains(@"yangkeduo.com"))
{
Regex pddurl = new Regex(@"[?&]goods_id=([^&#]+)");
string id = pddurl.Match(url).ToString();
//Regex idRegex = new Regex(@"[^0-9]+");
string Num = Regex.Replace(id, @"[^0-9]+", "");
return long.Parse(Num);
}
if (url.Contains(@"1688.com"))
{
string str = url.Substring(0, url.IndexOf(".html")).Replace(@"1688.com", "");
string num = Regex.Replace(str, @"[^0-9]+", "");
return long.Parse(num);
}
if (url.Contains("jd.com"))
{
//https://item.jd.com/69126349289.html
string str = url.Substring(url.IndexOf("jd.com/")).Replace(".html", "");
string num = Regex.Replace(str, @"[^0-9]+", "");
return long.Parse(num);
}
if (url.Contains("jinritemai.com"))
{
Regex pddurl = new Regex(@"[?&]id=([^&#]+)");
string id = pddurl.Match(url).ToString();
string Num = Regex.Replace(id, @"[^0-9]+", "");
return long.Parse(Num);
}
Regex urlRegex = new Regex(@"[?&]id=([^&#]+)");
string idStr = urlRegex.Match(url).ToString();
//Regex idRegex = new Regex(@"[^0-9]+");
string idNum = Regex.Replace(idStr, @"[^0-9]+", "");
return long.Parse(idNum);
}
catch (Exception ex)
{
return 0;
}
}
private void chkIsUseProxy_CheckedChanged(object sender, EventArgs e)
{
GlobalConfig.FissionSettingParam.IsUseProxyIp = chkIsUseProxy.Checked;
if (GlobalConfig.FissionSettingParam.IsUseProxyIp)
chkIsUseResourcePack.Checked = false;
if (chkIsUseProxy.Checked)
{
//if (string.IsNullOrEmpty(GlobalConfig.ConfigParam.ProxyUrl))
//{
// BaseMsgForm.ShowInfo("请检测代理ip设置是否正确或过期");
// chkIsUseProxy.Checked = false;
// return;
//}
//bool bo = PinDuoDuoHelperTemp.CheckProxyStatus(GlobalConfig.FissionSettingParam.IsUseProxyIp, null);
//if (!bo)
//{
// chkIsUseProxy.Checked = false;
//}
}
//StartUp.SaveFissionSettingParam();
}
private void txtProduct_TextChanged(object sender, EventArgs e)
{
if (txtProduct.Text.Length > 2000)
{
txtProduct.TextChanged -= txtProduct_TextChanged;
cleaningData = txtProduct.Text;
txtProduct.Text = cleaningData.Substring(0, 2000);
txtProduct.TextChanged += txtProduct_TextChanged;
}
else
{
cleaningData = txtProduct.Text;
}
if (this.cleaningData.Contains(@"yangkeduo.com"))
{
buttonLoginPdd.Visible = true;
}
else
{
buttonLoginPdd.Visible = false;
}
}
private void ShowFormInUiThread(string createHtmlPath, ProductCollectBase detailData)
{
Task.Run(() =>
{
//获取所有类目
//Invoke(new Action(() =>
//{
// label1.Visible = true;
//}));
//Fission.goodsCategoryList = Fission.GetShopGoodsCategories();
Invoke(new Action(() =>
{
Fission fs = new Fission(createHtmlPath, sFromUrl, null, detailData);
label1.Visible = false;
fs.FormLoadedEvent = formLoadEvent;
fs.Show();
}));
});
}
private void formLoadEvent()
{
ChangePicVisible();
}
private void ChangePicVisible()
{
if (this.pictureBox1.InvokeRequired)
{
deleChangePicVisible delChangePic = ChangePicVisible;
this.pictureBox1.Invoke(delChangePic);
}
else
{
this.pictureBox1.Visible = false;
}
}
}
}