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 { /// /// 小程序登录控制器 /// //[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 jwtSettings) { _weChat = weChat; _customerService = customerService; _jwtSettings = jwtSettings.Value; } /// /// 注册小程序客户 /// /// /// [HttpPost("Login")] public IActionResult AddCustomer([FromBody] CustomerDto parm) { if (parm == null) { throw new CustomException("请求参数错误"); } var addModal = parm.Adapt().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"); } } }