/// <summary>
        /// 注册
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public MsgModel SignUp(SysUser user)
        {
            var dict         = new Dictionary <string, object>();
            var stringRandom = StringHelper.GenerateRandom(10);

            user.username = stringRandom;
            //user.nickname = stringRandom;
            if (user.phone != null)
            {
                var queryUser = _baseService.GetModels(a => a.phone == user.phone).SingleOrDefault();
                if (queryUser == null)
                {
                    var sysUser = new sys_user();
                    sysUser = user.BuildAdapter().AdaptToType <sys_user>();
                    _baseService.AddRange(sysUser);

                    var customClaims = new List <Claim>
                    {
                        new Claim(ClaimAttributes.UserId, Convert.ToString(queryUser.id)),
                        new Claim(ClaimAttributes.UserName, queryUser.username)
                    };

                    var token = _jwtHelper.IssueJwt(customClaims);
                    dict.Add("token", token);
                    return(MsgModel.Success(dict));
                }
                else
                {
                    var userDto = new SysUser();
                    userDto = queryUser.BuildAdapter().AdaptToType <SysUser>();
                    // 用户存在直接登录
                    return(Login(userDto.username, userDto.password));
                }
            }
            else
            {
                return(MsgModel.Fail("参数格式错误!"));
            }
        }
Beispiel #2
0
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public MsgModel SignUp(SysUser user)
        {
            var dict         = new Dictionary <string, object>();
            var stringRandom = CommonUtils.GetStringRandom(10);

            user.username = stringRandom;
            //user.nickname = stringRandom;
            if (user.phone != null)
            {
                var queryUser = _baseService.GetModels(a => a.phone == user.phone).SingleOrDefault();
                if (queryUser == null)
                {
                    var sysUser = new sys_user();
                    sysUser = user.BuildAdapter().AdaptToType <sys_user>();
                    _baseService.Add(sysUser);
                    var playLoad = new Dictionary <string, object>
                    {
                        { ClaimAttributes.UserId, queryUser.id },
                        { ClaimAttributes.UserName, queryUser.username }
                    };

                    var token = JwtHelper.IssueJwt(playLoad);
                    dict.Add("token", token);
                    return(MsgModel.Success(dict));
                }
                else
                {
                    var userDto = new SysUser();
                    userDto = queryUser.BuildAdapter().AdaptToType <SysUser>();
                    // 用户存在直接登录
                    return(Login(userDto.username, userDto.password));
                }
            }
            else
            {
                return(MsgModel.Fail("参数格式错误!"));
            }
        }
        public async Task <IActionResult> Add([FromBody] SysUser sysUser)
        {
            var sys_user = sysUser.BuildAdapter().AdaptToType <sys_user>();

            return(Ok(await Task.FromResult(_sysUserService.AddUser(sys_user))));
        }