.NETAdmin/ZR.Admin.WebApi/Controllers/WxOpenController.cs
2025-08-01 13:57:31 +08:00

129 lines
4.3 KiB
C#

using Microsoft.AspNetCore.Mvc;
using SKIT.FlurlHttpClient.Wechat.Api;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
using System.Web;
using ZR.Model.Business.Dto;
using ZR.Model.Business;
using ZR.Service.Business.IBusinessService;
namespace ZR.Admin.WebApi.Controllers
{
/// <summary>
/// 微信公众号
/// </summary>
[Route("[controller]/[action]")]
[AllowAnonymous]
public class WxOpenController : BaseController
{
private readonly WechatApiClient _client;
/// <summary>
/// 礼品申领表接口
/// </summary>
private readonly IGiftClaimService _GiftClaimService;
/// <summary>
/// 微信用户表接口
/// </summary>
private readonly IGiftUserService _GiftUserService;
public WxOpenController(WechatApiClient client, IGiftUserService GiftUserService, IGiftClaimService giftClaimService)
{
_client = client;
_GiftUserService = GiftUserService;
_GiftClaimService = giftClaimService;
}
/// <summary>
/// 获取签名
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
[Log(Title = "获取微信签名")]
[HttpGet]
public IActionResult GetSignature(string url = "")
{
url = HttpUtility.UrlDecode(url);
var appId = AppSettings.App(new string[] { "WxOpen", "AppID" });
var noncestr = Guid.NewGuid().ToString().Replace("-", "");
var timestamp = DateTimeHelper.GetUnixTimeSeconds(DateTime.Now);
var ticketResult = WxHelper.GetTicket();
if (appId.IsEmpty()) return ToResponse(ResultCode.CUSTOM_ERROR, "appId未配置");
if (ticketResult?.errcode != 0)
{
return ToResponse(ResultCode.CUSTOM_ERROR, "获取配置失败");
}
var signature = WxHelper.GetSignature(ticketResult.ticket, timestamp.ToString(), noncestr, url);
return SUCCESS(new { appId, signature, noncestr, timestamp, url });
}
[HttpGet("login")]
[AllowAnonymous]
public async Task<IActionResult> 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)
{
//response.
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<IActionResult> GetRecord([FromQuery] int user_id)
{
if (user_id == 0)
{
return ToResponse(ResultCode.CUSTOM_ERROR, "用户不存在");
}
var user = await _GiftUserService.AsQueryable().Where(it => it.Id == user_id).FirstAsync();
if (user == null)
{
return ToResponse(ResultCode.CUSTOM_ERROR, "用户不存在");
}
var list = await _GiftClaimService.AsQueryable().Where(it => it.UserId == user_id).ToListAsync();
List<object> list1 = new List<object>();
list?.ForEach(it =>
{
list1.Add(new
{
it.Status,
it.Name,
it.Address,
it.Phone
});
});
return SUCCESS(list1);
}
}
}