admin.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package source
  2. import (
  3. "time"
  4. "github.com/flipped-aurora/gin-vue-admin/server/global"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  6. "github.com/gookit/color"
  7. uuid "github.com/satori/go.uuid"
  8. "gorm.io/gorm"
  9. )
  10. var Admin = new(admin)
  11. type admin struct{}
  12. var admins = []system.SysUser{
  13. {GVA_MODEL: global.GVA_MODEL{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}, UUID: uuid.NewV4(), Username: "admin", Password: "e10adc3949ba59abbe56e057f20f883e", NickName: "超级管理员", HeaderImg: "http://qmplusimg.henrongyi.top/gva_header.jpg", AuthorityId: "888"},
  14. {GVA_MODEL: global.GVA_MODEL{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}, UUID: uuid.NewV4(), Username: "a303176530", Password: "3ec063004a6f31642261936a379fde3d", NickName: "QMPlusUser", HeaderImg: "http://qmplusimg.henrongyi.top/1572075907logo.png", AuthorityId: "9528"},
  15. }
  16. //@author: [SliverHorn](https://github.com/SliverHorn)
  17. //@description: sys_users 表数据初始化
  18. func (a *admin) Init() error {
  19. return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  20. if tx.Where("id IN ?", []int{1, 2}).Find(&[]system.SysUser{}).RowsAffected == 2 {
  21. color.Danger.Println("\n[Mysql] --> sys_users 表的初始数据已存在!")
  22. return nil
  23. }
  24. if err := tx.Create(&admins).Error; err != nil { // 遇到错误时回滚事务
  25. return err
  26. }
  27. color.Info.Println("\n[Mysql] --> sys_users 表初始数据成功!")
  28. return nil
  29. })
  30. }