ソースを参照

一卡通

Lishi
EmilyEdna 2年前
コミット
8a8168d15a
8個のファイルの変更615行の追加13行の削除
  1. +7
    -0
      HKCardIN/HKCardIN.csproj
  2. バイナリ
     
  3. +13
    -0
      HKCardIN/Helper/DataBus.cs
  4. +31
    -0
      HKCardIN/Helper/HKHelper.cs
  5. +445
    -0
      HKCardIN/Helper/ThreadManage.cs
  6. +29
    -1
      HKCardIN/ViewModels/RootViewModel.cs
  7. +27
    -12
      HKCardIN/Views/RootView.xaml
  8. +63
    -0
      HKCardOUT/Bootstrapper.cs

+ 7
- 0
HKCardIN/HKCardIN.csproj ファイルの表示

@@ -20,12 +20,19 @@
</PropertyGroup>
<ItemGroup>
<None Remove="HKResouces\头像.png" />
<None Remove="HKResouces\背景.jpg" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="HandyControls" Version="3.4.1" />
<PackageReference Include="SqlSugarCore" Version="5.1.2.7" />
<PackageReference Include="Stylet" Version="1.3.6" />
<PackageReference Include="XExten.Advance" Version="1.2.4.2-preview" />
</ItemGroup>
<ItemGroup>
<Resource Include="HKResouces\头像.png" />
<Resource Include="HKResouces\背景.jpg" />
</ItemGroup>
<ItemGroup>
<Folder Include="Logic\" />
</ItemGroup>
</Project>

バイナリ
ファイルの表示


+ 13
- 0
HKCardIN/Helper/DataBus.cs ファイルの表示

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HKCardIN.Helper
{
public class DataBus
{
public static bool NetWordState { get; set; } = false;
}
}

+ 31
- 0
HKCardIN/Helper/HKHelper.cs ファイルの表示

@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace HKCardIN.Helper
{
public class HKHelper: Singleton<HKHelper>
{
/// <summary>
/// 判断网络状况的方法,返回值true为连接,false为未连接
/// </summary>
/// <param name="conState"></param>
/// <param name="reder"></param>
/// <returns></returns>
[DllImport("wininet")]
public extern static bool InternetGetConnectedState(out int conState, int reder);
/// <summary>
/// 获取当前网络连接状态
/// </summary>
/// <returns>成功连接网络返回 true,未连接返回 false</returns>
public bool GetNetworkState()
{
return InternetGetConnectedState(out int i, 0);
}

}
}

+ 445
- 0
HKCardIN/Helper/ThreadManage.cs ファイルの表示

