优化数据返回

This commit is contained in:
不做码农 2022-01-11 10:49:38 +08:00
parent 617b34c884
commit b1bd8f12bb
5 changed files with 21 additions and 18 deletions

View File

@ -18,18 +18,18 @@ namespace ZR.Admin.WebApi.Controllers
public static string TIME_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss"; public static string TIME_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
public static string TIME_FORMAT_FULL_2 = "MM-dd HH:mm:ss"; public static string TIME_FORMAT_FULL_2 = "MM-dd HH:mm:ss";
/// <summary>
/// 返回成功封装
/// </summary>
/// <param name="data"></param>
/// <param name="timeFormatStr"></param>
/// <returns></returns>
protected IActionResult SUCCESS(object data, string timeFormatStr = "yyyy-MM-dd HH:mm:ss") protected IActionResult SUCCESS(object data, string timeFormatStr = "yyyy-MM-dd HH:mm:ss")
{ {
string jsonStr = GetJsonStr(GetApiResult(data != null ? ResultCode.SUCCESS : ResultCode.FAIL, data), timeFormatStr); string jsonStr = GetJsonStr(GetApiResult(data != null ? ResultCode.SUCCESS : ResultCode.FAIL, data), timeFormatStr);
return Content(jsonStr, "application/json"); return Content(jsonStr, "application/json");
} }
protected IActionResult ToResponse(ResultCode resultCode, object data = null)
{
string jsonStr = GetJsonStr(GetApiResult(resultCode, data), "");
return Content(jsonStr, "application/json");
}
/// <summary> /// <summary>
/// json输出带时间格式的 /// json输出带时间格式的
/// </summary> /// </summary>
@ -42,6 +42,7 @@ namespace ZR.Admin.WebApi.Controllers
return Content(jsonStr, "application/json"); return Content(jsonStr, "application/json");
} }
protected IActionResult ToResponse(long rows, string timeFormatStr = "yyyy-MM-dd HH:mm:ss") protected IActionResult ToResponse(long rows, string timeFormatStr = "yyyy-MM-dd HH:mm:ss")
{ {
string jsonStr = GetJsonStr(ToJson(rows), timeFormatStr); string jsonStr = GetJsonStr(ToJson(rows), timeFormatStr);
@ -49,6 +50,13 @@ namespace ZR.Admin.WebApi.Controllers
return Content(jsonStr, "application/json"); return Content(jsonStr, "application/json");
} }
protected IActionResult ToResponse(ResultCode resultCode, string msg = "")
{
return ToResponse(GetApiResult(resultCode, msg));
}
#region
/// <summary> /// <summary>
/// 响应返回结果 /// 响应返回结果
/// </summary> /// </summary>
@ -96,11 +104,7 @@ namespace ZR.Admin.WebApi.Controllers
return JsonConvert.SerializeObject(apiResult, Formatting.Indented, serializerSettings); return JsonConvert.SerializeObject(apiResult, Formatting.Indented, serializerSettings);
} }
#endregion
protected IActionResult CustomError(ResultCode resultCode, string msg = "")
{
return ToResponse(GetApiResult(resultCode, msg));
}
/// <summary> /// <summary>
/// 导出Excel /// 导出Excel

View File

@ -140,7 +140,7 @@ namespace ZR.Admin.WebApi.Controllers
FileType = formFile.ContentType FileType = formFile.ContentType
}; };
long fileId = SysFileService.InsertFile(file); long fileId = SysFileService.InsertFile(file);
return ToResponse(ResultCode.SUCCESS, new return SUCCESS(new
{ {
url = uploadType == 1 ? finalFilePath : accessPath, url = uploadType == 1 ? finalFilePath : accessPath,
fileName, fileName,
@ -188,7 +188,7 @@ namespace ZR.Admin.WebApi.Controllers
Create_time = DateTime.Now, Create_time = DateTime.Now,
FileType = formFile.ContentType FileType = formFile.ContentType
}); });
return ToResponse(ResultCode.SUCCESS, new return SUCCESS(new
{ {
url = result.Item2, url = result.Item2,
fileName = result.Item3, fileName = result.Item3,

View File

@ -185,7 +185,7 @@ namespace ZR.Admin.WebApi.Controllers
} }
} }
return ToResponse(ResultCode.FAIL); return ToResponse(ResultCode.FAIL, "生成失败");
} }
/// <summary> /// <summary>

View File

@ -74,7 +74,7 @@ namespace ZR.Admin.WebApi.Controllers.System
SysConfig sysConfig = sysConfigService.GetSysConfigByKey("sys.account.captchaOnOff"); SysConfig sysConfig = sysConfigService.GetSysConfigByKey("sys.account.captchaOnOff");
if (sysConfig?.ConfigValue != "off" && CacheHelper.Get(loginBody.Uuid) is string str && !str.ToLower().Equals(loginBody.Code.ToLower())) if (sysConfig?.ConfigValue != "off" && CacheHelper.Get(loginBody.Uuid) is string str && !str.ToLower().Equals(loginBody.Code.ToLower()))
{ {
return CustomError(ResultCode.CAPTCHA_ERROR, "验证码错误"); return ToResponse(ResultCode.CAPTCHA_ERROR, "验证码错误");
} }
var user = sysLoginService.Login(loginBody, AsyncFactory.RecordLogInfo(httpContextAccessor.HttpContext, "0", "login")); var user = sysLoginService.Login(loginBody, AsyncFactory.RecordLogInfo(httpContextAccessor.HttpContext, "0", "login"));

View File

@ -7,7 +7,6 @@ using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Filters; using ZR.Admin.WebApi.Filters;
using ZR.Model.System.Dto; using ZR.Model.System.Dto;
using ZR.Model.System; using ZR.Model.System;
using ZR.Service;
using ZR.Service.System.IService; using ZR.Service.System.IService;
namespace ZR.Admin.WebApi.Controllers.System namespace ZR.Admin.WebApi.Controllers.System
@ -151,11 +150,11 @@ namespace ZR.Admin.WebApi.Controllers.System
{ {
if (sysMenuService.HasChildByMenuId(menuId)) if (sysMenuService.HasChildByMenuId(menuId))
{ {
return CustomError(ResultCode.CUSTOM_ERROR, "存在子菜单,不允许删除"); return ToResponse(ResultCode.CUSTOM_ERROR, "存在子菜单,不允许删除");
} }
if (sysMenuService.CheckMenuExistRole(menuId)) if (sysMenuService.CheckMenuExistRole(menuId))
{ {
return CustomError(ResultCode.CUSTOM_ERROR, "菜单已分配,不允许删除"); return ToResponse(ResultCode.CUSTOM_ERROR, "菜单已分配,不允许删除");
} }
int result = sysMenuService.DeleteMenuById(menuId); int result = sysMenuService.DeleteMenuById(menuId);