public static async Task <ApiResult> RunAsync(this ApiResult apiResult, Func <Task> invokeAsync, Func <Task> catchAsync = null) { try { await invokeAsync(); return(apiResult); } catch (Exception e) { try { if (catchAsync != null) { await catchAsync(); } } finally { } if (e is ApplicationServiceException || e is DomainException || e is InfrastructureException) { return(ApiResult.Err(e.Message)); } return(ApiResult.Err()); } }
public async Task <ApiResult> UploadByBase64(UploadImageDto input) { if (!string.IsNullOrEmpty(input.Base64Body)) { try { var dir = $"{DateTime.Now:yyyyMM}"; var imageurl = $"{dir}/{Guid.NewGuid()}.jpg"; byte[] imageBytes = Convert.FromBase64String(input.Base64Body.Replace("data:image/jpeg;base64,", "")); Directory.CreateDirectory($"wwwroot/{dir}"); using var ms = new FileStream($"wwwroot/{imageurl}", FileMode.Create); await ms.WriteAsync(imageBytes, 0, imageBytes.Length); return(ApiResult.Ok(imageurl, "")); } catch (Exception) { return(ApiResult.Err()); } } else { throw new ApplicationServiceException("无法识别的图片"); } }
public ApiResult GetUserInfo() { //var principal = User; var identity = User.Identity as ApplicationUser; if (identity == null) { return(ApiResult.Err("用户类型错误", 500)); } var data = new { identity.Avatar, identity.DisplayName, identity.ID, identity.Name, identity.RoleID, identity.RoleIDs, identity.Sex, Roles = identity.Roles.Select(s => new { s.ID, s.Name, s.Permission }) }; return(ApiResult.Ok(data)); }
public static async Task <ApiResult> Async <T>(this ApiResult <T> apiResult) { try { apiResult.Data = await apiResult.TaskData; } catch (Exception) { return(ApiResult.Err()); } return(apiResult); }
public async Task <ApiResult> Register(RequestRegister model) { var result = await _userService.CreateAsync(model.UserName, model.Password); if (result.Succeeded) { return(ApiResult.Ok()); } return(ApiResult.Err("注册失败")); }
public static async Task <object> ExceptionHandler(Exception exception) { //异常处理 if (exception is ApplicationServiceException || exception is DomainException || exception is InfrastructureException) { return(await ApiResult.Err(exception.Message).Async()); } else { Console.WriteLine("系统异常:" + exception.Message); return(await ApiResult.Err().Async()); } }
public static async Task <object> ExceptionHandler(Exception exception) { //异常处理 if (exception is ApplicationServiceException || exception is DomainException || exception is InfrastructureException) { return(await ApiResult.Err(exception.Message).Async()); } else { Console.WriteLine($"系统异常:{exception.GetBaseException().Message},调用堆栈:{exception.StackTrace}"); return(await ApiResult.Err().Async()); } }
public async Task <ApiResult> Register(RequestRegister model) { var user = new ApplicationUser { Name = model.UserName }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { return(ApiResult.Ok()); } return(ApiResult.Err("注册失败")); }
public async Task <ApiResult> UpdatePwd(RequestUpdateUserInfo requestUpdateUserInfo) { var user = await _userManager.FindByIdAsync(requestUpdateUserInfo.UserId); user.Password = requestUpdateUserInfo.Pwd; var appUser = AppUser; if (!IsSupperAdmin || appUser.ID != user.ID) { return(ApiResult.Err("无权限修改")); } await _userManager.UpdateAsync(user); return(ApiResult.Ok()); }
/// <summary> /// 返回默认状态为402的结果 /// </summary> /// <param name="msg"></param> /// <param name="status"></param> /// <returns></returns> protected ApiResult Error(String msg = null, Int32 status = 402) { return(ApiResult.Err(msg, status)); }