Exemple #1
0
        public async Task <ApiResult> EditUserInfoAsync([FromBody] EditUserInfoRequest request)
        {
            var user = GetUser();

            if (user == null)
            {
                return(AuthorizeError());
            }

            return(await _userService.EditUserInfoAsync(request, user.UserId));
        }
Exemple #2
0
        /// <summary>
        /// 编辑用户信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task <ApiResult> EditUserInfoAsync(EditUserInfoRequest request, long userId)
        {
            var response = new ApiResult();

            try
            {
                var user = await _userRepository.Table
                           .FirstOrDefaultAsync(item => item.Id == userId && item.Status == 1);

                if (user == null)
                {
                    response.Code    = Code.Error;
                    response.Message = "找不到用户";
                    return(response);
                }
                //设置用户名
                if (!string.IsNullOrEmpty(request.UserName))
                {
                    var isExist = await _userRepository.TableNotTracking
                                  .AnyAsync(item => item.UserName == request.UserName);

                    if (!isExist)
                    {
                        user.UserName   = request.UserName;
                        user.UpdateTime = DateTime.Now;
                    }
                }
                //设置昵称
                if (!string.IsNullOrEmpty(request.NickName))
                {
                    user.NickName   = request.NickName;
                    user.UpdateTime = DateTime.Now;
                }
                //设置邮箱
                if (!string.IsNullOrEmpty(request.Email))
                {
                    user.Email        = request.Email;
                    user.EmailConfirm = 0;
                    user.UpdateTime   = DateTime.Now;
                }
                //设置手机
                if (!string.IsNullOrEmpty(request.Phone))
                {
                    user.Phone        = request.Phone;
                    user.PhoneConfirm = 0;
                    user.UpdateTime   = DateTime.Now;
                }
                //头像
                if (!string.IsNullOrEmpty(request.HeadUrl))
                {
                    user.HeadUrl    = request.HeadUrl;
                    user.UpdateTime = DateTime.Now;
                }
                //备注
                if (!string.IsNullOrEmpty(request.Remark))
                {
                    user.Remark     = request.Remark;
                    user.UpdateTime = DateTime.Now;
                }
                await _work.SaveChangesAsync();

                response.Code    = Code.Ok;
                response.Message = "编辑成功";
                return(response);
            }
            catch (Exception ex)
            {
                _logger.LogError($"编辑用户信息异常;method={nameof(EditUserInfoAsync)};param={request.ToJson()};exception messges={ex.Message}");
                response.Code    = Code.Error;
                response.Message = $"编辑用户信息异常:{ex.Message}";
                return(response);
            }
        }