Compare commits
No commits in common. "283d8bee5fee532c6573db395ca478331bd9bc6b" and "10368f9c822a728f31eef827ef5b0bdcac3a4447" have entirely different histories.
283d8bee5f
...
10368f9c82
|
|
@ -8,7 +8,6 @@ CREATE TABLE IF NOT EXISTS `user` (
|
|||
`pwd` varchar(255) DEFAULT NULL COMMENT '密码',
|
||||
`salt` varchar(32) NOT NULL COMMENT '盐',
|
||||
`sex` TINYINT DEFAULT 0 COMMENT '性别:0-女,1-男',
|
||||
`status` TINYINT DEFAULT 1 NOT NULL COMMENT '状态:0-禁用,1-启用',
|
||||
`created_by` VARCHAR(64) DEFAULT '' COMMENT '创建人',
|
||||
`created_on` DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '记录创建时间',
|
||||
`modified_by` VARCHAR(64) DEFAULT '' COMMENT '修改人',
|
||||
|
|
|
|||
|
|
@ -1,12 +1,5 @@
|
|||
package models
|
||||
|
||||
type UserStatus int
|
||||
|
||||
const (
|
||||
EnableUserStatus UserStatus = 0
|
||||
DisableUserStatus UserStatus = 1
|
||||
)
|
||||
|
||||
type LoginInfo struct {
|
||||
Account string
|
||||
Pwd string
|
||||
|
|
@ -33,25 +26,3 @@ type User struct {
|
|||
Id uint
|
||||
UserInfo
|
||||
}
|
||||
|
||||
type ModifyInfo struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Sex int `json:"sex"`
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
Page int `form:"page" json:"page" binding:"required"`
|
||||
PageSize int `form:"page_size" json:"page_size" binding:"required"`
|
||||
Keyword string `form:"keyword" Json:"keyword"`
|
||||
Sort string `form:"sort" json:"sort" binding:"sql_sort"`
|
||||
}
|
||||
|
||||
type ResetPwdReq struct {
|
||||
Id uint `json:"id" binding:"required"`
|
||||
OldPwd string `json:"old_pwd" binding:"required"`
|
||||
Pwd string `json:"pwd" binding:"required"`
|
||||
}
|
||||
|
||||
type GetUserReq struct {
|
||||
Account string `form:"account" json:"account" binding:"required"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package repo
|
||||
|
||||
import (
|
||||
"busniess-user-center/internal/models"
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
|
@ -40,9 +39,6 @@ type UserRepo interface {
|
|||
GetUserByEmail(ctx context.Context, mobile string) (user User, err error)
|
||||
SaveUser(ctx context.Context, user User) error
|
||||
CreateUser(ctx context.Context, user *User) error
|
||||
SetUserStatus(ctx context.Context, id uint, status models.UserStatus) error
|
||||
Search(ctx context.Context, query *models.Query) ([]User, error)
|
||||
ResetPwd(ctx context.Context, user User) error
|
||||
}
|
||||
|
||||
type userRepoS struct {
|
||||
|
|
@ -81,26 +77,3 @@ func (u *userRepoS) GetUserByEmail(ctx context.Context, email string) (user User
|
|||
err = u.db.Where("email = ?", email).Take(&user).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (u *userRepoS) SetUserStatus(ctx context.Context, id uint, status models.UserStatus) error {
|
||||
err := u.db.Model(&User{}).Where("id = ?", id).Update("status", status).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func (u *userRepoS) Search(ctx context.Context, query *models.Query) ([]User, error) {
|
||||
users := make([]User, 0)
|
||||
|
||||
keyword := fmt.Sprintf("%%%s%%", query.Keyword)
|
||||
db := u.db.Model(&User{})
|
||||
if query.Keyword != "" {
|
||||
db = db.Where("account like ? or name like ?", keyword, keyword)
|
||||
}
|
||||
|
||||
err := db.Order(query.Sort).Limit(query.PageSize).Offset(query.Page * query.PageSize).Find(&users).Error
|
||||
return users, err
|
||||
}
|
||||
|
||||
func (u *userRepoS) ResetPwd(ctx context.Context, user User) error {
|
||||
err := u.db.Model(&User{}).Where("id = ?", user.ID).Updates(map[string]any{"pwd": user.Pwd, "salt": user.Salt}).Error
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,10 +9,4 @@ type UserService interface {
|
|||
Add(ctx context.Context, info *models.AddInfo) (id uint, err error)
|
||||
Login(ctx context.Context, lInfo models.LoginInfo) error
|
||||
Logout(ctx context.Context) error
|
||||
Modify(ctx context.Context, mInfo *models.ModifyInfo) error
|
||||
Disable(ctx context.Context) error
|
||||
Enable(ctx context.Context) error
|
||||
Search(ctx context.Context, query *models.Query) ([]models.User, error)
|
||||
ResetPwd(ctx context.Context, req *models.ResetPwdReq) error
|
||||
GetUser(ctx context.Context, req *models.GetUserReq) (user models.User, err error)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -92,7 +92,6 @@ func (u *userService) Add(ctx context.Context, info *models.AddInfo) (id uint, e
|
|||
Email: info.Email,
|
||||
Pwd: pwd,
|
||||
Sex: info.Sex,
|
||||
Salt: salt,
|
||||
CreatedBy: session.Account,
|
||||
}
|
||||
|
||||
|
|
@ -140,115 +139,38 @@ func (u *userService) Logout(ctx context.Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (u *userService) Modify(ctx context.Context, mInfo *models.ModifyInfo) error {
|
||||
func (u *userService) Modify() error {
|
||||
// 获取当前操作用户
|
||||
session, err := contextUtil.GetSession(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 判断修改用户是否是同一个人
|
||||
user, err := u.repo.GetUserByAccount(ctx, session.Account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user.Name = mInfo.Name
|
||||
user.Sex = mInfo.Sex
|
||||
return u.repo.SaveUser(ctx, user)
|
||||
}
|
||||
|
||||
func (u *userService) Disable(ctx context.Context) error {
|
||||
// 获取操作用户
|
||||
session, err := contextUtil.GetSession(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// todo 判断是否有权限
|
||||
|
||||
// 修改对应用户状态
|
||||
return u.repo.SetUserStatus(ctx, session.ID, models.DisableUserStatus)
|
||||
}
|
||||
|
||||
func (u *userService) Enable(ctx context.Context) error {
|
||||
session, err := contextUtil.GetSession(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// todo 判断是否有权限
|
||||
|
||||
// 修改对应用户状态
|
||||
return u.repo.SetUserStatus(ctx, session.ID, models.EnableUserStatus)
|
||||
}
|
||||
|
||||
func (u *userService) Search(ctx context.Context, query *models.Query) ([]models.User, error) {
|
||||
// 获取操作用户
|
||||
_, err := contextUtil.GetSession(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 返回用户列表
|
||||
users, err := u.repo.Search(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := convertUserList(users)
|
||||
return list, err
|
||||
}
|
||||
|
||||
func (u *userService) ResetPwd(ctx context.Context, req *models.ResetPwdReq) error {
|
||||
// 获取操作用户
|
||||
session, err := contextUtil.GetSession(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 判断是否本人操作
|
||||
rUser, err := u.repo.GetUserByAccount(ctx, session.Account)
|
||||
if err != nil && err != gorm.ErrRecordNotFound {
|
||||
return err
|
||||
}
|
||||
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return fmt.Errorf("重置用户不存在")
|
||||
}
|
||||
|
||||
if session.ID != rUser.ID {
|
||||
// 校验权限
|
||||
return fmt.Errorf("没权限")
|
||||
}
|
||||
|
||||
// 判断当前密码是否正确
|
||||
oldPwd := u.sha256(req.OldPwd, rUser.Salt)
|
||||
if oldPwd != rUser.Pwd {
|
||||
return fmt.Errorf("密码错误")
|
||||
}
|
||||
|
||||
rUser.Salt = stringUtil.RandStringRunes(saltLen)
|
||||
rUser.Pwd = u.sha256(req.Pwd, rUser.Salt)
|
||||
err = u.repo.ResetPwd(ctx, rUser)
|
||||
if err != nil {
|
||||
return fmt.Errorf("repo reset pwd fail:%s", err.Error())
|
||||
}
|
||||
|
||||
// 生成新的密码并保存
|
||||
if err = u.tokenRefresher.DeleteToken(rUser.ID); err != nil {
|
||||
return fmt.Errorf("token delete fail:%s", err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *userService) GetUser(ctx context.Context, req *models.GetUserReq) (user models.User, err error) {
|
||||
rUser, err := u.repo.GetUserByAccount(ctx, req.Account)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
user = convertUser(rUser)
|
||||
return
|
||||
func (u *userService) Disable() error {
|
||||
// 获取操作用户
|
||||
// 判断是否有权限
|
||||
// 修改对应用户状态
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *userService) Able() error {
|
||||
// 获取操作用户
|
||||
// 判断是否有权限
|
||||
// 修改对应用户状态
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *userService) Users() error {
|
||||
// 获取操作用户
|
||||
// 判断是否有权限
|
||||
// 返回用户列表
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *userService) ResetPwd() error {
|
||||
// 获取操作用户
|
||||
// 判断是否本人操作
|
||||
// 判断当前密码是否正确
|
||||
// 生成新的密码并保存
|
||||
// 删除旧的缓存
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package user
|
||||
|
||||
import (
|
||||
"busniess-user-center/internal/models"
|
||||
"busniess-user-center/internal/repo"
|
||||
"context"
|
||||
"fmt"
|
||||
|
|
@ -67,25 +66,3 @@ func (u *userService) removeCookie(ctx context.Context) {
|
|||
c.Writer.Header().Add("Set-Cookie", fmt.Sprintf("%s=; Max-Age=0; Path=/;Domain=%s", COOKIE_KEY_ID, domain))
|
||||
}
|
||||
}
|
||||
|
||||
func convertUserList(users []repo.User) []models.User {
|
||||
list := make([]models.User, len(users))
|
||||
for _, item := range users {
|
||||
list = append(list, convertUser(item))
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
func convertUser(user repo.User) models.User {
|
||||
return models.User{
|
||||
Id: user.ID,
|
||||
UserInfo: models.UserInfo{
|
||||
Name: user.Name,
|
||||
Account: user.Account,
|
||||
Mobile: user.Mobile,
|
||||
Email: user.Email,
|
||||
Sex: user.Sex,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,10 +6,6 @@ import (
|
|||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
var (
|
||||
sortMatch = regexp.MustCompile("^[a-zA-z0-9_]+")
|
||||
)
|
||||
|
||||
func tableName(fl validator.FieldLevel) bool {
|
||||
value := fl.Field().String()
|
||||
if value == "" {
|
||||
|
|
@ -22,16 +18,3 @@ func tableName(fl validator.FieldLevel) bool {
|
|||
|
||||
return false
|
||||
}
|
||||
|
||||
func sortVerify(fl validator.FieldLevel) bool {
|
||||
value := fl.Field().String()
|
||||
if value == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
if isOk := sortMatch.MatchString(value); isOk {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -92,10 +92,6 @@ func (c *customValidator) registerValidation() error {
|
|||
return err
|
||||
}
|
||||
|
||||
if err := c.validate.RegisterValidation("sql_sort", sortVerify); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -34,10 +34,6 @@ func RegisterRoute(api *gin.RouterGroup) {
|
|||
api.POST("/add", ginUtil.Wrap(server.Add))
|
||||
api.POST("/login", ginUtil.WrapNoRsp(server.Login))
|
||||
api.POST("/logout", ginUtil.WrapNo(server.Logout))
|
||||
api.POST("/modify", ginUtil.WrapNoRsp(server.Modify))
|
||||
api.GET("/search", ginUtil.Wrap(server.Search))
|
||||
api.GET("/user", ginUtil.Wrap(server.GetUser))
|
||||
api.POST("/reset", ginUtil.Wrap(server.GetUser))
|
||||
}
|
||||
|
||||
func (u *UserServer) Add(ctx context.Context, req *models.AddInfo) (rsp proto.AddResponse, err error) {
|
||||
|
|
@ -68,27 +64,3 @@ func (u *UserServer) Login(ctx context.Context, req *proto.LoginRequest) (err er
|
|||
func (u *UserServer) Logout(ctx context.Context) error {
|
||||
return u.userService.Logout(ctx)
|
||||
}
|
||||
|
||||
func (u *UserServer) Modify(ctx context.Context, req *models.ModifyInfo) error {
|
||||
return u.userService.Modify(ctx, req)
|
||||
}
|
||||
|
||||
func (u *UserServer) Disable(ctx context.Context) error {
|
||||
return u.userService.Disable(ctx)
|
||||
}
|
||||
|
||||
func (u *UserServer) Enable(ctx context.Context) error {
|
||||
return u.userService.Enable(ctx)
|
||||
}
|
||||
|
||||
func (u *UserServer) Search(ctx context.Context, query *models.Query) ([]models.User, error) {
|
||||
return u.userService.Search(ctx, query)
|
||||
}
|
||||
|
||||
func (u *UserServer) GetUser(ctx context.Context, req *models.GetUserReq) (models.User, error) {
|
||||
return u.userService.GetUser(ctx, req)
|
||||
}
|
||||
|
||||
func (u *UserServer) ResetPwd(ctx context.Context, req *models.ResetPwdReq) error {
|
||||
return u.userService.ResetPwd(ctx, req)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue