using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
using SKIT.FlurlHttpClient.Wechat.Api;
using System.Web;
using ZR.Model.Business;
using ZR.Service.Business.IBusinessService;
using ZR.Model.Business.Dto;
using Org.BouncyCastle.Utilities;
using System.Net;
namespace ZR.Admin.WebApi.Controllers
{
///
/// webApi
///
[Route("[controller]/[action]")]
[AllowAnonymous]
[ApiExplorerSettings(GroupName = "webapi")]
public class WebApiController : BaseController
{
private readonly WechatApiClient _client;
///
/// 礼品申领表接口
///
private readonly IGiftClaimService _GiftClaimService;
///
/// 微信用户表接口
///
private readonly IGiftUserService _GiftUserService;
private ISysFileService SysFileService;
public WebApiController(WechatApiClient client, IGiftUserService GiftUserService, IGiftClaimService giftClaimService, ISysFileService sysFileService)
{
_client = client;
_GiftUserService = GiftUserService;
_GiftClaimService = giftClaimService;
SysFileService = sysFileService;
}
///
///
///
///
///
///
[HttpPost()]
[Route("/userLogin")]
[AllowAnonymous]
public async Task GetOpenId([FromQuery] string code)
{
var response = await _client.ExecuteSnsJsCode2SessionAsync(new SnsJsCode2SessionRequest
{
JsCode = code,
GrantType = "authorization_code"
});
if (!response.IsSuccessful())
{
throw new Exception($"获取OpenId失败: {response.ErrorMessage}"); // 可以根据需要处理异常
}
var openId = response.OpenId;
var user = await _GiftUserService.AsQueryable().Where(it => it.Openid == openId).FirstAsync();
if (user == null)
{
user = new GiftUser()
{
AvatarUrl = "",
CreateTime = DateTime.Now,
Nickname = "微信用户",
Openid = openId,
UpdateTime = DateTime.Now,
LastLoginTime = DateTime.Now,
Phone = "",
Status = "0",
Unionid = ""
};
_GiftUserService.AddGiftUser(user);
}
return SUCCESS(new { user_id = user.Id });
}
[HttpGet("/getRecord")]
[AllowAnonymous]
public async Task GetRecord([FromQuery] int userId)
{
if (userId == 0)
{
return ToResponse(ResultCode.CUSTOM_ERROR, "用户不存在");
}
var user = await _GiftUserService.AsQueryable().Where(it => it.Id == userId).FirstAsync();
if (user == null)
{
return ToResponse(ResultCode.CUSTOM_ERROR, "用户不存在");
}
var list = await _GiftClaimService.AsQueryable().Where(it => it.UserId == userId).OrderByDescending(it => it.Id).ToListAsync();
List