duheng
2025-03-05 0f831db8df9c2e4adc7feca636967a0fb1cd5e29
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
using DevExpress.XtraEditors;
using HStation.RevitDev.RevitDataExport.Common;
using Newtonsoft.Json.Linq;
using System;
using System.Net.Http;
using System.Security.Policy;
using System.Text;
using System.Web;
 
namespace HStation.RevitDev.RevitDataExport
{
    public partial class WechatLoginCtrl : DevExpress.XtraEditors.XtraUserControl, ILogin
    {
        public WechatLoginCtrl()
        {
            InitializeComponent();
        }
 
        /// <summary>
        /// 登录开始事件
        /// </summary>
        public event Action LoginStartEvent;
 
        /// <summary>
        /// 登录取消事件
        /// </summary>
        public event Action LoginCancelEvent;
 
        /// <summary>
        /// 登录完成事件
        /// </summary>
        public event Action<HttpResultModel, string> LoginEndEvet;
 
        private bool _isInitialized = false;//初始化
        private const string _template = "hzkw_wx_template";//微信登录模板
 
        //获取登录URL
        private string GetLoginUrl()
        {
            if (string.IsNullOrEmpty(_loginUrl))
            {
                using (HttpClient client = new HttpClient())
                {
                    var res = client.GetAsync(GlobalResource.ConfigSettingModel.ApiUrl + $"/Auth/Tool/Wechat/GetLoginUrl@V1.0?TemplateCode={_template}")
                    .Result.Content.ReadAsStringAsync().Result;
                    var rst = JsonHelper.ToObject<JObject>(res);
                    string code = (string)rst["Code"];
                    if (code == "0")
                    {
                        string url = (string)rst["Data"];
                        if (url != null)
                        {
                            _loginUrl = url;
                            return _loginUrl;
                        }
                    }
                }
            }
            return _loginUrl;
        }
 
        private string _loginUrl;
 
        //重置登录URL
        private void ResetLoginUrl()
        {
            var loginUrl = GetLoginUrl();
 
            this.webView21.Source = new Uri(loginUrl);
        }
 
        /// <summary>
        /// 初始化
        /// </summary>
        public async void Initial()
        {
            if (_isInitialized)
            {
                return;
            }
            _isInitialized = true;
            await this.webView21.EnsureCoreWebView2Async();
            var loginUrl = GetLoginUrl();
            this.webView21.Source = new Uri(loginUrl);
            this.webView21.CoreWebView2.SourceChanged += CoreWebView2_SourceChanged;
        }
 
        //源改变
        private void CoreWebView2_SourceChanged(object sender, Microsoft.Web.WebView2.Core.CoreWebView2SourceChangedEventArgs e)
        {
            var collection = HttpUtility.ParseQueryString(this.webView21.Source.Query);
            var code = collection["code"];
            var state = collection["state"];
            if (!string.IsNullOrEmpty(code))
            {
                LoginStartEvent?.Invoke();
                try
                {
                    var loginModel = new
                    {
                        TemplateCode = _template,
                        Software = GlobalResource.ConfigSettingModel.Software,
                        WeChatCode = code,
                    };
                    using (var client = new HttpClient())
                    {
                        var content = new StringContent(JsonHelper.ToJson(loginModel), Encoding.UTF8, "application/json");
                        var res = client.PostAsync(GlobalResource.ConfigSettingModel.ApiUrl + "/Auth/User/Login/LoginSoftwareStandardByWechatAccount@V1.0", content)
                           .Result.Content.ReadAsStringAsync().Result;
                        var result = JsonHelper.ToObject<HttpResultModel>(res);
                        this.LoginEndEvet?.Invoke(result, res);
                        ResetLoginUrl();
                    }
                }
                catch (Exception ex)
                {
                    ResetLoginUrl();
                }
            }
        }
    }
}