采用强类型模型改造请求参数

This commit is contained in:
大石头 2020-09-07 21:51:36 +08:00
parent 58ead0037c
commit 9fa84bc498
3 changed files with 80 additions and 55 deletions

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Net; using System.Net;
using AntJob.Data; using AntJob.Data;
using AntJob.Data.Entity; using AntJob.Data.Entity;
using AntJob.Models;
using NewLife; using NewLife;
using NewLife.Data; using NewLife.Data;
using NewLife.Log; using NewLife.Log;
@ -28,84 +29,61 @@ namespace AntJob.Server
public IApiSession Session { get; set; } public IApiSession Session { get; set; }
/// <summary>应用登录</summary> /// <summary>应用登录</summary>
/// <param name="user">应用名</param> /// <param name="model">模型</param>
/// <param name="pass"></param>
/// <param name="displayName">显示名</param>
/// <param name="machine">机器码</param>
/// <param name="processId">进程Id</param>
/// <param name="version">版本</param>
/// <param name="compile">编译时间</param>
/// <returns></returns> /// <returns></returns>
[Api(nameof(Login))] [Api(nameof(Login))]
public Object Login(String user, String pass, String displayName, String machine, Int32 processId, String version, DateTime compile) public LoginResponse Login(LoginModel model)
{ {
if (user.IsNullOrEmpty()) throw new ArgumentNullException(nameof(user)); if (model.User.IsNullOrEmpty()) throw new ArgumentNullException(nameof(model.User));
//if (pass.IsNullOrEmpty()) throw new ArgumentNullException(nameof(pass));
//var ps = ControllerContext.Current.Parameters;
//var displayName = ps["DisplayName"] + "";
//var machine = ps["machine"] + "";
//var processid = ps["processid"].ToInt();
//var version = ps["version"] + "";
//var compile = ps["Compile"].ToDateTime();
var ns = Session as INetSession; var ns = Session as INetSession;
var ip = ns.Remote.Host; var ip = ns.Remote.Host;
WriteLog("[{0}]从[{1}]登录[{2}@{3}]", user, ns.Remote, machine, processId); WriteLog("[{0}]从[{1}]登录[{2}@{3}]", model.User, ns.Remote, model.Machine, model.ProcessId);
// 找应用 // 找应用
var autoReg = false; var autoReg = false;
var app = App.FindByName(user); var app = App.FindByName(model.User);
if (app == null || app.Secret.MD5() != pass) if (app == null || app.Secret.MD5() != model.Pass)
{ {
app = CheckApp(app, user, pass, ip); app = CheckApp(app, model.User, model.Pass, ip);
if (app == null) throw new ArgumentOutOfRangeException(nameof(user)); if (app == null) throw new ArgumentOutOfRangeException(nameof(model.User));
autoReg = true; autoReg = true;
} }
if (app == null) throw new Exception($"应用[{user}]不存在!"); if (app == null) throw new Exception($"应用[{model.User}]不存在!");
if (!app.Enable) throw new Exception("已禁用!"); if (!app.Enable) throw new Exception("已禁用!");
// 核对密码 // 核对密码
if (!autoReg && !app.Secret.IsNullOrEmpty()) if (!autoReg && !app.Secret.IsNullOrEmpty())
{ {
var pass2 = app.Secret.MD5(); var pass2 = app.Secret.MD5();
if (pass != pass2) throw new Exception("密码错误!"); if (model.Pass != pass2) throw new Exception("密码错误!");
} }
// 版本和编译时间 // 版本和编译时间
if (app.Version.IsNullOrEmpty() || app.Version.CompareTo(version) < 0) app.Version = version; if (app.Version.IsNullOrEmpty() || app.Version.CompareTo(model.Version) < 0) app.Version = model.Version;
if (app.CompileTime < compile) app.CompileTime = compile; if (app.CompileTime < model.Compile) app.CompileTime = model.Compile;
if (app.DisplayName.IsNullOrEmpty()) app.DisplayName = displayName; if (app.DisplayName.IsNullOrEmpty()) app.DisplayName = model.DisplayName;
app.Save(); app.Save();
// 应用上线 // 应用上线
var online = CreateOnline(app, ns, machine, processId); var online = CreateOnline(app, ns, model.Machine, model.ProcessId);
online.Version = version; online.Version = model.Version;
online.CompileTime = compile; online.CompileTime = model.Compile;
online.Save(); online.Save();
// 记录当前用户 // 记录当前用户
Session["App"] = app; Session["App"] = app;
WriteHistory(autoReg ? "注册" : "登录", true, $"[{user}/{pass}]在[{machine}@{processId}]登录[{app}]成功"); WriteHistory(autoReg ? "注册" : "登录", true, $"[{model.User}/{model.Pass}]在[{model.Machine}@{model.ProcessId}]登录[{app}]成功");
if (autoReg) var rs = new LoginResponse { Name = app.Name, DisplayName = app.DisplayName };
return new if (autoReg) rs.Secret = app.Secret;
{
app.Name, return rs;
app.Secret,
app.DisplayName,
};
else
return new
{
app.Name,
app.DisplayName,
};
} }
protected virtual App CheckApp(App app, String user, String pass, String ip) protected virtual App CheckApp(App app, String user, String pass, String ip)

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntJob.Models
{
/// <summary>登录模型</summary>
public class LoginModel
{
/// <summary>用户名</summary>
public String User { get; set; }
/// <summary>用户名</summary>
public String Pass { get; set; }
/// <summary>显示名</summary>
public String DisplayName { get; set; }
/// <summary>机器名</summary>
public String Machine { get; set; }
/// <summary>进程Id</summary>
public Int32 ProcessId { get; set; }
/// <summary>版本</summary>
public String Version { get; set; }
/// <summary>编译时间</summary>
public DateTime Compile { get; set; }
}
/// <summary>登录响应</summary>
public class LoginResponse
{
/// <summary>名称</summary>
public String Name { get; set; }
/// <summary>密钥。仅注册时返回</summary>
public String Secret { get; set; }
/// <summary>显示名</summary>
public String DisplayName { get; set; }
}
}

