first commit
This commit is contained in:
20
apps/user/internal/config/config.go
Normal file
20
apps/user/internal/config/config.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
AuthJWT AuthConfig // JWT 鉴权相关配置
|
||||
DataSource string // 数据库连接的 DSN 字符串
|
||||
CacheRedis cache.CacheConf // 缓存配置,使用 go-zero 自带的缓存配置结构体
|
||||
SentinelRpc zrpc.RpcClientConf
|
||||
}
|
||||
|
||||
// AuthConfig 用于 JWT 鉴权配置
|
||||
type AuthConfig struct {
|
||||
AccessSecret string // JWT 密钥,用于签发 Token
|
||||
AccessExpire int64 // Token 过期时间,单位为秒
|
||||
}
|
||||
60
apps/user/internal/logic/auth/loginuserlogic.go
Normal file
60
apps/user/internal/logic/auth/loginuserlogic.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package authlogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
jwtx "tianyuan-api/pkg/jwt"
|
||||
|
||||
"tianyuan-api/apps/user/internal/svc"
|
||||
"tianyuan-api/apps/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type LoginUserLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewLoginUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginUserLogic {
|
||||
return &LoginUserLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 登录接口
|
||||
func (l *LoginUserLogic) LoginUser(in *user.LoginReq) (*user.LoginResp, error) {
|
||||
if in.Username == "" || in.Password == "" {
|
||||
return nil, errors.New("用户名或密码不能为空")
|
||||
}
|
||||
// 使用 FindOneByUsername 查找用户
|
||||
users, err := l.svcCtx.UserModel.FindOneByUsername(l.ctx, in.Username)
|
||||
if err != nil {
|
||||
return nil, errors.New("用户未注册")
|
||||
}
|
||||
|
||||
// 验证密码
|
||||
if hashPassword(in.Password) != users.Password {
|
||||
return nil, errors.New("密码错误")
|
||||
}
|
||||
|
||||
// 生成 JWT token,调用封装好的函数
|
||||
token, err := jwtx.GenerateJwtToken(users.Id, l.svcCtx.Config.AuthJWT.AccessSecret, l.svcCtx.Config.AuthJWT.AccessExpire)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &user.LoginResp{
|
||||
Token: token,
|
||||
}, nil
|
||||
}
|
||||
func hashPassword(password string) string {
|
||||
h := sha256.New()
|
||||
h.Write([]byte(password))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
62
apps/user/internal/logic/auth/phoneloginuserlogic.go
Normal file
62
apps/user/internal/logic/auth/phoneloginuserlogic.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package authlogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/redis"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
jwtx "tianyuan-api/pkg/jwt"
|
||||
|
||||
"tianyuan-api/apps/user/internal/svc"
|
||||
"tianyuan-api/apps/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type PhoneLoginUserLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewPhoneLoginUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PhoneLoginUserLogic {
|
||||
return &PhoneLoginUserLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 手机登录接口
|
||||
func (l *PhoneLoginUserLogic) PhoneLoginUser(in *user.PhoneLoginReq) (*user.LoginResp, error) {
|
||||
// 从 Redis 获取验证码
|
||||
savedCode, err := l.svcCtx.Redis.Get(fmt.Sprintf("login:%s", in.Phone))
|
||||
if err != nil {
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, errors.New("验证码已过期")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 验证码不匹配
|
||||
if savedCode != in.Code {
|
||||
return nil, errors.New("验证码不正确")
|
||||
}
|
||||
// 查询用户是否存在,如果不存在则注册新用户
|
||||
users, err := l.svcCtx.UserModel.FindOneByPhone(l.ctx, in.Phone)
|
||||
if errors.Is(err, sqlx.ErrNotFound) {
|
||||
return nil, errors.New("手机号未注册")
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
token, err := jwtx.GenerateJwtToken(users.Id, l.svcCtx.Config.AuthJWT.AccessSecret, l.svcCtx.Config.AuthJWT.AccessExpire)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &user.LoginResp{
|
||||
Token: token,
|
||||
}, nil
|
||||
}
|
||||
136
apps/user/internal/logic/auth/registeruserlogic.go
Normal file
136
apps/user/internal/logic/auth/registeruserlogic.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package authlogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/redis"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"tianyuan-api/apps/user/internal/model"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"tianyuan-api/apps/user/internal/svc"
|
||||
"tianyuan-api/apps/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type RegisterUserLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewRegisterUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterUserLogic {
|
||||
return &RegisterUserLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 注册接口
|
||||
func (l *RegisterUserLogic) RegisterUser(in *user.RegisterReq) (*user.EmptyResponse, error) {
|
||||
// 检查密码是否一致
|
||||
if in.Password != in.ConfirmPassword {
|
||||
return nil, errors.New("密码不一致")
|
||||
}
|
||||
// 检查密码强度
|
||||
if err := checkPasswordStrength(in.Password); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 校验手机号码
|
||||
err := validatePhoneNumber(in.Phone)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 从 Redis 获取验证码
|
||||
savedCode, err := l.svcCtx.Redis.Get(fmt.Sprintf("register:%s", in.Phone))
|
||||
if err != nil {
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, errors.New("验证码已过期")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 验证码不匹配
|
||||
if savedCode != in.Code {
|
||||
return nil, errors.New("验证码不正确")
|
||||
}
|
||||
// 检查用户名是否已经存在
|
||||
existingUser, err := l.svcCtx.UserModel.FindOneByUsername(l.ctx, in.Username)
|
||||
if err != nil && err != sqlc.ErrNotFound {
|
||||
// 如果发生其他错误,返回错误
|
||||
return nil, err
|
||||
}
|
||||
if existingUser != nil {
|
||||
// 用户名已经存在,返回错误
|
||||
return nil, errors.New("用户名已存在,请选择其他用户名")
|
||||
}
|
||||
|
||||
// 检查手机号是否已经存在
|
||||
existingPhone, err := l.svcCtx.UserModel.FindOneByPhone(l.ctx, in.Phone)
|
||||
if err != nil && err != sqlc.ErrNotFound {
|
||||
// 如果发生其他错误,返回错误
|
||||
return nil, err
|
||||
}
|
||||
if existingPhone != nil {
|
||||
// 用户名已经存在,返回错误
|
||||
return nil, errors.New("手机号码已存在,请选择其他用户名")
|
||||
}
|
||||
// 加密密码
|
||||
hashedPassword := hashPassword(in.Password)
|
||||
|
||||
// 构建 Users 结构体
|
||||
users := &model.Users{
|
||||
Username: in.Username,
|
||||
Password: hashedPassword,
|
||||
Phone: in.Phone,
|
||||
AuthStatus: "unverified",
|
||||
}
|
||||
|
||||
// 调用 Insert 方法插入用户数据
|
||||
_, err = l.svcCtx.UserModel.Insert(l.ctx, users)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &user.EmptyResponse{}, nil
|
||||
}
|
||||
|
||||
// 密码强度检查
|
||||
func checkPasswordStrength(password string) error {
|
||||
// 检查密码长度是否不少于8位
|
||||
if len(password) < 8 {
|
||||
return errors.New("密码长度不能少于8位")
|
||||
}
|
||||
|
||||
// 检查密码是否为简单重复的字符(如"11111111" 或 "aaaaaaaa"等)
|
||||
firstChar := password[0]
|
||||
if strings.Count(password, string(firstChar)) == len(password) {
|
||||
return errors.New("密码不能是重复的字符")
|
||||
}
|
||||
|
||||
// 正则表达式:密码必须包含数字或字母,不能是全符号
|
||||
var passwordRegex = `^[A-Za-z0-9]+$`
|
||||
match, _ := regexp.MatchString(passwordRegex, password)
|
||||
if !match {
|
||||
return errors.New("密码只能包含字母和数字")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 校验手机号码的函数
|
||||
func validatePhoneNumber(phone string) error {
|
||||
// 定义正则表达式,匹配中国大陆的手机号格式
|
||||
var phoneRegex = `^1[3-9]\d{9}$`
|
||||
|
||||
// 检查手机号是否匹配正则表达式
|
||||
match, _ := regexp.MatchString(phoneRegex, phone)
|
||||
if !match {
|
||||
return errors.New("手机号码格式不正确")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package enterpriselogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"tianyuan-api/apps/user/internal/model"
|
||||
|
||||
"tianyuan-api/apps/user/internal/svc"
|
||||
"tianyuan-api/apps/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type CreateEnterpriseAuthLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewCreateEnterpriseAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateEnterpriseAuthLogic {
|
||||
return &CreateEnterpriseAuthLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CreateEnterpriseAuthLogic) CreateEnterpriseAuth(in *user.EnterpriseAuthReq) (*user.EmptyResponse, error) {
|
||||
|
||||
users, err := l.svcCtx.UserModel.FindOne(l.ctx, in.UserId)
|
||||
if err != nil || users == nil {
|
||||
return nil, errors.New("查询用户错误")
|
||||
}
|
||||
|
||||
if users.AuthStatus == "approved" || users.AuthStatus == "pending" {
|
||||
return nil, errors.New("当前企业认证已审核通过或正在审核中,无法重复认证")
|
||||
}
|
||||
|
||||
// 构建企业认证对象
|
||||
var enterpriseAuth model.EnterpriseAuth
|
||||
enterpriseAuth.UserId = in.UserId
|
||||
enterpriseAuth.EnterpriseName = in.EnterpriseName
|
||||
enterpriseAuth.EnterpriseContact = in.EnterpriseContact
|
||||
enterpriseAuth.AuthStatus = "pending"
|
||||
enterpriseAuth.BusinessLicense = in.BusinessLicense
|
||||
enterpriseAuth.LegalPerson = in.LegalPerson
|
||||
enterpriseAuth.CreditCode = in.CreditCode
|
||||
users.AuthStatus = "pending"
|
||||
// 使用事务更新企业认证和用户认证状态
|
||||
err = l.svcCtx.EnterpriseAuthModel.TransCtx(l.ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
// 插入和更新操作放在事务中
|
||||
if _, err := l.svcCtx.EnterpriseAuthModel.InsertEnterpriseAuthTrans(ctx, &enterpriseAuth, session); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = l.svcCtx.UserModel.UpdateUserTrans(ctx, users, session)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &user.EmptyResponse{}, nil
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package enterpriselogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tianyuan-api/apps/user/internal/svc"
|
||||
"tianyuan-api/apps/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetPendingEnterpriseLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetPendingEnterpriseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPendingEnterpriseLogic {
|
||||
return &GetPendingEnterpriseLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 获取待审核企业列表
|
||||
func (l *GetPendingEnterpriseLogic) GetPendingEnterprise() (*user.GetPendingEnterpriseResp, error) {
|
||||
// 调用 Model 层获取待审核企业列表
|
||||
enterprises, total, err := l.svcCtx.EnterpriseAuthModel.FindPendingList(l.ctx, in.Page, in.PageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 构造返回的企业列表
|
||||
var list []*user.EnterpriseItem
|
||||
for _, e := range enterprises {
|
||||
list = append(list, &user.EnterpriseItem{
|
||||
Id: e.Id,
|
||||
EnterpriseName: e.EnterpriseName,
|
||||
CreditCode: e.CreditCode,
|
||||
LegalPerson: e.LegalPerson,
|
||||
EnterpriseContact: e.EnterpriseContact,
|
||||
AuthStatus: e.AuthStatus,
|
||||
BusinessLicense: e.BusinessLicense,
|
||||
CreatedAt: e.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
UpdatedAt: e.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
return &user.GetPendingEnterpriseResp{
|
||||
Total: total,
|
||||
List: list,
|
||||
}, nil
|
||||
}
|
||||
91
apps/user/internal/logic/enterprise/reviewenterpriselogic.go
Normal file
91
apps/user/internal/logic/enterprise/reviewenterpriselogic.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package enterpriselogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"tianyuan-api/apps/sentinel/client/secret"
|
||||
"tianyuan-api/apps/user/internal/model"
|
||||
"tianyuan-api/apps/user/internal/svc"
|
||||
"tianyuan-api/apps/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ReviewEnterpriseLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewReviewEnterpriseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReviewEnterpriseLogic {
|
||||
return &ReviewEnterpriseLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 审核企业
|
||||
func (l *ReviewEnterpriseLogic) ReviewEnterprise(in *user.ReviewEnterpriseReq) (*user.EmptyResponse, error) {
|
||||
enterpriseAuth, err := l.svcCtx.EnterpriseAuthModel.FindOne(l.ctx, in.EnterpriseId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if enterpriseAuth == nil {
|
||||
return nil, errors.New("无ID相关认证")
|
||||
}
|
||||
if enterpriseAuth.AuthStatus != "pending" {
|
||||
return nil, errors.New("该认证不需要审核")
|
||||
}
|
||||
enterpriseAuth.AuthStatus = in.Status
|
||||
err = l.svcCtx.EnterpriseAuthModel.TransCtx(l.ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
// 更新 EnterpriseAuth
|
||||
_, updateAuthErr := l.svcCtx.EnterpriseAuthModel.UpdateEnterpriseAuthTrans(ctx, enterpriseAuth, session)
|
||||
if updateAuthErr != nil {
|
||||
return updateAuthErr
|
||||
}
|
||||
|
||||
// 查询用户信息
|
||||
users, findUserErr := l.svcCtx.UserModel.FindOneTrans(l.ctx, enterpriseAuth.UserId, session)
|
||||
if findUserErr != nil {
|
||||
return findUserErr
|
||||
}
|
||||
users.AuthStatus = in.Status
|
||||
// 更新用户信息
|
||||
_, updateUserErr := l.svcCtx.UserModel.UpdateUserTrans(ctx, users, session)
|
||||
if updateUserErr != nil {
|
||||
return updateUserErr
|
||||
}
|
||||
|
||||
if in.Status == "approved" {
|
||||
//审核通过
|
||||
var enterpriseInfo = model.EnterpriseInfo{
|
||||
UserId: enterpriseAuth.UserId,
|
||||
EnterpriseName: enterpriseAuth.EnterpriseName,
|
||||
EnterpriseContact: enterpriseAuth.EnterpriseContact,
|
||||
CreditCode: enterpriseAuth.CreditCode,
|
||||
LegalPerson: enterpriseAuth.LegalPerson,
|
||||
BusinessLicense: enterpriseAuth.BusinessLicense,
|
||||
}
|
||||
_, insertEnterpriseErr := l.svcCtx.EnterpriseModel.InsertEnterpriseInfoTrans(l.ctx, &enterpriseInfo, session)
|
||||
if insertEnterpriseErr != nil {
|
||||
return insertEnterpriseErr
|
||||
}
|
||||
|
||||
_, createSecretErr := l.svcCtx.SecretRpc.CreateSecret(l.ctx, &secret.CreateSecretRequest{
|
||||
UserId: enterpriseAuth.UserId,
|
||||
})
|
||||
if err != nil {
|
||||
return createSecretErr
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user.EmptyResponse{}, nil
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package userlogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
|
||||
"tianyuan-api/apps/user/internal/svc"
|
||||
"tianyuan-api/apps/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetEnterpriseAuthStatusLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetEnterpriseAuthStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetEnterpriseAuthStatusLogic {
|
||||
return &GetEnterpriseAuthStatusLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetEnterpriseAuthStatusLogic) GetEnterpriseAuthStatus(in *user.GetEnterpriseAuthStatusReq) (*user.GetEnterpriseAuthStatusResp, error) {
|
||||
// 查询企业信息
|
||||
enterprise, err := l.svcCtx.EnterpriseModel.FindOneByUserId(l.ctx, in.UserId)
|
||||
if err != nil {
|
||||
if errors.Is(err, sqlc.ErrNotFound) {
|
||||
return &user.GetEnterpriseAuthStatusResp{
|
||||
IsAuth: false,
|
||||
}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if enterprise == nil {
|
||||
return &user.GetEnterpriseAuthStatusResp{
|
||||
IsAuth: false,
|
||||
}, nil
|
||||
}
|
||||
return &user.GetEnterpriseAuthStatusResp{
|
||||
IsAuth: true,
|
||||
}, nil
|
||||
}
|
||||
58
apps/user/internal/logic/user/userinfologic.go
Normal file
58
apps/user/internal/logic/user/userinfologic.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package userlogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"tianyuan-api/apps/user/internal/svc"
|
||||
"tianyuan-api/apps/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UserInfoLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoLogic {
|
||||
return &UserInfoLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
func (l *UserInfoLogic) UserInfo(in *user.UserInfoReq) (*user.UserInfoResp, error) {
|
||||
// 查询用户信息
|
||||
users, err := l.svcCtx.UserModel.FindOne(l.ctx, in.UserId)
|
||||
if err != nil {
|
||||
return nil, errors.New("用户不存在")
|
||||
}
|
||||
|
||||
// 查询企业信息
|
||||
enterprise, err := l.svcCtx.EnterpriseModel.FindOneByUserId(l.ctx, users.Id)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, errors.New("failed to query enterprise auth info")
|
||||
}
|
||||
|
||||
if enterprise == nil {
|
||||
return &user.UserInfoResp{
|
||||
Username: users.Username,
|
||||
Phone: users.Phone,
|
||||
EnterpriseAuthStatus: users.AuthStatus,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 正常返回用户和企业信息
|
||||
return &user.UserInfoResp{
|
||||
Username: users.Username,
|
||||
Phone: users.Phone,
|
||||
EnterpriseAuthStatus: users.AuthStatus,
|
||||
EnterpriseName: enterprise.EnterpriseName,
|
||||
CreditCode: enterprise.CreditCode,
|
||||
LegalPerson: enterprise.LegalPerson,
|
||||
}, nil
|
||||
}
|
||||
102
apps/user/internal/model/enterpriseauthmodel.go
Normal file
102
apps/user/internal/model/enterpriseauthmodel.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
var _ EnterpriseAuthModel = (*customEnterpriseAuthModel)(nil)
|
||||
|
||||
type (
|
||||
// EnterpriseAuthModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customEnterpriseAuthModel.
|
||||
EnterpriseAuthModel interface {
|
||||
enterpriseAuthModel
|
||||
FindLatestByUserId(ctx context.Context, userId int64) (*EnterpriseAuth, error)
|
||||
TransCtx(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error
|
||||
InsertEnterpriseAuthTrans(ctx context.Context, auth *EnterpriseAuth, session sqlx.Session) (sql.Result, error)
|
||||
UpdateEnterpriseAuthTrans(ctx context.Context, auth *EnterpriseAuth, session sqlx.Session) (sql.Result, error)
|
||||
FindPendingList(ctx context.Context, page, pageSize int64) ([]EnterpriseAuth, int64, error)
|
||||
}
|
||||
|
||||
customEnterpriseAuthModel struct {
|
||||
*defaultEnterpriseAuthModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewEnterpriseAuthModel returns a model for the database table.
|
||||
func NewEnterpriseAuthModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) EnterpriseAuthModel {
|
||||
return &customEnterpriseAuthModel{
|
||||
defaultEnterpriseAuthModel: newEnterpriseAuthModel(conn, c, opts...),
|
||||
}
|
||||
}
|
||||
func (m *defaultEnterpriseAuthModel) FindLatestByUserId(ctx context.Context, userId int64) (*EnterpriseAuth, error) {
|
||||
query := fmt.Sprintf("SELECT * FROM %s WHERE user_id = ? ORDER BY created_at DESC LIMIT 1", m.table)
|
||||
var resp EnterpriseAuth
|
||||
err := m.QueryRowsNoCacheCtx(ctx, &resp, query, userId)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sql.ErrNoRows:
|
||||
return nil, sql.ErrNoRows
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) TransCtx(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error {
|
||||
// 使用带 ctx 的事务处理
|
||||
err := m.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
return fn(ctx, session)
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) InsertEnterpriseAuthTrans(ctx context.Context, auth *EnterpriseAuth, session sqlx.Session) (sql.Result, error) {
|
||||
enterpriseAuthIdKey := fmt.Sprintf("%s%v", cacheEnterpriseAuthIdPrefix, auth.Id)
|
||||
|
||||
query := fmt.Sprintf("INSERT INTO %s (user_id, enterprise_name, credit_code, legal_person, business_license, enterprise_contact, auth_status) VALUES (?, ?, ?, ?, ?, ?, ?)", m.table)
|
||||
ret, err := session.ExecCtx(ctx, query, auth.UserId, auth.EnterpriseName, auth.CreditCode, auth.LegalPerson, auth.BusinessLicense, auth.EnterpriseContact, auth.AuthStatus)
|
||||
if err == nil {
|
||||
err = m.DelCacheCtx(ctx, enterpriseAuthIdKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return ret, err
|
||||
}
|
||||
func (m *defaultEnterpriseAuthModel) UpdateEnterpriseAuthTrans(ctx context.Context, auth *EnterpriseAuth, session sqlx.Session) (sql.Result, error) {
|
||||
enterpriseAuthIdKey := fmt.Sprintf("%s%v", cacheEnterpriseAuthIdPrefix, auth.Id)
|
||||
query := fmt.Sprintf("UPDATE %s SET %s WHERE `id` = ?", m.table, enterpriseAuthRowsWithPlaceHolder)
|
||||
ret, err := session.ExecCtx(ctx, query, auth.UserId, auth.EnterpriseName, auth.CreditCode, auth.LegalPerson, auth.BusinessLicense, auth.EnterpriseContact, auth.AuthStatus, auth.Id)
|
||||
if err == nil {
|
||||
err = m.DelCacheCtx(ctx, enterpriseAuthIdKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return ret, err
|
||||
}
|
||||
func (m *defaultEnterpriseAuthModel) FindPendingList(ctx context.Context, page, pageSize int64) ([]EnterpriseAuth, int64, error) {
|
||||
offset := (page - 1) * pageSize
|
||||
var enterprises []EnterpriseAuth
|
||||
|
||||
query := fmt.Sprintf("SELECT * FROM %s WHERE auth_status = 'pending' ORDER BY created_at DESC LIMIT ?,?", m.table)
|
||||
err := m.QueryRowsNoCacheCtx(ctx, &enterprises, query, offset, pageSize)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 查询总数量
|
||||
var total int64
|
||||
countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE auth_status = 'pending'", m.table)
|
||||
err = m.QueryRowNoCacheCtx(ctx, &total, countQuery)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return enterprises, total, nil
|
||||
}
|
||||
119
apps/user/internal/model/enterpriseauthmodel_gen.go
Normal file
119
apps/user/internal/model/enterpriseauthmodel_gen.go
Normal file
@@ -0,0 +1,119 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// versions:
|
||||
// goctl version: 1.7.2
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
)
|
||||
|
||||
var (
|
||||
enterpriseAuthFieldNames = builder.RawFieldNames(&EnterpriseAuth{})
|
||||
enterpriseAuthRows = strings.Join(enterpriseAuthFieldNames, ",")
|
||||
enterpriseAuthRowsExpectAutoSet = strings.Join(stringx.Remove(enterpriseAuthFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||||
enterpriseAuthRowsWithPlaceHolder = strings.Join(stringx.Remove(enterpriseAuthFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||||
|
||||
cacheEnterpriseAuthIdPrefix = "cache:enterpriseAuth:id:"
|
||||
)
|
||||
|
||||
type (
|
||||
enterpriseAuthModel interface {
|
||||
Insert(ctx context.Context, data *EnterpriseAuth) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*EnterpriseAuth, error)
|
||||
Update(ctx context.Context, data *EnterpriseAuth) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
defaultEnterpriseAuthModel struct {
|
||||
sqlc.CachedConn
|
||||
table string
|
||||
}
|
||||
|
||||
EnterpriseAuth struct {
|
||||
Id int64 `db:"id"` // 认证ID
|
||||
UserId int64 `db:"user_id"` // 关联的用户ID
|
||||
EnterpriseName string `db:"enterprise_name"` // 企业名称
|
||||
CreditCode string `db:"credit_code"` // 企业统一信用代码
|
||||
LegalPerson string `db:"legal_person"` // 法人代表
|
||||
BusinessLicense string `db:"business_license"` // 营业执照存储路径
|
||||
EnterpriseContact string `db:"enterprise_contact"` // 企业联系方式
|
||||
AuthStatus string `db:"auth_status"` // 认证状态:unverified=未提交,pending=待审核,approved=审核通过,rejected=审核拒绝
|
||||
CreatedAt time.Time `db:"created_at"` // 认证创建时间
|
||||
UpdatedAt time.Time `db:"updated_at"` // 认证更新时间
|
||||
}
|
||||
)
|
||||
|
||||
func newEnterpriseAuthModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultEnterpriseAuthModel {
|
||||
return &defaultEnterpriseAuthModel{
|
||||
CachedConn: sqlc.NewConn(conn, c, opts...),
|
||||
table: "`enterprise_auth`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) Delete(ctx context.Context, id int64) error {
|
||||
enterpriseAuthIdKey := fmt.Sprintf("%s%v", cacheEnterpriseAuthIdPrefix, id)
|
||||
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
||||
return conn.ExecCtx(ctx, query, id)
|
||||
}, enterpriseAuthIdKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) FindOne(ctx context.Context, id int64) (*EnterpriseAuth, error) {
|
||||
enterpriseAuthIdKey := fmt.Sprintf("%s%v", cacheEnterpriseAuthIdPrefix, id)
|
||||
var resp EnterpriseAuth
|
||||
err := m.QueryRowCtx(ctx, &resp, enterpriseAuthIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", enterpriseAuthRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, id)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) Insert(ctx context.Context, data *EnterpriseAuth) (sql.Result, error) {
|
||||
enterpriseAuthIdKey := fmt.Sprintf("%s%v", cacheEnterpriseAuthIdPrefix, data.Id)
|
||||
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?)", m.table, enterpriseAuthRowsExpectAutoSet)
|
||||
return conn.ExecCtx(ctx, query, data.UserId, data.EnterpriseName, data.CreditCode, data.LegalPerson, data.BusinessLicense, data.EnterpriseContact, data.AuthStatus)
|
||||
}, enterpriseAuthIdKey)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) Update(ctx context.Context, data *EnterpriseAuth) error {
|
||||
enterpriseAuthIdKey := fmt.Sprintf("%s%v", cacheEnterpriseAuthIdPrefix, data.Id)
|
||||
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, enterpriseAuthRowsWithPlaceHolder)
|
||||
return conn.ExecCtx(ctx, query, data.UserId, data.EnterpriseName, data.CreditCode, data.LegalPerson, data.BusinessLicense, data.EnterpriseContact, data.AuthStatus, data.Id)
|
||||
}, enterpriseAuthIdKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) formatPrimary(primary any) string {
|
||||
return fmt.Sprintf("%s%v", cacheEnterpriseAuthIdPrefix, primary)
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", enterpriseAuthRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, primary)
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
60
apps/user/internal/model/enterpriseinfomodel.go
Normal file
60
apps/user/internal/model/enterpriseinfomodel.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
var _ EnterpriseInfoModel = (*customEnterpriseInfoModel)(nil)
|
||||
|
||||
type (
|
||||
// EnterpriseInfoModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customEnterpriseInfoModel.
|
||||
EnterpriseInfoModel interface {
|
||||
enterpriseInfoModel
|
||||
InsertEnterpriseInfoTrans(ctx context.Context, enterpriseInfo *EnterpriseInfo, session sqlx.Session) (sql.Result, error)
|
||||
}
|
||||
|
||||
customEnterpriseInfoModel struct {
|
||||
*defaultEnterpriseInfoModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewEnterpriseInfoModel returns a model for the database table.
|
||||
func NewEnterpriseInfoModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) EnterpriseInfoModel {
|
||||
return &customEnterpriseInfoModel{
|
||||
defaultEnterpriseInfoModel: newEnterpriseInfoModel(conn, c, opts...),
|
||||
}
|
||||
}
|
||||
func (m *defaultEnterpriseInfoModel) InsertEnterpriseInfoTrans(ctx context.Context, enterpriseInfo *EnterpriseInfo, session sqlx.Session) (sql.Result, error) {
|
||||
|
||||
enterpriseInfoCreditCodeKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoCreditCodePrefix, enterpriseInfo.CreditCode)
|
||||
enterpriseInfoEnterpriseNameKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoEnterpriseNamePrefix, enterpriseInfo.EnterpriseName)
|
||||
enterpriseInfoIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoIdPrefix, enterpriseInfo.Id)
|
||||
enterpriseInfoUserIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoUserIdPrefix, enterpriseInfo.UserId)
|
||||
|
||||
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (?, ?, ?, ?, ?, ?)", m.table, enterpriseInfoRowsExpectAutoSet)
|
||||
ret, err := session.ExecCtx(ctx, query, enterpriseInfo.UserId, enterpriseInfo.EnterpriseName, enterpriseInfo.CreditCode, enterpriseInfo.LegalPerson, enterpriseInfo.BusinessLicense, enterpriseInfo.EnterpriseContact)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 2. 更新缓存,保证所有缓存操作成功
|
||||
cacheKeys := []string{enterpriseInfoCreditCodeKey, enterpriseInfoEnterpriseNameKey, enterpriseInfoIdKey, enterpriseInfoUserIdKey}
|
||||
cacheErrors := make([]error, len(cacheKeys))
|
||||
|
||||
cacheErrors[0] = m.DelCacheCtx(ctx, enterpriseInfoCreditCodeKey)
|
||||
cacheErrors[1] = m.DelCacheCtx(ctx, enterpriseInfoEnterpriseNameKey)
|
||||
cacheErrors[2] = m.DelCacheCtx(ctx, enterpriseInfoIdKey)
|
||||
cacheErrors[3] = m.DelCacheCtx(ctx, enterpriseInfoUserIdKey)
|
||||
// 3. 检查缓存操作是否全部成功
|
||||
for _, cacheErr := range cacheErrors {
|
||||
if cacheErr != nil {
|
||||
return nil, cacheErr // 返回第一个缓存更新失败的错误
|
||||
}
|
||||
}
|
||||
return ret, err
|
||||
}
|
||||
203
apps/user/internal/model/enterpriseinfomodel_gen.go
Normal file
203
apps/user/internal/model/enterpriseinfomodel_gen.go
Normal file
@@ -0,0 +1,203 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// versions:
|
||||
// goctl version: 1.7.2
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
)
|
||||
|
||||
var (
|
||||
enterpriseInfoFieldNames = builder.RawFieldNames(&EnterpriseInfo{})
|
||||
enterpriseInfoRows = strings.Join(enterpriseInfoFieldNames, ",")
|
||||
enterpriseInfoRowsExpectAutoSet = strings.Join(stringx.Remove(enterpriseInfoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||||
enterpriseInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(enterpriseInfoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||||
|
||||
cacheEnterpriseInfoIdPrefix = "cache:enterpriseInfo:id:"
|
||||
cacheEnterpriseInfoCreditCodePrefix = "cache:enterpriseInfo:creditCode:"
|
||||
cacheEnterpriseInfoEnterpriseNamePrefix = "cache:enterpriseInfo:enterpriseName:"
|
||||
cacheEnterpriseInfoUserIdPrefix = "cache:enterpriseInfo:userId:"
|
||||
)
|
||||
|
||||
type (
|
||||
enterpriseInfoModel interface {
|
||||
Insert(ctx context.Context, data *EnterpriseInfo) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*EnterpriseInfo, error)
|
||||
FindOneByCreditCode(ctx context.Context, creditCode string) (*EnterpriseInfo, error)
|
||||
FindOneByEnterpriseName(ctx context.Context, enterpriseName string) (*EnterpriseInfo, error)
|
||||
FindOneByUserId(ctx context.Context, userId int64) (*EnterpriseInfo, error)
|
||||
Update(ctx context.Context, data *EnterpriseInfo) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
defaultEnterpriseInfoModel struct {
|
||||
sqlc.CachedConn
|
||||
table string
|
||||
}
|
||||
|
||||
EnterpriseInfo struct {
|
||||
Id int64 `db:"id"` // 企业信息ID
|
||||
UserId int64 `db:"user_id"` // 关联的用户ID
|
||||
EnterpriseName string `db:"enterprise_name"` // 企业名称
|
||||
CreditCode string `db:"credit_code"` // 企业统一信用代码
|
||||
LegalPerson string `db:"legal_person"` // 法人代表
|
||||
BusinessLicense string `db:"business_license"` // 营业执照存储路径
|
||||
EnterpriseContact string `db:"enterprise_contact"` // 企业联系方式
|
||||
CreatedAt time.Time `db:"created_at"` // 企业信息创建时间
|
||||
UpdatedAt time.Time `db:"updated_at"` // 企业信息更新时间
|
||||
}
|
||||
)
|
||||
|
||||
func newEnterpriseInfoModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultEnterpriseInfoModel {
|
||||
return &defaultEnterpriseInfoModel{
|
||||
CachedConn: sqlc.NewConn(conn, c, opts...),
|
||||
table: "`enterprise_info`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) Delete(ctx context.Context, id int64) error {
|
||||
data, err := m.FindOne(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
enterpriseInfoCreditCodeKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoCreditCodePrefix, data.CreditCode)
|
||||
enterpriseInfoEnterpriseNameKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoEnterpriseNamePrefix, data.EnterpriseName)
|
||||
enterpriseInfoIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoIdPrefix, id)
|
||||
enterpriseInfoUserIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoUserIdPrefix, data.UserId)
|
||||
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
||||
return conn.ExecCtx(ctx, query, id)
|
||||
}, enterpriseInfoCreditCodeKey, enterpriseInfoEnterpriseNameKey, enterpriseInfoIdKey, enterpriseInfoUserIdKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) FindOne(ctx context.Context, id int64) (*EnterpriseInfo, error) {
|
||||
enterpriseInfoIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoIdPrefix, id)
|
||||
var resp EnterpriseInfo
|
||||
err := m.QueryRowCtx(ctx, &resp, enterpriseInfoIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", enterpriseInfoRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, id)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) FindOneByCreditCode(ctx context.Context, creditCode string) (*EnterpriseInfo, error) {
|
||||
enterpriseInfoCreditCodeKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoCreditCodePrefix, creditCode)
|
||||
var resp EnterpriseInfo
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, enterpriseInfoCreditCodeKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `credit_code` = ? limit 1", enterpriseInfoRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, creditCode); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, m.queryPrimary)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) FindOneByEnterpriseName(ctx context.Context, enterpriseName string) (*EnterpriseInfo, error) {
|
||||
enterpriseInfoEnterpriseNameKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoEnterpriseNamePrefix, enterpriseName)
|
||||
var resp EnterpriseInfo
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, enterpriseInfoEnterpriseNameKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `enterprise_name` = ? limit 1", enterpriseInfoRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, enterpriseName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, m.queryPrimary)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) FindOneByUserId(ctx context.Context, userId int64) (*EnterpriseInfo, error) {
|
||||
enterpriseInfoUserIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoUserIdPrefix, userId)
|
||||
var resp EnterpriseInfo
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, enterpriseInfoUserIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `user_id` = ? limit 1", enterpriseInfoRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, userId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, m.queryPrimary)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) Insert(ctx context.Context, data *EnterpriseInfo) (sql.Result, error) {
|
||||
enterpriseInfoCreditCodeKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoCreditCodePrefix, data.CreditCode)
|
||||
enterpriseInfoEnterpriseNameKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoEnterpriseNamePrefix, data.EnterpriseName)
|
||||
enterpriseInfoIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoIdPrefix, data.Id)
|
||||
enterpriseInfoUserIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoUserIdPrefix, data.UserId)
|
||||
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?)", m.table, enterpriseInfoRowsExpectAutoSet)
|
||||
return conn.ExecCtx(ctx, query, data.UserId, data.EnterpriseName, data.CreditCode, data.LegalPerson, data.BusinessLicense, data.EnterpriseContact)
|
||||
}, enterpriseInfoCreditCodeKey, enterpriseInfoEnterpriseNameKey, enterpriseInfoIdKey, enterpriseInfoUserIdKey)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) Update(ctx context.Context, newData *EnterpriseInfo) error {
|
||||
data, err := m.FindOne(ctx, newData.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
enterpriseInfoCreditCodeKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoCreditCodePrefix, data.CreditCode)
|
||||
enterpriseInfoEnterpriseNameKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoEnterpriseNamePrefix, data.EnterpriseName)
|
||||
enterpriseInfoIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoIdPrefix, data.Id)
|
||||
enterpriseInfoUserIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoUserIdPrefix, data.UserId)
|
||||
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, enterpriseInfoRowsWithPlaceHolder)
|
||||
return conn.ExecCtx(ctx, query, newData.UserId, newData.EnterpriseName, newData.CreditCode, newData.LegalPerson, newData.BusinessLicense, newData.EnterpriseContact, newData.Id)
|
||||
}, enterpriseInfoCreditCodeKey, enterpriseInfoEnterpriseNameKey, enterpriseInfoIdKey, enterpriseInfoUserIdKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) formatPrimary(primary any) string {
|
||||
return fmt.Sprintf("%s%v", cacheEnterpriseInfoIdPrefix, primary)
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", enterpriseInfoRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, primary)
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
78
apps/user/internal/model/usersmodel.go
Normal file
78
apps/user/internal/model/usersmodel.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
var _ UsersModel = (*customUsersModel)(nil)
|
||||
|
||||
type (
|
||||
// UsersModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customUsersModel.
|
||||
UsersModel interface {
|
||||
usersModel
|
||||
UpdateUserTrans(ctx context.Context, user *Users, session sqlx.Session) (sql.Result, error)
|
||||
FindOneTrans(ctx context.Context, userId int64, session sqlx.Session) (*Users, error)
|
||||
}
|
||||
|
||||
customUsersModel struct {
|
||||
*defaultUsersModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewUsersModel returns a model for the database table.
|
||||
func NewUsersModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) UsersModel {
|
||||
return &customUsersModel{
|
||||
defaultUsersModel: newUsersModel(conn, c, opts...),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) UpdateUserTrans(ctx context.Context, user *Users, session sqlx.Session) (sql.Result, error) {
|
||||
userIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, user.Id)
|
||||
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, user.Phone)
|
||||
usersUsernameKey := fmt.Sprintf("%s%v", cacheUsersUsernamePrefix, user.Username)
|
||||
query := fmt.Sprintf("UPDATE %s SET %s WHERE `id` = ?", m.table, usersRowsWithPlaceHolder)
|
||||
ret, err := session.ExecCtx(ctx, query, user.Username, user.Password, user.Phone, user.AuthStatus, user.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 2. 删除缓存,保证所有缓存操作成功
|
||||
cacheKeys := []string{userIdKey, usersPhoneKey, usersUsernameKey}
|
||||
cacheErrors := make([]error, len(cacheKeys))
|
||||
|
||||
cacheErrors[0] = m.DelCacheCtx(ctx, userIdKey)
|
||||
cacheErrors[1] = m.DelCacheCtx(ctx, usersPhoneKey)
|
||||
cacheErrors[2] = m.DelCacheCtx(ctx, usersUsernameKey)
|
||||
|
||||
// 3. 检查缓存操作是否全部成功
|
||||
for _, cacheErr := range cacheErrors {
|
||||
if cacheErr != nil {
|
||||
return nil, cacheErr // 返回第一个缓存更新失败的错误
|
||||
}
|
||||
}
|
||||
return ret, err
|
||||
}
|
||||
func (m *defaultUsersModel) FindOneTrans(ctx context.Context, userId int64, session sqlx.Session) (*Users, error) {
|
||||
// 定义 SQL 查询语句
|
||||
query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` = ? LIMIT 1", usersRows, m.table)
|
||||
|
||||
var user Users
|
||||
// 在事务上下文中执行查询
|
||||
err := session.QueryRowCtx(ctx, &user, query, userId)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
// 如果没有找到记录,返回 nil 和 ErrNotFound 错误
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 返回查询结果
|
||||
return &user, nil
|
||||
}
|
||||
176
apps/user/internal/model/usersmodel_gen.go
Normal file
176
apps/user/internal/model/usersmodel_gen.go
Normal file
@@ -0,0 +1,176 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// versions:
|
||||
// goctl version: 1.7.2
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
)
|
||||
|
||||
var (
|
||||
usersFieldNames = builder.RawFieldNames(&Users{})
|
||||
usersRows = strings.Join(usersFieldNames, ",")
|
||||
usersRowsExpectAutoSet = strings.Join(stringx.Remove(usersFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||||
usersRowsWithPlaceHolder = strings.Join(stringx.Remove(usersFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||||
|
||||
cacheUsersIdPrefix = "cache:users:id:"
|
||||
cacheUsersPhonePrefix = "cache:users:phone:"
|
||||
cacheUsersUsernamePrefix = "cache:users:username:"
|
||||
)
|
||||
|
||||
type (
|
||||
usersModel interface {
|
||||
Insert(ctx context.Context, data *Users) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*Users, error)
|
||||
FindOneByPhone(ctx context.Context, phone string) (*Users, error)
|
||||
FindOneByUsername(ctx context.Context, username string) (*Users, error)
|
||||
Update(ctx context.Context, data *Users) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
defaultUsersModel struct {
|
||||
sqlc.CachedConn
|
||||
table string
|
||||
}
|
||||
|
||||
Users struct {
|
||||
Id int64 `db:"id"` // 用户ID
|
||||
Username string `db:"username"` // 用户名
|
||||
Password string `db:"password"` // 用户密码
|
||||
Phone string `db:"phone"` // 用户手机号
|
||||
AuthStatus string `db:"auth_status"` // 认证状态:unverified=未提交,pending=待审核,approved=审核通过,rejected=审核拒绝
|
||||
CreatedAt time.Time `db:"created_at"` // 用户创建时间
|
||||
UpdatedAt time.Time `db:"updated_at"` // 用户更新时间
|
||||
}
|
||||
)
|
||||
|
||||
func newUsersModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultUsersModel {
|
||||
return &defaultUsersModel{
|
||||
CachedConn: sqlc.NewConn(conn, c, opts...),
|
||||
table: "`users`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) Delete(ctx context.Context, id int64) error {
|
||||
data, err := m.FindOne(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
usersIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, id)
|
||||
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone)
|
||||
usersUsernameKey := fmt.Sprintf("%s%v", cacheUsersUsernamePrefix, data.Username)
|
||||
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
||||
return conn.ExecCtx(ctx, query, id)
|
||||
}, usersIdKey, usersPhoneKey, usersUsernameKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) FindOne(ctx context.Context, id int64) (*Users, error) {
|
||||
usersIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, id)
|
||||
var resp Users
|
||||
err := m.QueryRowCtx(ctx, &resp, usersIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", usersRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, id)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) FindOneByPhone(ctx context.Context, phone string) (*Users, error) {
|
||||
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, phone)
|
||||
var resp Users
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, usersPhoneKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `phone` = ? limit 1", usersRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, phone); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, m.queryPrimary)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) FindOneByUsername(ctx context.Context, username string) (*Users, error) {
|
||||
usersUsernameKey := fmt.Sprintf("%s%v", cacheUsersUsernamePrefix, username)
|
||||
var resp Users
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, usersUsernameKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `username` = ? limit 1", usersRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, username); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, m.queryPrimary)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) Insert(ctx context.Context, data *Users) (sql.Result, error) {
|
||||
usersIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, data.Id)
|
||||
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone)
|
||||
usersUsernameKey := fmt.Sprintf("%s%v", cacheUsersUsernamePrefix, data.Username)
|
||||
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?)", m.table, usersRowsExpectAutoSet)
|
||||
return conn.ExecCtx(ctx, query, data.Username, data.Password, data.Phone, data.AuthStatus)
|
||||
}, usersIdKey, usersPhoneKey, usersUsernameKey)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) Update(ctx context.Context, newData *Users) error {
|
||||
data, err := m.FindOne(ctx, newData.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
usersIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, data.Id)
|
||||
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone)
|
||||
usersUsernameKey := fmt.Sprintf("%s%v", cacheUsersUsernamePrefix, data.Username)
|
||||
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, usersRowsWithPlaceHolder)
|
||||
return conn.ExecCtx(ctx, query, newData.Username, newData.Password, newData.Phone, newData.AuthStatus, newData.Id)
|
||||
}, usersIdKey, usersPhoneKey, usersUsernameKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) formatPrimary(primary any) string {
|
||||
return fmt.Sprintf("%s%v", cacheUsersIdPrefix, primary)
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", usersRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, primary)
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
5
apps/user/internal/model/vars.go
Normal file
5
apps/user/internal/model/vars.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
var ErrNotFound = sqlx.ErrNotFound
|
||||
42
apps/user/internal/server/auth/authserver.go
Normal file
42
apps/user/internal/server/auth/authserver.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.7.2
|
||||
// Source: user.proto
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tianyuan-api/apps/user/internal/logic/auth"
|
||||
"tianyuan-api/apps/user/internal/svc"
|
||||
"tianyuan-api/apps/user/user"
|
||||
)
|
||||
|
||||
type AuthServer struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
user.UnimplementedAuthServer
|
||||
}
|
||||
|
||||
func NewAuthServer(svcCtx *svc.ServiceContext) *AuthServer {
|
||||
return &AuthServer{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 注册接口
|
||||
func (s *AuthServer) RegisterUser(ctx context.Context, in *user.RegisterReq) (*user.EmptyResponse, error) {
|
||||
l := authlogic.NewRegisterUserLogic(ctx, s.svcCtx)
|
||||
return l.RegisterUser(in)
|
||||
}
|
||||
|
||||
// 登录接口
|
||||
func (s *AuthServer) LoginUser(ctx context.Context, in *user.LoginReq) (*user.LoginResp, error) {
|
||||
l := authlogic.NewLoginUserLogic(ctx, s.svcCtx)
|
||||
return l.LoginUser(in)
|
||||
}
|
||||
|
||||
// 手机登录接口
|
||||
func (s *AuthServer) PhoneLoginUser(ctx context.Context, in *user.PhoneLoginReq) (*user.LoginResp, error) {
|
||||
l := authlogic.NewPhoneLoginUserLogic(ctx, s.svcCtx)
|
||||
return l.PhoneLoginUser(in)
|
||||
}
|
||||
42
apps/user/internal/server/enterprise/enterpriseserver.go
Normal file
42
apps/user/internal/server/enterprise/enterpriseserver.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.7.2
|
||||
// Source: user.proto
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tianyuan-api/apps/user/internal/logic/enterprise"
|
||||
"tianyuan-api/apps/user/internal/svc"
|
||||
"tianyuan-api/apps/user/user"
|
||||
)
|
||||
|
||||
type EnterpriseServer struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
user.UnimplementedEnterpriseServer
|
||||
}
|
||||
|
||||
func NewEnterpriseServer(svcCtx *svc.ServiceContext) *EnterpriseServer {
|
||||
return &EnterpriseServer{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 获取待审核企业列表
|
||||
func (s *EnterpriseServer) GetPendingEnterprise(ctx context.Context, in *user.GetPendingEnterpriseReq) (*user.GetPendingEnterpriseResp, error) {
|
||||
l := enterpriselogic.NewGetPendingEnterpriseLogic(ctx, s.svcCtx)
|
||||
return l.GetPendingEnterprise()
|
||||
}
|
||||
|
||||
// 审核企业
|
||||
func (s *EnterpriseServer) ReviewEnterprise(ctx context.Context, in *user.ReviewEnterpriseReq) (*user.EmptyResponse, error) {
|
||||
l := enterpriselogic.NewReviewEnterpriseLogic(ctx, s.svcCtx)
|
||||
return l.ReviewEnterprise(in)
|
||||
}
|
||||
|
||||
// 提交审核
|
||||
func (s *EnterpriseServer) CreateEnterpriseAuth(ctx context.Context, in *user.EnterpriseAuthReq) (*user.EmptyResponse, error) {
|
||||
l := enterpriselogic.NewCreateEnterpriseAuthLogic(ctx, s.svcCtx)
|
||||
return l.CreateEnterpriseAuth(in)
|
||||
}
|
||||
35
apps/user/internal/server/user/userserver.go
Normal file
35
apps/user/internal/server/user/userserver.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.7.2
|
||||
// Source: user.proto
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tianyuan-api/apps/user/internal/logic/user"
|
||||
"tianyuan-api/apps/user/internal/svc"
|
||||
"tianyuan-api/apps/user/user"
|
||||
)
|
||||
|
||||
type UserServer struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
user.UnimplementedUserServer
|
||||
}
|
||||
|
||||
func NewUserServer(svcCtx *svc.ServiceContext) *UserServer {
|
||||
return &UserServer{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
func (s *UserServer) UserInfo(ctx context.Context, in *user.UserInfoReq) (*user.UserInfoResp, error) {
|
||||
l := userlogic.NewUserInfoLogic(ctx, s.svcCtx)
|
||||
return l.UserInfo(in)
|
||||
}
|
||||
|
||||
func (s *UserServer) GetEnterpriseAuthStatus(ctx context.Context, in *user.GetEnterpriseAuthStatusReq) (*user.GetEnterpriseAuthStatusResp, error) {
|
||||
l := userlogic.NewGetEnterpriseAuthStatusLogic(ctx, s.svcCtx)
|
||||
return l.GetEnterpriseAuthStatus(in)
|
||||
}
|
||||
39
apps/user/internal/svc/servicecontext.go
Normal file
39
apps/user/internal/svc/servicecontext.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"github.com/zeromicro/go-zero/core/stores/redis"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"tianyuan-api/apps/sentinel/sentinel"
|
||||
"tianyuan-api/apps/user/internal/config"
|
||||
"tianyuan-api/apps/user/internal/model"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
Redis *redis.Redis
|
||||
UserModel model.UsersModel // 用户表的模型
|
||||
EnterpriseModel model.EnterpriseInfoModel
|
||||
EnterpriseAuthModel model.EnterpriseAuthModel
|
||||
SecretRpc sentinel.SecretClient
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
db := sqlx.NewMysql(c.DataSource) // 创建数据库连接
|
||||
redisConf := redis.RedisConf{
|
||||
Host: c.CacheRedis[0].Host,
|
||||
Pass: c.CacheRedis[0].Pass,
|
||||
Type: c.CacheRedis[0].Type, // Redis 节点类型,如 "node"
|
||||
}
|
||||
|
||||
// 使用 MustNewRedis 来初始化 Redis 客户端
|
||||
rds := redis.MustNewRedis(redisConf)
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
Redis: rds, // 单独使用的 Redis 客户端
|
||||
UserModel: model.NewUsersModel(db, c.CacheRedis), // 注入UserModel
|
||||
EnterpriseModel: model.NewEnterpriseInfoModel(db, c.CacheRedis),
|
||||
EnterpriseAuthModel: model.NewEnterpriseAuthModel(db, c.CacheRedis),
|
||||
SecretRpc: sentinel.NewSecretClient(zrpc.MustNewClient(c.SentinelRpc).Conn()),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user