Example #1
0
        public ApiResult UploadPhoto()
        {
            UserModel userModel = Auth.GetLoginUser(_httpContext);
            string    oldPath   = userModel.HeadPhoto;

            if (!string.IsNullOrEmpty(oldPath))
            {
                UploadHelper.DeleteFile(oldPath);
            }
            var       file      = Request.Form.Files[0];
            PathValue pathValue = UploadHelper.SaveFile(file.FileName);

            UploadHelper.CompressImage(pathValue.FilePath, file.OpenReadStream(), 168, 168, true);
            pathValue           = UploadHelper.Upload(pathValue.FilePath, file.FileName).GetAwaiter().GetResult();
            userModel.HeadPhoto = pathValue.FilePath;
            _userService.Update(userModel);
            IList <Claim> claims = new List <Claim>()
            {
                new Claim("account", userModel.Account),
                new Claim("username", userModel.Username),
                new Claim("sex", userModel.Sex),
                new Claim("birthDate", string.IsNullOrEmpty(userModel.BirthDate)?"":userModel.BirthDate),
                new Claim("email", string.IsNullOrEmpty(userModel.Email)?"":userModel.Email),
                new Claim("sign", string.IsNullOrEmpty(userModel.Sign)?"":userModel.Sign),
                new Claim("phone", userModel.Phone),
                new Claim("headPhoto", userModel.HeadPhoto)
            };
            string jwtToken = new JWT(_cacheClient).CreateToken(claims);

            return(ApiResult.Success(new { Path = pathValue.FilePath, token = jwtToken }));
        }