🆙优化bridge交互的参数;

完善web登录成功后回调插件的数据交互
This commit is contained in:
LittleBoy 2024-02-04 14:48:32 +08:00
parent 07b119f3e9
commit 0aa3757b52
11 changed files with 74 additions and 9 deletions

View File

@ -1,10 +1,16 @@
using Microsoft.Web.WebView2.Core; using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms; using Microsoft.Web.WebView2.WinForms;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace AIProofread namespace AIProofread
{ {
public enum BridgeEvent
{
LoginSuccess
}
public delegate void BridgeEventHandler(object sender, EventArgs e);
[ClassInterface(ClassInterfaceType.AutoDual)] [ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)] [ComVisible(true)]
public class Bridge public class Bridge
@ -13,12 +19,49 @@ namespace AIProofread
private static readonly Dictionary<string, WebView2> webViewDict = new Dictionary<string, WebView2>(); private static readonly Dictionary<string, WebView2> webViewDict = new Dictionary<string, WebView2>();
private static readonly Dictionary<BridgeEvent, List<BridgeEventHandler>> eventHandlers = new Dictionary<BridgeEvent, List<BridgeEventHandler>>();
public void showDialog(string message) public void showDialog(string message)
{ {
System.Windows.Forms.MessageBox.Show(message); System.Windows.Forms.MessageBox.Show(message);
} }
/// <summary>
/// 添加事件
/// </summary>
/// <param name="eventName"></param>
/// <param name="handler"></param>
public static void AddEventHandler(BridgeEvent eventName, BridgeEventHandler handler)
{
if (!eventHandlers.ContainsKey(eventName))
{
eventHandlers.Add(eventName, new List<BridgeEventHandler>());
}
eventHandlers[eventName].Add(handler);
}
public static void RemoveEventHandler(BridgeEvent eventName, BridgeEventHandler handler)
{
if (eventHandlers.ContainsKey(eventName))
{
eventHandlers[eventName].Remove(handler);
}
}
/// <summary>
/// 登录成功后通知后端
/// </summary>
public void loginSuccess(string loginToken)
{
showDialog(string.Format("登录成功,token:{0}", loginToken));
if (eventHandlers.ContainsKey(BridgeEvent.LoginSuccess))
{
foreach (var eventHandler in eventHandlers[BridgeEvent.LoginSuccess])
{
eventHandler(null, null);
}
}
}
// 获取文档所有文本数据 // 获取文档所有文本数据
public string getAllText() public string getAllText()
{ {

View File

@ -5,15 +5,25 @@ namespace AIProofread.Controls
{ {
public partial class FormLogin : Form public partial class FormLogin : Form
{ {
public event EventHandler OnLoginCompleted;
public FormLogin() public FormLogin()
{ {
InitializeComponent(); InitializeComponent();
Bridge.InitWebEnvAsync("login", web); Bridge.InitWebEnvAsync("login", web);
Bridge.AddEventHandler(BridgeEvent.LoginSuccess, OnLoginSuccess);
}
private void OnLoginSuccess(object sender, EventArgs e)
{
OnLoginCompleted?.Invoke(this, e);
} }
private void FormLogin_Load(object sender, EventArgs e) private void FormLogin_Load(object sender, EventArgs e)
{ {
this.web.Source = new Uri(Config.WebPath("#login")); this.web.Source = new Uri(Config.WebPath("#login"));
} }
} }
} }

View File

@ -39,7 +39,7 @@ namespace AIProofread.Properties {
internal static global::System.Resources.ResourceManager ResourceManager { internal static global::System.Resources.ResourceManager ResourceManager {
get { get {
if (object.ReferenceEquals(resourceMan, null)) { if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("WordAddInTest2024.Properties.Resources", typeof(Resources).Assembly); global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("AIProofread.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp; resourceMan = temp;
} }
return resourceMan; return resourceMan;

View File

@ -14,29 +14,41 @@ namespace AIProofread
private FormLogin formLogin; private FormLogin formLogin;
private void Ribbon1_Load(object sender, RibbonUIEventArgs e) private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{ {
formLogin = new FormLogin();
btnLogin.Label = "用户\n登录"; btnLogin.Label = "用户\n登录";
btnLogout.Label = "退出\n登录"; btnLogout.Label = "退出\n登录";
} }
// 处理是否登录的展示
private void ToggleLogin() private void ToggleLogin()
{ {
if(!IS_LOGIN)
{
if(formLogin == null || formLogin.IsDisposed) { formLogin = new FormLogin(); }
formLogin.Show();
}
IS_LOGIN = !IS_LOGIN;
gpLogin.Visible = !IS_LOGIN; gpLogin.Visible = !IS_LOGIN;
gpLogout.Visible = IS_LOGIN; gpLogout.Visible = IS_LOGIN;
} }
private void btnLogin_Click(object sender, RibbonControlEventArgs e) // 当登录成功后的回调处理
private void FormLogin_OnLoginCompleted(object sender, EventArgs e)
{ {
IS_LOGIN = true;
ToggleLogin(); ToggleLogin();
formLogin.Close();
formLogin = null;
} }
// 弹出登录窗口
private void btnLogin_Click(object sender, RibbonControlEventArgs e)
{
if (formLogin == null || formLogin.IsDisposed)
{
formLogin = new FormLogin();
formLogin.OnLoginCompleted += FormLogin_OnLoginCompleted;
}
formLogin.ShowDialog();
}
// 注销登录
private void btnLogout_Click(object sender, RibbonControlEventArgs e) private void btnLogout_Click(object sender, RibbonControlEventArgs e)
{ {
IS_LOGIN = false;
ToggleLogin(); ToggleLogin();
} }