admin
2020-06-17 9d3d08ba960fc739498b0648d57eaf2c50a40fd1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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);
            }
        }
    }
}