You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
164 lines
3.7 KiB
Go
164 lines
3.7 KiB
Go
1 month ago
|
package user
|
||
|
|
||
|
import (
|
||
|
"cls/internal/application/crypto"
|
||
|
"cls/internal/domain/user"
|
||
|
"cls/pkg/logger"
|
||
|
"cls/pkg/util/page"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"math/rand"
|
||
|
"regexp"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
ErrInvalidPhone = errors.New("手机号格式不正确")
|
||
|
ErrInvalidVerifyCode = errors.New("验证码不正确")
|
||
|
ErrVerifyCodeExpired = errors.New("验证码已过期")
|
||
|
ErrUserNotFound = errors.New("用户不存在")
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
VerifyCodeKeyPrefix = "sms:verify:code:" // Redis中验证码的key前缀
|
||
|
VerifyCodeExpiration = 5 * time.Minute // 验证码有效期5分钟
|
||
|
)
|
||
|
|
||
|
// UserService 用户业务逻辑
|
||
|
type UserService struct {
|
||
|
repo user.UserRepository
|
||
|
phoneEncryption *crypto.PhoneEncryptionService
|
||
|
passwordHash *crypto.PasswordHashService
|
||
|
log logger.Logger
|
||
|
}
|
||
|
|
||
|
func NewUserService(repo user.UserRepository,
|
||
|
phoneEncryption *crypto.PhoneEncryptionService,
|
||
|
passwordHash *crypto.PasswordHashService, logger logger.New) *UserService {
|
||
|
return &UserService{
|
||
|
repo: repo,
|
||
|
phoneEncryption: phoneEncryption,
|
||
|
passwordHash: passwordHash,
|
||
|
log: logger("panoramic:AccountModule:UserService"),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (u *UserService) SetPassword(phone, pwd string) error {
|
||
|
username := strings.Join(strings.Split(phone, "")[8:], "")
|
||
|
pwd, err := u.passwordHash.Hash(pwd)
|
||
|
if err != nil {
|
||
|
u.log.Error(err)
|
||
|
return err
|
||
|
}
|
||
|
ePhone, err := u.phoneEncryption.Encrypt(phone)
|
||
|
if err != nil {
|
||
|
u.log.Error(err)
|
||
|
return err
|
||
|
}
|
||
|
err = u.repo.SetPassword(username, ePhone, pwd)
|
||
|
if err != nil {
|
||
|
u.log.Error(err)
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// GetUserInfo 获取用户信息
|
||
|
func (u *UserService) GetUserInfo(username string) (*UserDto, error) {
|
||
|
existUser, err := u.repo.FindByUsername(username)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return &UserDto{
|
||
|
Uid: existUser.Id,
|
||
|
Username: existUser.Username,
|
||
|
Phone: existUser.Phone,
|
||
|
Status: existUser.Status,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
func (u *UserService) GetUserGiftCount(ePhone string) (int, error) {
|
||
|
c, err := u.repo.GetUserGiftCount(ePhone)
|
||
|
if err != nil {
|
||
|
u.log.Error(err)
|
||
|
}
|
||
|
return c, nil
|
||
|
}
|
||
|
|
||
|
// FindLoginUser 查找登录用户
|
||
|
func (u *UserService) FindLoginUser(username string) (*UserDto, error) {
|
||
|
existUser, err := u.repo.FindByUsername(username)
|
||
|
if err != nil {
|
||
|
u.log.Error(err)
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return &UserDto{
|
||
|
Uid: existUser.Id,
|
||
|
Username: existUser.Username,
|
||
|
Phone: existUser.Phone,
|
||
|
Status: existUser.Status,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
func (u *UserService) GetUserProfileByPhone(ePhone string) (*UserDto, error) {
|
||
|
user, err := u.repo.FindByPhone(ePhone)
|
||
|
if err != nil {
|
||
|
u.log.Error(err)
|
||
|
return nil, err
|
||
|
}
|
||
|
p, err := u.phoneEncryption.StringPhone(user.Phone)
|
||
|
if err != nil {
|
||
|
u.log.Error(err)
|
||
|
}
|
||
|
return &UserDto{
|
||
|
Uid: user.Id,
|
||
|
Username: user.Username,
|
||
|
Phone: p,
|
||
|
Password: user.Password != "",
|
||
|
Status: user.Status,
|
||
|
GiftCount: user.GiftCount,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
// ListUsers 获取用户列表
|
||
|
func (u *UserService) ListUsers(p *page.Page, params map[string]string) error {
|
||
|
//return u.repo.FindAll(p, params)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// DisableUser 禁用用户
|
||
|
func (u *UserService) DisableUser(id uint64) error {
|
||
|
existUser, err := u.repo.FindByID(id)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
existUser.Status = 0
|
||
|
return u.repo.Save(existUser)
|
||
|
}
|
||
|
|
||
|
// EnableUser 启用用户
|
||
|
func (u *UserService) EnableUser(id uint64) error {
|
||
|
existUser, err := u.repo.FindByID(id)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
existUser.Status = 1
|
||
|
return u.repo.Save(existUser)
|
||
|
}
|
||
|
|
||
|
// isValidPhone 验证手机号格式
|
||
|
func isValidPhone(phone string) bool {
|
||
|
pattern := `^1[3-9]\d{9}$`
|
||
|
reg := regexp.MustCompile(pattern)
|
||
|
return reg.MatchString(phone)
|
||
|
}
|
||
|
|
||
|
// generateVerifyCode 生成6位数字验证码
|
||
|
func generateVerifyCode() string {
|
||
|
rand.Seed(time.Now().UnixNano())
|
||
|
return fmt.Sprintf("%06d", rand.Intn(1000000))
|
||
|
}
|