key1_beacon_api/ARW.WebApi/Controllers/Api/Wechat/WeChatLoginController.cs

188 lines
6.1 KiB
C#

using ARW.Admin.WebApi.Controllers;
using ARW.Admin.WebApi.Extensions;
using ARW.Admin.WebApi.Framework;
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 Senparc.Weixin.WxOpen.AdvancedAPIs.WxApp;
using ARW.Common;
using ARW.Model.System.Dto;
using SixLabors.Shapes;
using ARW.Model.Dto.Business.Custom.Customers;
using ARW.Model.Models.Business.Custom.Customers;
using ARW.Service.System;
using Infrastructure.Attribute;
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;
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 async Task<IActionResult> Login([FromBody] CustomerLoginDto parm)
{
if (parm == null)
{
throw new CustomException("请求参数错误");
}
var addModal = parm.Adapt<Customer>().ToCreate(HttpContext);
addModal.CustomerMobilePhoneNumber = await GetUserPhoneNumber(parm.Code);
var user = await _customerService.GetFirstAsync(s => s.CustomerMobilePhoneNumber == addModal.CustomerMobilePhoneNumber);
if (user == null)
{
addModal.CustomerAvatar = "https://cdn-we-retail.ym.tencent.com/miniapp/usercenter/icon-user-center-avatar@2x.png";
addModal.CustomerNickname = "用户" + addModal.CustomerMobilePhoneNumber.Substring(addModal.CustomerMobilePhoneNumber.Length - 4); ;
addModal.CustomerLastLoginTime = DateTime.Now;
var response = await _customerService.InsertReturnSnowflakeIdAsync(addModal);
if (response == 0)
{
throw new CustomException("添加失败!");
}
user = await _customerService.GetFirstAsync(s => s.CustomerGuid == response);
}
LoginUser loginUser = new LoginUser
{
UserId = user.CustomerGuid,
UserName = user.CustomerNickname,
UserPhone = user.CustomerMobilePhoneNumber,
IsApi = true,
};
var jwt = JwtUtil.GenerateJwtToken(JwtUtil.AddClaims(loginUser), _jwtSettings.JwtSettings);
var dic = new Dictionary<string, object>
{
{ "jwt", jwt },
{ "user", user }
};
return SUCCESS(dic);
}
/// <summary>
/// 退出登录
/// </summary>
/// <returns></returns>
[Log(Title = "退出登录")]
[HttpPost("logout")]
public IActionResult LogOut()
{
var userid = HttpContext.GetUId();
var name = HttpContext.GetName();
CacheService.RemoveUserPerms(GlobalConstant.UserPermKEY + userid);
return SUCCESS(new { name, id = userid });
}
/// <summary>
/// 获取用户手机号
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public async Task<string> GetUserPhoneNumber(string code)
{
try
{
var Appid = AppSettings.GetConfig("SenparcWeixinSetting:TenPayV3_AppId");
var result = await BusinessApi.GetUserPhoneNumberAsync(Appid, code);
if (result.phone_info != null)
{
return result.phone_info.phoneNumber;
}
else
{
throw new Exception("获取手机号报错:" + result);
}
}
catch (Exception ex)
{
throw new Exception("获取手机号报错:" + ex);
}
}
/// <summary>
/// 发送手机验证码
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
[HttpPost("sendPhoneCode")]
public IActionResult PostEmailCode([FromBody] SendPhoneDto dto)
{
//var code = Tools.GetNumCode(4);
AliyunMsgHelper.SendPhoneMsgCode(dto.PhoneNumber);
//CacheHelper.SetCache(user.UserId.ToString() + "emailCode", code, 5);
//var emailCode = CacheHelper.GetCache(user.UserId.ToString() + "emailCode");
//Console.WriteLine(user.UserId.ToString() + ":" + emailCode);
return SUCCESS("发送成功!");
}
/// <summary>
/// 通过邮箱修改密码
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
[HttpPost("changePwByEmail")]
public IActionResult ChangePwByEmail([FromBody] ChangePwByEmailDto dto)
{
//var user = sysUserService.SelectUserByEmail(dto.Email);
//var emailCode = (string)CacheHelper.GetCache(user.UserId.ToString() + "emailCode");
//if (emailCode == null)
//{
// throw new CustomException("验证码已过期,请重新获取!");
//}
//if (dto.code == emailCode)
//{
// sysUserService.ResetPwd(user.UserId, dto.Password);
// return SUCCESS("密码重置成功");
//}
//else
//{
// throw new CustomException("验证码错误,请重试!");
//}
return SUCCESS("");
}
}
}