using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Data;
|
using System.Drawing;
|
using System.IO;
|
using System.Linq;
|
using System.Runtime.InteropServices;
|
using System.Security.Cryptography.X509Certificates;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
using System.Windows.Forms;
|
using CefSharp;
|
using CefSharp.WinForms;
|
using WindowsFormsApp1.utils.tb;
|
using static System.Collections.Specialized.NameObjectCollectionBase;
|
using WindowsFormsApp1.entity.tb;
|
using WindowsFormsApp1.utils;
|
|
namespace WindowsFormsApp1
|
{
|
|
public delegate void TBLoginSuccessDelegate();
|
public partial class TBLogin : Form
|
{
|
public ChromiumWebBrowser browser;
|
private int position;
|
|
private bool hasShown=false;//是否已经展示过
|
|
public bool HasShown {
|
get { return this.hasShown; }
|
}
|
|
private void init()
|
{
|
InitializeComponent();
|
InitBrowser();
|
this.timer1.Start();
|
}
|
|
public TBLogin()
|
{
|
init();
|
}
|
|
delegate void ChangeShow(bool show);
|
|
void setShow(bool show) {
|
//this.Visible = show;
|
if (!show)
|
{
|
//准备要刷新的链接
|
if (this.Visible) {
|
browser.Load("https://m.tb.cn/h.VN61jro?sm=9ed9c4");
|
this.Visible = false;
|
hasShown = true;
|
if (this.loginSuccess != null)
|
this.loginSuccess();
|
}
|
|
|
}
|
else {
|
browser.Reload();
|
}
|
}
|
|
|
|
public TBLogin(int position )
|
{
|
this.position = position;
|
init();
|
}
|
|
private TBLoginSuccessDelegate loginSuccess;
|
public void SetTBLoginSuccess(TBLoginSuccessDelegate loginSuccess) {
|
this.loginSuccess = loginSuccess;
|
}
|
|
private void InitBrowser()
|
{
|
string url = "http://login.taobao.com/member/login.jhtml?style=mini&newMini2=true&redirectURL=https%3a%2f%2fm.tb.cn%2fh.VOj5T1G%3fsm%3d7d686c&full_redirect=true&disableQuickLogin=true";
|
var setting = new RequestContextSettings()
|
{
|
CachePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BKZ\\Cache_tb_" + position),
|
PersistSessionCookies = true,
|
PersistUserPreferences = true
|
};
|
var context = new RequestContext(setting);
|
var cookieManager = context.GetCookieManager(null);
|
browser = new ChromiumWebBrowser(url, context);
|
TBCookieDelegate tbCookieDelegate = setCookies;
|
|
|
browser.RequestHandler = new TBLoginRequestHandler("https://h5api.m.tmall.com/h5/mtop.tmall.kangaroo.core.service.route.aldlampservicefixedres/1.0/?", tbCookieDelegate);
|
|
this.Controls.Clear();
|
this.Controls.Add(browser);
|
browser.Dock = DockStyle.Fill;
|
}
|
|
public void ReLogin() {
|
this.Visible = true;
|
InitBrowser();
|
}
|
|
|
|
//刷新Cookie
|
public void RefeshCookie() {//登录是否有效
|
if (TBCookieUtil.IsLoginValid(position))
|
{
|
Console.WriteLine(position + ":刷新Cookie");
|
String url = "https://m.tb.cn/h.VN61jro?sm=9ed9c4";
|
CefSharp.WebBrowserExtensions.Reload(browser, true);
|
}
|
}
|
|
|
private void setCookies(Dictionary<String, String> cookiesMap, String cookies,String url) {
|
|
if (!Constant.sdljBaseUrl.ContainsKey(position) || Constant.sdljBaseUrl[position] == null) {
|
Constant.sdljBaseUrl.Add(position, url);
|
}
|
if (TBCookieUtil.updateCookie(position, cookies)) {
|
if (this.Visible == true && !this.IsDisposed)
|
{
|
this.BeginInvoke(new ChangeShow(setShow), false);
|
}
|
}
|
}
|
|
private void TBLogin_FormClosed(object sender, FormClosedEventArgs e)
|
{
|
if(Login.loginContainer.ContainsKey(position)){
|
Login.loginContainer.Remove(position);
|
}
|
}
|
|
private void timer1_Tick(object sender, EventArgs e)
|
{
|
if (this.Visible == false)
|
{
|
Console.WriteLine(position + ":刷新Cookie");
|
String url = "https://m.tb.cn/h.VN61jro?sm=9ed9c4";
|
browser.Load(url);
|
}
|
}
|
}
|
}
|