Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
nbya
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
haoyanbin
nbya
Commits
a4b52796
Commit
a4b52796
authored
Jan 25, 2022
by
haoyanbin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Password
parent
37b174ab
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
16 deletions
+30
-16
sys_user.go
app/admin/models/sys_user.go
+13
-14
sys_user.go
app/admin/service/dto/sys_user.go
+17
-2
No files found.
app/admin/models/sys_user.go
View file @
a4b52796
...
@@ -2,7 +2,6 @@ package models
...
@@ -2,7 +2,6 @@ package models
import
(
import
(
"go-admin/common/models"
"go-admin/common/models"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
"gorm.io/gorm"
)
)
...
@@ -46,17 +45,17 @@ func (e *SysUser) GetId() interface{} {
...
@@ -46,17 +45,17 @@ func (e *SysUser) GetId() interface{} {
//加密
//加密
func
(
e
*
SysUser
)
Encrypt
()
(
err
error
)
{
func
(
e
*
SysUser
)
Encrypt
()
(
err
error
)
{
if
e
.
Password
==
""
{
//
if e.Password == "" {
return
return
}
//
}
var
hash
[]
byte
//
var hash []byte
if
hash
,
err
=
bcrypt
.
GenerateFromPassword
([]
byte
(
e
.
Password
),
bcrypt
.
DefaultCost
);
err
!=
nil
{
//
if hash, err = bcrypt.GenerateFromPassword([]byte(e.Password), bcrypt.DefaultCost); err != nil {
return
//
return
}
else
{
//
} else {
e
.
Password
=
string
(
hash
)
//
e.Password = string(hash)
return
//
return
}
//
}
}
}
func
(
e
*
SysUser
)
BeforeCreate
(
_
*
gorm
.
DB
)
error
{
func
(
e
*
SysUser
)
BeforeCreate
(
_
*
gorm
.
DB
)
error
{
...
@@ -65,9 +64,9 @@ func (e *SysUser) BeforeCreate(_ *gorm.DB) error {
...
@@ -65,9 +64,9 @@ func (e *SysUser) BeforeCreate(_ *gorm.DB) error {
func
(
e
*
SysUser
)
BeforeUpdate
(
_
*
gorm
.
DB
)
error
{
func
(
e
*
SysUser
)
BeforeUpdate
(
_
*
gorm
.
DB
)
error
{
var
err
error
var
err
error
//
if e.Password != "" {
if
e
.
Password
!=
""
{
//
err = e.Encrypt()
err
=
e
.
Encrypt
()
//
}
}
return
err
return
err
}
}
...
...
app/admin/service/dto/sys_user.go
View file @
a4b52796
...
@@ -2,6 +2,7 @@ package dto
...
@@ -2,6 +2,7 @@ package dto
import
(
import
(
"go-admin/app/admin/models"
"go-admin/app/admin/models"
"golang.org/x/crypto/bcrypt"
"go-admin/common/dto"
"go-admin/common/dto"
common
"go-admin/common/models"
common
"go-admin/common/models"
...
@@ -51,7 +52,7 @@ func (s *ResetSysUserPwdReq) Generate(model *models.SysUser) {
...
@@ -51,7 +52,7 @@ func (s *ResetSysUserPwdReq) Generate(model *models.SysUser) {
if
s
.
UserId
!=
0
{
if
s
.
UserId
!=
0
{
model
.
UserId
=
s
.
UserId
model
.
UserId
=
s
.
UserId
}
}
model
.
Password
=
s
.
Password
model
.
Password
=
Encrypt
(
s
.
Password
)
}
}
type
UpdateSysUserAvatarReq
struct
{
type
UpdateSysUserAvatarReq
struct
{
...
@@ -112,7 +113,7 @@ func (s *SysUserInsertReq) Generate(model *models.SysUser) {
...
@@ -112,7 +113,7 @@ func (s *SysUserInsertReq) Generate(model *models.SysUser) {
model
.
UserId
=
s
.
UserId
model
.
UserId
=
s
.
UserId
}
}
model
.
Username
=
s
.
Username
model
.
Username
=
s
.
Username
model
.
Password
=
s
.
Password
model
.
Password
=
Encrypt
(
s
.
Password
)
model
.
NickName
=
s
.
NickName
model
.
NickName
=
s
.
NickName
model
.
Phone
=
s
.
Phone
model
.
Phone
=
s
.
Phone
model
.
RoleId
=
s
.
RoleId
model
.
RoleId
=
s
.
RoleId
...
@@ -191,3 +192,17 @@ type PassWord struct {
...
@@ -191,3 +192,17 @@ type PassWord struct {
NewPassword
string
`json:"newPassword" vd:"len($)>0"`
NewPassword
string
`json:"newPassword" vd:"len($)>0"`
OldPassword
string
`json:"oldPassword" vd:"len($)>0"`
OldPassword
string
`json:"oldPassword" vd:"len($)>0"`
}
}
func
Encrypt
(
password
string
)
string
{
if
password
==
""
{
return
""
}
var
hash
[]
byte
var
err
error
if
hash
,
err
=
bcrypt
.
GenerateFromPassword
([]
byte
(
password
),
bcrypt
.
DefaultCost
);
err
!=
nil
{
return
""
}
else
{
return
string
(
hash
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment