feat: 更新手机用户首次登录逻辑

This commit is contained in:
Argo-Tianyi 2022-01-06 08:54:21 +08:00
parent 0d0443fddb
commit bdae9766dc
4 changed files with 41 additions and 24 deletions

View File

@ -44,8 +44,6 @@ class BootstrapAdminConventionMapper : ConventionMapper
var ci = base.GetColumnInfo(pocoProperty); var ci = base.GetColumnInfo(pocoProperty);
var resultColumns = new List<string> var resultColumns = new List<string>
{ {
nameof(Models.User.Password),
nameof(Models.User.PassSalt),
nameof(Models.User.NewPassword), nameof(Models.User.NewPassword),
nameof(Models.User.ConfirmPassword), nameof(Models.User.ConfirmPassword),
nameof(Models.User.Period), nameof(Models.User.Period),

View File

@ -49,7 +49,7 @@ class DefaultDataService<TModel> : DataServiceBase<TModel> where TModel : class,
{ {
if (model is User user) if (model is User user)
{ {
UserService.SaveUser(user.Id, user.UserName, user.DisplayName, user.NewPassword); UserService.SaveUser(user.UserName, user.DisplayName, user.NewPassword);
} }
else else
{ {

View File

@ -128,7 +128,7 @@ class UserService : BaseDatabase, IUser
} }
/// <summary> /// <summary>
/// /// 创建手机用户
/// </summary> /// </summary>
/// <param name="phone"></param> /// <param name="phone"></param>
/// <param name="appId"></param> /// <param name="appId"></param>
@ -139,11 +139,13 @@ class UserService : BaseDatabase, IUser
var ret = false; var ret = false;
try try
{ {
var user = GetAll().FirstOrDefault(user => user.UserName == phone); var salt = LgbCryptography.GenerateSalt();
var pwd = LgbCryptography.ComputeHash(code, salt);
var user = Database.FirstOrDefault<User>("UserName = @0", phone);
if (user == null) if (user == null)
{ {
var salt = LgbCryptography.GenerateSalt();
Database.BeginTransaction(); Database.BeginTransaction();
// 插入用户
user = new User() user = new User()
{ {
ApprovedBy = "Mobile", ApprovedBy = "Mobile",
@ -157,12 +159,17 @@ class UserService : BaseDatabase, IUser
App = appId App = appId
}; };
Database.Save(user); Database.Save(user);
// Authorization // Authorization
var roleIds = Database.Fetch<string>("select ID from Roles where RoleName in (@roles)", new { roles }); var roleIds = Database.Fetch<string>("select ID from Roles where RoleName in (@roles)", new { roles });
Database.InsertBatch("UserRole", roleIds.Select(g => new { RoleID = g, UserID = user.Id })); Database.InsertBatch("UserRole", roleIds.Select(g => new { RoleID = g, UserID = user.Id }));
Database.CompleteTransaction(); Database.CompleteTransaction();
} }
else
{
user.PassSalt = salt;
user.Password = pwd;
Database.Update(user);
}
ret = true; ret = true;
} }
catch (Exception) catch (Exception)
@ -173,26 +180,35 @@ class UserService : BaseDatabase, IUser
return ret; return ret;
} }
public bool SaveUser(string? id, string userName, string displayName, string password) public bool SaveUser(string userName, string displayName, string password)
{ {
var salt = LgbCryptography.GenerateSalt(); var salt = LgbCryptography.GenerateSalt();
var pwd = LgbCryptography.ComputeHash(password, salt); var pwd = LgbCryptography.ComputeHash(password, salt);
if (string.IsNullOrEmpty(id)) var user = Database.FirstOrDefault<User>("UserName = @0", userName);
bool ret;
if (user == null)
{ {
try try
{ {
if (!Database.Exists<User>("UserName = @0", userName)) // 开始事务
Database.BeginTransaction();
user = new User()
{ {
// 开始事务 ApprovedBy = "System",
Database.BeginTransaction(); ApprovedTime = DateTime.Now,
// 插入用户 DisplayName = "手机用户",
Database.Execute("INSERT INTO Users (UserName, Password, PassSalt, DisplayName, RegisterTime, ApprovedTime, ApprovedBy, Description) values (@0, @1, @2, @3, @4, @4, 'system', '系统默认创建');", userName, pwd, salt, displayName, DateTime.Now); UserName = userName,
Icon = "default.jpg",
// 授权 Default 角色 Description = "系统默认创建",
Database.Execute("insert into UserRole (UserID, RoleID) select ID, (select ID from Roles where RoleName = 'Default') RoleId from Users where UserName = @0", userName); PassSalt = salt,
// 结束事务 Password = pwd
Database.CompleteTransaction(); };
} Database.Save(user);
// 授权 Default 角色
Database.Execute("insert into UserRole (UserID, RoleID) select ID, (select ID from Roles where RoleName = 'Default') RoleId from Users where UserName = @0", userName);
// 结束事务
Database.CompleteTransaction();
ret = true;
} }
catch (Exception) catch (Exception)
{ {
@ -202,8 +218,12 @@ class UserService : BaseDatabase, IUser
} }
else else
{ {
Database.Update<User>("set Password = @1, PassSalt = @2, DisplayName = @3 where Id = @0", id, pwd, salt, displayName); user.DisplayName = displayName;
user.PassSalt = salt;
user.Password = pwd;
Database.Update(user);
ret = true;
} }
return true; return ret;
} }
} }

View File

@ -84,10 +84,9 @@ public interface IUser
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="id"></param>
/// <param name="userName"></param> /// <param name="userName"></param>
/// <param name="displayName"></param> /// <param name="displayName"></param>
/// <param name="password"></param> /// <param name="password"></param>
/// <returns></returns> /// <returns></returns>
bool SaveUser(string? id, string userName, string displayName, string password); bool SaveUser(string userName, string displayName, string password);
} }