package system
|
|
import (
|
"aps_crm/constvar"
|
"aps_crm/model"
|
"aps_crm/pkg/encrypt"
|
"aps_crm/pkg/logx"
|
"aps_crm/pkg/snowflake"
|
"aps_crm/service"
|
"context"
|
"fmt"
|
"github.com/pkg/errors"
|
"gorm.io/gorm"
|
)
|
|
const (
|
initMenuSequence = iota + 1
|
initUserSequence
|
)
|
|
type initUser struct{}
|
|
// auto run
|
func init() {
|
service.RegisterInit(initUserSequence, &initUser{})
|
}
|
|
func (i initUser) InitializerName() string {
|
return model.User{}.TableName()
|
}
|
|
func (i *initUser) InitializeData(ctx context.Context) (next context.Context, err error) {
|
adminPassword := encrypt.BcryptHash("123456")
|
entities := []*model.User{
|
{
|
ID: fmt.Sprintf("u%v", snowflake.GenerateId()),
|
Username: "admin",
|
Password: adminPassword,
|
NickName: "admin",
|
UserType: constvar.UserTypeSuper,
|
ParentId: "basic", // 超级管理员账户的父亲为空字符串,或者起个名字
|
Enable: true,
|
},
|
}
|
if err = model.NewUserSearch(nil).CreateBatch(entities); err != nil {
|
return ctx, errors.Wrap(err, model.User{}.TableName()+"表数据初始化失败!")
|
}
|
next = context.WithValue(ctx, i.InitializerName(), entities)
|
|
menuEntities, ok := ctx.Value(initMenu{}.InitializerName()).([]*model.Menu)
|
if !ok {
|
return next, errors.New("no find menus")
|
}
|
|
if err = model.NewUserSearch(nil).ReplaceMenu(entities[0], menuEntities); err != nil {
|
return next, err
|
}
|
logx.Infof("InitializeData success initName:%v", i.InitializerName())
|
return next, err
|
}
|
|
func (i *initUser) DataInserted(ctx context.Context) bool {
|
_, err := model.NewUserSearch(nil).SetUserName("admin").First()
|
if errors.Is(err, gorm.ErrRecordNotFound) { // 判断是否存在数据
|
return false
|
}
|
return true
|
}
|