@@ -0,0 +1,445 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace HKCardIN.Helper
{
public class Singleton<T> where T : new()
{
private static object _async = new object();

private static T _instance;

static readonly Lazy<T> instance = new();
/// <summary>
/// 获取实例
/// </summary>
/// <returns></returns>
public static T GetInstance()
{
return instance.Value;
}
}
public class ThreadManage : Singleton<ThreadManage>
{
string guid = "871d7e28-c413-4675-8d28-64e4dca4c2d3-";
private static readonly object _lock = new object();
StringBuilder callbackKey = new StringBuilder();
List<string> keys = new List<string>();
ConcurrentDictionary<string, Task> Threads = new ConcurrentDictionary<string, Task>();
ConcurrentDictionary<string, CancellationTokenSource> CancellationTokenSources = new ConcurrentDictionary<string, CancellationTokenSource>();

/// <summary>
/// 停止指定任务
/// </summary>
/// <param name="key">任务名</param>
/// <param name="ExitCallback">任务结束的回调</param>
public void StopTask(string key, Action ExitCallback = null)
{
if (CancellationTokenSources.ContainsKey(guid + key))
{
CancellationTokenSources[guid + key]?.Cancel();
ActionManage.GetInstance.Register(ExitCallback, guid + key);
}
else
{
if (ExitCallback != null) ExitCallback();
}
}

/// <summary>
/// 长任务,带 while true 的循环
/// </summary>
/// <param name="action"></param>
/// <param name="key"></param>
public void StartLong(Action action, string key, bool IsRestart = false, Action RunComplete = null)
{
CancellationTokenSources.TryAdd(guid + key, new CancellationTokenSource());
bool result = Threads.TryAdd(guid + key, Task.Factory.StartNew(new Action(() =>
{
Thread.CurrentThread.Name = key;
ReStart:
try
{
while (!CancellationTokenSources[guid + key].IsCancellationRequested)
{
if (action != null) action();
}
}
catch (Exception ex)
{
if (IsRestart)
{
Thread.Sleep(2000);
goto ReStart;
}
else
{
CancellationTokenSources.TryRemove(guid + key, out CancellationTokenSource temp);
Threads.TryRemove(guid + key, out Task temp1);
}
}
}), CancellationTokenSources[guid + key].Token).ContinueWith(new Action<Task, object>((t, o) =>
{
ThreadStatus(t, o.ToString());
if (RunComplete != null) RunComplete();
}), guid + key));
}


/// <summary>
/// 不带 while true 的循环任务
/// </summary>
/// <param name="action"></param>
/// <param name="key"></param>
public void Start(Action action, string key, bool isRestart = false)
{
CancellationTokenSources.TryAdd(guid + key, new CancellationTokenSource());
bool result = Threads.TryAdd(guid + key, Task.Factory.StartNew(new Action(() =>
{
Thread.CurrentThread.Name = key;
try
{
if (action != null) action();
}
catch (Exception ex)
{
if (isRestart)
{
CancellationTokenSources.TryRemove(guid + key, out CancellationTokenSource item1);
Threads.TryRemove(guid + key, out Task item2);
Start(action, key, isRestart);
}
else
{
}
}
}), CancellationTokenSources[guid + key].Token).ContinueWith(new Action<Task, object>((t, o) =>
{
ThreadStatus(t, o.ToString());
}), guid + key));
}

private void ThreadStatus(Task task, string key)
{
bool IsRemove = false;
string name = key.Substring(key.LastIndexOf('-') + 1);
switch (task.Status)
{
case TaskStatus.RanToCompletion:
IsRemove = true;
break;
case TaskStatus.Faulted:
IsRemove = true;
break;
case TaskStatus.Canceled:
IsRemove = true;
break;
default:
break;
}

if (IsRemove)
{
if (Threads.ContainsKey(key))
Threads.TryRemove(key, out Task t);
if (CancellationTokenSources.ContainsKey(key))
CancellationTokenSources.TryRemove(key, out CancellationTokenSource cts);
ActionManage.GetInstance.Send(key);
}
}

/// <summary>
/// 释放所有线程资源
/// </summary>
public void Dispose()
{
for (int i = 0; i < CancellationTokenSources.Count; i++)
{
CancellationTokenSources.ElementAt(i).Value.Cancel();
}
}

/// <summary>
/// 判断指定线程是否完成
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool IsComplete(string key)
{
if (Threads.ContainsKey(guid + key)) return Threads[guid + key].IsCompleted;
return false;
}

}
internal class Delegation
{
/// <summary>
/// 带参数的委托
/// </summary>
public Action<object> ActionPar { get; set; }
/// <summary>
/// 带参数的委托
/// </summary>
public Action<object[]> ActionPars { get; set; }
/// <summary>
/// 无参数的委托
/// </summary>
public Action ActionBus { get; set; }
/// <summary>
/// 有返回值的委托
/// </summary>
public Func<object> FuncObj { get; set; }
/// <summary>
/// 有返回值,有参数的委托
/// </summary>
public Func<object, object> FuncPar { get; set; }
}
public class ActionManage
{

private volatile static ActionManage _Instance;
public static ActionManage GetInstance => _Instance ?? (_Instance = new ActionManage());
private ActionManage() { }

//private static ConcurrentDictionary<string, delegate> actions = new ConcurrentDictionary<string, delegate>();
private static ConcurrentDictionary<string, Delegation> actions = new ConcurrentDictionary<string, Delegation>();

static readonly object SendLock = new object();
static readonly object SendParLock = new object();
static readonly object RegisterLock = new object();

/// <summary>
/// 注销委托
/// </summary>
/// <param name="key"></param>
public void CancelRegister(string key)
{
if (actions.ContainsKey(key))
actions.TryRemove(key, out Delegation t);
}

/// <summary>
/// 执行注册过的委托
/// </summary>
/// <param name="key">注册委托的key</param>
/// <param name="par">委托参数</param>
/// <param name="Callback">委托回调</param>
public void Send(string key, object par, Action Callback = null)
{
lock (SendLock)
if (actions.ContainsKey(key)) actions[key].ActionPar.Invoke(par, Callback);
}

/// <summary>
/// 执行注册过的委托
/// </summary>
/// <param name="key">注册委托的key</param>
/// <param name="par">委托参数</param>
/// <param name="Callback">委托回调</param>
public void Send(string key, object[] par, Action Callback = null)
{
lock (SendLock)
if (actions.ContainsKey(key)) actions[key].ActionPars.Invokes(par, Callback);
}

/// <summary>
/// 执行注册过的委托
/// </summary>
/// <param name="key">注册委托的key</param>
/// <param name="Callback">委托回调</param>
public void Send(string key, Action Callback = null)
{
lock (SendLock)
if (actions.ContainsKey(key)) actions[key].ActionBus?.Invoke(Callback);
}

public object SendResult(string key, object par = null)
{
lock (SendLock)
if (actions.ContainsKey(key))
if (par == null)
{
return actions[key].FuncObj?.Invoke();
}
else
{
return actions[key].FuncPar?.Invoke(par);
}
return default;
}

public void Register<T>(T action, string key)
{
lock (RegisterLock)
{
if (action != null)
{
if (!actions.ContainsKey(key))
{
if (action is Action actionBus)
actions.TryAdd(key, new Delegation() { ActionBus = actionBus });

if (action is Action<object> actionObj)
actions.TryAdd(key, new Delegation() { ActionPar = actionObj });

if (action is Action<object[]> actionObjs)
actions.TryAdd(key, new Delegation() { ActionPars = actionObjs });

if (action is Func<object> funcObj)
actions.TryAdd(key, new Delegation() { FuncObj = funcObj });

if (action is Func<object, object> puncPar)
actions.TryAdd(key, new Delegation() { FuncPar = puncPar });
}
}
}

}

}
public static class ExpandMethod
{
/// <summary>
/// 获取布尔数组指定值得索引
/// </summary>
/// <param name="obj">要获取索引的数组</param>
/// <param name="value">要获取索引的值</param>
/// <returns></returns>
public static int GetIndex(this bool[] obj, bool value)
{
if (obj == null) return -1;
return Array.FindIndex(obj, p => p == value);
}

/// <summary>
/// 获取字符串数组指定值得索引
/// </summary>
/// <param name="obj">要获取索引的数组</param>
/// <param name="value">要获取索引的值</param>
/// <returns></returns>
public static int GetIndex(this string[] obj, string value)
{
if (obj == null || value == null) return -1;
return Array.FindIndex(obj, p => p == value && p.Length > 0);
}

/// <summary>
/// 委托回调
/// </summary>
/// <param name="action">要执行的委托</param>
/// <param name="callback">委托回调</param>
public static void Invoke(this Action action, Action callback)
{
action?.Invoke();
callback?.Invoke();
}

/// <summary>
/// 委托回调
/// </summary>
/// <param name="action">要执行的委托</param>
/// <param name="par">要执行的委托的参数</param>
/// <param name="callback">委托回调</param>
public static void Invoke(this Action<object> action, object par, Action callback)
{
action?.Invoke(par);
callback?.Invoke();
}

public static void Invokes(this Action<object[]> action, object[] par, Action callback)
{
action?.Invoke(par);
callback?.Invoke();
}


/// <summary>
/// 字节数组转换成32位整数
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static int BytesToInt(this byte[] bytes)
{
if (bytes.Length > 4) return -1;
int ReturnVlaue = 0;
for (int i = 0; i < bytes.Length; i++)
{
ReturnVlaue += (int)(bytes[i] << (i * 8));
}
return ReturnVlaue;
}

/// <summary>
/// 字节数组转换成 ushort 数组
/// </summary>
/// <param name="bytes">要转换的字节数组</param>
/// <param name="reverse">字节高度顺序控制</param>
/// <returns></returns>
public static ushort[] BytesToUshorts(this byte[] bytes, bool reverse = false)
{
int len = bytes.Length;

byte[] srcPlus = new byte[len + 1];
bytes.CopyTo(srcPlus, 0);
int count = len >> 1;

if (len % 2 != 0)
{
count += 1;
}

ushort[] dest = new ushort[count];
if (reverse)
{
for (int i = 0; i < count; i++)
{
dest[i] = (ushort)(srcPlus[i * 2] << 8 | srcPlus[2 * i + 1] & 0xff);
}
}
else
{
for (int i = 0; i < count; i++)
{
dest[i] = (ushort)(srcPlus[i * 2] & 0xff | srcPlus[2 * i + 1] << 8);
}
}

return dest;
}

/// <summary>
/// ushort 数组转换成字节数组
/// </summary>
/// <param name="src">需要转换的 ushort数组</param>
/// <param name="reverse">高低字节的设置</param>
/// <returns></returns>
public static byte[] UshortsToBytes(this ushort[] src, bool reverse = false)
{

int count = src.Length;
byte[] dest = new byte[count << 1];
if (reverse)
{
for (int i = 0; i < count; i++)
{
dest[i * 2] = (byte)(src[i] >> 8);
dest[i * 2 + 1] = (byte)(src[i] >> 0);
}
}
else
{
for (int i = 0; i < count; i++)
{
dest[i * 2] = (byte)(src[i] >> 0);
dest[i * 2 + 1] = (byte)(src[i] >> 8);
}
}
return dest;
}
}
}

