using DevExpress.XtraEditors;
using NPOI.OpenXmlFormats.Spreadsheet;
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.Web;
using System.Windows.Forms;
using Yw;
using Yw.Dto;
namespace HStation.Desktop
{
public partial class WechatLoginCtrl : DevExpress.XtraEditors.XtraUserControl, ILogin
{
public WechatLoginCtrl()
{
InitializeComponent();
}
///
/// 登录开始事件
///
public event Action LoginStartEvent;
///
/// 登录取消事件
///
public event Action LoginCancelEvent;
///
/// 登录完成事件
///
public event Action LoginEndEvet;
private bool _isInitialized = false;//初始化
private const string _template = "hzkw_wx_template";//微信登录模板
private const string _software = "HStation_XHS_DESKTOP";//软件编码
//获取登录URL
private async Task GetLoginUrl()
{
if (string.IsNullOrEmpty(_loginUrl))
{
_loginUrl = await BLLFactory.Instance.GetLoginUrl(_template);
}
return _loginUrl;
}
private string _loginUrl;
//重置登录URL
private async Task ResetLoginUrl()
{
var loginUrl = await GetLoginUrl();
this.webView21.Source = new Uri(loginUrl);
}
///
/// 初始化
///
public async void Initial()
{
if (_isInitialized)
{
return;
}
_isInitialized = true;
await this.webView21.EnsureCoreWebView2Async();
var loginUrl = await GetLoginUrl();
this.webView21.Source = new Uri(loginUrl);
this.webView21.CoreWebView2.SourceChanged += CoreWebView2_SourceChanged;
}
//源改变
private async 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();
UserLoginOutput loginUserInfo = null;
try
{
loginUserInfo = await BLLFactory.Instance.LoginSoftwareStandardByWechatAccount(_template, _software, code, null);
if (loginUserInfo.Status != Yw.Auth.eLoginStatus.Success)
{
await ResetLoginUrl();
}
}
catch
{
await ResetLoginUrl();
}
LoginEndEvet?.Invoke(loginUserInfo);
}
}
}
}