157 lines
5.6 KiB
C#
157 lines
5.6 KiB
C#
using CloudBuilder;
|
||
using CloudBuilder.Core.Authorization;
|
||
using CloudBuilder.Core.DatabaseAccessor;
|
||
using CloudBuilder.Core.DatabaseAccessor.Entity;
|
||
using CloudBuilder.Core.DependencyInjection.Form;
|
||
using CloudBuilder.Core.DependencyInjection.Request;
|
||
using CloudBuilder.Core.DependencyInjection.Task;
|
||
using CloudBuilder.Core.Policy;
|
||
using CloudBuilder.Core.Service;
|
||
using CloudBuilder.Request.Security;
|
||
using CloudBuilder.Security.Data;
|
||
using CloudBuilder.Security.Policy;
|
||
using CloudBuilder.Topshelf.Task;
|
||
using Microsoft.AspNetCore.Http;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using System.Reflection;
|
||
using System.Text;
|
||
|
||
public partial class Program
|
||
{
|
||
/// <summary>
|
||
/// request:Y 要求调用webApi参数
|
||
/// </summary>
|
||
/// <param name="args"></param>
|
||
static void Main(string[] args)
|
||
{
|
||
try
|
||
{
|
||
// 设置控制台编码为UTF-8,解决中文乱码问题
|
||
Console.OutputEncoding = Encoding.UTF8;
|
||
Console.InputEncoding = Encoding.UTF8;
|
||
|
||
string taskName = null;
|
||
Dictionary<string, string> bodyDict = new Dictionary<string, string>();
|
||
|
||
//File.AppendAllText("log.txt", "Main:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
||
|
||
if (args != null && args.Length > 0)
|
||
{
|
||
var services = new ServiceCollection();
|
||
|
||
services.Initialize();
|
||
|
||
var serviceProvider = services.BuildServiceProvider();
|
||
|
||
ApplicationServiceContext applicationServiceContext = serviceProvider.GetService<ApplicationServiceContext>()!;
|
||
|
||
string key = null;
|
||
string[] temp = null;
|
||
|
||
foreach (string s in args)
|
||
{
|
||
temp = s.Split(new char[] { ':' }, 2, StringSplitOptions.RemoveEmptyEntries);
|
||
if (temp.Length <= 1) continue;
|
||
if (temp[1] == null || temp[1].Trim() == string.Empty) continue;
|
||
|
||
key = temp[0].Trim();
|
||
|
||
if (key == "task")
|
||
{
|
||
taskName = temp[1];// config.TaskMappings.Where(x => x.Task == temp[1]).FirstOrDefault();
|
||
if (taskName == null) continue;
|
||
|
||
continue;
|
||
}
|
||
|
||
if (bodyDict.ContainsKey(key)) continue;
|
||
|
||
bodyDict.Add(key, temp[1]);
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(taskName)) return;
|
||
|
||
try
|
||
{
|
||
|
||
IApplicationService applicationService = serviceProvider.GetService<IApplicationService>();
|
||
applicationService.UserId = applicationService.Configuration["IApplicationService:UserId"];
|
||
//如果有传入用户ID,则使用传入的用户ID
|
||
if (bodyDict.ContainsKey("user_id"))
|
||
{
|
||
applicationService.UserId = bodyDict["user_id"];
|
||
}
|
||
|
||
IEnumerable<IScheduleTask> tasks = serviceProvider.GetServices<IScheduleTask>();
|
||
|
||
foreach (var task in tasks)
|
||
{
|
||
if (task.GetType().Name.ToLower() != taskName.ToLower()) continue;
|
||
|
||
if (bodyDict.ContainsKey("request") && bodyDict["request"].ToUpper().Trim() == "Y") Login(applicationService);
|
||
|
||
task.Run(bodyDict);
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine($"[{ex.Message}]");
|
||
}
|
||
finally
|
||
{
|
||
Environment.Exit(0);
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine(ex.ToString());
|
||
}
|
||
}
|
||
|
||
static void Login(IApplicationService applicationService)
|
||
{
|
||
#region 系统用户登录
|
||
try
|
||
{
|
||
LoginData loginData = new LoginData();
|
||
//从容器获取请求对象
|
||
AuthRequest authRequest = ServiceHelper.GetTypeService<AuthRequest>();
|
||
//获取RSA公钥
|
||
string pubKey = authRequest.GetRsaPublicKey();
|
||
//加密用户密码
|
||
string psw = applicationService.Configuration["CloudBuilder:SecurityUser:UserPassword"]!;
|
||
loginData.Guid = psw.Substring(0, 36);
|
||
var rsa = new RSA(pubKey, true);
|
||
loginData.UserMobile = applicationService.Configuration["CloudBuilder:SecurityUser:UserMobile"]!;
|
||
loginData.RsaUserPassword = rsa.Encode(psw);
|
||
//设置当前文化
|
||
loginData.Culture = CulturePolicy.ZH_CN;
|
||
|
||
LoginProfile loginProfile;
|
||
|
||
loginProfile = authRequest.Login(loginData);
|
||
|
||
IContext loginContext = ServiceHelper.GetInterfaceService<IContext>()!;
|
||
//登录成功后设置登录上下文
|
||
loginContext.Requestor = loginProfile.Username;
|
||
loginContext.SetPermissionInfo(loginProfile.PermissionInfo);
|
||
loginContext.SetToken(loginProfile.Token.AccessToken);
|
||
loginContext.LoginDate = DateTime.Now;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
|
||
//ISecurityMasterService securityMasterService = service.ServiceProvider.GetService<ISecurityMasterService>();
|
||
|
||
//SecurityMasterEntityKey key = new SecurityMasterEntityKey();
|
||
//key.MasterCodeItem = "default_work_shift";
|
||
//key.MasterCategory = "CloudBuilder.Hr.Policy.Master";
|
||
//key.MasterSubcategory = "StaffWorkShiftPolicy";
|
||
//SecurityMasterEntity securityMaster = securityMasterService.FindByKey(key);
|
||
#endregion
|
||
}
|
||
} |