优化数据返回

This commit is contained in:
不做码农 2023-06-02 18:33:07 +08:00
parent 65543e651c
commit c9d18f12cf
12 changed files with 66 additions and 63 deletions

View File

@ -7,7 +7,7 @@ namespace Infrastructure
[Description("success")]
SUCCESS = 200,
[Description("no data")]
[Description("没有更多数据")]
NO_DATA = 210,
[Description("参数错误")]

View File

@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
//using Newtonsoft.Json;
namespace Infrastructure.Extensions
{

View File

@ -57,12 +57,12 @@ namespace Infrastructure.Model
return this;
}
/// <summary>
/// 返回成功消息
/// </summary>
/// <param name="data">数据对象</param>
/// <returns>成功消息</returns>
public static ApiResult Success(object data) { return new ApiResult(HttpStatus.SUCCESS, "success", data); }
///// <summary>
///// 返回成功消息
///// </summary>
///// <param name = "data" > 数据对象 </ param >
///// < returns > 成功消息 </ returns >
//public static ApiResult Success(object data) { return new ApiResult(HttpStatus.SUCCESS, "success", data); }
/// <summary>
/// 返回成功消息
@ -82,7 +82,6 @@ namespace Infrastructure.Model
/// <summary>
/// 访问被拒
/// </summary>
/// <param name="apiResult"></param>
/// <returns></returns>
public ApiResult On401()
{

View File

@ -1,4 +1,5 @@
using Infrastructure;
using Infrastructure.Extensions;
using Infrastructure.Model;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
@ -12,7 +13,7 @@ namespace ZR.Admin.WebApi.Controllers
public class BaseController : ControllerBase
{
public static string TIME_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
/// <summary>
/// 返回成功封装
/// </summary>
@ -29,11 +30,10 @@ namespace ZR.Admin.WebApi.Controllers
/// json输出带时间格式的
/// </summary>
/// <param name="apiResult"></param>
/// <param name="timeFormatStr"></param>
/// <returns></returns>
protected IActionResult ToResponse(ApiResult apiResult, string timeFormatStr = "yyyy-MM-dd HH:mm:ss")
protected IActionResult ToResponse(ApiResult apiResult)
{
string jsonStr = GetJsonStr(apiResult, timeFormatStr);
string jsonStr = GetJsonStr(apiResult, TIME_FORMAT_FULL);
return Content(jsonStr, "application/json");
}
@ -47,7 +47,7 @@ namespace ZR.Admin.WebApi.Controllers
protected IActionResult ToResponse(ResultCode resultCode, string msg = "")
{
return ToResponse(GetApiResult(resultCode, msg));
return ToResponse(new ApiResult((int)resultCode, msg));
}
/// <summary>
@ -61,26 +61,25 @@ namespace ZR.Admin.WebApi.Controllers
//IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
//string fileDir = Path.Combine(webHostEnvironment.WebRootPath, path, fileName);
Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
var stream = Io.File.OpenRead(path); //创建文件流
Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", HttpUtility.UrlEncode(fileName));
}
#region
/// <summary>
/// 响应返回结果
/// </summary>
/// <param name="rows">受影响行数</param>
/// <param name="data"></param>
/// <returns></returns>
protected ApiResult ToJson(long rows)
protected ApiResult ToJson(long rows, object? data = null)
{
return rows > 0 ? GetApiResult(ResultCode.SUCCESS) : GetApiResult(ResultCode.FAIL);
}
protected ApiResult ToJson(long rows, object data)
{
return rows > 0 ? GetApiResult(ResultCode.SUCCESS, data) : GetApiResult(ResultCode.FAIL);
return rows > 0 ? ApiResult.Success("success", data) : GetApiResult(ResultCode.FAIL);
}
/// <summary>
/// 全局Code使用
/// </summary>
@ -89,17 +88,17 @@ namespace ZR.Admin.WebApi.Controllers
/// <returns></returns>
protected ApiResult GetApiResult(ResultCode resultCode, object? data = null)
{
var apiResult = new ApiResult((int)resultCode, resultCode.ToString())
{
Data = data
};
var msg = resultCode.GetDescription();
return apiResult;
}
protected ApiResult GetApiResult(ResultCode resultCode, string msg)
{
return new ApiResult((int)resultCode, msg);
return new ApiResult((int)resultCode, msg, data);
}
/// <summary>
///
/// </summary>
/// <param name="apiResult"></param>
/// <param name="timeFormatStr"></param>
/// <returns></returns>
private static string GetJsonStr(ApiResult apiResult, string timeFormatStr)
{
if (string.IsNullOrEmpty(timeFormatStr))
@ -129,12 +128,20 @@ namespace ZR.Admin.WebApi.Controllers
return ExportExcelMini(list, sheetName, fileName).Item1;
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="sheetName"></param>
/// <param name="fileName"></param>
/// <returns></returns>
protected (string, string) ExportExcelMini<T>(List<T> list, string sheetName, string fileName)
{
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
string sFileName = $"{fileName}{DateTime.Now:MM-dd-HHmmss}.xlsx";
string fullPath = Path.Combine(webHostEnvironment.WebRootPath, "export", sFileName);
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
MiniExcel.SaveAs(fullPath, list, sheetName: sheetName);
@ -172,7 +179,7 @@ namespace ZR.Admin.WebApi.Controllers
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
string sFileName = $"{fileName}模板.xlsx";
string newFileName = Path.Combine(webHostEnvironment.WebRootPath, "ImportTemplate", sFileName);
if (!Directory.Exists(newFileName))
{
Directory.CreateDirectory(Path.GetDirectoryName(newFileName));

View File

@ -3,6 +3,7 @@ using Infrastructure.Attribute;
using Infrastructure.Enums;
using Microsoft.AspNetCore.Mvc;
using System.Collections;
using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Filters;
using ZR.Common;
using ZR.Model.System;
@ -114,10 +115,10 @@ namespace ZR.Admin.WebApi.Controllers.System
{
if (UserConstants.NOT_UNIQUE.Equals(DeptService.CheckDeptNameUnique(dept)))
{
return ToResponse(GetApiResult(ResultCode.CUSTOM_ERROR, $"新增部门{dept.DeptName}失败,部门名称已存在"));
return ToResponse(ResultCode.CUSTOM_ERROR, $"新增部门{dept.DeptName}失败,部门名称已存在");
}
dept.Create_by = User.Identity.Name;
return ToResponse(ToJson(DeptService.InsertDept(dept)));
dept.Create_by = HttpContext.GetName();
return ToResponse(DeptService.InsertDept(dept));
}
/// <summary>
@ -132,14 +133,14 @@ namespace ZR.Admin.WebApi.Controllers.System
{
if (UserConstants.NOT_UNIQUE.Equals(DeptService.CheckDeptNameUnique(dept)))
{
return ToResponse(GetApiResult(ResultCode.CUSTOM_ERROR, $"修改部门{dept.DeptName}失败,部门名称已存在"));
return ToResponse(ResultCode.CUSTOM_ERROR, $"修改部门{dept.DeptName}失败,部门名称已存在");
}
else if (dept.ParentId.Equals(dept.DeptId))
{
return ToResponse(GetApiResult(ResultCode.CUSTOM_ERROR, $"修改部门{dept.DeptName}失败,上级部门不能是自己"));
return ToResponse(ResultCode.CUSTOM_ERROR, $"修改部门{dept.DeptName}失败,上级部门不能是自己");
}
dept.Update_by = User.Identity.Name;
return ToResponse(ToJson(DeptService.UpdateDept(dept)));
dept.Update_by = HttpContext.GetName();
return ToResponse(DeptService.UpdateDept(dept));
}
/// <summary>
@ -153,11 +154,11 @@ namespace ZR.Admin.WebApi.Controllers.System
{
if (DeptService.Queryable().Count(it => it.ParentId == deptId && it.DelFlag == "0") > 0)
{
return ToResponse(GetApiResult(ResultCode.CUSTOM_ERROR, $"存在下级部门,不允许删除"));
return ToResponse(ResultCode.CUSTOM_ERROR, $"存在下级部门,不允许删除");
}
if (UserService.Queryable().Count(it => it.DeptId == deptId && it.DelFlag == 0) > 0)
{
return ToResponse(GetApiResult(ResultCode.CUSTOM_ERROR, $"部门存在用户,不允许删除"));
return ToResponse(ResultCode.CUSTOM_ERROR, $"部门存在用户,不允许删除");
}
return SUCCESS(DeptService.Delete(deptId));

View File

@ -140,7 +140,7 @@ namespace ZR.Admin.WebApi.Controllers.System
long uid = HttpContext.GetUId();
var menus = sysMenuService.SelectMenuTreeByUserId(uid);
return ToResponse(ToJson(1, sysMenuService.BuildMenus(menus)));
return SUCCESS(sysMenuService.BuildMenus(menus));
}
/// <summary>
@ -148,7 +148,7 @@ namespace ZR.Admin.WebApi.Controllers.System
/// </summary>
/// <returns></returns>
[HttpGet("captchaImage")]
public ApiResult CaptchaImage()
public IActionResult CaptchaImage()
{
string uuid = Guid.NewGuid().ToString().Replace("-", "");
@ -157,7 +157,7 @@ namespace ZR.Admin.WebApi.Controllers.System
var info = SecurityCodeHelper.Generate(uuid, 60);
var obj = new { captchaOff, uuid, img = info.Base64 };// File(stream, "image/png")
return ToJson(1, obj);
return SUCCESS(obj);
}
/// <summary>

View File

@ -73,7 +73,7 @@ namespace ZR.Admin.WebApi.Controllers.System
}
post.Create_by = HttpContext.GetName();
return ToResponse(ToJson(PostService.Add(post)));
return ToResponse(PostService.Add(post));
}
/// <summary>
@ -109,7 +109,7 @@ namespace ZR.Admin.WebApi.Controllers.System
public IActionResult Delete(string id)
{
int[] ids = Tools.SpitIntArrary(id);
return ToResponse(ToJson(PostService.Delete(ids)));
return ToResponse(PostService.Delete(ids));
}
/// <summary>

View File

@ -76,7 +76,7 @@ namespace ZR.Admin.WebApi.Controllers.System
sysRoleDto.Create_by = HttpContext.GetName();
long roleId = sysRoleService.InsertRole(sysRoleDto);
return ToResponse(ToJson(roleId));
return ToResponse(roleId);
}
/// <summary>
@ -145,7 +145,7 @@ namespace ZR.Admin.WebApi.Controllers.System
long[] roleIds = Tools.SpitLongArrary(roleId);
int result = sysRoleService.DeleteRoleByRoleId(roleIds);
return ToResponse(ToJson(result));
return ToResponse(result);
}
/// <summary>
@ -161,7 +161,7 @@ namespace ZR.Admin.WebApi.Controllers.System
sysRoleService.CheckRoleAllowed(roleDto);
int result = sysRoleService.UpdateRoleStatus(roleDto);
return ToResponse(ToJson(result));
return ToResponse(result);
}
/// <summary>