+ 29
- 1
HKCardIN/ViewModels/RootViewModel.cs ファイルの表示

@@ -1,4 +1,7 @@
using Stylet;
using HKCardIN.Helper;
using Stylet;
using System;
using System.Threading;
using System.Windows;

namespace HKCardIN.ViewModels
@@ -11,6 +14,7 @@ namespace HKCardIN.ViewModels
{
CodeVisible = Visibility.Collapsed;
ContentVisible = Visibility.Visible;
MainThread();
}

#region 属性
@@ -82,5 +86,29 @@ namespace HKCardIN.ViewModels

}
#endregion

#region 方法
private void MainThread() {
ThreadManage.GetInstance().StartLong(new Action(() =>
{
try
{
//1.检测网络上下线
bool network = HKHelper.GetInstance().GetNetworkState();
if (network != DataBus.NetWordState)
{
if (network) HandyControl.Controls.Growl.InfoGlobal("网络连接成功");
else HandyControl.Controls.Growl.InfoGlobal("系统已离线,请连接网络!!!");
DataBus.NetWordState = network;
}
Thread.Sleep(3000);
}
catch (Exception ex)
{
HandyControl.Controls.Growl.InfoGlobal(ex.Message);
}
}), "循环状态监测线程", false);
}
#endregion
}
}

