File size: 10,865 Bytes
5fc700d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using ToolHub.Models;
using ToolHub.Services;
namespace ToolHub.Controllers;
[Authorize(Roles = "Admin")]
public class ToolController : Controller
{
private readonly IToolService _toolService;
private readonly IFreeSql _freeSql;
public ToolController(IToolService toolService, IFreeSql freeSql)
{
_toolService = toolService;
_freeSql = freeSql;
}
// 工具管理页面
public async Task<IActionResult> Index(int page = 1, int categoryId = 0)
{
ViewBag.Categories = await _toolService.GetCategoriesAsync();
ViewBag.CurrentCategory = categoryId;
ViewBag.CurrentPage = page;
var pageSize = 20;
var tools = await _toolService.GetToolsByCategoryAsync(categoryId, page, pageSize,true);
// 获取总数用于分页
var totalCount = await GetToolsCountAsync(categoryId);
var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
ViewBag.TotalCount = totalCount;
ViewBag.TotalPages = totalPages;
ViewBag.PageSize = pageSize;
return View(tools);
}
// 获取工具总数
private async Task<int> GetToolsCountAsync(int categoryId = 0)
{
var query = _freeSql.Select<Tool>().Where(t => t.IsActive);
if (categoryId > 0)
{
query = query.Where(t => t.CategoryId == categoryId);
}
return (int)await query.CountAsync();
}
// 获取分页工具列表(AJAX)
[HttpGet]
public async Task<IActionResult> GetTools(int page = 1, int categoryId = 0, string search = "")
{
try
{
var pageSize = 20;
List<Tool> tools;
int totalCount;
if (!string.IsNullOrEmpty(search))
{
tools = await _toolService.SearchToolsAsync(search, page, pageSize);
totalCount = (int)await _freeSql.Select<Tool>()
.Where(t => t.IsActive && (t.Name.Contains(search) || t.Description!.Contains(search)))
.CountAsync();
}
else
{
tools = await _toolService.GetToolsByCategoryAsync(categoryId, page, pageSize);
totalCount = await GetToolsCountAsync(categoryId);
}
var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
return Json(new
{
tools = tools,
pagination = new
{
currentPage = page,
totalPages = totalPages,
totalCount = totalCount,
pageSize = pageSize,
hasNext = page < totalPages,
hasPrev = page > 1
}
});
}
catch
{
return Json(new { success = false, message = "获取工具列表失败" });
}
}
// 获取所有工具列表(用于统计页面)
[HttpGet]
public async Task<IActionResult> GetAllTools()
{
try
{
var tools = await _freeSql.Select<Tool>()
.Where(t => t.IsActive)
.OrderBy(t => t.Name)
.ToListAsync();
return Json(new { success = true, tools = tools });
}
catch
{
return Json(new { success = false, message = "获取工具列表失败" });
}
}
// 获取单个工具信息
[HttpGet]
public async Task<IActionResult> Get(int id)
{
try
{
var tool = await _freeSql.Select<Tool>()
.Where(t => t.Id == id)
.FirstAsync();
if (tool == null)
return NotFound();
return Json(tool);
}
catch
{
return StatusCode(500);
}
}
// 保存工具(添加/编辑)
[HttpPost]
public async Task<IActionResult> Save([FromBody] ToolDto toolDto)
{
try
{
if (toolDto.Id == 0)
{
// 添加新工具
var tool = new Tool
{
Name = toolDto.Name,
Description = toolDto.Description,
Icon = toolDto.Icon,
Image = toolDto.Image,
Url = toolDto.Url,
CategoryId = toolDto.CategoryId,
IsHot = toolDto.IsHot,
IsNew = toolDto.IsNew,
IsRecommended = toolDto.IsRecommended,
SortOrder = toolDto.SortOrder,
IsActive = true
};
await _freeSql.Insert(tool).ExecuteAffrowsAsync();
}
else
{
// 更新工具
await _freeSql.Update<Tool>()
.Where(t => t.Id == toolDto.Id)
.Set(t => t.Name, toolDto.Name)
.Set(t => t.Description, toolDto.Description)
.Set(t => t.Icon, toolDto.Icon)
.Set(t => t.Image, toolDto.Image)
.Set(t => t.Url, toolDto.Url)
.Set(t => t.CategoryId, toolDto.CategoryId)
.Set(t => t.IsHot, toolDto.IsHot)
.Set(t => t.IsNew, toolDto.IsNew)
.Set(t => t.IsRecommended, toolDto.IsRecommended)
.Set(t => t.SortOrder, toolDto.SortOrder)
.Set(t => t.UpdatedAt, DateTime.Now)
.ExecuteAffrowsAsync();
}
return Json(new { success = true });
}
catch
{
return Json(new { success = false });
}
}
// 删除工具
[HttpPost]
public async Task<IActionResult> Delete([FromBody] DeleteToolRequest request)
{
try
{
var result = await _toolService.DeleteToolAsync(request.Id);
return Json(new { success = result });
}
catch
{
return Json(new { success = false });
}
}
// 切换工具状态
[HttpPost]
public async Task<IActionResult> ToggleStatus([FromBody] ToggleToolStatusRequest request)
{
try
{
await _freeSql.Update<Tool>()
.Where(t => t.Id == request.Id)
.Set(t => t.IsActive, request.IsActive)
.Set(t => t.UpdatedAt, DateTime.Now)
.ExecuteAffrowsAsync();
return Json(new { success = true });
}
catch
{
return Json(new { success = false });
}
}
// 更新工具标志
[HttpPost]
public async Task<IActionResult> UpdateFlags([FromBody] UpdateToolFlagsRequest request)
{
try
{
await _freeSql.Update<Tool>()
.Where(t => t.Id == request.Id)
.Set(t => t.IsHot, request.IsHot)
.Set(t => t.IsNew, request.IsNew)
.Set(t => t.IsRecommended, request.IsRecommended)
.Set(t => t.UpdatedAt, DateTime.Now)
.ExecuteAffrowsAsync();
return Json(new { success = true });
}
catch
{
return Json(new { success = false });
}
}
// 初始化图片压缩工具
[HttpGet]
public async Task<IActionResult> InitImageCompressor()
{
try
{
// 检查是否已存在图片压缩工具
var existingTool = await _freeSql.Select<Tool>()
.Where(t => t.Name == "图片压缩工具" || t.Url!.Contains("/Tools/ImageCompressor"))
.FirstAsync();
if (existingTool != null)
{
return Json(new { success = false, message = "图片压缩工具已存在" });
}
// 查找或创建图像处理分类
var category = await _freeSql.Select<Category>()
.Where(c => c.Name == "图像处理" || c.Name == "图片工具")
.FirstAsync();
if (category == null)
{
// 创建图像处理分类
category = new Category
{
Name = "图像处理",
Description = "图片处理、编辑、转换等相关工具",
Icon = "fas fa-image",
Color = "#FF6B35",
SortOrder = 3,
IsActive = true,
CreatedAt = DateTime.Now
};
var categoryId = await _freeSql.Insert(category).ExecuteIdentityAsync();
category.Id = (int)categoryId;
}
// 添加图片压缩工具
var tool = new Tool
{
Name = "图片压缩工具",
Description = "快速压缩图片文件,支持JPG、PNG、WebP等格式,保持高质量的同时大幅减小文件体积",
Icon = "fas fa-compress-alt",
Image = "",
Url = "/Tools/ImageCompressor",
CategoryId = category.Id,
IsHot = true,
IsNew = true,
IsRecommended = true,
IsActive = true,
SortOrder = 1,
ViewCount = 0,
CreatedAt = DateTime.Now
};
await _freeSql.Insert(tool).ExecuteAffrowsAsync();
return Json(new { success = true, message = "图片压缩工具初始化成功" });
}
catch (Exception ex)
{
return Json(new { success = false, message = ex.Message });
}
}
}
// DTOs
public class ToolDto
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public string? Icon { get; set; }
public string? Image { get; set; }
public string? Url { get; set; }
public int CategoryId { get; set; }
public bool IsHot { get; set; }
public bool IsNew { get; set; }
public bool IsRecommended { get; set; }
public int SortOrder { get; set; }
}
public class DeleteToolRequest
{
public int Id { get; set; }
}
public class ToggleToolStatusRequest
{
public int Id { get; set; }
public bool IsActive { get; set; }
}
public class UpdateToolFlagsRequest
{
public int Id { get; set; }
public bool IsHot { get; set; }
public bool IsNew { get; set; }
public bool IsRecommended { get; set; }
}
|