View File

@ -76,7 +76,7 @@ namespace ZR.Admin.WebApi.Controllers.System
dic.Add("roleIds", sysUser.RoleIds);
}
return ToResponse(ApiResult.Success(dic));
return SUCCESS(dic);
}
/// <summary>
@ -132,7 +132,7 @@ namespace ZR.Admin.WebApi.Controllers.System
if (user == null) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
int result = UserService.ChangeUserStatus(user);
return ToResponse(ToJson(result));
return ToResponse(result);
}
/// <summary>
@ -149,7 +149,7 @@ namespace ZR.Admin.WebApi.Controllers.System
if (userid == 1) return ToResponse(Infrastructure.ResultCode.FAIL, "不能删除管理员账号");
int result = UserService.DeleteUser(userid);
return ToResponse(ToJson(result));
return ToResponse(result);
}
/// <summary>
@ -165,7 +165,7 @@ namespace ZR.Admin.WebApi.Controllers.System
sysUser.Password = NETCore.Encrypt.EncryptProvider.Md5(sysUser.Password);
int result = UserService.ResetPwd(sysUser.UserId, sysUser.Password);
return ToResponse(ToJson(result));
return ToResponse(result);
}
/// <summary>

View File

@ -64,7 +64,7 @@ namespace ZR.Admin.WebApi.Controllers.System
int result = tasksLogService.Delete(jobIdArr);
return ToResponse(ToJson(result, result));
return ToResponse(result);
}
/// <summary>

View File

@ -30,9 +30,9 @@ namespace ZR.Admin.WebApi.Controllers.monitor
/// </summary>
/// <returns></returns>
[HttpGet("monitor/cache")]
public ApiResult GetCache()
public IActionResult GetCache()
{
return ToJson(1);
return SUCCESS(1);
}
/// <summary>

View File

@ -65,15 +65,15 @@ namespace ZR.Admin.WebApi.Controllers.monitor
[Log(Title = "清空操作日志", BusinessType = BusinessType.CLEAN)]
[ActionPermissionFilter(Permission = "monitor:operlog:delete")]
[HttpDelete("clean")]
public ApiResult ClearOperLog()
public IActionResult ClearOperLog()
{
if (!HttpContextExtension.IsAdmin(HttpContext))
{
return ApiResult.Error("操作失败");
return ToResponse(Infrastructure.ResultCode.CUSTOM_ERROR,"操作失败");
}
sysOperLogService.CleanOperLog();
return ToJson(1);
return SUCCESS(1);
}
/// <summary>