Files
KJD/TEMUKJD/KJDForms/ShopForms/ShopUpdateCookieForm.cs
T

340 lines
15 KiB
C#

using AllBase.Helper;
using AllBase.Model.Result;
using TemuKJD.BaseForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AllBase;
using NaiBa.Util.Extension;
using Newtonsoft.Json.Linq;
using Work;
using TemuKJD.KJDForms.AdminForms;
using TemuKJD.BLL;
namespace TemuKJD.KJDForms.ShopForms
{
public enum SetCookieFromEnum
{
normal_user,
sa,
}
public partial class ShopUpdateCookieForm : LRBaseForm
{
ShopLoginInfo shopLoginInfo = null;
SetCookieFromEnum updateFrom;
string cookieVersion = "";
public ShopUpdateCookieForm(ShopLoginInfo shop = null, SetCookieFromEnum from = SetCookieFromEnum.normal_user, string _cookieVersion="国内版")
{
InitializeComponent();
shopLoginInfo = shop;
updateFrom = from;
cookieVersion = _cookieVersion;
this.TitleText = "更新Cookie";
if (cookieVersion == "国内版")
this.radioButton1.Checked = true;
else
this.radioButton2.Checked = true;
}
private void ShopUpdateCookieForm_Load(object sender, EventArgs e)
{
if (shopLoginInfo != null)
txtLoginName.Text = shopLoginInfo.LoginName + " " + shopLoginInfo.ShopAccount + " " + shopLoginInfo.Notes;
}
/*
localStorage.clear();
sessionStorage.clear();
var cookieDate = new Date(2037, 01, 01);
var cookieDateEnd = new Date(2000, 01, 01);
document.cookie = "SUB_PASS_ID=; path='/'; expires=" + cookieDateEnd.toUTCString();
document.cookie = "_nano_fp=; path='/'; expires=" + cookieDateEnd.toUTCString();
document.cookie = "api_uid=; path='/'; domain=.kuajingmaihuo.com; expires=" + cookieDateEnd.toUTCString();
document.cookie = "_bee=; path='/'; domain=.kuajingmaihuo.com; expires=" + cookieDateEnd.toUTCString();
document.cookie = "ru2k=; path='/'; domain=.kuajingmaihuo.com; expires=" + cookieDateEnd.toUTCString();
document.cookie = "_f77=; path='/'; domain=.kuajingmaihuo.com; expires=" + cookieDateEnd.toUTCString();
document.cookie = "_a42=; path='/'; domain=.kuajingmaihuo.com; expires=" + cookieDateEnd.toUTCString();
document.cookie = "rckk=; path='/'; domain=.kuajingmaihuo.com; expires=" + cookieDateEnd.toUTCString();
document.cookie = "ru1k=; path='/'; domain=.kuajingmaihuo.com; expires=" + cookieDateEnd.toUTCString();
document.cookie = "api_uid=CmP1wGhwa64RUwBSBILCAg==; path='/'; expires=" + cookieDate.toUTCString();
document.cookie = "_nano_fp=XpmyX5gbl09jlpdoXo_mijphq7nL0UmRaA77jyt3; domain=.kuajingmaihuo.com; path='/'; expires=" + cookieDate.toUTCString();
document.cookie = "_bee=NkjhfozSFfDuGSJ2zBlOqjOh3KVvJam3; path='/'; expires=" + cookieDate.toUTCString();
document.cookie = "_f77=2f854ce8-8d0f-4a78-84a1-04e19842a1cb; path='/'; expires=" + cookieDate.toUTCString();
document.cookie = "_a42=1e97ae4a-c357-4c22-a53f-db43655371e3; path='/'; expires=" + cookieDate.toUTCString();
document.cookie = "rckk=NkjhfozSFfDuGSJ2zBlOqjOh3KVvJam3; path='/'; expires=" + cookieDate.toUTCString();
document.cookie = "ru1k=2f854ce8-8d0f-4a78-84a1-04e19842a1cb; path='/'; expires=" + cookieDate.toUTCString();
document.cookie = "ru2k=1e97ae4a-c357-4c22-a53f-db43655371e3; path='/'; expires=" + cookieDate.toUTCString();
document.cookie = "SUB_PASS_ID=eyJ0IjoiRDZZY1F2N2JjMnZNK1E0eEZjTm1GVmxsN20yN3N3Ymg2dnVjdmxiclZPQW5uRkJlendUOUtKTHRoWHJyZWlNUCIsInYiOjEsInMiOjEwMDAwLCJ1IjoyMzAxMjcwOTkwNDI5OH0=; domain=.kuajingmaihuo.com; path='/'; expires=" + cookieDate.toUTCString();
location.reload();
window.location.href = 'https://seller.kuajingmaihuo.com/goods/product/list';
*/
private void btnOk_Click(object sender, EventArgs e)
{
if (shopLoginInfo == null)
{
BaseMsgForm.ShowError("店铺为空!");
return;
}
cookieVersion = this.radioButton1.Checked ? "国内版" : "国际版";
bool ok1 = false;
bool ok2 = false;
if (this.txtCookieJs1.Text.Contains("SUB_PASS_ID") || !string.IsNullOrWhiteSpace(txtSubPassId.Text))
{
ok1 = GetCookie1();
}
if (this.txtCookieJs2.Text.Contains("seller_temp") || !string.IsNullOrWhiteSpace(txtSellerTemp.Text)) // 国际版
{
ok2 = GetCookie2();
}
if (ok1 || ok2)
{
this.OnSaveComplete(shopLoginInfo);
this.Close();
}
}
bool GetCookie1()
{
string txt = this.txtCookieJs1.Text;
txt = txt.Replace("SUB_PASS_ID=;", "").Replace("_nano_fp=;", "").Replace("api_uid=;", "")
.Replace("_bee=;", "").Replace("ru2k=;", "").Replace("_f77=;", "")
.Replace("_a42=;", "").Replace("rckk=;", "").Replace("ru1k=;", "");
string api_uid = BaseRegex.GetStringCookie(ref txt, "api_uid").ParseToString().Trim('\'');
string _nano_fp = BaseRegex.GetStringCookie(ref txt, "_nano_fp").ParseToString().Trim('\'');
string _bee = BaseRegex.GetStringCookie(ref txt, "_bee").ParseToString().Trim('\'');
string _f77 = BaseRegex.GetStringCookie(ref txt, "_f77").ParseToString().Trim('\'');
string _a42 = BaseRegex.GetStringCookie(ref txt, "_a42").ParseToString().Trim('\'');
string rckk = BaseRegex.GetStringCookie(ref txt, "rckk").ParseToString().Trim('\'');
string ru1k = BaseRegex.GetStringCookie(ref txt, "ru1k").ParseToString().Trim('\'');
string ru2k = BaseRegex.GetStringCookie(ref txt, "ru2k").ParseToString().Trim('\'');
string SUB_PASS_ID = BaseRegex.GetStringCookie(ref txt, "SUB_PASS_ID").ParseToString().Trim('\'');
if (!string.IsNullOrWhiteSpace(this.txtSubPassId.Text))
SUB_PASS_ID = this.txtSubPassId.Text.Trim();
string cookie = "api_uid=" + api_uid + ";" +
"_nano_fp=" + _nano_fp + ";" +
"_bee=" + _bee + ";" +
"_f77=" + _f77 + ";" +
"_a42=" + _a42 + ";" +
"rckk=" + rckk + ";" +
"ru1k=" + ru1k + ";" +
"ru2k=" + ru2k + ";" +
"SUB_PASS_ID=" + SUB_PASS_ID + ";";
if (string.IsNullOrEmpty(SUB_PASS_ID))
{
BaseMsgForm.ShowError("Cookie缺少关键字段,请检查!");
return false;
}
var cookieShop = new ShopLoginInfo();
cookieShop.Cookies = cookie;
var data = "";
data = ShopListForm.GetShopStatus(cookieShop);
if (!data.Contains("\"success\":true"))
{
BaseMsgForm.ShowError("cookie已失效!");
return false;
}
var maskMobile = BaseRegex.GetStringJson(ref data, "maskOwnerMobile");
if (!shopLoginInfo.LoginName.EndsWith(maskMobile))
{
BaseMsgForm.ShowError("cookie与国内版店铺不匹配,请核对店铺!");
return false;
}
List<ShopLoginInfo> serverShopLst = new List<ShopLoginInfo>();
var rst = ShopBLL.AdminGetServerShopLstForSetCookie(shopLoginInfo, serverShopLst);
if (rst != "success")
{
BaseMsgForm.ShowError(rst);
return false;
}
//如果店铺没有cookie登录过,处理店铺第一次登录(分裂cookie下的所有店铺)
GetShopFirstLogin(data, serverShopLst);
if (updateFrom == SetCookieFromEnum.normal_user) // 普通用户更新本地店铺
{
var localShop = GlobalConfig.ShopLoginList.Where(v => v.LoginName == shopLoginInfo.LoginName).ToList();
foreach (var sc in localShop)
{
sc.Cookies = cookie;
sc.LoginStatus = "登录成功";
}
}
shopLoginInfo.Cookies = cookie;
shopLoginInfo.LoginStatus = "登录成功";
ShopListForm.UpdateServerShopCookies(shopLoginInfo); // 店铺cookie写入服务端,多店共用一个登录账号一组cookie
return true;
}
bool GetCookie2()
{
string txt = this.txtCookieJs2.Text;
txt = txt.Replace("_bee=;", "").Replace("_nano_fp=;", "").Replace("api_uid=;", "")
.Replace("dilx=;", "").Replace("hfsc=;", "").Replace("mallid=;", "")
.Replace("njrpl=;", "").Replace("seller_temp=;", "");
string api_uid = BaseRegex.GetStringCookie(ref txt, "api_uid").ParseToString().Trim('\'');
string _nano_fp = BaseRegex.GetStringCookie(ref txt, "_nano_fp").ParseToString().Trim('\'');
string _bee = BaseRegex.GetStringCookie(ref txt, "_bee").ParseToString().Trim('\'');
string dilx = BaseRegex.GetStringCookie(ref txt, "dilx").ParseToString().Trim('\'');
string hfsc = BaseRegex.GetStringCookie(ref txt, "hfsc").ParseToString().Trim('\'');
string mallid = BaseRegex.GetStringCookie(ref txt, "mallid").ParseToString().Trim('\'');
string njrpl = BaseRegex.GetStringCookie(ref txt, "njrpl").ParseToString().Trim('\'');
string seller_temp = BaseRegex.GetStringCookie(ref txt, "seller_temp").ParseToString().Trim('\'');
if (!string.IsNullOrWhiteSpace(txtSellerTemp.Text))
seller_temp = txtSellerTemp.Text.Trim();
string cookie2 = "api_uid=" + api_uid + ";" +
"_nano_fp=" + _nano_fp + ";" +
"_bee=" + _bee + ";" +
"dilx=" + dilx + ";" +
"hfsc=" + hfsc + ";" +
"mallid=" + mallid + ";" +
"njrpl=" + njrpl + ";" +
"seller_temp=" + seller_temp + ";";
if (string.IsNullOrEmpty(seller_temp))
{
BaseMsgForm.ShowError("Cookie缺少关键字段,请检查!\r\n请从Temu国际版https://agentseller.temu.com处获得cookie");
return false;
}
var cookieShop = new ShopLoginInfo();
cookieShop.Cookies2 = cookie2;
var data = ShopListForm.GetShopStatus2(cookieShop);
if (!data.Contains("\"success\":true"))
{
BaseMsgForm.ShowError("cookie已失效!");
return false;
}
//var maskMobile = BaseRegex.GetStringJson(ref data, "maskOwnerMobile");
//if (!shopLoginInfo.LoginName.EndsWith(maskMobile))
if (!data.Contains(shopLoginInfo.ShopAccount))
{
BaseMsgForm.ShowError("cookie与国际版店铺不匹配,请核对店铺!");
return false;
}
List<ShopLoginInfo> serverShopLst = new List<ShopLoginInfo>();
var rst = ShopBLL.AdminGetServerShopLstForSetCookie(shopLoginInfo, serverShopLst);
if (rst != "success")
{
BaseMsgForm.ShowError(rst);
return false;
}
//如果店铺没有cookie登录过,处理店铺第一次登录(分裂cookie下的所有店铺)
GetShopFirstLogin(data, serverShopLst);
if (updateFrom == SetCookieFromEnum.normal_user) // 普通用户更新本地店铺
{
var localShop = GlobalConfig.ShopLoginList.Where(v => v.LoginName == shopLoginInfo.LoginName).ToList();
foreach (var sc in localShop)
{
sc.Cookies2 = cookie2;
sc.LoginStatus2 = "国际版在线";
}
}
shopLoginInfo.Cookies2 = cookie2;
shopLoginInfo.LoginStatus2 = "国际版在线";
ShopListForm.UpdateServerShopCookies(shopLoginInfo); // 店铺cookie写入服务端,多店共用一个登录账号一组cookie
return true;
}
/// <summary>
/// 如果店铺没有cookie登录过,处理店铺第一次登录(分裂cookie下的所有店铺)
/// </summary>
/// <param name="loginData"></param>
void GetShopFirstLogin(string loginData, List<ShopLoginInfo> lstServerShop)
{
if (this.shopLoginInfo == null)
return;
if (lstServerShop == null)
lstServerShop = new List<ShopLoginInfo>();
List<ShopCompany> lstShopCompany = new List<ShopCompany>();
var jObj = JObject.Parse(loginData);
var jArr = jObj["result"]?["companyList"]?[0]?["malInfoList"] as JArray;
if (jArr == null)
jArr = new JArray();
foreach (var productObj in jArr)
{
var prod = JsonHelper.SerializeObject(productObj);
ShopCompany sc = new ShopCompany()
{
AccountId = BaseRegex.GetStringJson(ref prod, "mallId"),
AccountName = BaseRegex.GetStringJson(ref prod, "mallName"),
LoginName = shopLoginInfo.LoginName,
UserName = BaseClass.UserName,
LoginStatus = "登录成功",
};
lstShopCompany.Add(sc);
}
var lstShopCompanyCopy = lstShopCompany.ToList();
foreach (ShopCompany sc in lstShopCompanyCopy)
{
ShopLoginInfo newShop = new ShopLoginInfo()
{
UserName = shopLoginInfo.UserName,
LoginName = shopLoginInfo.LoginName,
Password = shopLoginInfo.Password,
GroupName = shopLoginInfo.GroupName,
ShopAccount = sc.AccountName,
ShopId = sc.AccountId,
SubUserName = shopLoginInfo.SubUserName,
Notes = shopLoginInfo.Notes,
};
var oldServerShop = lstServerShop.FirstOrDefault(s =>
s.LoginName == newShop.LoginName && s.ShopId == newShop.ShopId);
if (oldServerShop == null)
{
if (updateFrom != SetCookieFromEnum.normal_user)
ShopBLL.AdminAddServerShop(newShop); // 店铺写入到服务端
else
ShopBLL.AddServerShop(newShop); // 店铺写入到服务端
}
}
}
private void btnGetCookie_Click(object sender, EventArgs e)
{
//复制cookie
var cookJs = @"
document.body.innerHTML = '<button id=""button1"" onclick=""geCookie();"" style=""margin:10px;"">点击我复制cookie</button>' + document.body.innerHTML;
function geCookie()
{
var cookieText = document.cookie;
navigator.clipboard.writeText(cookieText).then(function() {
alert('复制cookie成功!');
}, function(err) {
alert('复制cookie失败: ', err);
});
}";
Clipboard.SetDataObject(cookJs);
BaseMsgForm.ShowInfo("js复制成功");
}
}
}