vending-machine/backend/src/VendingMachine.Api/Controllers/CouponController.cs
2026-04-03 06:07:13 +08:00

101 lines
3.0 KiB
C#

using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using VendingMachine.Application.Common;
using VendingMachine.Application.Services;
namespace VendingMachine.Api.Controllers;
[ApiController]
[Route("api/[controller]")]
public class CouponController : ControllerBase
{
private readonly ICouponService _couponService;
public CouponController(ICouponService couponService)
{
_couponService = couponService;
}
/// <summary>
/// 获取可兑换优惠券列表
/// </summary>
[HttpGet("redeemable")]
public async Task<IActionResult> GetRedeemableCoupons()
{
var lang = GetLanguage();
var result = await _couponService.GetRedeemableCouponsAsync(lang);
return Ok(result);
}
/// <summary>
/// 兑换优惠券
/// </summary>
[Authorize]
[HttpPost("redeem/{couponId}")]
public async Task<IActionResult> RedeemCoupon(string couponId)
{
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrEmpty(uid))
return Unauthorized(ApiResponse.Fail("未授权"));
var result = await _couponService.RedeemCouponAsync(uid, couponId);
return result.Success ? Ok(result) : BadRequest(result);
}
/// <summary>
/// 获取用户优惠券列表
/// </summary>
[Authorize]
[HttpGet("my")]
public async Task<IActionResult> GetMyCoupons([FromQuery] string? status)
{
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrEmpty(uid))
return Unauthorized(ApiResponse.Fail("未授权"));
var lang = GetLanguage();
var result = await _couponService.GetMyCouponsAsync(uid, status, lang);
return Ok(result);
}
/// <summary>
/// 获取印花优惠券列表
/// </summary>
[Authorize]
[HttpGet("stamps")]
public async Task<IActionResult> GetStampCoupons()
{
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrEmpty(uid))
return Unauthorized(ApiResponse.Fail("未授权"));
var lang = GetLanguage();
var result = await _couponService.GetStampCouponsAsync(uid, lang);
return Ok(result);
}
/// <summary>
/// 兑换印花优惠券
/// </summary>
[Authorize]
[HttpPost("stamps/redeem/{stampCouponId}")]
public async Task<IActionResult> RedeemStampCoupon(string stampCouponId)
{
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrEmpty(uid))
return Unauthorized(ApiResponse.Fail("未授权"));
var result = await _couponService.RedeemStampCouponAsync(uid, stampCouponId);
return result.Success ? Ok(result) : BadRequest(result);
}
/// <summary>
/// 从请求头获取语言设置
/// </summary>
private string GetLanguage()
{
return HttpContext.Items["Language"]?.ToString() ?? "zh-CN";
}
}