+ 27
- 12
HKCardIN/Views/RootView.xaml ファイルの表示

@@ -37,7 +37,16 @@
<Setter Property="Foreground" Value="Red" />
<Setter Property="FontSize" Value="18" />
</Style>
<Style
x:Key="Title"
BasedOn="{StaticResource TextBlockDefaultBold}"
TargetType="TextBlock">
<Setter Property="Foreground" Value="WhiteSmoke" />
</Style>
</hc:Window.Resources>
<hc:Window.Background>
<ImageBrush ImageSource="/HKResouces/背景.jpg" />
</hc:Window.Background>
<hc:Window.InputBindings>
<KeyBinding
Key="F1"
@@ -113,7 +122,7 @@
<WrapPanel>
<TextBlock
FontSize="18"
Style="{StaticResource TextBlockDefaultBold}"
Style="{StaticResource Title}"
Text="姓名:" />
<TextBlock
FontSize="18"
@@ -124,7 +133,7 @@
<WrapPanel Margin="0,20,0,0">
<TextBlock
FontSize="18"
Style="{StaticResource TextBlockDefaultBold}"
Style="{StaticResource Title}"
Text="手机:" />
<TextBlock
FontSize="18"
@@ -135,7 +144,7 @@
<WrapPanel Margin="0,20,0,0">
<TextBlock
FontSize="18"
Style="{StaticResource TextBlockDefaultBold}"
Style="{StaticResource Title}"
Text="员工号:" />
<TextBlock
FontSize="18"
@@ -148,12 +157,13 @@
<hc:Divider
Content="卡信息"
FontSize="16"
FontWeight="Bold" />
FontWeight="Bold"
Foreground="WhiteSmoke" />
<StackPanel Margin="20,0,0,0">
<WrapPanel>
<TextBlock
FontSize="18"
Style="{StaticResource TextBlockDefaultBold}"
Style="{StaticResource Title}"
Text="卡号:" />
<TextBlock
FontSize="18"
@@ -164,7 +174,7 @@
<WrapPanel Margin="0,20,0,0">
<TextBlock
FontSize="18"
Style="{StaticResource TextBlockDefaultBold}"
Style="{StaticResource Title}"
Text="卡状态:" />
<TextBlock
FontSize="18"
@@ -175,7 +185,7 @@
<WrapPanel Margin="0,20,0,0">
<TextBlock
FontSize="18"
Style="{StaticResource TextBlockDefaultBold}"
Style="{StaticResource Title}"
Text="卡上余额:" />
<TextBlock
FontSize="18"
@@ -186,7 +196,7 @@
<WrapPanel Margin="0,20,0,0">
<TextBlock
FontSize="18"
Style="{StaticResource TextBlockDefaultBold}"
Style="{StaticResource Title}"
Text="启用日期:" />
<TextBlock
FontSize="18"
@@ -200,9 +210,13 @@
<hc:Divider
Content="充值金额"
FontSize="16"
FontWeight="Bold" />
<TextBlock Margin="5,0,0,0" FontSize="15">
<Run Text="当前充值金额:" />
FontWeight="Bold"
Foreground="WhiteSmoke" />
<TextBlock
Margin="5,0,0,0"
FontSize="18"
Style="{StaticResource Title}">
<Run Foreground="WhiteSmoke" Text="当前充值金额:" />
<Run Foreground="Red" Text="{Binding ShowMoney}" />
</TextBlock>
<WrapPanel>
@@ -268,7 +282,8 @@
<hc:Divider
Content="操作"
FontSize="16"
FontWeight="Bold" />
FontWeight="Bold"
Foreground="WhiteSmoke" />
<WrapPanel HorizontalAlignment="Center">
<Button
Command="{s:Action SaveAction}"


