101 lines
4.6 KiB
Go
101 lines
4.6 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"tool-api/internal/consts"
|
|
)
|
|
|
|
// Seed 启动时幂等写入种子数据:管理员、模块、等级、工具目录。
|
|
// 数据库未就绪时仅告警,不阻断服务启动。
|
|
func Seed(ctx context.Context) {
|
|
if err := seedAdmin(ctx); err != nil {
|
|
g.Log().Warning(ctx, "[seed] admin:", err)
|
|
}
|
|
if err := seedRows(ctx, consts.TableModules, "module_key", seedModules()); err != nil {
|
|
g.Log().Warning(ctx, "[seed] modules:", err)
|
|
}
|
|
if err := seedRows(ctx, consts.TableLevels, "level_key", seedLevels()); err != nil {
|
|
g.Log().Warning(ctx, "[seed] levels:", err)
|
|
}
|
|
if err := seedRows(ctx, consts.TableTools, "tool_key", seedTools()); err != nil {
|
|
g.Log().Warning(ctx, "[seed] tools:", err)
|
|
}
|
|
}
|
|
|
|
func seedAdmin(ctx context.Context) error {
|
|
count, err := g.Model(consts.TableAdminUsers).Count()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if count > 0 {
|
|
return nil
|
|
}
|
|
hash, err := bcrypt.GenerateFromPassword([]byte("admin123"), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = g.Model(consts.TableAdminUsers).Data(g.Map{
|
|
"account": "admin",
|
|
"password": string(hash),
|
|
"nick_name": "管理员",
|
|
"role_name": "超级管理员",
|
|
"role_value": "super",
|
|
}).Insert()
|
|
return err
|
|
}
|
|
|
|
// seedRows 按 uniqueKey 幂等插入(存在即跳过该行)
|
|
func seedRows(ctx context.Context, table, uniqueKey string, rows []g.Map) error {
|
|
for _, row := range rows {
|
|
count, err := g.Model(table).Where(uniqueKey, row[uniqueKey]).Count()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if count > 0 {
|
|
continue
|
|
}
|
|
if _, err := g.Model(table).Data(row).Insert(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func seedModules() []g.Map {
|
|
return []g.Map{
|
|
{"module_key": "image", "name": "图片处理", "icon": "picture", "description": "压缩、转换、抠图等图片工具", "sort": 1},
|
|
{"module_key": "doc", "name": "文档处理", "icon": "description", "description": "文档转换与处理(规划中)", "sort": 2},
|
|
{"module_key": "calc", "name": "计算换算", "icon": "computer", "description": "单位换算、房贷计算等", "sort": 3},
|
|
{"module_key": "time", "name": "时间效率", "icon": "time", "description": "番茄钟、倒数日等", "sort": 4},
|
|
{"module_key": "create", "name": "生成制作", "icon": "edit-outline", "description": "二维码、随机决定等", "sort": 5},
|
|
{"module_key": "life", "name": "生活查询", "icon": "home", "description": "日期计算、汇率等", "sort": 6},
|
|
}
|
|
}
|
|
|
|
func seedLevels() []g.Map {
|
|
return []g.Map{
|
|
{"level_key": "v1", "name": "基础版", "modules": `["image"]`, "sort": 1, "remark": "默认等级:仅图片处理模块"},
|
|
{"level_key": "v2", "name": "进阶版", "modules": `["image","doc"]`, "sort": 2, "remark": "示例:追加文档处理模块"},
|
|
}
|
|
}
|
|
|
|
func seedTools() []g.Map {
|
|
return []g.Map{
|
|
{"tool_key": "img-compress", "module_key": "image", "name": "图片压缩", "icon": "picture", "description": "批量压缩图片体积,画质几乎无损", "sort": 1, "is_hot": 1},
|
|
{"tool_key": "img-convert", "module_key": "image", "name": "格式转换", "icon": "picture", "description": "PNG / JPG / WebP 互转", "sort": 2, "is_hot": 1},
|
|
{"tool_key": "img-resize", "module_key": "image", "name": "修改尺寸", "icon": "picture", "description": "按宽高、比例或最长边缩放", "sort": 3},
|
|
{"tool_key": "img-crop", "module_key": "image", "name": "裁剪旋转", "icon": "picture", "description": "自由裁剪、旋转与镜像", "sort": 4},
|
|
{"tool_key": "img-watermark", "module_key": "image", "name": "文字水印", "icon": "picture", "description": "给图片添加平铺或居中水印", "sort": 5},
|
|
{"tool_key": "img-matting", "module_key": "image", "name": "去除背景", "icon": "picture", "description": "一键抠除纯色背景", "sort": 6, "is_hot": 1},
|
|
{"tool_key": "img-avatarfx", "module_key": "image", "name": "头像特效", "icon": "picture", "description": "国庆红旗圈、节日边框等头像特效制作", "sort": 7, "is_hot": 1},
|
|
{"tool_key": "img-grid9", "module_key": "image", "name": "九宫格切图", "icon": "picture", "description": "照片切成 9 张发朋友圈", "sort": 8},
|
|
{"tool_key": "img-collage", "module_key": "image", "name": "图片拼图", "icon": "picture", "description": "2-4 张照片拼成一张", "sort": 9},
|
|
{"tool_key": "img-qrcode", "module_key": "image", "name": "二维码生成", "icon": "picture", "description": "文本链接生成二维码图片", "sort": 10},
|
|
{"tool_key": "img-annotate", "module_key": "image", "name": "图片标注", "icon": "picture", "description": "涂鸦、文字、马赛克标注", "sort": 11},
|
|
}
|
|
}
|