网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
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);
    }
}
⚠⚠    以下内容为AI分析的结果,请根据实际情况进行判断。

上述代码是一个 ASP.NET Core MVC 控制器类 MemberController,它主要负责处理与用户会员相关的操作。以下是对代码中各个部分功能的详细解释:

1. 控制器构造函数

public MemberController(
    IArticleService articleService,
    IPictureRecordService pictureRecordService,
    IUserTwoFactorService userTwoFactorService,
    IMediator mediator
)
  • 该构造函数接受多个服务的实例,这些服务用于处理文章、图片记录、用户双因素认证和请求的中介。

2. Index 方法

[CheckAuthorize, Route("member.html")]
public async Task<IActionResult> Index()
  • 该方法处理 GET 请求,返回会员主页。
  • 它获取所有文章标签和图片标签,并获取当前用户的信息,最后将这些数据封装到 MemberModel 中并返回视图。

3. MyInfo 方法

[CheckAuthorize, Route("my/info")]
public async Task<IActionResult> MyInfo()
  • 该方法返回当前用户的基本信息,包括用户 ID、姓名、性别、签名、头像和双因素认证绑定状态。

4. BindTwoFactor 方法

[HttpGet]
[CheckAuthorize]
[Route("my/two-factor")]
public async Task<IActionResult> BindTwoFactor()
  • 该方法用于生成双因素认证的设置代码。
  • 如果用户已经绑定了双因素认证,则返回空模型;否则,生成并返回设置代码。

5. HandleBindTwoFactor 方法

[HttpPost]
[CheckAuthorize]
[Route("my/bind-two-factor")]
public async Task<IActionResult> HandleBindTwoFactor(string pinCode)
  • 该方法处理用户绑定双因素认证的请求。
  • 它验证用户提供的 PIN 码,如果验证成功,则将双因素认证绑定到用户账户。

6. UnbindTwoFactor 方法

[HttpPost]
[CheckAuthorize]
[Route("my/unbind-two-factor")]
public async Task<IActionResult> UnbindTwoFactor(string pinCode)
  • 该方法处理用户解除双因素认证绑定的请求。
  • 它验证用户提供的 PIN 码,如果验证成功,则解除绑定。

7. GetAlbums 方法

[HttpGet, CheckAuthorize, Route("my/photos")]
public async Task<IActionResult> GetAlbums(string? tag, string? description, int pageIndex = 1, int pageSize = 12)
  • 该方法用于获取用户的相册列表,支持按标签和描述进行过滤,并支持分页。

8. GetPhoto 方法

[HttpGet, CheckAuthorize, Route("my/photos/get/{id}")]
public async Task<IActionResult> GetPhoto(string id)
  • 该方法根据相册照片的 ID 获取具体的照片记录。

9. GetPhotoTags 方法

[HttpGet, CheckAuthorize, Route("my/photos/tags")]
public async Task<IActionResult> GetPhotoTags(string id)
  • 该方法获取所有相册照片的标签。

10. DeletePhoto 方法

[HttpPost, CheckAuthorize, Route("my/photos/delete")]
public async Task<IActionResult> DeletePhoto(string id)
  • 该方法处理删除相册照片的请求,只有照片的创建者才能删除。

11. PublishPhoto 方法

[HttpPost, CheckAuthorize, Route("my/photos/publish")]
public async Task<IActionResult> PublishPhoto(IFormFile? photo, string? description, List<string>? tags, string? id = null)
  • 该方法处理发布相册照片的请求,接收照片文件、描述和标签,并通过中介发送请求。

总结

MemberController 主要负责处理与用户会员相关的操作,包括用户信息、双因素认证、相册管理等。它通过依赖注入的方式使用多个服务来实现这些功能,并通过 JSON 格式返回结果。

loading