View File

@ -6,6 +6,7 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using AntJob.Data; using AntJob.Data;
using AntJob.Models;
using NewLife; using NewLife;
using NewLife.Log; using NewLife.Log;
using NewLife.Net; using NewLife.Net;
@ -29,7 +30,7 @@ namespace AntJob.Providers
public Boolean Logined { get; set; } public Boolean Logined { get; set; }
/// <summary>最后一次登录成功后的消息</summary> /// <summary>最后一次登录成功后的消息</summary>
public IDictionary<String, Object> Info { get; private set; } public LoginResponse Info { get; private set; }
#endregion #endregion
#region #region
@ -79,26 +80,26 @@ namespace AntJob.Providers
var des = asmx?.Asm.GetCustomAttribute<DescriptionAttribute>(); var des = asmx?.Asm.GetCustomAttribute<DescriptionAttribute>();
var dname = title?.Title ?? dis?.DisplayName ?? des?.Description; var dname = title?.Title ?? dis?.DisplayName ?? des?.Description;
var arg = new var arg = new LoginModel
{ {
user = UserName, User = UserName,
pass = Password.IsNullOrEmpty() ? null : Password.MD5(), Pass = Password.IsNullOrEmpty() ? null : Password.MD5(),
DisplayName = dname, DisplayName = dname,
machine = Environment.MachineName, Machine = Environment.MachineName,
processid = Process.GetCurrentProcess().Id, ProcessId = Process.GetCurrentProcess().Id,
version = asmx?.Version, Version = asmx.Version,
asmx?.Compile, Compile = asmx.Compile,
}; };
var rs = await base.InvokeWithClientAsync<IDictionary<String, Object>>(client, "Login", arg); var rs = await base.InvokeWithClientAsync<LoginResponse>(client, "Login", arg);
var set = AntSetting.Current; var set = AntSetting.Current;
if (set.Debug) XTrace.WriteLine("登录{0}成功!{1}", client, rs.ToJson()); if (set.Debug) XTrace.WriteLine("登录{0}成功!{1}", client, rs.ToJson());
// 保存下发密钥 // 保存下发密钥
if (rs.TryGetValue("Secret", out var secret)) if (!rs.Secret.IsNullOrEmpty())
{ {
set.Secret = secret + ""; set.Secret = rs.Secret;
set.Save(); set.Save();
} }