Skip to content

Commit

Permalink
更新部分api
Browse files Browse the repository at this point in the history
  • Loading branch information
MonoLogueChi committed Mar 14, 2020
1 parent cef0ee6 commit a6c08a2
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 55 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,5 @@ ASALocalRun/
# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
yarn-error.log*
/Danmu/appsettings.Development.json
6 changes: 3 additions & 3 deletions Danmu/Controllers/Account/AccountBaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task<WebResult> Login([FromBody] dynamic data)
{
var userName = data.TryGetProperty("name", out JsonElement a) ? a.GetString() : null;
var password = data.TryGetProperty("password", out JsonElement b) ? b.GetString() : null;
var returnUrl = data.TryGetProperty("url", out JsonElement c) ? c.GetString() : null;
var url = data.TryGetProperty("url", out JsonElement c) ? c.GetString() : null;

var r = await _userDao.VerPasswordAsync(userName, password);
if (r.Succeed)
Expand All @@ -71,11 +71,11 @@ await HttpContext.SignInAsync(new ClaimsPrincipal(new ClaimsIdentity(claims,
});


if (Url.IsLocalUrl(returnUrl)) return new WebResult(0) {Data = new {url = returnUrl}};
if (Url.IsLocalUrl(url)) return new WebResult(0) {Data = new {url = url}};
return new WebResult(0) {Data = new {url = "/"}};
}

return new WebResult(1) {Data = new {url = returnUrl}};
return new WebResult(1) {Data = new {url = url}};
}

[HttpGet("logout")]
Expand Down
51 changes: 20 additions & 31 deletions Danmu/Controllers/Admin/DanmakuListController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,22 @@ public class DanmuListController : AdminBaseController
public DanmuListController(DanmuDao danmuDao, VideoDao videoDao) : base(danmuDao, videoDao) { }

/// <summary>
/// 获取全部弹幕数量
/// 获取弹幕数量
/// </summary>
/// <returns></returns>
[HttpGet("count")]
public async Task<WebResult<int>> GetCount()
public async Task<WebResult<int>> GetCount(string vid = null)
{
var count = await DanmuDao.GetAllDanmuAsync();
return new WebResult<int>
if (string.IsNullOrEmpty(vid))
{
Code = 0,
Data = count
};
}
var allCount = await DanmuDao.GetAllDanmuAsync();
return new WebResult<int>
{
Code = 0,
Data = allCount
};
}

/// <summary>
/// 获取全部弹幕
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<WebResult<DanmuTable[]>> GetDanmuList(int page = 1, int size = 30, bool descending = true)
{
var allDanmu = await DanmuDao.GetAllDanmuAsync(page, size, descending);
return new WebResult<DanmuTable[]>(allDanmu);
}

