old_flsystem/应用/UserFission/QRCodeForm.cs

178 lines
5.9 KiB
C#
Raw Permalink Normal View History

2022-09-20 03:10:29 +00:00
using Api.Framework.Tools;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using UI.Framework.Forms;
namespace UserFission
{
public partial class QRCodeForm : BaseForm
{
public QRCodeForm()
{
InitializeComponent();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (listView1.SelectedItems.Count == 0) return;
var name = listView1.SelectedItems[0].SubItems[0].Text;
var nick = listView1.SelectedItems[0].SubItems[1].Text;
var url = listView1.SelectedItems[0].SubItems[2].Text;
var data = comboBox1.DataSource as List<ItemObj>;
if (data != null)
{
var entry = data.FirstOrDefault(f => f.value == name);
if (entry != null)
{
var index = data.IndexOf(entry);
comboBox1.SelectedIndex = index;
return;
}
}
ShowError($"选中的账号未存在机器人列表中,({name}机器人未登录无法修改)");
}
catch (Exception ex)
{
ShowError(ex.Message);
}
}
private void QRCodeForm_Load(object sender, EventArgs e)
{
try
{
var mylist = new List<ItemObj>();
mylist.Add(new ItemObj("请选择机器人", "请选择机器人"));
var results = Chat.Framework.ChatClient.WXClient.Values;
foreach (var item in results)
{
mylist.Add(new ItemObj(item.WeixinHao, $"{item.WeixinHao}({item.User.Nick})"));
}
comboBox1.DataSource = mylist;
comboBox1.DisplayMember = "text";
comboBox1.ValueMember = "value";
ShowQrCodeList();
}
catch (Exception ex)
{ }
}
private void btn_QRCode_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Filter = "图片文件|*.jpg;*.png;*.gif|All files(*.*)|*.*";
if (openDialog.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrEmpty(openDialog.FileName))
throw new Exception("请先打开一张二维码图片!");
else
tBox_QRCode.Text = Util.DecodeQRCode(openDialog.FileName, Util.DecodeQRCodeType.Caoliao);
if(string.IsNullOrWhiteSpace(tBox_QRCode.Text)) throw new Exception("Invalid number of Finder Pattern detected");
}
}
catch (Exception ex)
{
if (ex.Message == "Invalid number of Finder Pattern detected")
ShowError("二维码无法解析,请换一个重试");
else
ShowError(ex);
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (comboBox1.SelectedIndex == 0) throw new Exception("请选择对应的机器人");
if (string.IsNullOrWhiteSpace(tBox_QRCode.Text)) throw new Exception("请填写二维码地址");
var nick = string.Empty;
var itemObj = (ItemObj)comboBox1.SelectedItem;
var name = itemObj.value;
var rinfo = itemObj.text;
var reg = Regex.Match(rinfo, @"" + name + @"\((?<昵称>.*?)\)");
if (reg.Success)
nick = reg.Groups["昵称"].Value;
var item = Class1.Config.QrCodes.FirstOrDefault(f => f.rname == name);
if (item != null)
{
item.rnick = nick;
item.url = tBox_QRCode.Text.Trim();
}
else
{
Class1.Config.QrCodes.Add(new Entitys.QrCode() { rname = name, rnick = nick, url = tBox_QRCode.Text });
}
ShowQrCodeList();
}
catch (Exception ex)
{
ShowError(ex.Message);
}
}
private void ShowQrCodeList()
{
try
{
listView1.Items.Clear();
foreach (var item in Class1.Config.QrCodes)
{
ListViewItem _item = new ListViewItem(item.rname);
_item.SubItems.Add(item.rnick);
_item.SubItems.Add(item.url);
listView1.Items.Add(_item);
}
}
catch (Exception ex)
{ }
}
private class ItemObj
{
public ItemObj(string value, string text)
{
this.text = text;
this.value = value;
}
public string text { get; set; }
public string value { get; set; }
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (listView1.SelectedItems.Count != 0)
{
var name = listView1.SelectedItems[0].SubItems[0].Text;
var item = Class1.Config.QrCodes.FirstOrDefault(f => f.rname == name);
if (item != null)
Class1.Config.QrCodes.Remove(item);
ShowQrCodeList();
}
}
catch (Exception ex)
{ }
}
}
}