网站首页 网站源码
using Dpz.Core.Authenticator;
using Dpz.Core.Public.ViewModel.V4;
using Dpz.Core.Service.V4.Services;
using Dpz.Core.Web.Models.EventRequest;
namespace Dpz.Core.Web.Controllers;
public class MemberController(
IArticleService articleService,
IPictureRecordService pictureRecordService,
IUserTwoFactorService userTwoFactorService,
IMediator mediator
) : Controller
{
[CheckAuthorize, Route("member.html")]
public async Task<IActionResult> Index()
{
var articleTags = await articleService.GetAllTagsAsync();
var pictureTags = await pictureRecordService.GetTagsAsync();
var userInfo = User.GetStrictIdentity();
var model = new MemberModel(articleTags, pictureTags, userInfo);
return View(model);
}
[CheckAuthorize, Route("my/info")]
public async Task<IActionResult> MyInfo()
{
var userInfo = User.GetStrictIdentity();
var (_, isBind) = await userTwoFactorService.GetKeyAsync(userInfo.Id);
var model = new MyInformationModel(
userInfo.Id,
userInfo.Name,
userInfo.Sex.ToString(),
userInfo.Sign,
userInfo.Avatar,
isBind
);
return Json(new ResultInfo(model));
}
[HttpGet]
[CheckAuthorize]
[Route("my/two-factor")]
public async Task<IActionResult> BindTwoFactor()
{
var userInfo = User.GetStrictIdentity();
SetupCode? model = null;
var (key, isBind) = await userTwoFactorService.GetKeyAsync(userInfo.Id);
if (isBind)
{
return Json(model);
}
var twoFactorAuthenticator = new TwoFactorAuthenticator();
model = twoFactorAuthenticator.GenerateSetupCode(
issuer: "叫我阿胖",
accountTitleNoSpaces: userInfo.Id,
accountSecretKey: key,
secretIsBase32: false
);
return Json(model);
}
[CheckAuthorize]
[HttpPost]
[Route("my/bind-two-factor")]
public async Task<IActionResult> HandleBindTwoFactor(string pinCode)
{
var userInfo = User.GetStrictIdentity();
var (key, isBind) = await userTwoFactorService.GetKeyAsync(userInfo.Id);
if (isBind)
{
return Json(new ResultInfo("已绑定双因素验证"));
}
var twoFactorAuthenticator = new TwoFactorAuthenticator();
var keyBuffer = Encoding.UTF8.GetBytes(key);
var keyBase32 = Base32Encoding.ToString(keyBuffer);
var twoFactorResult = twoFactorAuthenticator.ValidateTwoFactorPIN(keyBase32, pinCode, true);
if (!twoFactorResult)
{
return Json(new ResultInfo("PIN码验证失败"));
}
await userTwoFactorService.BindAsync(userInfo.Id);
return Json(new ResultInfo(true));
}
[HttpPost]
[CheckAuthorize]
[Route("my/unbind-two-factor")]
public async Task<IActionResult> UnbindTwoFactor(string pinCode)
{
var userInfo = User.GetStrictIdentity();
var (key, isBind) = await userTwoFactorService.GetKeyAsync(userInfo.Id);
if (!isBind)
{
return Json(new ResultInfo("未绑定双因素验证"));
}
var twoFactorAuthenticator = new TwoFactorAuthenticator();
var keyBuffer = Encoding.UTF8.GetBytes(key);
var keyBase32 = Base32Encoding.ToString(keyBuffer);
var twoFactorResult = twoFactorAuthenticator.ValidateTwoFactorPIN(keyBase32, pinCode, true);
if (!twoFactorResult)
{
return Json(new ResultInfo("PIN码验证错误!"));
}
await userTwoFactorService.UnbindAsync(userInfo.Id);
return Json(new ResultInfo(true));
}
[HttpGet, CheckAuthorize, Route("my/photos")]
public async Task<IActionResult> GetAlbums(
string? tag,
string? description,
int pageIndex = 1,
int pageSize = 12
)
{
var account = User.GetStrictIdentity().Id;
if (string.IsNullOrEmpty(account))
{
return Json(new ResultInfo("未授权"));
}
var list = await pictureRecordService.GetPagesAsync(
tag == null ? null : [tag],
description,
pageIndex,
pageSize,
account
);
var data = Pagination<VmPictureRecord>.Create(list);
return Json(new ResultInfo(data));
}
/// <summary>
/// 获取相册照片
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet, CheckAuthorize, Route("my/photos/get/{id}")]
public async Task<IActionResult> GetPhoto(string id)
{
var pictureRecord = await pictureRecordService.GetPictureRecordAsync(id);
return Json(new ResultInfo(pictureRecord));
}
/// <summary>
/// 获取相册照片标签
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet, CheckAuthorize, Route("my/photos/tags")]
public async Task<IActionResult> GetPhotoTags(string id)
{
var tags = await pictureRecordService.GetTagsAsync();
return Json(new ResultInfo(tags));
}
/// <summary>
/// 删除相册照片
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost, CheckAuthorize, Route("my/photos/delete")]
public async Task<IActionResult> DeletePhoto(string id)
{
var pictureRecord = await pictureRecordService.GetPictureRecordAsync(id);
if (pictureRecord == null || pictureRecord.Creator.Id != User.Identity?.Name)
{
return Json(new ResultInfo("删除失败,没有权限删除或者不存在!"));
}
await pictureRecordService.DeleteAsync(id);
return Json(new ResultInfo(true));
}
/// <summary>
/// 发布相册照片
/// </summary>
/// <param name="photo"></param>
/// <param name="description"></param>
/// <param name="tags"></param>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost, CheckAuthorize, Route("my/photos/publish")]
public async Task<IActionResult> PublishPhoto(
IFormFile? photo,
string? description,
List<string>? tags,
string? id = null
)
{
var request = new PublishPhotoRequest
{
Creator = User.GetStrictIdentity(),
Photo = photo,
Description = description,
Tags = tags,
Id = id,
};
return await mediator.Send(request);
}
}
上述代码是一个 ASP.NET Core MVC 控制器类 MemberController
,它主要负责处理与用户会员相关的操作。以下是对代码中各个部分功能的详细解释:
public MemberController(
IArticleService articleService,
IPictureRecordService pictureRecordService,
IUserTwoFactorService userTwoFactorService,
IMediator mediator
)
[CheckAuthorize, Route("member.html")]
public async Task<IActionResult> Index()
MemberModel
中并返回视图。[CheckAuthorize, Route("my/info")]
public async Task<IActionResult> MyInfo()
[HttpGet]
[CheckAuthorize]
[Route("my/two-factor")]
public async Task<IActionResult> BindTwoFactor()
[HttpPost]
[CheckAuthorize]
[Route("my/bind-two-factor")]
public async Task<IActionResult> HandleBindTwoFactor(string pinCode)
[HttpPost]
[CheckAuthorize]
[Route("my/unbind-two-factor")]
public async Task<IActionResult> UnbindTwoFactor(string pinCode)
[HttpGet, CheckAuthorize, Route("my/photos")]
public async Task<IActionResult> GetAlbums(string? tag, string? description, int pageIndex = 1, int pageSize = 12)
[HttpGet, CheckAuthorize, Route("my/photos/get/{id}")]
public async Task<IActionResult> GetPhoto(string id)
[HttpGet, CheckAuthorize, Route("my/photos/tags")]
public async Task<IActionResult> GetPhotoTags(string id)
[HttpPost, CheckAuthorize, Route("my/photos/delete")]
public async Task<IActionResult> DeletePhoto(string id)
[HttpPost, CheckAuthorize, Route("my/photos/publish")]
public async Task<IActionResult> PublishPhoto(IFormFile? photo, string? description, List<string>? tags, string? id = null)
MemberController
主要负责处理与用户会员相关的操作,包括用户信息、双因素认证、相册管理等。它通过依赖注入的方式使用多个服务来实现这些功能,并通过 JSON 格式返回结果。