first commit
This commit is contained in:
31
apps/user/Dockerfile
Normal file
31
apps/user/Dockerfile
Normal file
@@ -0,0 +1,31 @@
|
||||
FROM golang:alpine AS builder
|
||||
|
||||
LABEL stage=gobuilder
|
||||
|
||||
ENV CGO_ENABLED 0
|
||||
ENV GOPROXY https://goproxy.cn,direct
|
||||
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
|
||||
|
||||
RUN apk update --no-cache && apk add --no-cache tzdata
|
||||
|
||||
WORKDIR /build
|
||||
|
||||
ADD go.mod .
|
||||
ADD go.sum .
|
||||
RUN go mod download
|
||||
COPY . .
|
||||
COPY apps/user/etc /app/etc
|
||||
RUN go build -ldflags="-s -w" -o /app/user apps/user/.\user.go
|
||||
|
||||
|
||||
FROM scratch
|
||||
|
||||
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
|
||||
COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/Shanghai
|
||||
ENV TZ Asia/Shanghai
|
||||
|
||||
WORKDIR /app
|
||||
COPY --from=builder /app/user /app/user
|
||||
COPY --from=builder /app/etc /app/etc
|
||||
|
||||
CMD ["./user", "-f", "etc/user.yaml"]
|
||||
68
apps/user/client/auth/auth.go
Normal file
68
apps/user/client/auth/auth.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.7.2
|
||||
// Source: user.proto
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tianyuan-api/apps/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type (
|
||||
EmptyResponse = user.EmptyResponse
|
||||
EnterpriseAuthReq = user.EnterpriseAuthReq
|
||||
EnterpriseItem = user.EnterpriseItem
|
||||
GetEnterpriseAuthStatusReq = user.GetEnterpriseAuthStatusReq
|
||||
GetEnterpriseAuthStatusResp = user.GetEnterpriseAuthStatusResp
|
||||
GetPendingEnterpriseReq = user.GetPendingEnterpriseReq
|
||||
GetPendingEnterpriseResp = user.GetPendingEnterpriseResp
|
||||
LoginReq = user.LoginReq
|
||||
LoginResp = user.LoginResp
|
||||
PhoneLoginReq = user.PhoneLoginReq
|
||||
RegisterReq = user.RegisterReq
|
||||
ReviewEnterpriseReq = user.ReviewEnterpriseReq
|
||||
UserInfoReq = user.UserInfoReq
|
||||
UserInfoResp = user.UserInfoResp
|
||||
|
||||
Auth interface {
|
||||
// 注册接口
|
||||
RegisterUser(ctx context.Context, in *RegisterReq, opts ...grpc.CallOption) (*EmptyResponse, error)
|
||||
// 登录接口
|
||||
LoginUser(ctx context.Context, in *LoginReq, opts ...grpc.CallOption) (*LoginResp, error)
|
||||
// 手机登录接口
|
||||
PhoneLoginUser(ctx context.Context, in *PhoneLoginReq, opts ...grpc.CallOption) (*LoginResp, error)
|
||||
}
|
||||
|
||||
defaultAuth struct {
|
||||
cli zrpc.Client
|
||||
}
|
||||
)
|
||||
|
||||
func NewAuth(cli zrpc.Client) Auth {
|
||||
return &defaultAuth{
|
||||
cli: cli,
|
||||
}
|
||||
}
|
||||
|
||||
// 注册接口
|
||||
func (m *defaultAuth) RegisterUser(ctx context.Context, in *RegisterReq, opts ...grpc.CallOption) (*EmptyResponse, error) {
|
||||
client := user.NewAuthClient(m.cli.Conn())
|
||||
return client.RegisterUser(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 登录接口
|
||||
func (m *defaultAuth) LoginUser(ctx context.Context, in *LoginReq, opts ...grpc.CallOption) (*LoginResp, error) {
|
||||
client := user.NewAuthClient(m.cli.Conn())
|
||||
return client.LoginUser(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 手机登录接口
|
||||
func (m *defaultAuth) PhoneLoginUser(ctx context.Context, in *PhoneLoginReq, opts ...grpc.CallOption) (*LoginResp, error) {
|
||||
client := user.NewAuthClient(m.cli.Conn())
|
||||
return client.PhoneLoginUser(ctx, in, opts...)
|
||||
}
|
||||
68
apps/user/client/enterprise/enterprise.go
Normal file
68
apps/user/client/enterprise/enterprise.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.7.2
|
||||
// Source: user.proto
|
||||
|
||||
package enterprise
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tianyuan-api/apps/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type (
|
||||
EmptyResponse = user.EmptyResponse
|
||||
EnterpriseAuthReq = user.EnterpriseAuthReq
|
||||
EnterpriseItem = user.EnterpriseItem
|
||||
GetEnterpriseAuthStatusReq = user.GetEnterpriseAuthStatusReq
|
||||
GetEnterpriseAuthStatusResp = user.GetEnterpriseAuthStatusResp
|
||||
GetPendingEnterpriseReq = user.GetPendingEnterpriseReq
|
||||
GetPendingEnterpriseResp = user.GetPendingEnterpriseResp
|
||||
LoginReq = user.LoginReq
|
||||
LoginResp = user.LoginResp
|
||||
PhoneLoginReq = user.PhoneLoginReq
|
||||
RegisterReq = user.RegisterReq
|
||||
ReviewEnterpriseReq = user.ReviewEnterpriseReq
|
||||
UserInfoReq = user.UserInfoReq
|
||||
UserInfoResp = user.UserInfoResp
|
||||
|
||||
Enterprise interface {
|
||||
// 获取待审核企业列表
|
||||
GetPendingEnterprise(ctx context.Context, in *GetPendingEnterpriseReq, opts ...grpc.CallOption) (*GetPendingEnterpriseResp, error)
|
||||
// 审核企业
|
||||
ReviewEnterprise(ctx context.Context, in *ReviewEnterpriseReq, opts ...grpc.CallOption) (*EmptyResponse, error)
|
||||
// 提交审核
|
||||
CreateEnterpriseAuth(ctx context.Context, in *EnterpriseAuthReq, opts ...grpc.CallOption) (*EmptyResponse, error)
|
||||
}
|
||||
|
||||
defaultEnterprise struct {
|
||||
cli zrpc.Client
|
||||
}
|
||||
)
|
||||
|
||||
func NewEnterprise(cli zrpc.Client) Enterprise {
|
||||
return &defaultEnterprise{
|
||||
cli: cli,
|
||||
}
|
||||
}
|
||||
|
||||
// 获取待审核企业列表
|
||||
func (m *defaultEnterprise) GetPendingEnterprise(ctx context.Context, in *GetPendingEnterpriseReq, opts ...grpc.CallOption) (*GetPendingEnterpriseResp, error) {
|
||||
client := user.NewEnterpriseClient(m.cli.Conn())
|
||||
return client.GetPendingEnterprise(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 审核企业
|
||||
func (m *defaultEnterprise) ReviewEnterprise(ctx context.Context, in *ReviewEnterpriseReq, opts ...grpc.CallOption) (*EmptyResponse, error) {
|
||||
client := user.NewEnterpriseClient(m.cli.Conn())
|
||||
return client.ReviewEnterprise(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 提交审核
|
||||
func (m *defaultEnterprise) CreateEnterpriseAuth(ctx context.Context, in *EnterpriseAuthReq, opts ...grpc.CallOption) (*EmptyResponse, error) {
|
||||
client := user.NewEnterpriseClient(m.cli.Conn())
|
||||
return client.CreateEnterpriseAuth(ctx, in, opts...)
|
||||
}
|
||||
58
apps/user/client/user/user.go
Normal file
58
apps/user/client/user/user.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.7.2
|
||||
// Source: user.proto
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tianyuan-api/apps/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type (
|
||||
EmptyResponse = user.EmptyResponse
|
||||
EnterpriseAuthReq = user.EnterpriseAuthReq
|
||||
EnterpriseItem = user.EnterpriseItem
|
||||
GetEnterpriseAuthStatusReq = user.GetEnterpriseAuthStatusReq
|
||||
GetEnterpriseAuthStatusResp = user.GetEnterpriseAuthStatusResp
|
||||
GetPendingEnterpriseReq = user.GetPendingEnterpriseReq
|
||||
GetPendingEnterpriseResp = user.GetPendingEnterpriseResp
|
||||
LoginReq = user.LoginReq
|
||||
LoginResp = user.LoginResp
|
||||
PhoneLoginReq = user.PhoneLoginReq
|
||||
RegisterReq = user.RegisterReq
|
||||
ReviewEnterpriseReq = user.ReviewEnterpriseReq
|
||||
UserInfoReq = user.UserInfoReq
|
||||
UserInfoResp = user.UserInfoResp
|
||||
|
||||
User interface {
|
||||
// 获取用户信息
|
||||
UserInfo(ctx context.Context, in *UserInfoReq, opts ...grpc.CallOption) (*UserInfoResp, error)
|
||||
GetEnterpriseAuthStatus(ctx context.Context, in *GetEnterpriseAuthStatusReq, opts ...grpc.CallOption) (*GetEnterpriseAuthStatusResp, error)
|
||||
}
|
||||
|
||||
defaultUser struct {
|
||||
cli zrpc.Client
|
||||
}
|
||||
)
|
||||
|
||||
func NewUser(cli zrpc.Client) User {
|
||||
return &defaultUser{
|
||||
cli: cli,
|
||||
}
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
func (m *defaultUser) UserInfo(ctx context.Context, in *UserInfoReq, opts ...grpc.CallOption) (*UserInfoResp, error) {
|
||||
client := user.NewUserClient(m.cli.Conn())
|
||||
return client.UserInfo(ctx, in, opts...)
|
||||
}
|
||||
|
||||
func (m *defaultUser) GetEnterpriseAuthStatus(ctx context.Context, in *GetEnterpriseAuthStatusReq, opts ...grpc.CallOption) (*GetEnterpriseAuthStatusResp, error) {
|
||||
client := user.NewUserClient(m.cli.Conn())
|
||||
return client.GetEnterpriseAuthStatus(ctx, in, opts...)
|
||||
}
|
||||
19
apps/user/etc/user.yaml
Normal file
19
apps/user/etc/user.yaml
Normal file
@@ -0,0 +1,19 @@
|
||||
Name: user.rpc
|
||||
ListenOn: 0.0.0.0:11001
|
||||
Etcd:
|
||||
Hosts:
|
||||
- 127.0.0.1:2379
|
||||
Key: user.rpc
|
||||
DataSource: "tianyuanapi:g3h98u0291j@tcp(127.0.0.1:3307)/tianyuanapi?charset=utf8mb4&parseTime=True&loc=Local"
|
||||
AuthJWT:
|
||||
AccessSecret: "Mf5Xph3PoyKzVpRw0Zy1+X4uR/tM7JvGMEV/5p2M/tU="
|
||||
AccessExpire: 86400 # JWT过期时间
|
||||
CacheRedis:
|
||||
- Host: "127.0.0.1:6379"
|
||||
Pass: "" # Redis 密码,如果未设置则留空
|
||||
Type: "node" # 单节点模式
|
||||
SentinelRpc:
|
||||
Etcd:
|
||||
Hosts:
|
||||
- 127.0.0.1:2379
|
||||
Key: sentinel.rpc
|
||||
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()),
|
||||
}
|
||||
}
|
||||
43
apps/user/user.go
Normal file
43
apps/user/user.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"tianyuan-api/apps/user/internal/config"
|
||||
authServer "tianyuan-api/apps/user/internal/server/auth"
|
||||
enterpriseServer "tianyuan-api/apps/user/internal/server/enterprise"
|
||||
userServer "tianyuan-api/apps/user/internal/server/user"
|
||||
"tianyuan-api/apps/user/internal/svc"
|
||||
"tianyuan-api/apps/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/core/service"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/user.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
ctx := svc.NewServiceContext(c)
|
||||
|
||||
s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
|
||||
user.RegisterEnterpriseServer(grpcServer, enterpriseServer.NewEnterpriseServer(ctx))
|
||||
user.RegisterAuthServer(grpcServer, authServer.NewAuthServer(ctx))
|
||||
user.RegisterUserServer(grpcServer, userServer.NewUserServer(ctx))
|
||||
|
||||
if c.Mode == service.DevMode || c.Mode == service.TestMode {
|
||||
reflection.Register(grpcServer)
|
||||
}
|
||||
})
|
||||
defer s.Stop()
|
||||
|
||||
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
|
||||
s.Start()
|
||||
}
|
||||
126
apps/user/user.proto
Normal file
126
apps/user/user.proto
Normal file
@@ -0,0 +1,126 @@
|
||||
syntax = "proto3";
|
||||
|
||||
|
||||
option go_package = "./user";
|
||||
|
||||
|
||||
//-------------------------global message -----------------------
|
||||
message EmptyResponse {}
|
||||
|
||||
//------------------------ enterprise ------------------------
|
||||
// 请求获取待审核企业列表
|
||||
message GetPendingEnterpriseReq {
|
||||
int64 page = 1;
|
||||
int64 page_size = 2;
|
||||
}
|
||||
|
||||
|
||||
message EnterpriseItem {
|
||||
int64 id = 1;
|
||||
string enterprise_name = 2;
|
||||
string credit_code = 3;
|
||||
string legal_person = 4;
|
||||
string enterprise_contact = 5;
|
||||
string auth_status = 6;
|
||||
string business_license = 7;
|
||||
string created_at = 8;
|
||||
string updated_at = 9;
|
||||
}
|
||||
|
||||
message GetPendingEnterpriseResp {
|
||||
int64 total = 1;
|
||||
repeated EnterpriseItem list = 2;
|
||||
}
|
||||
|
||||
// 审核企业请求
|
||||
message ReviewEnterpriseReq {
|
||||
int64 enterprise_id = 1;
|
||||
string status = 2;
|
||||
string remarks = 3;
|
||||
}
|
||||
message EnterpriseAuthReq {
|
||||
int64 userId = 1;
|
||||
string enterprise_name = 2; // 企业名称
|
||||
string credit_code = 3; // 信用代码
|
||||
string legal_person = 4; // 法人
|
||||
string business_license = 5; // 营业执照
|
||||
string enterprise_contact = 6; // 联系人
|
||||
}
|
||||
|
||||
// 定义服务
|
||||
service Enterprise {
|
||||
// 获取待审核企业列表
|
||||
rpc GetPendingEnterprise(GetPendingEnterpriseReq) returns (GetPendingEnterpriseResp);
|
||||
|
||||
// 审核企业
|
||||
rpc ReviewEnterprise(ReviewEnterpriseReq) returns (EmptyResponse);
|
||||
|
||||
// 提交审核
|
||||
rpc CreateEnterpriseAuth(EnterpriseAuthReq) returns (EmptyResponse);
|
||||
}
|
||||
|
||||
//------------------------ User Auth ------------------------
|
||||
// 定义注册请求
|
||||
message RegisterReq {
|
||||
string username = 1;
|
||||
string password = 2;
|
||||
string confirmPassword = 3;
|
||||
string phone = 4;
|
||||
string code = 5;
|
||||
}
|
||||
|
||||
// 定义登录请求
|
||||
message LoginReq {
|
||||
string username = 1;
|
||||
string password = 2;
|
||||
}
|
||||
|
||||
// 定义手机登录请求
|
||||
message PhoneLoginReq {
|
||||
string phone = 1;
|
||||
string code = 2;
|
||||
}
|
||||
|
||||
message LoginResp{
|
||||
string token = 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 定义认证服务
|
||||
service Auth {
|
||||
// 注册接口
|
||||
rpc RegisterUser(RegisterReq) returns (EmptyResponse);
|
||||
|
||||
// 登录接口
|
||||
rpc LoginUser(LoginReq) returns (LoginResp);
|
||||
|
||||
// 手机登录接口
|
||||
rpc PhoneLoginUser(PhoneLoginReq) returns (LoginResp);
|
||||
}
|
||||
|
||||
// 定义手机登录请求
|
||||
message UserInfoReq {
|
||||
int64 userId = 1;
|
||||
}
|
||||
|
||||
message UserInfoResp{
|
||||
string username = 1;
|
||||
string phone = 2;
|
||||
string enterpriseAuthStatus = 3;
|
||||
string enterpriseName = 4;
|
||||
string creditCode = 5;
|
||||
string legalPerson = 6;
|
||||
}
|
||||
|
||||
message GetEnterpriseAuthStatusReq {
|
||||
int64 userId = 1;
|
||||
}
|
||||
message GetEnterpriseAuthStatusResp {
|
||||
bool isAuth = 1;
|
||||
}
|
||||
service User {
|
||||
// 获取用户信息
|
||||
rpc UserInfo(UserInfoReq) returns (UserInfoResp);
|
||||
rpc GetEnterpriseAuthStatus(GetEnterpriseAuthStatusReq) returns (GetEnterpriseAuthStatusResp);
|
||||
}
|
||||
37
apps/user/user.sql
Normal file
37
apps/user/user.sql
Normal file
@@ -0,0 +1,37 @@
|
||||
CREATE TABLE users (
|
||||
id INT(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
|
||||
username VARCHAR(100) NOT NULL UNIQUE COMMENT '用户名',
|
||||
password VARCHAR(100) NOT NULL COMMENT '用户密码',
|
||||
phone VARCHAR(15) NOT NULL UNIQUE COMMENT '用户手机号',
|
||||
auth_status ENUM('unverified','pending', 'approved', 'rejected') DEFAULT 'unverified' COMMENT '认证状态:unverified=未提交,pending=待审核,approved=审核通过,rejected=审核拒绝',
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '用户创建时间',
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '用户更新时间',
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表,存储用户的基本信息';
|
||||
|
||||
CREATE TABLE enterprise_info (
|
||||
id INT(11) NOT NULL AUTO_INCREMENT COMMENT '企业信息ID',
|
||||
user_id BIGINT NOT NULL UNIQUE COMMENT '关联的用户ID',
|
||||
enterprise_name VARCHAR(150) NOT NULL UNIQUE COMMENT '企业名称',
|
||||
credit_code VARCHAR(150) NOT NULL UNIQUE COMMENT '企业统一信用代码',
|
||||
legal_person VARCHAR(150) NOT NULL COMMENT '法人代表',
|
||||
business_license VARCHAR(1024) NOT NULL COMMENT '营业执照存储路径',
|
||||
enterprise_contact VARCHAR(100) NOT NULL COMMENT '企业联系方式',
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '企业信息创建时间',
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '企业信息更新时间',
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企业信息表,存储认证通过的企业详细信息';
|
||||
|
||||
CREATE TABLE enterprise_auth (
|
||||
id INT(11) NOT NULL AUTO_INCREMENT COMMENT '认证ID',
|
||||
user_id INT(11) NOT NULL COMMENT '关联的用户ID',
|
||||
enterprise_name VARCHAR(150) NOT NULL COMMENT '企业名称',
|
||||
credit_code VARCHAR(150) NOT NULL COMMENT '企业统一信用代码',
|
||||
legal_person VARCHAR(150) NOT NULL COMMENT '法人代表',
|
||||
business_license VARCHAR(1000) NOT NULL COMMENT '营业执照存储路径',
|
||||
enterprise_contact VARCHAR(100) NOT NULL COMMENT '企业联系方式',
|
||||
auth_status ENUM('unverified','pending', 'approved', 'rejected') DEFAULT 'unverified' COMMENT '认证状态:unverified=未提交,pending=待审核,approved=审核通过,rejected=审核拒绝',
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '认证创建时间',
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '认证更新时间',
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企业认证表,存储企业认证的相关信息';
|
||||
1287
apps/user/user/user.pb.go
Normal file
1287
apps/user/user/user.pb.go
Normal file
File diff suppressed because it is too large
Load Diff
504
apps/user/user/user_grpc.pb.go
Normal file
504
apps/user/user/user_grpc.pb.go
Normal file
@@ -0,0 +1,504 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.4.0
|
||||
// - protoc v3.19.4
|
||||
// source: user.proto
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
// Requires gRPC-Go v1.62.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion8
|
||||
|
||||
const (
|
||||
Enterprise_GetPendingEnterprise_FullMethodName = "/Enterprise/GetPendingEnterprise"
|
||||
Enterprise_ReviewEnterprise_FullMethodName = "/Enterprise/ReviewEnterprise"
|
||||
Enterprise_CreateEnterpriseAuth_FullMethodName = "/Enterprise/CreateEnterpriseAuth"
|
||||
)
|
||||
|
||||
// EnterpriseClient is the client API for Enterprise service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
//
|
||||
// 定义服务
|
||||
type EnterpriseClient interface {
|
||||
// 获取待审核企业列表
|
||||
GetPendingEnterprise(ctx context.Context, in *GetPendingEnterpriseReq, opts ...grpc.CallOption) (*GetPendingEnterpriseResp, error)
|
||||
// 审核企业
|
||||
ReviewEnterprise(ctx context.Context, in *ReviewEnterpriseReq, opts ...grpc.CallOption) (*EmptyResponse, error)
|
||||
// 提交审核
|
||||
CreateEnterpriseAuth(ctx context.Context, in *EnterpriseAuthReq, opts ...grpc.CallOption) (*EmptyResponse, error)
|
||||
}
|
||||
|
||||
type enterpriseClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewEnterpriseClient(cc grpc.ClientConnInterface) EnterpriseClient {
|
||||
return &enterpriseClient{cc}
|
||||
}
|
||||
|
||||
func (c *enterpriseClient) GetPendingEnterprise(ctx context.Context, in *GetPendingEnterpriseReq, opts ...grpc.CallOption) (*GetPendingEnterpriseResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(GetPendingEnterpriseResp)
|
||||
err := c.cc.Invoke(ctx, Enterprise_GetPendingEnterprise_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *enterpriseClient) ReviewEnterprise(ctx context.Context, in *ReviewEnterpriseReq, opts ...grpc.CallOption) (*EmptyResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(EmptyResponse)
|
||||
err := c.cc.Invoke(ctx, Enterprise_ReviewEnterprise_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *enterpriseClient) CreateEnterpriseAuth(ctx context.Context, in *EnterpriseAuthReq, opts ...grpc.CallOption) (*EmptyResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(EmptyResponse)
|
||||
err := c.cc.Invoke(ctx, Enterprise_CreateEnterpriseAuth_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// EnterpriseServer is the server API for Enterprise service.
|
||||
// All implementations must embed UnimplementedEnterpriseServer
|
||||
// for forward compatibility
|
||||
//
|
||||
// 定义服务
|
||||
type EnterpriseServer interface {
|
||||
// 获取待审核企业列表
|
||||
GetPendingEnterprise(context.Context, *GetPendingEnterpriseReq) (*GetPendingEnterpriseResp, error)
|
||||
// 审核企业
|
||||
ReviewEnterprise(context.Context, *ReviewEnterpriseReq) (*EmptyResponse, error)
|
||||
// 提交审核
|
||||
CreateEnterpriseAuth(context.Context, *EnterpriseAuthReq) (*EmptyResponse, error)
|
||||
mustEmbedUnimplementedEnterpriseServer()
|
||||
}
|
||||
|
||||
// UnimplementedEnterpriseServer must be embedded to have forward compatible implementations.
|
||||
type UnimplementedEnterpriseServer struct {
|
||||
}
|
||||
|
||||
func (UnimplementedEnterpriseServer) GetPendingEnterprise(context.Context, *GetPendingEnterpriseReq) (*GetPendingEnterpriseResp, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method GetPendingEnterprise not implemented")
|
||||
}
|
||||
func (UnimplementedEnterpriseServer) ReviewEnterprise(context.Context, *ReviewEnterpriseReq) (*EmptyResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method ReviewEnterprise not implemented")
|
||||
}
|
||||
func (UnimplementedEnterpriseServer) CreateEnterpriseAuth(context.Context, *EnterpriseAuthReq) (*EmptyResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method CreateEnterpriseAuth not implemented")
|
||||
}
|
||||
func (UnimplementedEnterpriseServer) mustEmbedUnimplementedEnterpriseServer() {}
|
||||
|
||||
// UnsafeEnterpriseServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to EnterpriseServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeEnterpriseServer interface {
|
||||
mustEmbedUnimplementedEnterpriseServer()
|
||||
}
|
||||
|
||||
func RegisterEnterpriseServer(s grpc.ServiceRegistrar, srv EnterpriseServer) {
|
||||
s.RegisterService(&Enterprise_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _Enterprise_GetPendingEnterprise_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(GetPendingEnterpriseReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(EnterpriseServer).GetPendingEnterprise(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: Enterprise_GetPendingEnterprise_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(EnterpriseServer).GetPendingEnterprise(ctx, req.(*GetPendingEnterpriseReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _Enterprise_ReviewEnterprise_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(ReviewEnterpriseReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(EnterpriseServer).ReviewEnterprise(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: Enterprise_ReviewEnterprise_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(EnterpriseServer).ReviewEnterprise(ctx, req.(*ReviewEnterpriseReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _Enterprise_CreateEnterpriseAuth_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(EnterpriseAuthReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(EnterpriseServer).CreateEnterpriseAuth(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: Enterprise_CreateEnterpriseAuth_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(EnterpriseServer).CreateEnterpriseAuth(ctx, req.(*EnterpriseAuthReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// Enterprise_ServiceDesc is the grpc.ServiceDesc for Enterprise service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var Enterprise_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "Enterprise",
|
||||
HandlerType: (*EnterpriseServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "GetPendingEnterprise",
|
||||
Handler: _Enterprise_GetPendingEnterprise_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "ReviewEnterprise",
|
||||
Handler: _Enterprise_ReviewEnterprise_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "CreateEnterpriseAuth",
|
||||
Handler: _Enterprise_CreateEnterpriseAuth_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "user.proto",
|
||||
}
|
||||
|
||||
const (
|
||||
Auth_RegisterUser_FullMethodName = "/Auth/RegisterUser"
|
||||
Auth_LoginUser_FullMethodName = "/Auth/LoginUser"
|
||||
Auth_PhoneLoginUser_FullMethodName = "/Auth/PhoneLoginUser"
|
||||
)
|
||||
|
||||
// AuthClient is the client API for Auth service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
//
|
||||
// 定义认证服务
|
||||
type AuthClient interface {
|
||||
// 注册接口
|
||||
RegisterUser(ctx context.Context, in *RegisterReq, opts ...grpc.CallOption) (*EmptyResponse, error)
|
||||
// 登录接口
|
||||
LoginUser(ctx context.Context, in *LoginReq, opts ...grpc.CallOption) (*LoginResp, error)
|
||||
// 手机登录接口
|
||||
PhoneLoginUser(ctx context.Context, in *PhoneLoginReq, opts ...grpc.CallOption) (*LoginResp, error)
|
||||
}
|
||||
|
||||
type authClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewAuthClient(cc grpc.ClientConnInterface) AuthClient {
|
||||
return &authClient{cc}
|
||||
}
|
||||
|
||||
func (c *authClient) RegisterUser(ctx context.Context, in *RegisterReq, opts ...grpc.CallOption) (*EmptyResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(EmptyResponse)
|
||||
err := c.cc.Invoke(ctx, Auth_RegisterUser_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *authClient) LoginUser(ctx context.Context, in *LoginReq, opts ...grpc.CallOption) (*LoginResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(LoginResp)
|
||||
err := c.cc.Invoke(ctx, Auth_LoginUser_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *authClient) PhoneLoginUser(ctx context.Context, in *PhoneLoginReq, opts ...grpc.CallOption) (*LoginResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(LoginResp)
|
||||
err := c.cc.Invoke(ctx, Auth_PhoneLoginUser_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// AuthServer is the server API for Auth service.
|
||||
// All implementations must embed UnimplementedAuthServer
|
||||
// for forward compatibility
|
||||
//
|
||||
// 定义认证服务
|
||||
type AuthServer interface {
|
||||
// 注册接口
|
||||
RegisterUser(context.Context, *RegisterReq) (*EmptyResponse, error)
|
||||
// 登录接口
|
||||
LoginUser(context.Context, *LoginReq) (*LoginResp, error)
|
||||
// 手机登录接口
|
||||
PhoneLoginUser(context.Context, *PhoneLoginReq) (*LoginResp, error)
|
||||
mustEmbedUnimplementedAuthServer()
|
||||
}
|
||||
|
||||
// UnimplementedAuthServer must be embedded to have forward compatible implementations.
|
||||
type UnimplementedAuthServer struct {
|
||||
}
|
||||
|
||||
func (UnimplementedAuthServer) RegisterUser(context.Context, *RegisterReq) (*EmptyResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method RegisterUser not implemented")
|
||||
}
|
||||
func (UnimplementedAuthServer) LoginUser(context.Context, *LoginReq) (*LoginResp, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method LoginUser not implemented")
|
||||
}
|
||||
func (UnimplementedAuthServer) PhoneLoginUser(context.Context, *PhoneLoginReq) (*LoginResp, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method PhoneLoginUser not implemented")
|
||||
}
|
||||
func (UnimplementedAuthServer) mustEmbedUnimplementedAuthServer() {}
|
||||
|
||||
// UnsafeAuthServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to AuthServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeAuthServer interface {
|
||||
mustEmbedUnimplementedAuthServer()
|
||||
}
|
||||
|
||||
func RegisterAuthServer(s grpc.ServiceRegistrar, srv AuthServer) {
|
||||
s.RegisterService(&Auth_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _Auth_RegisterUser_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(RegisterReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(AuthServer).RegisterUser(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: Auth_RegisterUser_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(AuthServer).RegisterUser(ctx, req.(*RegisterReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _Auth_LoginUser_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(LoginReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(AuthServer).LoginUser(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: Auth_LoginUser_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(AuthServer).LoginUser(ctx, req.(*LoginReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _Auth_PhoneLoginUser_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(PhoneLoginReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(AuthServer).PhoneLoginUser(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: Auth_PhoneLoginUser_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(AuthServer).PhoneLoginUser(ctx, req.(*PhoneLoginReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// Auth_ServiceDesc is the grpc.ServiceDesc for Auth service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var Auth_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "Auth",
|
||||
HandlerType: (*AuthServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "RegisterUser",
|
||||
Handler: _Auth_RegisterUser_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "LoginUser",
|
||||
Handler: _Auth_LoginUser_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "PhoneLoginUser",
|
||||
Handler: _Auth_PhoneLoginUser_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "user.proto",
|
||||
}
|
||||
|
||||
const (
|
||||
User_UserInfo_FullMethodName = "/User/UserInfo"
|
||||
User_GetEnterpriseAuthStatus_FullMethodName = "/User/GetEnterpriseAuthStatus"
|
||||
)
|
||||
|
||||
// UserClient is the client API for User service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
type UserClient interface {
|
||||
// 获取用户信息
|
||||
UserInfo(ctx context.Context, in *UserInfoReq, opts ...grpc.CallOption) (*UserInfoResp, error)
|
||||
GetEnterpriseAuthStatus(ctx context.Context, in *GetEnterpriseAuthStatusReq, opts ...grpc.CallOption) (*GetEnterpriseAuthStatusResp, error)
|
||||
}
|
||||
|
||||
type userClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewUserClient(cc grpc.ClientConnInterface) UserClient {
|
||||
return &userClient{cc}
|
||||
}
|
||||
|
||||
func (c *userClient) UserInfo(ctx context.Context, in *UserInfoReq, opts ...grpc.CallOption) (*UserInfoResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(UserInfoResp)
|
||||
err := c.cc.Invoke(ctx, User_UserInfo_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *userClient) GetEnterpriseAuthStatus(ctx context.Context, in *GetEnterpriseAuthStatusReq, opts ...grpc.CallOption) (*GetEnterpriseAuthStatusResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(GetEnterpriseAuthStatusResp)
|
||||
err := c.cc.Invoke(ctx, User_GetEnterpriseAuthStatus_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// UserServer is the server API for User service.
|
||||
// All implementations must embed UnimplementedUserServer
|
||||
// for forward compatibility
|
||||
type UserServer interface {
|
||||
// 获取用户信息
|
||||
UserInfo(context.Context, *UserInfoReq) (*UserInfoResp, error)
|
||||
GetEnterpriseAuthStatus(context.Context, *GetEnterpriseAuthStatusReq) (*GetEnterpriseAuthStatusResp, error)
|
||||
mustEmbedUnimplementedUserServer()
|
||||
}
|
||||
|
||||
// UnimplementedUserServer must be embedded to have forward compatible implementations.
|
||||
type UnimplementedUserServer struct {
|
||||
}
|
||||
|
||||
func (UnimplementedUserServer) UserInfo(context.Context, *UserInfoReq) (*UserInfoResp, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method UserInfo not implemented")
|
||||
}
|
||||
func (UnimplementedUserServer) GetEnterpriseAuthStatus(context.Context, *GetEnterpriseAuthStatusReq) (*GetEnterpriseAuthStatusResp, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method GetEnterpriseAuthStatus not implemented")
|
||||
}
|
||||
func (UnimplementedUserServer) mustEmbedUnimplementedUserServer() {}
|
||||
|
||||
// UnsafeUserServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to UserServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeUserServer interface {
|
||||
mustEmbedUnimplementedUserServer()
|
||||
}
|
||||
|
||||
func RegisterUserServer(s grpc.ServiceRegistrar, srv UserServer) {
|
||||
s.RegisterService(&User_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _User_UserInfo_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(UserInfoReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServer).UserInfo(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: User_UserInfo_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServer).UserInfo(ctx, req.(*UserInfoReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _User_GetEnterpriseAuthStatus_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(GetEnterpriseAuthStatusReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServer).GetEnterpriseAuthStatus(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: User_GetEnterpriseAuthStatus_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServer).GetEnterpriseAuthStatus(ctx, req.(*GetEnterpriseAuthStatusReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// User_ServiceDesc is the grpc.ServiceDesc for User service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var User_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "User",
|
||||
HandlerType: (*UserServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "UserInfo",
|
||||
Handler: _User_UserInfo_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "GetEnterpriseAuthStatus",
|
||||
Handler: _User_GetEnterpriseAuthStatus_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "user.proto",
|
||||
}
|
||||
Reference in New Issue
Block a user