key1_beacon_api/ARW.WebApi/Controllers/Api/Wechat/WeChatLoginController.cs
2023-06-05 16:59:30 +08:00

95 lines
3.1 KiB
C#

using ARW.Admin.WebApi.Controllers;
using ARW.Admin.WebApi.Extensions;
using ARW.Admin.WebApi.Framework;
using ARW.Model.Dto.Business.Customers;
using ARW.Model.Models.Business.Customers;
using ARW.Model.System;
using ARW.Service.Business.IBusinessService.Customers;
using Infrastructure.WeChat.Login;
using Infrastructure;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Net;
using Senparc.Weixin.WxOpen.AdvancedAPIs.WxApp.Business.JsonResult;
using Senparc.Weixin.WxOpen;
using Senparc.Weixin;
using Senparc.Weixin.CommonAPIs;
namespace ARW.WebApi.Controllers.Api.Wechat
{
/// <summary>
/// 小程序登录控制器
/// </summary>
//[Verify]
[Route("api/[controller]")]
public class WeChatLoginController : BaseController
{
private readonly WeChatLogin _weChat;
private readonly ICustomerService _customerService;
private readonly OptionsSetting _jwtSettings;
static string session_key = "";
static string openId = "";
public WeChatLoginController(WeChatLogin weChat, ICustomerService customerService, IOptions<OptionsSetting> jwtSettings)
{
_weChat = weChat;
_customerService = customerService;
_jwtSettings = jwtSettings.Value;
}
/// <summary>
/// 注册小程序客户
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
[HttpPost("Login")]
public IActionResult AddCustomer([FromBody] CustomerDto parm)
{
if (parm == null) { throw new CustomException("请求参数错误"); }
var addModal = parm.Adapt<Customer>().ToCreate(HttpContext);
var user = _customerService.GetFirst(s => s.CustomerXcxOpenid == parm.CustomerXcxOpenid);
if (user == null)
{
var response = _customerService.InsertReturnSnowflakeId(addModal);
if (response == 0)
throw new CustomException("添加失败!");
var newUser = _customerService.GetFirst(s => s.CustomerGuid == response);
if (newUser != null)
{
LoginUser loginUser = new LoginUser
{
UserId = newUser.CustomerGuid,
UserName = newUser.CustomerNickname,
IsApi = true,
};
var jwt = JwtUtil.GenerateJwtToken(JwtUtil.AddClaims(loginUser), _jwtSettings.JwtSettings);
return SUCCESS(jwt);
}
}
else
{
LoginUser loginUser = new LoginUser
{
UserId = user.CustomerGuid,
UserName = user.CustomerNickname,
IsApi = true,
};
var jwt = JwtUtil.GenerateJwtToken(JwtUtil.AddClaims(loginUser), _jwtSettings.JwtSettings);
return SUCCESS(jwt);
}
return SUCCESS("666");
}
}
}