Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/v1/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ var (

var (
ErrorUserNameAndPassword = newError(100, "用户名和密码错误")
ErrorUserOldPassword = newError(100, "原密码错误")
ErrorTokenGeneration = newError(101, "令牌生成错误")
)
132 changes: 56 additions & 76 deletions internal/dal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go.uber.org/zap"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gen/field"
"gorm.io/gorm"

"github.com/ch3nnn/webstack-go/internal/dal/model"
Expand Down Expand Up @@ -91,7 +92,18 @@ func NewDB(conf *viper.Viper, l *log.Logger) *gorm.DB {
}

func autoMigrateAndInitialize(db *gorm.DB) {
err := db.AutoMigrate(
ctx := context.Background()

err := db.Migrator().DropTable(
&model.SysUserMenu{},
&model.SysMenu{},
)
if err != nil {
fmt.Println("migrate drop table error")
os.Exit(0)
}

err = db.AutoMigrate(
&model.SysConfig{},
&model.SysUser{},
&model.SysUserMenu{},
Expand All @@ -104,34 +116,24 @@ func autoMigrateAndInitialize(db *gorm.DB) {
os.Exit(0)
}

ctx := context.Background()

umCtn, err := query.SysUserMenu.WithContext(ctx).Count()
if err != nil {
os.Exit(0)
}

uCtn, err := query.SysUser.WithContext(ctx).Count()
if err != nil {
os.Exit(0)
}

mCtn, err := query.SysMenu.WithContext(ctx).Count()
_, err = query.SysUser.WithContext(ctx).
Where(
query.SysUser.ID.Eq(1),
query.SysUser.Username.Eq(DefaultUname),
).
Attrs(
field.Attrs(&model.SysUser{
Password: cryptor.Md5String(DefaultUPassword)},
),
).
FirstOrCreate()
if err != nil {
fmt.Println("user migrate error")
os.Exit(0)
}

if umCtn == 0 && uCtn == 0 && mCtn == 0 {
err := query.SysUser.WithContext(ctx).Create(&model.SysUser{
ID: 1,
Username: "admin",
Password: cryptor.Md5String("admin"),
})
if err != nil {
os.Exit(0)
}

err = query.SysMenu.WithContext(ctx).Create(
err = query.SysMenu.WithContext(ctx).
Create(
&model.SysMenu{
ID: 1,
Pid: 0,
Expand Down Expand Up @@ -177,11 +179,13 @@ func autoMigrateAndInitialize(db *gorm.DB) {
IsUsed: true,
},
)
if err != nil {
os.Exit(0)
}
if err != nil {
fmt.Println("menu migrate error")
os.Exit(0)
}

err = query.SysUserMenu.WithContext(ctx).Create(
err = query.SysUserMenu.WithContext(ctx).
Create(
&model.SysUserMenu{
UserID: 1,
MenuID: 1,
Expand All @@ -203,54 +207,30 @@ func autoMigrateAndInitialize(db *gorm.DB) {
MenuID: 5,
},
)
if err != nil {
os.Exit(0)
}

err = query.SysConfig.WithContext(ctx).Create(
&model.SysConfig{
ID: 1,
AboutSite: "> ❤️ 基于 Golang 开源的网址导航网站项目,具备完整的前后台,您可以拿来制作自己平日收藏的网址导航。\n\n\n> 如果你也是开发者,如果你也正好喜欢折腾,那希望这个网站能给你带来一些作用。",
AboutAuthor: `
<div class="col-sm-4">
<div class="xe-widget xe-conversations box2 label-info" onclick="window.open('https://blog.ch3nnn.cn/about/', '_blank')" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="https://blog.ch3nnn.cn/about/">
<div class="xe-comment-entry">
<a class="xe-user-img">
<img src="https://s2.loli.net/2023/02/20/H1k52mlXNYKWDrU.png" class="img-circle" width="40">
</a>
<div class="xe-comment">
<a href="#" class="xe-user-name overflowClip_1">
<strong>Developer. Ch3nnn.</strong>
</a>
<p class="overflowClip_2"> 折腾不息 · 乐此不疲.</p>
</div>
</div>
</div>
</div>
if err != nil {
fmt.Println("user menu migrate error")
os.Exit(0)
}

<div class="col-md-8">
<div class="row">
<div class="col-sm-12">
<br>
<blockquote>
<p>
这是一个公益项目,而且是<a href="https://github.com/ch3nnn/webstack-go"> 开源 </a>的。你也可以拿来制作自己的网址导航。如果你有更好的想法,可以通过个人网站<a href="https://ch3nnn.cn/about/">ch3nnn.cn</a>中的联系方式找到我,欢迎与我交流分享。
</p>
</blockquote>
</div>
</div>
<br>
</div>
`,
IsAbout: false,
SiteTitle: "WebStack-Go - 网址导航",
SiteKeyword: "网址导航",
SiteDesc: "WebStack-Go - 基于 Golang 开源的网址导航网站",
})
if err != nil {
os.Exit(0)
}
_, err = query.SysConfig.WithContext(ctx).
Where(
query.SysConfig.ID.Eq(1),
).
Attrs(
field.Attrs(&model.SysConfig{
AboutSite: DefaultAboutSite,
AboutAuthor: DefaultAuthor,
SiteTitle: DefaultSiteTitle,
SiteKeyword: DefaultSiteKeyword,
SiteDesc: DefaultSiteDesc,
}),
).
FirstOrCreate()

fmt.Println("success initialize")
if err != nil {
fmt.Println("config migrate error")
os.Exit(0)
}

fmt.Println("success initialize")
}
38 changes: 38 additions & 0 deletions internal/dal/repository/sys_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,44 @@ import (
"github.com/ch3nnn/webstack-go/internal/dal/query"
)

const (
DefaultAboutSite = "> ❤️ 基于 Golang 开源的网址导航网站项目,具备完整的前后台,您可以拿来制作自己平日收藏的网址导航。\n\n\n> 如果你也是开发者,如果你也正好喜欢折腾,那希望这个网站能给你带来一些作用。"
DefaultAuthor = `
<div class="col-sm-4">
<div class="xe-widget xe-conversations box2 label-info" onclick="window.open('https://blog.ch3nnn.cn/about/', '_blank')" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="https://blog.ch3nnn.cn/about/">
<div class="xe-comment-entry">
<a class="xe-user-img">
<img src="https://s2.loli.net/2023/02/20/H1k52mlXNYKWDrU.png" class="img-circle" width="40">
</a>
<div class="xe-comment">
<a href="#" class="xe-user-name overflowClip_1">
<strong>Developer. Ch3nnn.</strong>
</a>
<p class="overflowClip_2"> 折腾不息 · 乐此不疲.</p>
</div>
</div>
</div>
</div>

<div class="col-md-8">
<div class="row">
<div class="col-sm-12">
<br>
<blockquote>
<p>
这是一个公益项目,而且是<a href="https://github.com/ch3nnn/webstack-go"> 开源 </a>的。你也可以拿来制作自己的网址导航。如果你有更好的想法,可以通过个人网站<a href="https://ch3nnn.cn/about/">ch3nnn.cn</a>中的联系方式找到我,欢迎与我交流分享。
</p>
</blockquote>
</div>
</div>
<br>
</div>
`
DefaultSiteTitle = "WebStack-Go - 网址导航"
DefaultSiteKeyword = "网址导航"
DefaultSiteDesc = "WebStack-Go - 基于 Golang 开源的网址导航网站"
)

var _ iCustomGenSysConfigFunc = (*customSysConfigDao)(nil)

type (
Expand Down
5 changes: 5 additions & 0 deletions internal/dal/repository/sys_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"github.com/ch3nnn/webstack-go/internal/dal/query"
)

const (
DefaultUname = "admin"
DefaultUPassword = "admin"
)

var _ iCustomGenSysUserFunc = (*customSysUserDao)(nil)

type (
Expand Down
2 changes: 1 addition & 1 deletion internal/service/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func (s *service) GetConfig(ctx context.Context) (*v1.ConfigResp, error) {
conf, err := s.configRepo.WithContext(ctx).FindOne(s.configRepo.WhereByID(1))
conf, err := s.configRepo.WithContext(ctx).FindOne()
if err != nil {
return nil, err
}
Expand Down
7 changes: 5 additions & 2 deletions internal/service/user/updatepassword.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func (s *service) UpdatePassword(ctx *gin.Context, req *v1.UpdatePasswordReq) (*
user, err := s.userRepo.WithContext(ctx).
FindOne(
s.userRepo.WhereByID(ctx.GetInt(middleware.UserID)),
s.userRepo.WhereByPassword(req.OldPassword),
)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
Expand All @@ -29,10 +28,14 @@ func (s *service) UpdatePassword(ctx *gin.Context, req *v1.UpdatePasswordReq) (*
return nil, err
}

if user.Password != req.OldPassword {
return nil, v1.ErrorUserOldPassword
}

_, err = s.userRepo.WithContext(ctx).Update(&model.SysUser{Password: req.NewPassword}, s.userRepo.WhereByID(user.ID))
if err != nil {
return nil, err
}

return &v1.UpdatePasswordResp{}, nil
return nil, nil
}
Loading