609f7d06cf
- pets-fe: 微信原生小程序(首页/计划/记录/报告/社区/引导), 服务端驱动、无假数据;弹层改用 scroll-view,打开时隐藏自定义 tabBar - pets-be: Gin + GORM(MySQL, sundynix_ 前缀) + MinIO,统一响应/分页, 微信 code2session 登录,provider-neutral AI(DeepSeek),go:embed React 后台 - 修复:分段选择类型不匹配(字符串 vs 数字)导致选不中 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
54 lines
1.8 KiB
Go
54 lines
1.8 KiB
Go
package database
|
|
|
|
import (
|
|
"golang.org/x/crypto/bcrypt"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/sundynix/pets-be/internal/config"
|
|
"github.com/sundynix/pets-be/internal/model"
|
|
)
|
|
|
|
// Seed 初始化种子数据:管理员账号 + 新手文章
|
|
func Seed(db *gorm.DB, cfg *config.Config) error {
|
|
if err := seedAdmin(db, cfg); err != nil {
|
|
return err
|
|
}
|
|
return seedArticles(db)
|
|
}
|
|
|
|
func seedAdmin(db *gorm.DB, cfg *config.Config) error {
|
|
var count int64
|
|
if err := db.Model(&model.Admin{}).Where("username = ?", cfg.Admin.Username).Count(&count).Error; err != nil {
|
|
return err
|
|
}
|
|
if count > 0 {
|
|
return nil
|
|
}
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(cfg.Admin.Password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return db.Create(&model.Admin{
|
|
Username: cfg.Admin.Username,
|
|
PasswordHash: string(hash),
|
|
Role: "admin",
|
|
}).Error
|
|
}
|
|
|
|
func seedArticles(db *gorm.DB) error {
|
|
var count int64
|
|
if err := db.Model(&model.Article{}).Count(&count).Error; err != nil {
|
|
return err
|
|
}
|
|
if count > 0 {
|
|
return nil
|
|
}
|
|
articles := []model.Article{
|
|
{Icon: "💉", Title: "幼猫疫苗和驱虫怎么安排?", Description: "适合搜索流量和社群传播,引导生成提醒。", Category: "vaccine", RelatedSheetType: "vaccine", Published: true},
|
|
{Icon: "💩", Title: "猫咪软便要不要去医院?", Description: "接异常记录和 AI 观察建议。", Category: "symptom", RelatedSheetType: "symptom", Published: true},
|
|
{Icon: "🍽️", Title: "7 天换粮计划怎么做?", Description: "接计划模板,适合一次性付费。", Category: "food", RelatedSheetType: "applyPlan", Published: true},
|
|
{Icon: "💰", Title: "一个月养猫大概花多少钱?", Description: "接养宠账本和年度账单。", Category: "cost", RelatedSheetType: "cost", Published: true},
|
|
}
|
|
return db.Create(&articles).Error
|
|
}
|