change layout
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package config
|
||||
|
||||
type Config struct {
|
||||
Nacos NacosConf
|
||||
}
|
||||
|
||||
type NacosConf struct {
|
||||
Hosts []string
|
||||
NamespaceId string `json:",optional"`
|
||||
Group string `json:",optional"`
|
||||
RegisterIP string `json:",optional"`
|
||||
ConfigID string `json:",optional"`
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package dao
|
||||
|
||||
import "lone-services/pkg/utils"
|
||||
|
||||
type UserCreate struct {
|
||||
Id int64 `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
|
||||
Openid string `gorm:"column:openid" json:"openid"`
|
||||
Username string `gorm:"column:username" json:"username"`
|
||||
Nickname string `gorm:"column:nickname" json:"nickname"`
|
||||
Avatar string `gorm:"column:avatar" json:"avatar"`
|
||||
Mobile string `gorm:"column:mobile" json:"mobile"`
|
||||
Gender uint8 `gorm:"column:gender" json:"gender"`
|
||||
Birthday string `gorm:"column:birthday" json:"birthday"`
|
||||
AppId uint32 `gorm:"column:app_id" json:"app_id"`
|
||||
Status uint8 `gorm:"column:status" json:"status"`
|
||||
IsRegistered uint8 `gorm:"column:is_registered" json:"is_registered"`
|
||||
IsFaceVerified uint8 `gorm:"column:is_face_verified" json:"is_face_verified"`
|
||||
IsProfileCompleted uint8 `gorm:"column:is_profile_completed" json:"is_profile_completed"`
|
||||
OnTrialNum int `gorm:"column:on_trial_num" json:"on_trial_num"`
|
||||
}
|
||||
|
||||
type UserCreateByPwd struct {
|
||||
Id int64 `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
|
||||
Type uint8 `gorm:"column:type" json:"type"`
|
||||
Account string `gorm:"column:account" json:"account"`
|
||||
Password string `gorm:"column:password" json:"password"`
|
||||
Salt string `gorm:"column:salt" json:"salt"`
|
||||
Mobile string `gorm:"column:mobile" json:"mobile"`
|
||||
AppId uint32 `gorm:"column:app_id" json:"app_id"`
|
||||
Status uint8 `gorm:"column:status" json:"status"`
|
||||
IsRegistered uint8 `gorm:"column:is_registered" json:"is_registered"`
|
||||
IsFaceVerified uint8 `gorm:"column:is_face_verified" json:"is_face_verified"`
|
||||
IsProfileCompleted uint8 `gorm:"column:is_profile_completed" json:"is_profile_completed"`
|
||||
OnTrialNum int `gorm:"column:on_trial_num" json:"on_trial_num"`
|
||||
}
|
||||
|
||||
type UserExist struct {
|
||||
Id int64 `gorm:"column:id" json:"id"`
|
||||
}
|
||||
|
||||
type UserInfo struct {
|
||||
Id int64 `gorm:"column:id" json:"id"`
|
||||
Openid string `gorm:"column:openid" json:"openid"`
|
||||
Username string `gorm:"column:username" json:"username"`
|
||||
Nickname string `gorm:"column:nickname" json:"nickname"`
|
||||
Avatar string `gorm:"column:avatar" json:"avatar"`
|
||||
Mobile string `gorm:"column:mobile" json:"mobile"`
|
||||
Gender uint8 `gorm:"column:gender" json:"gender"`
|
||||
Birthday string `gorm:"column:birthday" json:"birthday"`
|
||||
Type uint8 `gorm:"column:type" json:"type"`
|
||||
Account string `gorm:"column:account" json:"account"`
|
||||
AppId uint32 `gorm:"column:app_id" json:"app_id"`
|
||||
Status uint8 `gorm:"column:status" json:"status"`
|
||||
}
|
||||
|
||||
type UserLoginRow struct {
|
||||
UserInfo
|
||||
Salt string `gorm:"column:salt" json:"-"`
|
||||
Password string `gorm:"column:password" json:"-"`
|
||||
}
|
||||
|
||||
type Token struct {
|
||||
Token string `json:"token"`
|
||||
Refresh string `json:"refresh"`
|
||||
Info UserLoginSession `json:"info"`
|
||||
}
|
||||
|
||||
type UserLoginSession struct {
|
||||
Id int64 `json:"id"`
|
||||
Openid string `json:"openid"`
|
||||
Username string `json:"username"`
|
||||
Nickname string `json:"nickname"`
|
||||
Avatar string `json:"avatar"`
|
||||
Mobile string `json:"mobile"`
|
||||
Gender uint8 `json:"gender"`
|
||||
Birthday string `json:"birthday"`
|
||||
Type uint8 `json:"type"`
|
||||
Account string `json:"account"`
|
||||
AppId uint32 `json:"app_id"`
|
||||
LastTime utils.CustomTime `json:"last_time"`
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"lone-services/pkg/log"
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/pkg/validate"
|
||||
user "lone-services/rpc/user/pb"
|
||||
"lone-services/services/user/internal/dao"
|
||||
"lone-services/services/user/internal/model"
|
||||
"lone-services/services/user/internal/svc"
|
||||
"lone-services/services/user/validator"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type LoginLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
|
||||
return &LoginLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *LoginLogic) Login(in *user.LoginReq) (*user.Response, error) {
|
||||
var v validator.LoginValidator
|
||||
if msg := validate.ValidateFromProto(in, &v); msg != utils.StringEmpty {
|
||||
return outResponse(utils.ErrorParams, msg), nil
|
||||
}
|
||||
|
||||
var row dao.UserLoginRow
|
||||
err := model.UserModel{}.Init().GetOne(modelbase.Params{
|
||||
Eq: map[string]string{
|
||||
"openid": v.Openid,
|
||||
"status": utils.StringStatusOk,
|
||||
},
|
||||
}, &row)
|
||||
if err != nil {
|
||||
log.Errorf("login by openid: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
if row.Id < utils.NumberOne {
|
||||
return failResponse(utils.ErrorNotFund), nil
|
||||
}
|
||||
|
||||
mobile := decryptMobile(row.Mobile)
|
||||
session := toSession(row.UserInfo, mobile)
|
||||
ret := buildToken(session)
|
||||
if status := setLogin(ret.Token, ret.Refresh, session); status.Code != utils.Ok.Code {
|
||||
return failResponse(status), nil
|
||||
}
|
||||
return okResponse(ret), nil
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"lone-services/pkg/log"
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/redis"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/pkg/validate"
|
||||
user "lone-services/rpc/user/pb"
|
||||
"lone-services/services/user/internal/dao"
|
||||
"lone-services/services/user/internal/model"
|
||||
"lone-services/services/user/internal/svc"
|
||||
"lone-services/services/user/validator"
|
||||
"strconv"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type RegisterByUserLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewRegisterByUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterByUserLogic {
|
||||
return &RegisterByUserLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *RegisterByUserLogic) RegisterByUser(in *user.RegisterByUserReq) (*user.Response, error) {
|
||||
var v validator.RegisterByUserValidator
|
||||
if msg := validate.ValidateFromProto(in, &v); msg != utils.StringEmpty {
|
||||
return outResponse(utils.ErrorParams, msg), nil
|
||||
}
|
||||
|
||||
codePrefix := utils.StringEmpty
|
||||
if v.Type == utils.NumberTwo {
|
||||
codePrefix = "1:"
|
||||
}
|
||||
|
||||
test := utils.GetConfigBool("sms.sms_test")
|
||||
isTest := test && v.Code == utils.GetConfigString("sms.sms_test_code")
|
||||
codeKey := utils.CodeKey + codePrefix + v.Account
|
||||
code, err := redis.Client.Get(l.ctx, codeKey).Result()
|
||||
if (err != nil || code != v.Code) && !isTest {
|
||||
return outResponse(utils.ErrorMissingParams, "验证码错误"), nil
|
||||
}
|
||||
|
||||
var old dao.UserExist
|
||||
err = model.UserModel{}.Init().GetOne(modelbase.Params{
|
||||
Eq: map[string]string{
|
||||
"account": v.Account,
|
||||
"type": strconv.FormatUint(uint64(v.Type), utils.NumberTen),
|
||||
},
|
||||
}, &old)
|
||||
if err != nil {
|
||||
log.Errorf("registerByUser check account: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
if old.Id > utils.NumberZero {
|
||||
return failResponse(utils.ErrorDataIsExist), nil
|
||||
}
|
||||
|
||||
add := dao.UserCreateByPwd{
|
||||
Type: uint8(v.Type),
|
||||
Account: v.Account,
|
||||
Salt: utils.GetRandString(utils.NumberFive),
|
||||
AppId: v.AppId,
|
||||
Status: utils.NumberOne,
|
||||
IsRegistered: utils.NumberOne,
|
||||
IsFaceVerified: utils.NumberTwo,
|
||||
IsProfileCompleted: utils.NumberTwo,
|
||||
OnTrialNum: utils.NumberOne,
|
||||
}
|
||||
pwd := utils.GetSaltPassword(add.Salt, v.Pwd)
|
||||
add.Password, err = utils.EncryptPassword(pwd)
|
||||
if err != nil {
|
||||
log.Errorf("registerByUser encrypt password: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
if v.Type == utils.NumberOne {
|
||||
encryptMobile, cErr := utils.Crypto{}.AESEncryptECB(v.Account)
|
||||
if cErr != nil {
|
||||
log.Errorf("registerByUser encrypt mobile: %v", cErr)
|
||||
return failResponse(utils.ErrorEncryptAesError), nil
|
||||
}
|
||||
add.Mobile = encryptMobile
|
||||
}
|
||||
|
||||
err = model.UserModel{}.Init().CreateByPwd(&add)
|
||||
if err != nil || add.Id < utils.NumberOne {
|
||||
log.Errorf("registerByUser create: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
redis.Client.Del(l.ctx, codeKey)
|
||||
return okResponse(map[string]any{"id": add.Id}), nil
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"lone-services/pkg/log"
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/pkg/validate"
|
||||
user "lone-services/rpc/user/pb"
|
||||
"lone-services/services/user/internal/dao"
|
||||
"lone-services/services/user/internal/model"
|
||||
"lone-services/services/user/internal/svc"
|
||||
"lone-services/services/user/validator"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type RegisterLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
|
||||
return &RegisterLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *RegisterLogic) Register(in *user.RegisterReq) (*user.Response, error) {
|
||||
var v validator.RegisterValidator
|
||||
if msg := validate.ValidateFromProto(in, &v); msg != utils.StringEmpty {
|
||||
return outResponse(utils.ErrorParams, msg), nil
|
||||
}
|
||||
|
||||
openid := v.Openid
|
||||
if openid == utils.StringEmpty {
|
||||
openid = "mock_" + utils.MD5Encrypt(v.Mobile+utils.Now().String())
|
||||
}
|
||||
|
||||
var exist dao.UserExist
|
||||
err := model.UserModel{}.Init().GetOne(modelbase.Params{
|
||||
Eq: map[string]string{"openid": openid},
|
||||
}, &exist)
|
||||
if err != nil {
|
||||
log.Errorf("register check openid: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
if exist.Id > utils.NumberZero {
|
||||
return failResponse(utils.ErrorExist), nil
|
||||
}
|
||||
|
||||
encryptMobile, cErr := utils.Crypto{}.AESEncryptECB(v.Mobile)
|
||||
if cErr != nil {
|
||||
log.Errorf("register encrypt mobile: %v", cErr)
|
||||
return failResponse(utils.ErrorEncryptAesError), nil
|
||||
}
|
||||
|
||||
add := dao.UserCreate{
|
||||
Openid: openid,
|
||||
Username: v.Username,
|
||||
Nickname: v.Nickname,
|
||||
Avatar: v.Avatar,
|
||||
Mobile: encryptMobile,
|
||||
Gender: uint8(v.Gender),
|
||||
Birthday: v.Birthday,
|
||||
AppId: v.AppId,
|
||||
Status: utils.NumberOne,
|
||||
IsRegistered: utils.NumberOne,
|
||||
IsFaceVerified: utils.NumberTwo,
|
||||
IsProfileCompleted: utils.NumberTwo,
|
||||
OnTrialNum: utils.NumberOne,
|
||||
}
|
||||
err = model.UserModel{}.Init().Create(&add)
|
||||
if err != nil {
|
||||
log.Errorf("register create: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
session := toSession(dao.UserInfo{
|
||||
Id: add.Id,
|
||||
Openid: add.Openid,
|
||||
Username: add.Username,
|
||||
Nickname: add.Nickname,
|
||||
Avatar: add.Avatar,
|
||||
Gender: add.Gender,
|
||||
Birthday: add.Birthday,
|
||||
AppId: add.AppId,
|
||||
Status: add.Status,
|
||||
}, v.Mobile)
|
||||
ret := buildToken(session)
|
||||
if status := setLogin(ret.Token, ret.Refresh, session); status.Code != utils.Ok.Code {
|
||||
return failResponse(status), nil
|
||||
}
|
||||
return okResponse(ret), nil
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"lone-services/pkg/redis"
|
||||
"lone-services/pkg/utils"
|
||||
user "lone-services/rpc/user/pb"
|
||||
"lone-services/services/user/internal/dao"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func okResponse(data any) *user.Response {
|
||||
buf, _ := jsoniter.Marshal(data)
|
||||
return &user.Response{
|
||||
Code: utils.Ok.Code,
|
||||
Msg: utils.Ok.Msg,
|
||||
Data: string(buf),
|
||||
}
|
||||
}
|
||||
|
||||
func failResponse(status utils.Status) *user.Response {
|
||||
return &user.Response{
|
||||
Code: status.Code,
|
||||
Msg: status.Msg,
|
||||
}
|
||||
}
|
||||
|
||||
func outResponse(status utils.Status, msg string) *user.Response {
|
||||
return &user.Response{
|
||||
Code: status.Code,
|
||||
Msg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
func decryptMobile(mobile string) string {
|
||||
if mobile == utils.StringEmpty {
|
||||
return utils.StringEmpty
|
||||
}
|
||||
plain, err := utils.Crypto{}.AESDecryptECB(mobile)
|
||||
if err != nil {
|
||||
return utils.StringEmpty
|
||||
}
|
||||
return plain
|
||||
}
|
||||
|
||||
func toSession(info dao.UserInfo, mobilePlain string) dao.UserLoginSession {
|
||||
return dao.UserLoginSession{
|
||||
Id: info.Id,
|
||||
Openid: info.Openid,
|
||||
Username: info.Username,
|
||||
Nickname: info.Nickname,
|
||||
Avatar: info.Avatar,
|
||||
Mobile: mobilePlain,
|
||||
Gender: info.Gender,
|
||||
Birthday: info.Birthday,
|
||||
Type: info.Type,
|
||||
Account: info.Account,
|
||||
AppId: info.AppId,
|
||||
LastTime: utils.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func buildToken(session dao.UserLoginSession) dao.Token {
|
||||
seed := session.Mobile + session.Openid + utils.Now().String()
|
||||
token := utils.MD5Encrypt(seed)
|
||||
return dao.Token{
|
||||
Token: token,
|
||||
Refresh: utils.MD5Encrypt(seed + token),
|
||||
Info: session,
|
||||
}
|
||||
}
|
||||
|
||||
func setLogin(token, refresh string, session dao.UserLoginSession) utils.Status {
|
||||
expireMin := utils.GetConfigInt("base.login_out_time")
|
||||
expire := time.Duration(expireMin) * time.Minute
|
||||
refreshMin := utils.GetConfigInt("base.login_refresh_out_time")
|
||||
refreshExpire := time.Duration(refreshMin) * time.Minute
|
||||
|
||||
ctx := context.Background()
|
||||
userStr, _ := jsoniter.Marshal(session)
|
||||
|
||||
redisKey := utils.GetLoginKey(utils.LoginTypeUser, token)
|
||||
if err := redis.Client.Set(ctx, redisKey, userStr, expire).Err(); err != nil {
|
||||
return utils.Fail
|
||||
}
|
||||
|
||||
redisKeysKey := utils.GetLoginKeysKey(utils.LoginTypeUser, strconv.FormatInt(session.Id, utils.NumberTen))
|
||||
var loginInfo utils.LoginRedis
|
||||
if oldAuth, err := redis.Client.Get(ctx, redisKeysKey).Result(); err == nil {
|
||||
_ = jsoniter.Unmarshal([]byte(oldAuth), &loginInfo)
|
||||
if len(loginInfo.Token) > utils.NumberOne {
|
||||
redis.Client.Del(ctx, utils.GetLoginKey(utils.LoginTypeUser, loginInfo.Token))
|
||||
}
|
||||
if len(loginInfo.Refresh) > utils.NumberOne {
|
||||
redis.Client.Del(ctx, utils.GetLoginRefreshKey(utils.LoginTypeUser, loginInfo.Refresh))
|
||||
}
|
||||
}
|
||||
|
||||
loginInfo.Token = token
|
||||
loginInfo.Refresh = refresh
|
||||
loginInfo.Info = userStr
|
||||
newStr, _ := jsoniter.Marshal(loginInfo)
|
||||
if err := redis.Client.Set(ctx, redisKeysKey, newStr, refreshExpire).Err(); err != nil {
|
||||
return utils.Fail
|
||||
}
|
||||
refreshKey := utils.GetLoginRefreshKey(utils.LoginTypeUser, refresh)
|
||||
if err := redis.Client.Set(ctx, refreshKey, strconv.FormatInt(session.Id, utils.NumberTen), refreshExpire).Err(); err != nil {
|
||||
return utils.Fail
|
||||
}
|
||||
return utils.Ok
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/services/user/internal/dao"
|
||||
)
|
||||
|
||||
type UserModel struct {
|
||||
modelbase.Base
|
||||
}
|
||||
|
||||
func (m UserModel) TableName() string {
|
||||
return modelbase.Prefix() + "users"
|
||||
}
|
||||
|
||||
func (m UserModel) Init() UserModel {
|
||||
m.Table = m.TableName()
|
||||
return m
|
||||
}
|
||||
|
||||
func (m UserModel) Create(data *dao.UserCreate) error {
|
||||
return m.Base.Create(data)
|
||||
}
|
||||
|
||||
func (m UserModel) CreateByPwd(data *dao.UserCreateByPwd) error {
|
||||
return m.Base.Create(data)
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.10.1
|
||||
// Source: user.proto
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
user "lone-services/rpc/user/pb"
|
||||
"lone-services/services/user/internal/logic"
|
||||
"lone-services/services/user/internal/svc"
|
||||
)
|
||||
|
||||
type UserServer struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
user.UnimplementedUserServer
|
||||
}
|
||||
|
||||
func NewUserServer(svcCtx *svc.ServiceContext) *UserServer {
|
||||
return &UserServer{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *UserServer) Register(ctx context.Context, in *user.RegisterReq) (*user.Response, error) {
|
||||
l := logic.NewRegisterLogic(ctx, s.svcCtx)
|
||||
return l.Register(in)
|
||||
}
|
||||
|
||||
func (s *UserServer) RegisterByUser(ctx context.Context, in *user.RegisterByUserReq) (*user.Response, error) {
|
||||
l := logic.NewRegisterByUserLogic(ctx, s.svcCtx)
|
||||
return l.RegisterByUser(in)
|
||||
}
|
||||
|
||||
func (s *UserServer) Login(ctx context.Context, in *user.LoginReq) (*user.Response, error) {
|
||||
l := logic.NewLoginLogic(ctx, s.svcCtx)
|
||||
return l.Login(in)
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/services/user/internal/config"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
DB *gorm.DB
|
||||
Prefix string
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
DB: db,
|
||||
Prefix: utils.GetConfigString("mysql.prefix"),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user