2022-11-28 16:38:30 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crm/common"
|
2023-01-25 16:16:36 +08:00
|
|
|
"crm/dao"
|
2022-11-28 16:38:30 +08:00
|
|
|
"crm/models"
|
|
|
|
|
"crm/response"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
TOKEN_MAX_EXPIRE_TIME = 7 * 24 // Token最长有效期
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type UserService struct {
|
2023-01-25 16:16:36 +08:00
|
|
|
userDao *dao.UserDao
|
|
|
|
|
subscribeDao *dao.SubscribeDao
|
|
|
|
|
noticeDao *dao.NoticeDao
|
2023-01-02 11:00:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewUserService() *UserService {
|
|
|
|
|
userService := UserService{
|
2023-01-25 16:16:36 +08:00
|
|
|
userDao: dao.NewUserDao(),
|
|
|
|
|
subscribeDao: dao.NewSubscribeDao(),
|
|
|
|
|
noticeDao: dao.NewNoticeDao(),
|
2023-01-02 11:00:29 +08:00
|
|
|
}
|
|
|
|
|
return &userService
|
2022-11-28 16:38:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 用户注册
|
|
|
|
|
func (u *UserService) Register(param *models.UserCreateParam) int {
|
|
|
|
|
|
|
|
|
|
// 判断用户是否存在
|
2023-01-25 16:16:36 +08:00
|
|
|
if u.userDao.IsExists(param.Email) {
|
2022-11-28 16:38:30 +08:00
|
|
|
return response.ErrCodeUserHasExist
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 校验验证码是否正确
|
2023-01-25 16:16:36 +08:00
|
|
|
code := u.userDao.GetCode(param.Email)
|
2022-11-28 16:38:30 +08:00
|
|
|
if code != param.Code {
|
|
|
|
|
return response.ErrCodeVerityCodeInvalid
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 对密码进行加密
|
|
|
|
|
password, err := bcrypt.GenerateFromPassword([]byte(param.Password), bcrypt.DefaultCost)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return response.ErrCodeFailed
|
|
|
|
|
}
|
2023-01-25 16:16:36 +08:00
|
|
|
param.Password = string(password)
|
2022-11-28 16:38:30 +08:00
|
|
|
|
|
|
|
|
// 创建用户
|
2023-01-25 16:16:36 +08:00
|
|
|
if err := u.userDao.Create(param); err != nil {
|
|
|
|
|
return response.ErrCodeFailed
|
2022-11-28 16:38:30 +08:00
|
|
|
}
|
2023-01-25 16:16:36 +08:00
|
|
|
// 获取UID
|
|
|
|
|
uid, err := u.userDao.GetUid(param.Email)
|
|
|
|
|
if err != nil {
|
2022-11-28 16:38:30 +08:00
|
|
|
return response.ErrCodeFailed
|
|
|
|
|
}
|
2022-12-16 20:33:18 +08:00
|
|
|
|
|
|
|
|
// 新用户默认订阅免费版
|
2023-01-25 16:16:36 +08:00
|
|
|
if !u.subscribeDao.IsExists(uid) {
|
|
|
|
|
subscribe := models.SubscribeCreateParam{
|
|
|
|
|
Uid: uid,
|
|
|
|
|
Version: 1,
|
|
|
|
|
}
|
|
|
|
|
if err := u.subscribeDao.Create(&subscribe); err != nil {
|
2022-12-16 20:33:18 +08:00
|
|
|
return response.ErrCodeFailed
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-01-25 16:16:36 +08:00
|
|
|
subscribe := models.SubscribeUpdateParam{
|
|
|
|
|
Uid: uid,
|
|
|
|
|
Version: 1,
|
|
|
|
|
}
|
|
|
|
|
if err := u.subscribeDao.Update(&subscribe); err != nil {
|
2022-12-16 20:33:18 +08:00
|
|
|
return response.ErrCodeFailed
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-02 11:00:29 +08:00
|
|
|
// 注册通知
|
2023-01-25 16:16:36 +08:00
|
|
|
u.noticeDao.Create(&models.NoticeCreateParam{
|
2023-01-02 11:00:29 +08:00
|
|
|
Content: REGISTER_NOTICE_TEMPLATE,
|
2023-01-25 16:16:36 +08:00
|
|
|
Creator: uid,
|
2023-01-02 11:00:29 +08:00
|
|
|
})
|
|
|
|
|
|
2022-11-28 16:38:30 +08:00
|
|
|
return response.ErrCodeSuccess
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 用户登录
|
|
|
|
|
func (u *UserService) Login(param *models.UserLoginParam) (*models.UserInfo, int) {
|
|
|
|
|
|
|
|
|
|
// 判断用户是否存在
|
2023-01-25 16:16:36 +08:00
|
|
|
if !u.userDao.IsExists(param.Email) {
|
2022-11-28 16:38:30 +08:00
|
|
|
return nil, response.ErrCodeUserNotExist
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-25 16:16:36 +08:00
|
|
|
// 获取用户信息
|
|
|
|
|
user, err := u.userDao.GetUser(param.Email)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, response.ErrCodeFailed
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 16:38:30 +08:00
|
|
|
// 校验账号密码
|
|
|
|
|
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(param.Password))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, response.ErrCOdeUserEmailOrPass
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 生成并保存Token
|
2022-12-11 20:02:19 +08:00
|
|
|
token, err := common.GenToken(user.Id)
|
2022-11-28 16:38:30 +08:00
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("[error]Login:GenerateToken:%s", err)
|
|
|
|
|
return nil, response.ErrCodeFailed
|
|
|
|
|
}
|
2022-12-16 20:33:18 +08:00
|
|
|
|
2022-11-28 16:38:30 +08:00
|
|
|
userInfo := models.UserInfo{
|
|
|
|
|
Uid: user.Id,
|
|
|
|
|
Token: token,
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-02 11:00:29 +08:00
|
|
|
// 登录通知
|
2023-01-25 16:16:36 +08:00
|
|
|
u.noticeDao.Create(&models.NoticeCreateParam{
|
2023-01-02 11:00:29 +08:00
|
|
|
Content: LOGIN_NOTICE_TEMPLATE,
|
|
|
|
|
Creator: userInfo.Uid,
|
|
|
|
|
})
|
|
|
|
|
|
2022-11-28 16:38:30 +08:00
|
|
|
return &userInfo, response.ErrCodeSuccess
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取验证码
|
|
|
|
|
func (u *UserService) GetVerifyCode(email string) int {
|
|
|
|
|
// 生成6位随机数
|
|
|
|
|
code := common.RandInt(100000, 999998)
|
|
|
|
|
|
|
|
|
|
// 保存验证码
|
2023-01-25 16:16:36 +08:00
|
|
|
if err := u.userDao.SetCode(code, email); err != nil {
|
2022-11-28 16:38:30 +08:00
|
|
|
return response.ErrCodeFailed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发送验证码
|
|
|
|
|
content := fmt.Sprintf("验证码%v,您正在找回密码,切勿向他人泄露。", code)
|
|
|
|
|
err := common.SendMail(email, content)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return response.ErrCodeVerityCodeSendFailed
|
|
|
|
|
}
|
|
|
|
|
return response.ErrCodeSuccess
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 忘记密码
|
|
|
|
|
func (u *UserService) ForgotPass(param *models.UserPassParam) int {
|
|
|
|
|
// 判断用户是否存在
|
2023-01-25 16:16:36 +08:00
|
|
|
if !u.userDao.IsExists(param.Email) {
|
2022-11-28 16:38:30 +08:00
|
|
|
return response.ErrCodeUserNotExist
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 校验验证码是否正确
|
2023-01-25 16:16:36 +08:00
|
|
|
code := u.userDao.GetCode(param.Email)
|
2022-11-28 16:38:30 +08:00
|
|
|
if code != param.Code {
|
|
|
|
|
return response.ErrCodeVerityCodeInvalid
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 对密码进行加密,更新密码
|
|
|
|
|
password, err := bcrypt.GenerateFromPassword([]byte(param.Password), bcrypt.DefaultCost)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return response.ErrCodeFailed
|
|
|
|
|
}
|
2023-01-25 16:16:36 +08:00
|
|
|
if err := u.userDao.UpdatePass(param.Email, string(password)); err != nil {
|
2022-11-28 16:38:30 +08:00
|
|
|
return response.ErrCodeUserPassResetFailed
|
|
|
|
|
}
|
|
|
|
|
return response.ErrCodeSuccess
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 注销账号
|
|
|
|
|
func (u *UserService) Delete(param models.UserDeleteParam) int {
|
|
|
|
|
// 校验验证码是否正确
|
2023-01-25 16:16:36 +08:00
|
|
|
code := u.userDao.GetCode(param.Email)
|
2022-11-28 16:38:30 +08:00
|
|
|
if code != param.Code {
|
|
|
|
|
return response.ErrCodeVerityCodeInvalid
|
|
|
|
|
}
|
2023-01-25 16:16:36 +08:00
|
|
|
// 删除用户所有数据
|
|
|
|
|
if err := u.userDao.DeleteData(param); err != nil {
|
2022-11-28 16:38:30 +08:00
|
|
|
return response.ErrCodeFailed
|
|
|
|
|
}
|
|
|
|
|
return response.ErrCodeSuccess
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取用户信息
|
|
|
|
|
func (u *UserService) GetInfo(uid int64) (*models.UserPersonInfo, int) {
|
2023-01-25 16:16:36 +08:00
|
|
|
userInfo, err := u.userDao.GetInfo(uid)
|
2022-11-28 16:38:30 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, response.ErrCodeFailed
|
|
|
|
|
}
|
2023-01-25 16:16:36 +08:00
|
|
|
return userInfo, response.ErrCodeSuccess
|
2022-11-28 16:38:30 +08:00
|
|
|
}
|