/// <summary>
/// 通过vid查询到的弹幕总数
/// </summary>
/// <param name="vid"></param>
/// <returns></returns>
[HttpGet("count" + "by" + "vid")]
public async Task<WebResult<int>> GetCountByVid(string vid)
{
var count = await DanmuDao.GetDanmuByVidAsync(vid);
return new WebResult<int>
{
Expand All @@ -55,13 +38,19 @@ public async Task<WebResult<int>> GetCountByVid(string vid)
}

/// <summary>
/// 通过vid查询弹幕
/// 获取弹幕
/// </summary>
/// <returns></returns>
[HttpGet("query" + "by" + "vid")]
public async Task<WebResult<DanmuTable[]>> GetDanmuListByVid(string vid, int page = 1, int size = 30,
bool descending = true)
[HttpGet]
public async Task<WebResult<DanmuTable[]>> GetDanmuList(string vid = null, int page = 1, int size = 30,
bool descending = true)
{
if (string.IsNullOrEmpty(vid))
{
var allDanmu = await DanmuDao.GetAllDanmuAsync(page, size, descending);
return new WebResult<DanmuTable[]>(allDanmu);
}

var danmu = await DanmuDao.GetDanmuByVidAsync(vid, page, size, descending);
return new WebResult<DanmuTable[]>(danmu);
}
Expand Down
14 changes: 9 additions & 5 deletions Danmu/Model/DataTable/UserTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Danmu.Utils.Common;

namespace Danmu.Model.DataTable
{
Expand All @@ -20,26 +21,29 @@ public class UserTable
/// 用户名
/// </summary>
[Required]
[MaxLength(16)]
[MinLength(4)]
public string Name { get; set; }

/// <summary>
/// 密码
/// </summary>
[Required]
[MinLength(6)]
public string PassWord { get; set; }

/// <summary>
/// 盐
/// </summary>
[Required]
public string Salt { get; set; }
public string Salt { get; set; } = new RandomStringBuilder().Create(6);

/// <summary>
/// 用户角色
/// </summary>
[Required]
[DefaultValue(UserRole.GeneralUser)]
public UserRole Role { get; set; } = UserRole.GeneralUser;
public UserRole Role { get; set; } = UserRole.Guests;

/// <summary>
/// 手机号
Expand All @@ -66,9 +70,9 @@ public class UserTable

public enum UserRole
{
SuperAdmin = 0,
Admin = 1,
Guests = 2,
Guests = 0,
SuperAdmin = 1,
Admin = 2,
GeneralUser = 3
}
}
2 changes: 1 addition & 1 deletion Danmu/Utils/Common/RandomStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ public string Create(int length)
return sb.ToString();
}
}
}
}
101 changes: 96 additions & 5 deletions Danmu/Utils/Dao/UserDao.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Danmu.Model.Config;
Expand All @@ -12,17 +13,23 @@ namespace Danmu.Utils.Dao
public class UserDao
{
private readonly Admin _admin;
private readonly DanmuContext _context;
private readonly DanmuContext _con;

public UserDao(AppConfiguration appConfiguration, DanmuContext context)
public UserDao(AppConfiguration appConfiguration, DanmuContext con)
{
_admin = appConfiguration.GetAppSetting().Admin;
_context = context;
_con = con;
}

/// <summary>
/// 验证用户名和密码是否匹配
/// </summary>
/// <param name="name">用户名</param>
/// <param name="password">密码</param>
/// <returns></returns>
public async Task<dynamic> VerPasswordAsync(string name, string password)
{
var r = _context.User.Where(e => e.Name.Equals(name));
var r = _con.User.Where(e => e.Name.Equals(name));
if (await r.CountAsync() == 0) return new {Succeed = false, Role = UserRole.GeneralUser};

var salt = await r.Select(s => s.Salt).FirstOrDefaultAsync();
Expand All @@ -38,5 +45,89 @@ await r.Where(e => e.PassWord.Equals(vpd))

//TODO 这里需要修改,增加一种重置密码的方式,不再使用配置文件保存密码
}

/// <summary>
/// 创建用户
/// </summary>
/// <param name="name"></param>
/// <param name="password"></param>
/// <param name="phoneNumber"></param>
/// <param name="email"></param>
/// <param name="role"></param>
/// <returns></returns>
public async Task<bool> InsertAsync(string name, string password, string phoneNumber = "",
string email = "", UserRole role = UserRole.GeneralUser)
{
if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(password)) return false;
var salt = new RandomStringBuilder().Create(6);
var user = new UserTable
{
Name = name,
Email = email,
PhoneNumber = phoneNumber,
Salt = salt,
PassWord = Md5.GetMd5(password, salt),
Role = role
};

await _con.User.AddAsync(user);
return await _con.SaveChangesAsync() > 0;
}

/// <summary>
/// 修改密码
/// </summary>
/// <param name="id">用户id</param>
/// <param name="oldP">旧密码</param>
/// <param name="newP">新密码</param>
/// <returns></returns>
public async Task<bool> ChangePasswordAsync(int id, string oldP, string newP)
{
if (string.IsNullOrWhiteSpace(oldP) || string.IsNullOrWhiteSpace(newP)) return false;
var r0 = _con.User.Where(e => e.Id.Equals(id));
if (await r0.CountAsync() == 0) return false;
var salt = await r0.Select(s => s.Salt).FirstOrDefaultAsync();
var ovpd = Md5.GetMd5(oldP, salt);
var r1 = r0.Where(e => e.PassWord.Equals(ovpd));
if (await r1.CountAsync() == 0) return false;
var salt1 = new RandomStringBuilder().Create(6);
var r2 = await r1.FirstOrDefaultAsync();
r2.PassWord = Md5.GetMd5(newP, salt1);
r2.Salt = salt1;
r2.UpDateTime = DateTime.UtcNow;
_con.User.Update(r2);
return await _con.SaveChangesAsync() > 0;
}

/// <summary>
/// 检查已使用用户名个数
/// </summary>
/// <param name="name">用户名</param>
/// <returns></returns>
public async Task<int> CheckNameAsync(string name)
{
return await _con.User.Where(e => e.Name.Equals(name)).CountAsync();
}

/// <summary>
/// 修改用户信息
/// </summary>
/// <param name="id">用户id</param>
/// <param name="name">用户名</param>
/// <param name="email">邮箱</param>
/// <param name="phoneNumber">手机号</param>
/// <returns></returns>
public async Task<bool> ChangeUserInfoAsync(int id, string name = null, string email = null,
string phoneNumber = null)
{
if (await _con.User.Where(e => e.Name.Equals(name)).CountAsync() > 0) return false;
var r = await _con.User.Where(e => e.Id.Equals(id)).FirstOrDefaultAsync();
r.Name = name ?? r.Name;
r.Email = email ?? r.Email;
r.PhoneNumber = phoneNumber ?? r.PhoneNumber;
r.UpDateTime = DateTime.UtcNow;
_con.User.Update(r);
return await _con.SaveChangesAsync() > 0;
}
}
}
}
9 changes: 0 additions & 9 deletions Danmu/appsettings.Development.json

This file was deleted.

0 comments on commit a6c08a2

Please sign in to comment.