Files
MyHomepage/backend/Services/SearchEngineService.cs
T
g82tt 68be41e7a2 初始提交:浏览器首页 MyHomePage 全栈项目
# 项目概述
个人浏览器首页导航应用,支持书签分类管理、搜索引擎快捷搜索、
必应每日壁纸轮播、前后端分离部署,适配 1Panel 服务器(Docker 模式)。

# 技术栈
- 前端:Vue 3 + TypeScript + Vite + Pinia + Capacitor(Android 打包)
- 后端:.NET 8 + SqlSugar(多数据库) + SQLite/MySQL + Swashbuckle
- 部署:1Panel 应用商店自定义应用(Docker Compose 模式)

# 项目结构
- backend/    .NET 8 API 后端(8 个 Controller + 15 个 Service)
- frontend/   Vue 3 前端(19 个组件 + 9 个 API 模块 + 5 个 Store)
- docker/     Docker 部署文件(后端镜像 + Nginx 反代)
- docs/       部署手册(1Panel 实战版)
- scripts/    E2E 测试脚本

# 已实现功能
- 书签管理:增删改查 + 树形分类 + 拖拽排序 + 主色自适应
- 搜索引擎:8 个内置引擎 + 自定义引擎 + favicon 自动抓取
- 必应壁纸:每日轮播 + 多分辨率自动选择 + 1.6MP 质量优先
- 全局设置:主题/行为/数据/工具 4 分类 + 跨设备同步
- 文件上传:图标/书签/通用(容器持久化 + 跨域 URL 拼接)
- 同步:基于变更日志的设备间数据同步
- 跨域部署:前后端分离 + runtime config.json 无需重新编译

# 进度记录
- 已完成 P0~P52 共 53 个开发节点(详细见 说明文档.md)
- 当前版本:v1.0 部署就绪

# 部署文档
- README.md:项目说明 + 快速开始
- 说明文档.md:完整开发进度(中文)
- docs/DEPLOY.md:1Panel 部署手册(Docker 模式)
2026-07-05 05:09:56 +08:00

122 lines
4.3 KiB
C#

using MyHomePage.Api.Common;
using MyHomePage.Api.Models.Dtos;
using MyHomePage.Api.Models.Entities;
using SqlSugar;
namespace MyHomePage.Api.Services;
/// <inheritdoc />
public class SearchEngineService : ISearchEngineService
{
private readonly ISqlSugarClient _db;
private readonly SyncLogHelper _sync;
public SearchEngineService(ISqlSugarClient db, SyncLogHelper sync)
{
_db = db;
_sync = sync;
}
/// <inheritdoc />
public async Task<List<SearchEngineDto>> ListAsync()
{
var list = await _db.Queryable<SearchEngine>()
.OrderBy(e => e.Sort)
.OrderBy(e => e.Id)
.ToListAsync();
return list.Select(SearchEngineDto.FromEntity).ToList();
}
/// <inheritdoc />
public async Task<SearchEngineDto?> GetByIdAsync(int id)
{
var e = await _db.Queryable<SearchEngine>().InSingleAsync(id);
return e is null ? null : SearchEngineDto.FromEntity(e);
}
/// <inheritdoc />
public async Task<SearchEngineDto> CreateAsync(SearchEngineUpsertRequest request)
{
Validate(request);
var now = DateTime.UtcNow;
var entity = new SearchEngine
{
Name = request.Name.Trim(),
UrlTemplate = request.UrlTemplate.Trim(),
IconType = request.IconType,
Icon = request.Icon,
IconUrl = request.IconUrl,
ColorBg = request.ColorBg,
Sort = request.Sort,
IsDefault = request.IsDefault,
CreatedAt = now,
UpdatedAt = now
};
entity.Id = await _db.Insertable(entity).ExecuteReturnIdentityAsync();
if (entity.IsDefault) await ResetDefaultAsync(entity.Id);
await _sync.WriteAsync("search_engine", entity.Id, "create");
return SearchEngineDto.FromEntity(entity);
}
/// <inheritdoc />
public async Task<SearchEngineDto> UpdateAsync(int id, SearchEngineUpsertRequest request)
{
Validate(request);
var entity = await _db.Queryable<SearchEngine>().InSingleAsync(id)
?? throw new BusinessException("搜索引擎不存在", 404);
entity.Name = request.Name.Trim();
entity.UrlTemplate = request.UrlTemplate.Trim();
entity.IconType = request.IconType;
entity.Icon = request.Icon;
entity.IconUrl = request.IconUrl;
entity.ColorBg = request.ColorBg;
entity.Sort = request.Sort;
entity.IsDefault = request.IsDefault;
entity.UpdatedAt = DateTime.UtcNow;
await _db.Updateable(entity).ExecuteCommandAsync();
if (entity.IsDefault) await ResetDefaultAsync(entity.Id);
await _sync.WriteAsync("search_engine", id, "update");
return SearchEngineDto.FromEntity(entity);
}
/// <inheritdoc />
public async Task DeleteAsync(int id)
{
var entity = await _db.Queryable<SearchEngine>().InSingleAsync(id)
?? throw new BusinessException("搜索引擎不存在", 404);
await _db.Deleteable<SearchEngine>(id).ExecuteCommandAsync();
await _sync.WriteAsync("search_engine", id, "delete");
}
/// <inheritdoc />
public async Task<SearchEngineDto> SetDefaultAsync(int id)
{
var entity = await _db.Queryable<SearchEngine>().InSingleAsync(id)
?? throw new BusinessException("搜索引擎不存在", 404);
await ResetDefaultAsync(id);
entity.IsDefault = true;
entity.UpdatedAt = DateTime.UtcNow;
await _db.Updateable(entity).ExecuteCommandAsync();
await _sync.WriteAsync("search_engine", id, "update");
return SearchEngineDto.FromEntity(entity);
}
/// <summary>把其他引擎的 IsDefault 全部置为 false</summary>
private async Task ResetDefaultAsync(int keepId)
{
await _db.Updateable<SearchEngine>()
.SetColumns(e => e.IsDefault == false)
.Where(e => e.Id != keepId && e.IsDefault)
.ExecuteCommandAsync();
}
private static void Validate(SearchEngineUpsertRequest req)
{
if (string.IsNullOrWhiteSpace(req.Name)) throw new BusinessException("名称不能为空", 400);
if (string.IsNullOrWhiteSpace(req.UrlTemplate)) throw new BusinessException("URL 模板不能为空", 400);
if (!req.UrlTemplate.Contains("{q}")) throw new BusinessException("URL 模板必须包含 {q} 占位符", 400);
}
}