⚡ 优化数据返回
This commit is contained in:
parent
65543e651c
commit
c9d18f12cf
@ -7,7 +7,7 @@ namespace Infrastructure
|
|||||||
[Description("success")]
|
[Description("success")]
|
||||||
SUCCESS = 200,
|
SUCCESS = 200,
|
||||||
|
|
||||||
[Description("no data")]
|
[Description("没有更多数据")]
|
||||||
NO_DATA = 210,
|
NO_DATA = 210,
|
||||||
|
|
||||||
[Description("参数错误")]
|
[Description("参数错误")]
|
||||||
|
|||||||
@ -1,11 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
//using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace Infrastructure.Extensions
|
namespace Infrastructure.Extensions
|
||||||
{
|
{
|
||||||
|
|||||||
@ -57,12 +57,12 @@ namespace Infrastructure.Model
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
///// <summary>
|
||||||
/// 返回成功消息
|
///// 返回成功消息
|
||||||
/// </summary>
|
///// </summary>
|
||||||
/// <param name="data">数据对象</param>
|
///// <param name = "data" > 数据对象 </ param >
|
||||||
/// <returns>成功消息</returns>
|
///// < returns > 成功消息 </ returns >
|
||||||
public static ApiResult Success(object data) { return new ApiResult(HttpStatus.SUCCESS, "success", data); }
|
//public static ApiResult Success(object data) { return new ApiResult(HttpStatus.SUCCESS, "success", data); }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 返回成功消息
|
/// 返回成功消息
|
||||||
@ -82,7 +82,6 @@ namespace Infrastructure.Model
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 访问被拒
|
/// 访问被拒
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="apiResult"></param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public ApiResult On401()
|
public ApiResult On401()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using Infrastructure;
|
using Infrastructure;
|
||||||
|
using Infrastructure.Extensions;
|
||||||
using Infrastructure.Model;
|
using Infrastructure.Model;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using MiniExcelLibs;
|
using MiniExcelLibs;
|
||||||
@ -29,11 +30,10 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
/// json输出带时间格式的
|
/// json输出带时间格式的
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="apiResult"></param>
|
/// <param name="apiResult"></param>
|
||||||
/// <param name="timeFormatStr"></param>
|
|
||||||
/// <returns></returns>
|
/// <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");
|
return Content(jsonStr, "application/json");
|
||||||
}
|
}
|
||||||
@ -47,7 +47,7 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
|
|
||||||
protected IActionResult ToResponse(ResultCode resultCode, string msg = "")
|
protected IActionResult ToResponse(ResultCode resultCode, string msg = "")
|
||||||
{
|
{
|
||||||
return ToResponse(GetApiResult(resultCode, msg));
|
return ToResponse(new ApiResult((int)resultCode, msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -61,8 +61,9 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
//IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
|
//IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
|
||||||
//string fileDir = Path.Combine(webHostEnvironment.WebRootPath, path, fileName);
|
//string fileDir = Path.Combine(webHostEnvironment.WebRootPath, path, fileName);
|
||||||
|
|
||||||
Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
|
|
||||||
var stream = Io.File.OpenRead(path); //创建文件流
|
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));
|
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", HttpUtility.UrlEncode(fileName));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,15 +73,13 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
/// 响应返回结果
|
/// 响应返回结果
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="rows">受影响行数</param>
|
/// <param name="rows">受影响行数</param>
|
||||||
|
/// <param name="data"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected ApiResult ToJson(long rows)
|
protected ApiResult ToJson(long rows, object? data = null)
|
||||||
{
|
{
|
||||||
return rows > 0 ? GetApiResult(ResultCode.SUCCESS) : GetApiResult(ResultCode.FAIL);
|
return rows > 0 ? ApiResult.Success("success", data) : GetApiResult(ResultCode.FAIL);
|
||||||
}
|
|
||||||
protected ApiResult ToJson(long rows, object data)
|
|
||||||
{
|
|
||||||
return rows > 0 ? GetApiResult(ResultCode.SUCCESS, data) : GetApiResult(ResultCode.FAIL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 全局Code使用
|
/// 全局Code使用
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -89,17 +88,17 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected ApiResult GetApiResult(ResultCode resultCode, object? data = null)
|
protected ApiResult GetApiResult(ResultCode resultCode, object? data = null)
|
||||||
{
|
{
|
||||||
var apiResult = new ApiResult((int)resultCode, resultCode.ToString())
|
var msg = resultCode.GetDescription();
|
||||||
{
|
|
||||||
Data = data
|
|
||||||
};
|
|
||||||
|
|
||||||
return apiResult;
|
return new ApiResult((int)resultCode, msg, data);
|
||||||
}
|
|
||||||
protected ApiResult GetApiResult(ResultCode resultCode, string msg)
|
|
||||||
{
|
|
||||||
return new ApiResult((int)resultCode, msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="apiResult"></param>
|
||||||
|
/// <param name="timeFormatStr"></param>
|
||||||
|
/// <returns></returns>
|
||||||
private static string GetJsonStr(ApiResult apiResult, string timeFormatStr)
|
private static string GetJsonStr(ApiResult apiResult, string timeFormatStr)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(timeFormatStr))
|
if (string.IsNullOrEmpty(timeFormatStr))
|
||||||
@ -129,6 +128,14 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
return ExportExcelMini(list, sheetName, fileName).Item1;
|
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)
|
protected (string, string) ExportExcelMini<T>(List<T> list, string sheetName, string fileName)
|
||||||
{
|
{
|
||||||
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
|
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
|
||||||
|
|||||||
@ -3,6 +3,7 @@ using Infrastructure.Attribute;
|
|||||||
using Infrastructure.Enums;
|
using Infrastructure.Enums;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
using ZR.Admin.WebApi.Extensions;
|
||||||
using ZR.Admin.WebApi.Filters;
|
using ZR.Admin.WebApi.Filters;
|
||||||
using ZR.Common;
|
using ZR.Common;
|
||||||
using ZR.Model.System;
|
using ZR.Model.System;
|
||||||
@ -114,10 +115,10 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
{
|
{
|
||||||
if (UserConstants.NOT_UNIQUE.Equals(DeptService.CheckDeptNameUnique(dept)))
|
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;
|
dept.Create_by = HttpContext.GetName();
|
||||||
return ToResponse(ToJson(DeptService.InsertDept(dept)));
|
return ToResponse(DeptService.InsertDept(dept));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -132,14 +133,14 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
{
|
{
|
||||||
if (UserConstants.NOT_UNIQUE.Equals(DeptService.CheckDeptNameUnique(dept)))
|
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))
|
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;
|
dept.Update_by = HttpContext.GetName();
|
||||||
return ToResponse(ToJson(DeptService.UpdateDept(dept)));
|
return ToResponse(DeptService.UpdateDept(dept));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -153,11 +154,11 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
{
|
{
|
||||||
if (DeptService.Queryable().Count(it => it.ParentId == deptId && it.DelFlag == "0") > 0)
|
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)
|
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));
|
return SUCCESS(DeptService.Delete(deptId));
|
||||||
|
|||||||
@ -140,7 +140,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
long uid = HttpContext.GetUId();
|
long uid = HttpContext.GetUId();
|
||||||
var menus = sysMenuService.SelectMenuTreeByUserId(uid);
|
var menus = sysMenuService.SelectMenuTreeByUserId(uid);
|
||||||
|
|
||||||
return ToResponse(ToJson(1, sysMenuService.BuildMenus(menus)));
|
return SUCCESS(sysMenuService.BuildMenus(menus));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -148,7 +148,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet("captchaImage")]
|
[HttpGet("captchaImage")]
|
||||||
public ApiResult CaptchaImage()
|
public IActionResult CaptchaImage()
|
||||||
{
|
{
|
||||||
string uuid = Guid.NewGuid().ToString().Replace("-", "");
|
string uuid = Guid.NewGuid().ToString().Replace("-", "");
|
||||||
|
|
||||||
@ -157,7 +157,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
var info = SecurityCodeHelper.Generate(uuid, 60);
|
var info = SecurityCodeHelper.Generate(uuid, 60);
|
||||||
var obj = new { captchaOff, uuid, img = info.Base64 };// File(stream, "image/png")
|
var obj = new { captchaOff, uuid, img = info.Base64 };// File(stream, "image/png")
|
||||||
|
|
||||||
return ToJson(1, obj);
|
return SUCCESS(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -73,7 +73,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
}
|
}
|
||||||
|
|
||||||
post.Create_by = HttpContext.GetName();
|
post.Create_by = HttpContext.GetName();
|
||||||
return ToResponse(ToJson(PostService.Add(post)));
|
return ToResponse(PostService.Add(post));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -109,7 +109,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
public IActionResult Delete(string id)
|
public IActionResult Delete(string id)
|
||||||
{
|
{
|
||||||
int[] ids = Tools.SpitIntArrary(id);
|
int[] ids = Tools.SpitIntArrary(id);
|
||||||
return ToResponse(ToJson(PostService.Delete(ids)));
|
return ToResponse(PostService.Delete(ids));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -76,7 +76,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
sysRoleDto.Create_by = HttpContext.GetName();
|
sysRoleDto.Create_by = HttpContext.GetName();
|
||||||
long roleId = sysRoleService.InsertRole(sysRoleDto);
|
long roleId = sysRoleService.InsertRole(sysRoleDto);
|
||||||
|
|
||||||
return ToResponse(ToJson(roleId));
|
return ToResponse(roleId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -145,7 +145,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
long[] roleIds = Tools.SpitLongArrary(roleId);
|
long[] roleIds = Tools.SpitLongArrary(roleId);
|
||||||
int result = sysRoleService.DeleteRoleByRoleId(roleIds);
|
int result = sysRoleService.DeleteRoleByRoleId(roleIds);
|
||||||
|
|
||||||
return ToResponse(ToJson(result));
|
return ToResponse(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -161,7 +161,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
sysRoleService.CheckRoleAllowed(roleDto);
|
sysRoleService.CheckRoleAllowed(roleDto);
|
||||||
int result = sysRoleService.UpdateRoleStatus(roleDto);
|
int result = sysRoleService.UpdateRoleStatus(roleDto);
|
||||||
|
|
||||||
return ToResponse(ToJson(result));
|
return ToResponse(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -76,7 +76,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
dic.Add("roleIds", sysUser.RoleIds);
|
dic.Add("roleIds", sysUser.RoleIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ToResponse(ApiResult.Success(dic));
|
return SUCCESS(dic);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -132,7 +132,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
if (user == null) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
|
if (user == null) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
|
||||||
|
|
||||||
int result = UserService.ChangeUserStatus(user);
|
int result = UserService.ChangeUserStatus(user);
|
||||||
return ToResponse(ToJson(result));
|
return ToResponse(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -149,7 +149,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
if (userid == 1) return ToResponse(Infrastructure.ResultCode.FAIL, "不能删除管理员账号");
|
if (userid == 1) return ToResponse(Infrastructure.ResultCode.FAIL, "不能删除管理员账号");
|
||||||
int result = UserService.DeleteUser(userid);
|
int result = UserService.DeleteUser(userid);
|
||||||
|
|
||||||
return ToResponse(ToJson(result));
|
return ToResponse(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -165,7 +165,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
sysUser.Password = NETCore.Encrypt.EncryptProvider.Md5(sysUser.Password);
|
sysUser.Password = NETCore.Encrypt.EncryptProvider.Md5(sysUser.Password);
|
||||||
|
|
||||||
int result = UserService.ResetPwd(sysUser.UserId, sysUser.Password);
|
int result = UserService.ResetPwd(sysUser.UserId, sysUser.Password);
|
||||||
return ToResponse(ToJson(result));
|
return ToResponse(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -64,7 +64,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
|
|
||||||
int result = tasksLogService.Delete(jobIdArr);
|
int result = tasksLogService.Delete(jobIdArr);
|
||||||
|
|
||||||
return ToResponse(ToJson(result, result));
|
return ToResponse(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -30,9 +30,9 @@ namespace ZR.Admin.WebApi.Controllers.monitor
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet("monitor/cache")]
|
[HttpGet("monitor/cache")]
|
||||||
public ApiResult GetCache()
|
public IActionResult GetCache()
|
||||||
{
|
{
|
||||||
return ToJson(1);
|
return SUCCESS(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -65,15 +65,15 @@ namespace ZR.Admin.WebApi.Controllers.monitor
|
|||||||
[Log(Title = "清空操作日志", BusinessType = BusinessType.CLEAN)]
|
[Log(Title = "清空操作日志", BusinessType = BusinessType.CLEAN)]
|
||||||
[ActionPermissionFilter(Permission = "monitor:operlog:delete")]
|
[ActionPermissionFilter(Permission = "monitor:operlog:delete")]
|
||||||
[HttpDelete("clean")]
|
[HttpDelete("clean")]
|
||||||
public ApiResult ClearOperLog()
|
public IActionResult ClearOperLog()
|
||||||
{
|
{
|
||||||
if (!HttpContextExtension.IsAdmin(HttpContext))
|
if (!HttpContextExtension.IsAdmin(HttpContext))
|
||||||
{
|
{
|
||||||
return ApiResult.Error("操作失败");
|
return ToResponse(Infrastructure.ResultCode.CUSTOM_ERROR,"操作失败");
|
||||||
}
|
}
|
||||||
sysOperLogService.CleanOperLog();
|
sysOperLogService.CleanOperLog();
|
||||||
|
|
||||||
return ToJson(1);
|
return SUCCESS(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user