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();
|
}
|
}
|
}
|
}
|
}
|