123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- using Dicom.Log;
- using JsonRpcLite.Network;
- using JsonRpcLite.Rpc;
- using JsonRpcLite.Services;
- using MyMD5;
- using Newtonsoft.Json;
- using NPOI;
- using Syncfusion.CompoundFile.DocIO.Native;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Vinno.IUS.Common.Utilities;
- using vStation.Database;
- using vStation.Infrastructure;
- using vStation.Module.Workstation;
- using vStation.Utilities;
- using WingInterfaceLibrary.DTO.Management;
- using WingInterfaceLibrary.DTO.User;
- using WingInterfaceLibrary.Enum;
- using WingInterfaceLibrary.Interface;
- using WingInterfaceLibrary.Request.Management;
- using WingInterfaceLibrary.Request.User;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
- using Logger = vStation.Infrastructure.Logger;
- using Platform = WingInterfaceLibrary.Enum.Platform;
- namespace vStation.URMStationRelevant
- {
- public static class RPCShell
- {
- private static JsonRpcClient _client;
- private static JsonRpcHttpClientEngine _clientEngine;
- private static IManagementService _managementService;
- private static ILoginService _loginService;
- private static IUserService _userService;
- private static Module.UserModule.IUserService _localUserService;
- private static Module.Login.ILoginService _localLoginService;
- private static string _fixedPassword = "vinno123";
- private static string _oldAdminPassword;
- private static string _oldClientPassword;
- static RPCShell() {
- if (_client == null)
- {
- _client = new JsonRpcClient();
- }
- if (_clientEngine == null)
- {
- _clientEngine = new JsonRpcHttpClientEngine(URMConfig.RPCAddress);
- }
- _client.UseEngine(_clientEngine);
- _managementService = _client?.CreateProxy<IManagementService>();
- _loginService = _client?.CreateProxy<ILoginService>();
- _userService = _client?.CreateProxy<IUserService>();
- _localUserService = AppManager.Instance.GetFunction<Module.UserModule.IUserService>();
- _localLoginService = AppManager.Instance.GetFunction<Module.Login.ILoginService>();
- }
- ///对称加密
- public static string SymmetryEncrypt(string text)
- {
- if (string.IsNullOrEmpty(text))
- {
- return "";
- }
- try
- {
- List<string> base64Code = new List<string>
- {
- "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/", "a", "b", "c", "d",
- "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
- "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
- "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "="
- };
- var empty =new byte();
- List<byte> byteMessage = Encoding.UTF8.GetBytes(text).ToList();
- StringBuilder outmessage;
- int messageLen = byteMessage.Count;
- int page = messageLen / 3;
- int use = 0;
- if ((use = messageLen % 3) > 0)
- {
- for (int i = 0; i < 3 - use; i++) byteMessage.Add(empty);
- page++;
- }
- outmessage = new StringBuilder();
- for (int i = 0; i < page; i++)
- {
- List<int> instr = new List<int>
- {
- byteMessage[i * 3],
- byteMessage[i * 3 + 1],
- byteMessage[i * 3 + 2]
- };
- int[] outstr = new int[4];
- outstr[0] = instr[0] >> 2;
- outstr[1] = ((instr[0] & 0x03) << 4) ^ (instr[1] >> 4);
- if (instr[1] != empty)
- {
- outstr[2] = ((instr[1] & 0x0f) << 2) ^ (instr[2] >> 6);
- }
- else
- {
- outstr[2] = 64;
- }
- if (instr[2] != empty)
- {
- outstr[3] = (instr[2] & 0x3f);
- }
- else
- {
- outstr[3] = 64;
- }
- outmessage.Append(base64Code[outstr[0]]);
- outmessage.Append(base64Code[outstr[1]]);
- outmessage.Append(base64Code[outstr[2]]);
- outmessage.Append(base64Code[outstr[3]]);
- }
- var finalStr = "Symmetry_" + outmessage.ToString();
- //Console.WriteLine(finalStr);
- return finalStr;
- }
- catch (Exception e)
- {
- throw e;
- }
- }
- public static async Task<string> AdminLogin(string account,string pwd)
- {
- try
- {
- var adminPWD=SymmetryEncrypt(pwd);
- var lonInRequest = new LoginRequest()
- {
- AdminName = account.ToLower(),
- Password = adminPWD
- };
-
-
- var result =
- await _managementService.AdminLogin(lonInRequest);
- if(result == null)
- {
- return "SignOrLoginFail";
- }
- if (result.LoginState == LoginStateEnum.Succeed)
- {
- URMConfig.ManagementURL = result?.ManagementUrl;
- URMConfig.URMUserType = URMUserType.Admin;
- URMConfig.Token = result?.Token;
- var tryLoginResult = _localLoginService.Login(account.ToLower(), pwd, false);
- if (tryLoginResult == Module.Workstation.LoginResult.LoginSuccess)
- {
- return "success";
- }
- if (tryLoginResult == Module.Workstation.LoginResult.InvalidPassowrd)
- {
- _localUserService.UpdatePasswordForAdmin(account.ToLower(), pwd, pwd);
- }
- if (tryLoginResult == Module.Workstation.LoginResult.PasswordNotReset)
- {
- _oldAdminPassword = pwd;
- }
- return "success";
- }
- if (result.LoginState == LoginStateEnum.PasswordIncorrect)
- {
- return "PasswordIncorrect";
- }
- return "SignOrLoginFail";
- }
- catch (Exception ex)
- {
- if (ex is RpcException rpcException)
- {
- Logger.WriteLineWarn($"Error happened while client login for urm work station {rpcException}");
- if (rpcException.ErrorCode == 6037)
- {
- return "Usernotregistered";
- }
- else if(rpcException.ErrorCode == 9020)
- {
- return "NoFoundDongle";//未读取到有效的License信息
- }
- else if (rpcException.ErrorCode == 6062)
- {
- return "AccountIsLocked";//密码错误次数过多,账号已锁定,次日解锁
- }
- }
- else
- {
- Logger.WriteLineError($"Error happened while client login for urm work station {ex}");
- }
- return "SignOrLoginFail";
- }
- }
- public static async Task<Module.Workstation.LoginResult> DongleLogin(string anyCode, string labDongle, string anyAccount)
- {
- try
- {
- var lonInRequest = new CommonLoginRequest()
- {
- AnyAccount = anyAccount,
- AnyCode = anyCode,
- LabDongle = labDongle,
- Platform = Platform.Windows,
- LoginSource = LoginSource.PC,
- LanguageCode = GetLanguageCode()
- };
- var result =
- await _loginService.CommonLoginAsync(lonInRequest);
- if (result == null)
- {
- Logger.WriteLineError($"Login result is null");
- return Module.Workstation.LoginResult.UserNotExist;
- }
-
- if (result.LoginState == LoginStateEnum.Succeed)
- {
- URMConfig.Token = result.Token;
- var userInfoResult =
- await _userService.GetUserInfoAsync(new GetUserInfoRequest()
- {
- Token = URMConfig.Token
- });
- URMConfig.UserName = userInfoResult.UserName;
- URMConfig.Pwd =_fixedPassword;
- URMConfig.Code = userInfoResult.UserCode;
- try
- {
- if (File.Exists(URMConfig.StatesPath))
- {
- var statesText = File.ReadAllText(URMConfig.StatesPath);
- var oldState = JsonConvert.DeserializeObject<userData>(statesText);
- oldState.user.fullName = userInfoResult.FullName;
- oldState.user.account = userInfoResult.UserName;
- oldState.user.password = URMConfig.Pwd;
- oldState.user.isAutoLogin = true;
- oldState.user.isAgreeClause = true;
- oldState.user.code = userInfoResult.UserCode;
- oldState.user.token = result.Token;
- File.WriteAllText(URMConfig.StatesPath, JsonConvert.SerializeObject(oldState));
- }
- else
- {
- var newState = new userData();
- newState.app = new app()
- {
- locale = new List<string> { "zh", "CN" },
- issFollowSystemLocale = false,
- enableVidLoopPlayback = false
- };
- newState.user = new user()
- {
- fullName = userInfoResult.FullName,
- account = userInfoResult.UserName,
- password = _fixedPassword,
- isAutoLogin = true,
- isAgreeClause = true,
- code = userInfoResult.UserCode,
- token = result.Token
- };
- File.WriteAllText(URMConfig.StatesPath, JsonConvert.SerializeObject(newState));
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError("Error hanppened while read states {ex}");
- var newState = new userData();
- newState.app = new app()
- {
- locale = new List<string> { "zh", "CN" },
- issFollowSystemLocale = false,
- enableVidLoopPlayback = false
- };
- newState.user = new user()
- {
- fullName = userInfoResult.FullName,
- account = userInfoResult.UserName,
- password = _fixedPassword,
- isAutoLogin = true,
- isAgreeClause = true,
- code = userInfoResult.UserCode,
- token = result.Token
- };
- File.WriteAllText(URMConfig.StatesPath, JsonConvert.SerializeObject(newState));
- }
- var user = new Database.User
- {
- Account = userInfoResult.UserName,
- Description = userInfoResult.FullName,
- FullName = userInfoResult.FullName,
- Type = UserType.Normal,
- Status = StateType.Active,
- Password = Md5Builder.GetMd5Code(_fixedPassword),
- DigitalSignature = null
- };
- var tryLoginState = _localLoginService.Login(URMConfig.UserName, URMConfig.Pwd, false,false);
- if (tryLoginState == Module.Workstation.LoginResult.LoginSuccess)
- {
- return Module.Workstation.LoginResult.LoginSuccess;
- }
- else
- {
- var addUserState = _localUserService.CreateUser(user);
- if (addUserState == Module.Workstation.CreateUserResult.Successful)
- {
- var tryLoginStateAfter = _localLoginService.Login(URMConfig.UserName, URMConfig.Pwd, false, false);
-
- if (tryLoginStateAfter == Module.Workstation.LoginResult.LoginSuccess)
- {
- Logger.WriteLineInfo("Dongle Login local success");
- }
- else
- {
- Logger.WriteLineInfo("Dongle Login local failed");
- }
- return Module.Workstation.LoginResult.LoginSuccess;
- }
- }
- return Module.Workstation.LoginResult.LoginSuccess;
- }
- else
- {
- Logger.WriteLineError($"Server dongle login failed");
- return Module.Workstation.LoginResult.UserNotExist;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"Error happened while do {ex}");
- return Module.Workstation.LoginResult.Unknown;
- }
-
- }
- public static async Task<string> ClientLogin(string account, string pwd)
- {
- try
- {
- if (account.ToLower() == "admin")
- {
- return "AdminError";//admin 不正确
- }
- var clientPWD = SymmetryEncrypt(pwd);
- var lonInRequest = new CommonLoginRequest()
- {
- AnyAccount = account,
- Password = clientPWD,
- Platform=Platform.Windows,
- LoginSource=LoginSource.PC,
- LanguageCode=GetLanguageCode()
- };
- var result =
- await _loginService.CommonLoginAsync(lonInRequest);
- if (result == null)
- {
- return "ErrorException";//异常错误
- }
- if (result.LoginState == LoginStateEnum.Succeed)
- {
- URMConfig.UserName = account;
- URMConfig.Pwd= pwd;
- URMConfig.Token = result.Token;
- var result2 =
- await _userService.GetUserInfoAsync(new GetUserInfoRequest()
- {
- Token = URMConfig.Token
- });
-
- URMConfig.Code = result2.UserCode;
- var tryLoginState = _localLoginService.Login(account, pwd, false);
- var needCreateState = false;
- if (File.Exists(URMConfig.StatesPath))
- {
- var statesText = File.ReadAllText(URMConfig.StatesPath);
- try
- {
- var oldState = JsonConvert.DeserializeObject<userData>(statesText);
- oldState.user.fullName = result2.FullName;
- oldState.user.account = account.ToLower();
- oldState.user.password = pwd;
- oldState.user.isAutoLogin = true;
- oldState.user.isAgreeClause = true;
- oldState.user.code = result2.UserCode;
- oldState.user.token = result.Token.Substring(1,5);
- File.WriteAllText(URMConfig.StatesPath, JsonConvert.SerializeObject(oldState));
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"Error happened while converting states {ex}");
- File.Delete(URMConfig.StatesPath);
- needCreateState = true;
- }
-
-
- }
- if (needCreateState)
- {
- var newState = new userData();
- newState.app = new app()
- {
- locale = new List<string> { "zh", "CN" },
- issFollowSystemLocale = false,
- enableVidLoopPlayback = false
- };
- newState.user = new user()
- {
- fullName = result2.FullName,
- account = account.ToLower(),
- password = pwd,
- isAutoLogin = true,
- isAgreeClause = true,
- code = result2.UserCode,
- token = result.Token
- };
- File.WriteAllText(URMConfig.StatesPath, JsonConvert.SerializeObject(newState));
- }
- var user = new Database.User
- {
- Account = account,
- Description = result2.FullName,
- FullName = result2.FullName,
- Type = UserType.Normal,
- Status = StateType.Active,
- Password = Md5Builder.GetMd5Code(pwd),
- DigitalSignature = new byte[1],
- };
- if (tryLoginState ==Module.Workstation.LoginResult.UserNotExist)
- {
- _localUserService.CreateUser(user);
- }
- if (tryLoginState == Module.Workstation.LoginResult.InvalidPassowrd)
- {
- var forceUpdatePasswordResult = _localUserService.ForceUpdatePasswordByName(account, pwd);
- if (forceUpdatePasswordResult == DbOperationResult.UpdateSuccess)
- {
- return "success";
- }
- else
- {
- return "PasswordIncorrect";
- }
- }
- return "success";
- }
- else if(result.LoginState == LoginStateEnum.PasswordIncorrect)
- {
- return "PasswordIncorrect";
- }
- else if (result.LoginState == LoginStateEnum.SignOrLoginFail)
- {
- return "SignOrLoginFail";
- }
- return "SignOrLoginFail";
- }
- catch (Exception ex)
- {
- if(ex is RpcException rpcException)
- {
- Logger.WriteLineWarn($"Error happened while client login for urm work station {rpcException}");
- if (rpcException.ErrorCode== 6037)
- {
- return "Usernotregistered";
- }
-
- }
- else
- {
- Logger.WriteLineError($"Error happened while client login for urm work station {ex}");
- }
- return "SignOrLoginFail";
- }
- }
- private static string GetLanguageCode()
- {
- return TranslateHelper.CurrentLanguage;
- }
- internal static async Task<bool> ResetPassword(string account, string newPassword, string oldPassword="abc123")
- {
- try {
- var isAdmin = account.ToLower() == "admin";
- if (isAdmin)
- {
- var result = await _managementService.ModifyAdminPassword(new ModifyAdminPasswordRequest
- {
- Token = URMConfig.Token,
- NewPassword = SymmetryEncrypt(newPassword),
- OldPassword = SymmetryEncrypt(_oldAdminPassword)
- });
- if (result)
- {
- var resultLogin = await AdminLogin(account, newPassword);
- if (resultLogin == "Success")
- {
- return true;
- }
- }
- return result;
- }
- else
- {
- var result = await _loginService.ModifyPasswordAsync(new ModifyPasswordRequest()
- {
- Token = URMConfig.Token,
- Password = SymmetryEncrypt(URMConfig.Pwd),
- NewPassword = SymmetryEncrypt(newPassword),
- AnyAccount = account,
- });
- if (result)
- {
- Logger.WriteLineInfo("Reset password success");
- var resultLogin = await ClientLogin(account, newPassword);
- if (resultLogin== "success")
- {
- return true;
- }
- }
- return result;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"Error happened while Admin login for urm work station {ex}");
- return false;
- }
- }
- }
- }
|