392 lines
9.6 KiB
Go
392 lines
9.6 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
"nl-video-api/internal/dao"
|
|
"nl-video-api/internal/model/entity"
|
|
"nl-video-api/utility/response"
|
|
)
|
|
|
|
// ConfigService 系统配置服务
|
|
type ConfigService struct{}
|
|
|
|
var Config = &ConfigService{}
|
|
|
|
// NewConfigService 创建配置服务实例
|
|
func NewConfigService() *ConfigService {
|
|
return &ConfigService{}
|
|
}
|
|
|
|
// AdminList 管理员获取配置列表
|
|
func (s *ConfigService) AdminList(r *ghttp.Request) {
|
|
response.Success(r, g.Map{
|
|
"list": []interface{}{},
|
|
"total": 0,
|
|
})
|
|
}
|
|
|
|
// AdminDetail 管理员获取配置详情
|
|
func (s *ConfigService) AdminDetail(r *ghttp.Request) {
|
|
response.Success(r, g.Map{
|
|
"id": 1,
|
|
"key": "site_name",
|
|
"value": "NL视频网站",
|
|
"desc": "网站名称",
|
|
})
|
|
}
|
|
|
|
// AdminCreate 管理员创建配置
|
|
func (s *ConfigService) AdminCreate(r *ghttp.Request) {
|
|
response.Success(r, "创建成功")
|
|
}
|
|
|
|
// AdminUpdate 管理员更新配置
|
|
func (s *ConfigService) AdminUpdate(r *ghttp.Request) {
|
|
response.Success(r, "更新成功")
|
|
}
|
|
|
|
// AdminDelete 管理员删除配置
|
|
func (s *ConfigService) AdminDelete(r *ghttp.Request) {
|
|
response.Success(r, "删除成功")
|
|
}
|
|
|
|
// AdminBatchDelete 管理员批量删除配置
|
|
func (s *ConfigService) AdminBatchDelete(r *ghttp.Request) {
|
|
response.Success(r, "批量删除成功")
|
|
}
|
|
|
|
// AdminGetByKey 管理员根据键获取配置
|
|
func (s *ConfigService) AdminGetByKey(r *ghttp.Request) {
|
|
response.Success(r, g.Map{
|
|
"key": "site_name",
|
|
"value": "NL视频网站",
|
|
})
|
|
}
|
|
|
|
// AdminGetByGroup 管理员根据分组获取配置
|
|
func (s *ConfigService) AdminGetByGroup(r *ghttp.Request) {
|
|
response.Success(r, g.Map{
|
|
"list": []interface{}{},
|
|
"total": 0,
|
|
})
|
|
}
|
|
|
|
// AdminSet 管理员设置配置
|
|
func (s *ConfigService) AdminSet(r *ghttp.Request) {
|
|
response.Success(r, "设置成功")
|
|
}
|
|
|
|
// AdminBatchSet 管理员批量设置配置
|
|
func (s *ConfigService) AdminBatchSet(r *ghttp.Request) {
|
|
response.Success(r, "批量设置成功")
|
|
}
|
|
|
|
// AdminGetGroupList 管理员获取配置分组列表
|
|
func (s *ConfigService) AdminGetGroupList(r *ghttp.Request) {
|
|
response.Success(r, g.Map{
|
|
"list": []string{"基础设置", "系统设置", "邮件设置"},
|
|
})
|
|
}
|
|
|
|
// AdminExport 管理员导出配置
|
|
func (s *ConfigService) AdminExport(r *ghttp.Request) {
|
|
response.Success(r, "导出成功")
|
|
}
|
|
|
|
// AdminImport 管理员导入配置
|
|
func (s *ConfigService) AdminImport(r *ghttp.Request) {
|
|
response.Success(r, "导入成功")
|
|
}
|
|
|
|
// AdminCache 管理员获取缓存配置
|
|
func (s *ConfigService) AdminCache(r *ghttp.Request) {
|
|
response.Success(r, g.Map{
|
|
"cache_enabled": true,
|
|
"cache_time": 3600,
|
|
})
|
|
}
|
|
|
|
// AdminClearCache 管理员清除配置缓存
|
|
func (s *ConfigService) AdminClearCache(r *ghttp.Request) {
|
|
response.Success(r, "缓存清除成功")
|
|
}
|
|
|
|
// AdminValidate 管理员验证配置
|
|
func (s *ConfigService) AdminValidate(r *ghttp.Request) {
|
|
response.Success(r, "配置验证通过")
|
|
}
|
|
|
|
// AdminBackup 管理员备份配置
|
|
func (s *ConfigService) AdminBackup(r *ghttp.Request) {
|
|
response.Success(r, "配置备份成功")
|
|
}
|
|
|
|
// AdminRestore 管理员恢复配置
|
|
func (s *ConfigService) AdminRestore(r *ghttp.Request) {
|
|
response.Success(r, "配置恢复成功")
|
|
}
|
|
|
|
// AdminGetHistory 管理员获取配置历史
|
|
func (s *ConfigService) AdminGetHistory(r *ghttp.Request) {
|
|
response.Success(r, g.Map{
|
|
"list": []interface{}{},
|
|
"total": 0,
|
|
})
|
|
}
|
|
|
|
// GetList 获取配置列表
|
|
func (s *ConfigService) GetList(r *ghttp.Request) {
|
|
// 获取请求参数
|
|
keyword := r.Get("keyword").String()
|
|
pageStr := r.Get("page", "1").String()
|
|
pageSizeStr := r.Get("page_size", "10").String()
|
|
|
|
page, err := strconv.Atoi(pageStr)
|
|
if err != nil || page < 1 {
|
|
page = 1
|
|
}
|
|
|
|
pageSize, err := strconv.Atoi(pageSizeStr)
|
|
if err != nil || pageSize < 1 {
|
|
pageSize = 10
|
|
}
|
|
|
|
// 构建查询条件
|
|
query := dao.Config.Ctx(r.Context())
|
|
if keyword != "" {
|
|
query = query.Where("`key` LIKE ? OR name LIKE ?",
|
|
fmt.Sprintf("%%%s%%", keyword),
|
|
fmt.Sprintf("%%%s%%", keyword))
|
|
}
|
|
|
|
// 获取总数
|
|
total, err := query.Count()
|
|
if err != nil {
|
|
response.Error(r, response.CodeInternalError, "获取配置总数失败")
|
|
return
|
|
}
|
|
|
|
// 获取列表
|
|
var list []*entity.Config
|
|
err = query.Page(page, pageSize).OrderAsc("`key`").Scan(&list)
|
|
if err != nil {
|
|
response.Error(r, response.CodeInternalError, "获取配置列表失败")
|
|
return
|
|
}
|
|
|
|
response.Success(r, g.Map{
|
|
"list": list,
|
|
"total": total,
|
|
"page": page,
|
|
"page_size": pageSize,
|
|
})
|
|
}
|
|
|
|
// GetByKey 根据键获取配置
|
|
func (s *ConfigService) GetByKey(r *ghttp.Request) {
|
|
// 获取请求参数
|
|
configKey := r.Get("key").String()
|
|
if configKey == "" {
|
|
response.Error(r, response.CodeInvalidParam, "配置键不能为空")
|
|
return
|
|
}
|
|
|
|
// 查询配置
|
|
var config entity.Config
|
|
err := dao.Config.Ctx(r.Context()).Where("`key`", configKey).Scan(&config)
|
|
if err != nil {
|
|
response.Error(r, response.CodeInternalError, "获取配置失败")
|
|
return
|
|
}
|
|
|
|
if config.Id == 0 {
|
|
response.Error(r, response.CodeNotFound, "配置不存在")
|
|
return
|
|
}
|
|
|
|
response.Success(r, config)
|
|
}
|
|
|
|
// Add 添加配置
|
|
func (s *ConfigService) Add(r *ghttp.Request) {
|
|
// 获取请求参数
|
|
configKey := r.Get("config_key").String()
|
|
configName := r.Get("config_name").String()
|
|
configValue := r.Get("config_value").String()
|
|
configType := r.Get("config_type").String()
|
|
description := r.Get("description").String()
|
|
|
|
// 参数验证
|
|
if configKey == "" || configName == "" {
|
|
response.Error(r, response.CodeInvalidParam, "配置键和配置名称不能为空")
|
|
return
|
|
}
|
|
|
|
// 检查配置键是否已存在
|
|
count, err := dao.Config.Ctx(r.Context()).Where("config_key", configKey).Count()
|
|
if err != nil {
|
|
response.Error(r, response.CodeInternalError, "检查配置键失败")
|
|
return
|
|
}
|
|
if count > 0 {
|
|
response.Error(r, response.CodeInvalidParam, "配置键已存在")
|
|
return
|
|
}
|
|
|
|
// 创建配置
|
|
_, err = dao.Config.Ctx(r.Context()).Data(g.Map{
|
|
"config_key": configKey,
|
|
"config_name": configName,
|
|
"config_value": configValue,
|
|
"config_type": configType,
|
|
"description": description,
|
|
"created_at": gtime.Now(),
|
|
"updated_at": gtime.Now(),
|
|
}).Insert()
|
|
|
|
if err != nil {
|
|
response.Error(r, response.CodeInternalError, "添加配置失败")
|
|
return
|
|
}
|
|
|
|
response.Success(r, "添加成功")
|
|
}
|
|
|
|
// Update 更新配置
|
|
func (s *ConfigService) Update(r *ghttp.Request) {
|
|
// 获取请求参数
|
|
idStr := r.Get("id").String()
|
|
configName := r.Get("config_name").String()
|
|
configValue := r.Get("config_value").String()
|
|
configType := r.Get("config_type").String()
|
|
description := r.Get("description").String()
|
|
|
|
if idStr == "" {
|
|
response.Error(r, response.CodeInvalidParam, "配置ID不能为空")
|
|
return
|
|
}
|
|
|
|
id, err := strconv.Atoi(idStr)
|
|
if err != nil {
|
|
response.Error(r, response.CodeInvalidParam, "配置ID格式错误")
|
|
return
|
|
}
|
|
|
|
// 检查配置是否存在
|
|
count, err := dao.Config.Ctx(r.Context()).Where("id", id).Count()
|
|
if err != nil {
|
|
response.Error(r, response.CodeInternalError, "检查配置失败")
|
|
return
|
|
}
|
|
if count == 0 {
|
|
response.Error(r, response.CodeNotFound, "配置不存在")
|
|
return
|
|
}
|
|
|
|
// 更新配置
|
|
updateData := g.Map{
|
|
"updated_at": gtime.Now(),
|
|
}
|
|
if configName != "" {
|
|
updateData["config_name"] = configName
|
|
}
|
|
if configValue != "" {
|
|
updateData["config_value"] = configValue
|
|
}
|
|
if configType != "" {
|
|
updateData["config_type"] = configType
|
|
}
|
|
if description != "" {
|
|
updateData["description"] = description
|
|
}
|
|
|
|
_, err = dao.Config.Ctx(r.Context()).Where("id", id).Data(updateData).Update()
|
|
if err != nil {
|
|
response.Error(r, response.CodeInternalError, "更新配置失败")
|
|
return
|
|
}
|
|
|
|
response.Success(r, "更新成功")
|
|
}
|
|
|
|
// Delete 删除配置
|
|
func (s *ConfigService) Delete(r *ghttp.Request) {
|
|
// 获取请求参数
|
|
idStr := r.Get("id").String()
|
|
if idStr == "" {
|
|
response.Error(r, response.CodeInvalidParam, "配置ID不能为空")
|
|
return
|
|
}
|
|
|
|
id, err := strconv.Atoi(idStr)
|
|
if err != nil {
|
|
response.Error(r, response.CodeInvalidParam, "配置ID格式错误")
|
|
return
|
|
}
|
|
|
|
// 检查配置是否存在
|
|
count, err := dao.Config.Ctx(r.Context()).Where("id", id).Count()
|
|
if err != nil {
|
|
response.Error(r, response.CodeInternalError, "检查配置失败")
|
|
return
|
|
}
|
|
if count == 0 {
|
|
response.Error(r, response.CodeNotFound, "配置不存在")
|
|
return
|
|
}
|
|
|
|
// 删除配置
|
|
_, err = dao.Config.Ctx(r.Context()).Where("id", id).Delete()
|
|
if err != nil {
|
|
response.Error(r, response.CodeInternalError, "删除配置失败")
|
|
return
|
|
}
|
|
|
|
response.Success(r, "删除成功")
|
|
}
|
|
|
|
// BatchDelete 批量删除配置
|
|
func (s *ConfigService) BatchDelete(r *ghttp.Request) {
|
|
// 获取请求参数
|
|
var ids []int
|
|
err := r.Parse(&ids)
|
|
if err != nil {
|
|
response.Error(r, response.CodeInvalidParam, "参数格式错误")
|
|
return
|
|
}
|
|
|
|
if len(ids) == 0 {
|
|
response.Error(r, response.CodeInvalidParam, "请选择要删除的配置")
|
|
return
|
|
}
|
|
|
|
// 批量删除
|
|
_, err = dao.Config.Ctx(r.Context()).Where("id IN (?)", ids).Delete()
|
|
if err != nil {
|
|
response.Error(r, response.CodeInternalError, "批量删除失败")
|
|
return
|
|
}
|
|
|
|
response.Success(r, "删除成功")
|
|
}
|
|
|
|
// GetPublicConfigs 获取公开配置(用户端)
|
|
func (s *ConfigService) GetPublicConfigs(r *ghttp.Request) {
|
|
// 返回默认的公开配置
|
|
configMap := map[string]interface{}{
|
|
"site_name": "NL在线影院",
|
|
"site_logo": "",
|
|
"site_description": "NL在线影院 - 海量高清影视资源在线观看",
|
|
"site_keywords": "NL影院,在线观看,高清影视",
|
|
"upload_max_size": "10485760",
|
|
"upload_allowed_types": "jpg,jpeg,png,gif,mp4,avi,mkv",
|
|
}
|
|
|
|
response.Success(r, configMap)
|
|
}
|