+ 63
- 0
HKCardOUT/Bootstrapper.cs ファイルの表示

@@ -1,11 +1,74 @@
using HKCardOUT.ViewModels;
using Stylet;
using StyletIoC;
using System;
using System.Windows.Threading;
using System.Windows;

namespace HKCardOUT
{
public class Bootstrapper : Bootstrapper<RootViewModel>
{
/// <summary>
/// 程序启动
/// </summary>
protected override void OnStart()
{
}

protected override void ConfigureIoC(IStyletIoCBuilder builder)
{
}

/// <summary>
/// 初始化系统相关参数配置
/// </summary>
protected override void Configure()
{
base.Configure();
}

/// <summary>
/// 初始化VM
/// </summary>
protected override void Launch()
{
base.Launch();
}

/// <summary>
/// 加载首页VM
/// </summary>
/// <param name="rootViewModel"></param>
protected override void DisplayRootView(object rootViewModel)
{
base.DisplayRootView(rootViewModel);
}

/// <summary>
///VM加载完毕
/// </summary>
protected override void OnLaunch()
{
base.OnLaunch();
}

/// <summary>
/// 退出
/// </summary>
/// <param name="e"></param>
protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
}

/// <summary>
/// 全局异常捕获
/// </summary>
/// <param name="e"></param>
protected override void OnUnhandledException(DispatcherUnhandledExceptionEventArgs e)
{
GC.Collect();
}
}
}

読み込み中…
キャンセル
保存