refactor: 重构用户与商品模块,统一业务模型与服务
1. 新增用户状态枚举UserStatusEnum,统一用户状态定义 2. 重构用户体系:合并WxUser与User实体为Users实体,统一用户管理 3. 新增微信小程序认证相关服务与控制器,实现一键登录功能 4. 新增商品管理完整服务与控制器,修复商品表字段映射问题 5. 删除冗余的WxUser相关服务与控制器代码 6. 新增Newtonsoft.Json依赖用于微信接口响应解析 7. 清理无用的文件夹引用配置
This commit is contained in:
151
QYZH.InteractiveMagazine.WebApi/Controllers/ProductController.cs
Normal file
151
QYZH.InteractiveMagazine.WebApi/Controllers/ProductController.cs
Normal file
@ -0,0 +1,151 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using QYZH.InteractiveMagazine.IService;
|
||||
using QYZH.InteractiveMagazine.Models.Common;
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
using QYZH.InteractiveMagazine.Models.Enum;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.WebApi.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 商品管理控制器
|
||||
/// </summary>
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[ApiExplorerSettings(GroupName = nameof(ApiVersionEnum.Platform))]
|
||||
public class ProductController : BaseController
|
||||
{
|
||||
private readonly IProductService _productService;
|
||||
private readonly ILogger<ProductController> _logger;
|
||||
|
||||
public ProductController(IProductService productService, ILogger<ProductController> logger)
|
||||
{
|
||||
_productService = productService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建商品
|
||||
/// </summary>
|
||||
/// <param name="input">商品信息</param>
|
||||
/// <returns>创建的商品信息</returns>
|
||||
[HttpPost]
|
||||
public async Task<BaseResponse<ProductOutput>> CreateAsync([FromBody] ProductInput input)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _productService.CreateAsync(input);
|
||||
return Success(result, "创建商品成功");
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "创建商品业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<ProductOutput>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "创建商品系统异常,参数:{Input}", input);
|
||||
return BaseResponse<ProductOutput>.Fail("创建商品失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新商品
|
||||
/// </summary>
|
||||
/// <param name="id">商品ID</param>
|
||||
/// <param name="input">商品信息</param>
|
||||
/// <returns>更新后的商品信息</returns>
|
||||
[HttpPut("{id}")]
|
||||
public async Task<BaseResponse<ProductOutput>> UpdateAsync(long id, [FromBody] ProductInput input)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _productService.UpdateAsync(id, input);
|
||||
return Success(result, "更新商品成功");
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "更新商品业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<ProductOutput>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "更新商品系统异常,ID:{Id},参数:{Input}", id, input);
|
||||
return BaseResponse<ProductOutput>.Fail("更新商品失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除商品
|
||||
/// </summary>
|
||||
/// <param name="id">商品ID</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<BaseResponse<object>> DeleteAsync(long id)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _productService.DeleteAsync(id);
|
||||
return Success(new object(), "删除商品成功");
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "删除商品业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<object>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "删除商品系统异常,ID:{Id}", id);
|
||||
return BaseResponse<object>.Fail("删除商品失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据ID获取商品
|
||||
/// </summary>
|
||||
/// <param name="id">商品ID</param>
|
||||
/// <returns>商品信息</returns>
|
||||
[HttpGet("{id}")]
|
||||
public async Task<BaseResponse<ProductOutput>> GetByIdAsync(long id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _productService.GetByIdAsync(id);
|
||||
return Success(result);
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "获取商品业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<ProductOutput>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "获取商品系统异常,ID:{Id}", id);
|
||||
return BaseResponse<ProductOutput>.Fail("获取商品信息失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询商品列表
|
||||
/// </summary>
|
||||
/// <param name="input">查询条件</param>
|
||||
/// <returns>分页结果</returns>
|
||||
[HttpPost("list")]
|
||||
public async Task<BaseResponse<PageListModel<ProductOutput>>> GetListAsync([FromBody] ProductQueryInput input)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _productService.GetListAsync(input);
|
||||
return Success(result);
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "查询商品列表业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<PageListModel<ProductOutput>>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "查询商品列表系统异常,参数:{Input}", input);
|
||||
return BaseResponse<PageListModel<ProductOutput>>.Fail("查询商品列表失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,51 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using QYZH.InteractiveMagazine.IService;
|
||||
using QYZH.InteractiveMagazine.IService.Dto;
|
||||
using QYZH.InteractiveMagazine.Models.Common;
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.WebApi.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 用户控制器
|
||||
/// </summary>
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class UsersController : BaseController
|
||||
{
|
||||
private readonly IUsersService _usersService;
|
||||
private readonly ILogger<UsersController> _logger;
|
||||
|
||||
public UsersController(IUsersService usersService, ILogger<UsersController> logger)
|
||||
{
|
||||
_usersService = usersService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户列表(分页)
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public async Task<BaseResponse<PageListModel<UsersOutput>>> GetList([FromQuery] UsersQueryInput input)
|
||||
{
|
||||
return await _usersService.GetListAsync(input);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户详情
|
||||
/// </summary>
|
||||
[HttpGet("{id}")]
|
||||
public async Task<BaseResponse<UsersOutput>> GetDetail(long id)
|
||||
{
|
||||
return await _usersService.GetDetailAsync(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新用户状态
|
||||
/// </summary>
|
||||
[HttpPut("{id}/status")]
|
||||
public async Task<BaseResponse> UpdateStatus(long id, [FromBody] UpdateUserStatusInput input)
|
||||
{
|
||||
return await _usersService.UpdateStatusAsync(id, input);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,49 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using QYZH.InteractiveMagazine.IService;
|
||||
using QYZH.InteractiveMagazine.IService.Dto;
|
||||
using QYZH.InteractiveMagazine.Models.Common;
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.WebApi.Controllers.WeChat;
|
||||
|
||||
/// <summary>
|
||||
/// 微信小程序认证控制器
|
||||
/// </summary>
|
||||
public class WeChatAuthController : WeChatBaseController
|
||||
{
|
||||
private readonly IWeChatAuthService _weChatAuthService;
|
||||
private readonly ILogger<WeChatAuthController> _logger;
|
||||
|
||||
public WeChatAuthController(IWeChatAuthService weChatAuthService, ILogger<WeChatAuthController> logger)
|
||||
{
|
||||
_weChatAuthService = weChatAuthService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 微信小程序一键登录
|
||||
/// </summary>
|
||||
/// <param name="input">登录输入(含微信 code)</param>
|
||||
/// <returns>登录结果(含 Token 和用户信息)</returns>
|
||||
[AllowAnonymous]
|
||||
[HttpPost("login")]
|
||||
public async Task<BaseResponse<WeChatLoginOutput>> LoginAsync([FromBody] WeChatLoginInput input)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _weChatAuthService.LoginAsync(input);
|
||||
return Success(result);
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "微信登录业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<WeChatLoginOutput>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "微信登录系统异常");
|
||||
return BaseResponse<WeChatLoginOutput>.Fail("微信登录失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,63 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
using QYZH.InteractiveMagazine.Models.Enum;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.WebApi.Controllers.WeChat;
|
||||
|
||||
/// <summary>
|
||||
/// 小程序基础控制器
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
[ApiExplorerSettings(GroupName = nameof(ApiVersionEnum.Wechat))]
|
||||
public abstract class WeChatBaseController : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当前用户ID
|
||||
/// </summary>
|
||||
/// <returns>用户ID</returns>
|
||||
protected long? GetCurrentUserId()
|
||||
{
|
||||
var userIdClaim = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier);
|
||||
if (userIdClaim != null && long.TryParse(userIdClaim.Value, out var userId))
|
||||
{
|
||||
return userId;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前用户名
|
||||
/// </summary>
|
||||
/// <returns>用户名</returns>
|
||||
protected string? GetCurrentUserName()
|
||||
{
|
||||
return User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 成功响应
|
||||
/// </summary>
|
||||
/// <typeparam name="T">数据类型</typeparam>
|
||||
/// <param name="data">数据</param>
|
||||
/// <param name="message">提示信息</param>
|
||||
/// <returns>统一响应对象</returns>
|
||||
protected BaseResponse<T> Success<T>(T data, string message = "操作成功")
|
||||
{
|
||||
return BaseResponse<T>.Success(data, message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 失败响应
|
||||
/// </summary>
|
||||
/// <param name="message">提示信息</param>
|
||||
/// <param name="code">状态码</param>
|
||||
/// <returns>统一响应对象</returns>
|
||||
protected BaseResponse<object> Fail(string message, int code = 500)
|
||||
{
|
||||
return BaseResponse<object>.Fail(ResultCode.GLOBAL_ERROR, message);
|
||||
}
|
||||
}
|
||||
@ -1,147 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using QYZH.InteractiveMagazine.IService;
|
||||
using QYZH.InteractiveMagazine.IService.Dto;
|
||||
using QYZH.InteractiveMagazine.Models.Common;
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.WebApi.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 微信用户控制器
|
||||
/// </summary>
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class WxUserController : BaseController
|
||||
{
|
||||
private readonly IWxUserService _wxUserService;
|
||||
private readonly ILogger<WxUserController> _logger;
|
||||
|
||||
public WxUserController(IWxUserService wxUserService, ILogger<WxUserController> logger)
|
||||
{
|
||||
_wxUserService = wxUserService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建微信用户
|
||||
/// </summary>
|
||||
/// <param name="input">创建微信用户输入参数</param>
|
||||
/// <returns>创建的微信用户输出参数</returns>
|
||||
[HttpPost("users")]
|
||||
public async Task<BaseResponse<WxUserOutput>> CreateUserAsync([FromBody] WxUserInput input)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _wxUserService.CreateAsync(input);
|
||||
return Success(result, "创建微信用户成功");
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "创建微信用户业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<WxUserOutput>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "创建微信用户系统异常,参数:{Input}", input);
|
||||
return BaseResponse<WxUserOutput>.Fail("创建微信用户失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新微信用户
|
||||
/// </summary>
|
||||
/// <param name="id">微信用户ID</param>
|
||||
/// <param name="input">更新微信用户输入参数</param>
|
||||
/// <returns>更新的微信用户输出参数</returns>
|
||||
[HttpPut("users/{id}")]
|
||||
public async Task<BaseResponse<WxUserOutput>> UpdateUserAsync(long id, [FromBody] WxUserInput input)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _wxUserService.UpdateAsync(id, input);
|
||||
return Success(result, "更新微信用户成功");
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "更新微信用户业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<WxUserOutput>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "更新微信用户系统异常,ID:{Id},参数:{Input}", id, input);
|
||||
return BaseResponse<WxUserOutput>.Fail("更新微信用户失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除微信用户
|
||||
/// </summary>
|
||||
/// <param name="id">微信用户ID</param>
|
||||
/// <returns>删除的微信用户输出参数</returns>
|
||||
[HttpDelete("users/{id}")]
|
||||
public async Task<BaseResponse<object>> DeleteUserAsync(long id)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _wxUserService.DeleteAsync(id);
|
||||
return Success(new object(), "删除微信用户成功");
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "删除微信用户业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<object>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "删除微信用户系统异常,ID:{Id}", id);
|
||||
return BaseResponse<object>.Fail("删除微信用户失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取微信用户信息
|
||||
/// </summary>
|
||||
/// <param name="id">微信用户ID</param>
|
||||
/// <returns>微信用户信息输出参数</returns>
|
||||
[HttpGet("users/{id}")]
|
||||
public async Task<BaseResponse<WxUserOutput>> GetUserByIdAsync(long id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _wxUserService.GetByIdAsync(id);
|
||||
return Success(result);
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "获取微信用户业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<WxUserOutput>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "获取微信用户系统异常,ID:{Id}", id);
|
||||
return BaseResponse<WxUserOutput>.Fail("获取微信用户信息失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取微信用户列表
|
||||
/// </summary>
|
||||
/// <param name="input">查询微信用户列表输入参数</param>
|
||||
/// <returns>微信用户列表输出参数</returns>
|
||||
[HttpPost("users/list")]
|
||||
public async Task<BaseResponse<PageListModel<WxUserOutput>>> GetUsersListAsync([FromBody] WxUserQueryInput input)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _wxUserService.GetListAsync(input);
|
||||
return Success(result);
|
||||
}
|
||||
catch (BusinessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "查询微信用户列表业务异常: {Message}", ex.Message);
|
||||
return BaseResponse<PageListModel<WxUserOutput>>.Fail(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "查询微信用户列表系统异常,参数:{Input}", input);
|
||||
return BaseResponse<PageListModel<WxUserOutput>>.Fail("查询微信用户列表失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user