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

371 lines
18 KiB
C#
Raw 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 AllBase.Helper;
using AllBase.Model.Result;
using TemuKJD.KJDForms.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;
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 = "更新" + cookieVersion + "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 ? "国内版" : "国际版";
if (cookieVersion == "国内版")
{
string txt = this.txtCookieJs.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('\'');
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;
}
var cookieShop = new ShopLoginInfo();
cookieShop.Cookies = cookie;
var data = ShopListForm.GetShopStatus(cookieShop);
if (!data.Contains("\"success\":true"))
{
BaseMsgForm.ShowError("cookie已失效!");
return;
}
var maskMobile = BaseRegex.GetStringJson(ref data, "maskOwnerMobile");
if (!shopLoginInfo.LoginName.EndsWith(maskMobile))
{
BaseMsgForm.ShowError("cookie与店铺不匹配,请核对店铺!");
return;
}
List<ShopLoginInfo> serverShopLst = new List<ShopLoginInfo>();
var rst = AdminGetServerShopListForSetCookie(shopLoginInfo, serverShopLst);
if (rst != "success")
{
BaseMsgForm.ShowError(rst);
return;
}
//如果店铺没有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
this.OnSaveComplete(shopLoginInfo);
this.Close();
}
else // 国际版
{
string txt = this.txtCookieJs.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('\'');
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;
}
var cookieShop = new ShopLoginInfo();
cookieShop.Cookies2 = cookie2;
var data = ShopListForm.GetShopStatus2(cookieShop);
if (!data.Contains("\"success\":true"))
{
BaseMsgForm.ShowError("cookie已失效!");
return;
}
//var maskMobile = BaseRegex.GetStringJson(ref data, "maskOwnerMobile");
//if (!shopLoginInfo.LoginName.EndsWith(maskMobile))
if(!data.Contains(shopLoginInfo.ShopAccount))
{
BaseMsgForm.ShowError("cookie与店铺不匹配,请核对店铺!");
return;
}
List<ShopLoginInfo> serverShopLst = new List<ShopLoginInfo>();
var rst = AdminGetServerShopListForSetCookie(shopLoginInfo, serverShopLst);
if (rst != "success")
{
BaseMsgForm.ShowError(rst);
return;
}
//如果店铺没有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 = cookie2;
sc.LoginStatus = "登录成功";
}
}
shopLoginInfo.Cookies = cookie2;
shopLoginInfo.LoginStatus = "登录成功";
ShopListForm.UpdateServerShopCookies(shopLoginInfo); // 店铺cookie写入服务端,多店共用一个登录账号一组cookie
this.OnSaveComplete(shopLoginInfo);
this.Close();
}
}
/// <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)
AdminShopForm.AdminAddServerShop(newShop); // 店铺写入到服务端
else
ShopListForm.AddServerDouShop(newShop); // 店铺写入到服务端
}
}
}
public static string AdminGetServerShopListForSetCookie(ShopLoginInfo loginShop, List<ShopLoginInfo> lstRst)
{
if (lstRst == null)
lstRst = new List<ShopLoginInfo>();
var url = BaseClass.BuildObjString("shop/admin_get_shop_list");
var data = UserLogin.GetSignaturex() +
"&search_user_name=" + loginShop.UserName +
"&search_login_name=" + loginShop.LoginName;
var html = GetHtml.PostUtf8(url, data);
if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200"))
html = GetHtml.PostUtf8(url, data);
if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200"))
html = GetHtml.PostUtf8(url, data);
if (string.IsNullOrEmpty(html)
|| !html.Contains("shop_id") || !html.Contains("shop_account") || !html.Contains("login_pass"))
{
return "获取失败:" + BaseRegex.GetStringJson(ref html, "msg");
}
var jArr = JObject.Parse(html)["data"];
if (jArr.Count() == 0)
return "获取失败:未获取到data数据";
foreach (JObject jObj in jArr)
{
string shopStr = jObj.ToString();
ShopLoginInfo shop = new ShopLoginInfo();
shop.DbId = BaseRegex.GetStringJson(ref shopStr, "id");
if (shop.DbId == null)
continue;
shop.LoginName = BaseRegex.GetStringJson(ref shopStr, "login_name");
shop.Password = BaseRegex.GetStringJson(ref shopStr, "login_pass");
shop.ShopId = BaseRegex.GetStringJson(ref shopStr, "shop_id");
shop.ShopAccount = BaseRegex.GetStringJson(ref shopStr, "shop_account");
shop.UserName = BaseRegex.GetStringJson(ref shopStr, "user_name");
shop.SubUserName = BaseRegex.GetStringJson(ref shopStr, "sub_user_name");
shop.GroupName = BaseRegex.GetStringJson(ref shopStr, "shop_group");
shop.Cookies = BaseRegex.GetStringJson(ref shopStr, "cookie");
shop.Cookies = shop.Cookies.Replace("&", "&").Replace("%", "%").Replace("+", "+");
shop.Cookies2 = BaseRegex.GetStringJson(ref shopStr, "cookie2");
shop.Cookies2 = shop.Cookies2.Replace("&", "&").Replace("%", "%").Replace("+", "+");
shop.CompanyName = BaseRegex.GetStringJson(ref shopStr, "company_name");
shop.Notes = BaseRegex.GetStringJson(ref shopStr, "notes");
shop.LoginStatus = BaseRegex.GetStringJson(ref shopStr, "login_status");
shop.LoginStatus2 = BaseRegex.GetStringJson(ref shopStr, "login_status2");
shop.OperateSubSaName = BaseRegex.GetStringJson(ref shopStr, "operate_sub_sa_name");
lstRst.Add(shop);
}
return "success";
}
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复制成功");
}
}
}