username@email.com
2024-08-15 24a3e64f5713fb0c6f8bae865715c66308371955
cylsg/cylsg.Application/LogoInController.cs
New file
@@ -0,0 +1,164 @@
using cylsg.Authorization;
using cylsg.Core;
using cylsg.Model.UserModel;
using cylsg.utility.Extend;
using EzWechat;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cylsg.Application
{
    /// <summary>
    /// 登录
    /// </summary>
    [DynamicApiController]
    public class LogoIn
    {
        private readonly IWechatService _wechatService;
        private readonly IEzAuthorizationService _ezAuthorizationService;
        public LogoIn(IWechatService wechatService,
            IEzAuthorizationService ezAuthorizationService)
        {
            _wechatService = wechatService;
            _ezAuthorizationService = ezAuthorizationService;
        }
        /// <summary>
        /// 快速登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [QueryParameters]
        public async Task<TokenInfo> LogoinFast(string jscode)
        {
            var opeid = await _wechatService.GetOpenID(jscode);
            var UserRes = new BaseRepository<User>();
            var user = await UserRes.GetFirstAsync(x => x.WxOpenId == opeid);
            if (user == null)
            {
                throw Oops.Oh("没找到用户,需要重新注册登录");
            }
            var jwt = new EzJwtModel()
            {
                ITCode = user.ItCode,
                NickName = user.Nickname,
                UserID = user.Id,
            };
            return   _ezAuthorizationService.CreateToken<EzJwtModel>(jwt);
        }
        /// <summary>
        /// 注册用户并登录
        /// </summary>
        /// <param name="Param">logoinjscode</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<TokenInfo> CreateUser(UserInfoIn Param)
        {
            var opeid = await _wechatService.GetOpenID(Param.JsCode);
            var UserRes = new BaseRepository<User>();
            var user = await UserRes.GetFirstAsync(x => x.WxOpenId == opeid);
            if (user == null)
            {
                //没有用户 ,需要新建用户
                var phone = await _wechatService.GetPhone(Param.Bindgetphonenumber);
                user = new User
                {
                    Avatar = phone,
                    Nickname = Param.Nickname??phone.PrivacyStr(),
                    name = Param.Name ?? phone,
                    Phone = phone,
                    ItCode = phone,
                    PassWord = "123456",
                    WxOpenId = opeid,
                };
                await UserRes.EzInsertAsync(user);
            }
            var jwt = new EzJwtModel()
            {
                ITCode = user.ItCode,
                NickName = user.Nickname,
                UserID = user.Id,
            };
            return _ezAuthorizationService.CreateToken<EzJwtModel>(jwt);
        }
    }
    /// <summary>
    /// 创建用户模型
    /// </summary>
      public class  UserInfoIn()
    {
        /// <summary>
        /// 头像
        /// </summary>
        public string? Avatar { get; set; }
        /// <summary>
        /// 昵称
        /// </summary>
        public string? Nickname { get; set; }
        /// <summary>
        /// 电话
        /// </summary>
        public string? Phone { get; set; }
        /// <summary>
        /// 名称
        /// </summary>
        public string? Name { get; set; }
        /// <summary>
        /// 手机号换取码
        /// </summary>
        [Required]
        public string Bindgetphonenumber {  get; set; }
        /// <summary>
        /// jscode
        /// </summary>
        [Required]
        public string JsCode { get; set;}
    }
}