user

package
v0.0.0-...-ad531c1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2023 License: MIT Imports: 11 Imported by: 11

README

User 服务模块

一、提供的接口

1、注册 Register
2、登录 Login
3、用户信息 UserInfo

二、文件目录结构

以此目录文件为例:之后的每一个模块,基本都是这个结构

user                    # 用户模块
├── app.go              # 此模块的名称、此模块model的方法[包括构造方法]
├── http                # 对外暴露的HTTP服务
│   ├── http.go         # UserHandler,需要放入IOC容器中
│   └── user.go         # UserHandler接口所需要的方法
├── impl                # UserServiceImpl 用户模块 业务层 接口实现
│   ├── dao.go          # 可以当作是持久层,这里并没有分那么干净(因为没必要)
│   ├── impl.go         # 定义UserServiceImpl,注意需要加入 GRPC 所需的对象(否则不能作为GRPC接口)
│   ├── user.go         # 实现UserService接口定义的方法(注:接口是在GRPC接口之上继续拓展的)
│   └── user_test.go    # 此模块测试用例【一般用于验证数据库操作是否正确,这里也可以直接从IOC里面取出依赖】
├── pb                  
│   └── user.proto      # protobuf文件,此文件需要定义好接口方法、请求参数结构体、响应参数结构体
├── README.md           # 此模块描述文件
├── user.pb.go          # 利用 protc 生成,里面主要有生成的结构体
└── user_grpc.pb.go     # 利用 protc 生成,里面主要有生成的接口方法
核心编写模块 impl

核心就是面向接口编程的思想。此模块实现(impl)完接口(业务逻辑)后,上层业务就基于 user.ServiceServer 进行编程。

将其简单抽象一下就是这样的:

img.png

user.ServiceServer 定义并把接口实现,可以有很多使用的方式:

  • 其他业务员模块,基于它封装更高一层的业务逻辑,比如发布服务
  • user.ServiceServer 对外暴露:HTTP协议(暴露给用户)
  • user.ServiceServer 对外暴露:GRPC(暴露给内部服务)

相当于按照下面的方式开发:

img.png

我们只需要关注业务实现即可

  • 接口与模型定义利用protobuf生成
  • 实例依赖全交由IoC管理
  • 项目工程化交由Makefile管理
  • 项目配置交由conf管理
  • ....

Documentation

Overview

@Author: Ciusyan 2023/1/25

Index

Constants

View Source
const (
	AppName = "user"
)

Variables

View Source
var File_apps_user_pb_user_proto protoreflect.FileDescriptor
View Source
var Service_ServiceDesc = grpc.ServiceDesc{
	ServiceName: "dousheng.user.Service",
	HandlerType: (*ServiceServer)(nil),
	Methods: []grpc.MethodDesc{
		{
			MethodName: "Register",
			Handler:    _Service_Register_Handler,
		},
		{
			MethodName: "Login",
			Handler:    _Service_Login_Handler,
		},
		{
			MethodName: "UserInfo",
			Handler:    _Service_UserInfo_Handler,
		},
		{
			MethodName: "UserMap",
			Handler:    _Service_UserMap_Handler,
		},
	},
	Streams:  []grpc.StreamDesc{},
	Metadata: "apps/user/pb/user.proto",
}

Service_ServiceDesc is the grpc.ServiceDesc for Service service. It's only intended for direct use with grpc.RegisterService, and not to be introspected or modified (even as a copy)

Functions

func RegisterServiceServer

func RegisterServiceServer(s grpc.ServiceRegistrar, srv ServiceServer)

Types

type LoginAndRegisterRequest

type LoginAndRegisterRequest struct {

	// 用户名【注册的话,最长32个字符】
	// @gotags: json:"username" form:"username" binding:"required,max=32" validate:"required,max=32"
	Username string `` /* 128-byte string literal not displayed */
	// 密码
	// @gotags: json:"password" form:"password" binding:"required,max=32" validate:"required,max=32"
	Password string `` /* 128-byte string literal not displayed */
	// contains filtered or unexported fields
}

用户注册 & 登录 的接口请求 model

func NewLoginAndRegisterRequest

func NewLoginAndRegisterRequest() *LoginAndRegisterRequest

func (*LoginAndRegisterRequest) Descriptor deprecated

func (*LoginAndRegisterRequest) Descriptor() ([]byte, []int)

Deprecated: Use LoginAndRegisterRequest.ProtoReflect.Descriptor instead.

func (*LoginAndRegisterRequest) GetPassword

func (x *LoginAndRegisterRequest) GetPassword() string

func (*LoginAndRegisterRequest) GetUsername

func (x *LoginAndRegisterRequest) GetUsername() string

func (*LoginAndRegisterRequest) Hash

Hash 将敏感信息做Hash

func (*LoginAndRegisterRequest) ProtoMessage

func (*LoginAndRegisterRequest) ProtoMessage()

func (*LoginAndRegisterRequest) ProtoReflect

func (x *LoginAndRegisterRequest) ProtoReflect() protoreflect.Message

func (*LoginAndRegisterRequest) Reset

func (x *LoginAndRegisterRequest) Reset()

func (*LoginAndRegisterRequest) String

func (x *LoginAndRegisterRequest) String() string

func (*LoginAndRegisterRequest) Validate

func (r *LoginAndRegisterRequest) Validate() error

Validate 参数校验

type ServiceClient

type ServiceClient interface {
	// 用户注册
	Register(ctx context.Context, in *LoginAndRegisterRequest, opts ...grpc.CallOption) (*TokenResponse, error)
	// 用户登录
	Login(ctx context.Context, in *LoginAndRegisterRequest, opts ...grpc.CallOption) (*TokenResponse, error)
	// 获取用户信息
	UserInfo(ctx context.Context, in *UserInfoRequest, opts ...grpc.CallOption) (*UserInfoResponse, error)
	// 获取用户信息 map[userId] = User
	UserMap(ctx context.Context, in *UserMapRequest, opts ...grpc.CallOption) (*UserMapResponse, error)
}

ServiceClient is the client API for Service 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.

func NewServiceClient

func NewServiceClient(cc grpc.ClientConnInterface) ServiceClient

type ServiceServer

type ServiceServer interface {
	// 用户注册
	Register(context.Context, *LoginAndRegisterRequest) (*TokenResponse, error)
	// 用户登录
	Login(context.Context, *LoginAndRegisterRequest) (*TokenResponse, error)
	// 获取用户信息
	UserInfo(context.Context, *UserInfoRequest) (*UserInfoResponse, error)
	// 获取用户信息 map[userId] = User
	UserMap(context.Context, *UserMapRequest) (*UserMapResponse, error)
	// contains filtered or unexported methods
}

ServiceServer is the server API for Service service. All implementations must embed UnimplementedServiceServer for forward compatibility

type TokenResponse

type TokenResponse struct {

	// 用户ID
	// @gotags: json:"user_id"
	UserId int64 `protobuf:"varint,3,opt,name=user_id,json=userId,proto3" json:"user_id"`
	// 用户鉴权Token
	// @gotags: json:"token"
	Token string `protobuf:"bytes,4,opt,name=token,proto3" json:"token"`
	// contains filtered or unexported fields
}

用户注册 & 登录 的接口响应 model

func NewTokenResponse

func NewTokenResponse(id int64, token string) *TokenResponse

func (*TokenResponse) Descriptor deprecated

func (*TokenResponse) Descriptor() ([]byte, []int)

Deprecated: Use TokenResponse.ProtoReflect.Descriptor instead.

func (*TokenResponse) GetToken

func (x *TokenResponse) GetToken() string

func (*TokenResponse) GetUserId

func (x *TokenResponse) GetUserId() int64

func (*TokenResponse) ProtoMessage

func (*TokenResponse) ProtoMessage()

func (*TokenResponse) ProtoReflect

func (x *TokenResponse) ProtoReflect() protoreflect.Message

func (*TokenResponse) Reset

func (x *TokenResponse) Reset()

func (*TokenResponse) String

func (x *TokenResponse) String() string

type UnimplementedServiceServer

type UnimplementedServiceServer struct {
}

UnimplementedServiceServer must be embedded to have forward compatible implementations.

func (UnimplementedServiceServer) Login

func (UnimplementedServiceServer) Register

func (UnimplementedServiceServer) UserInfo

func (UnimplementedServiceServer) UserMap

type UnsafeServiceServer

type UnsafeServiceServer interface {
	// contains filtered or unexported methods
}

UnsafeServiceServer may be embedded to opt out of forward compatibility for this service. Use of this interface is not recommended, as added methods to ServiceServer will result in compilation errors.

type User

type User struct {

	// @gotags: json:"id"
	Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id"` // 用户id
	// @gotags: json:"name"
	Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name"` // 用户名称
	// @gotags: json:"follow_count"
	FollowCount *int64 `protobuf:"varint,3,opt,name=follow_count,json=followCount,proto3,oneof" json:"follow_count"` // 关注总数
	// @gotags: json:"follower_count"
	FollowerCount *int64 `protobuf:"varint,4,opt,name=follower_count,json=followerCount,proto3,oneof" json:"follower_count"` // 粉丝总数
	// @gotags: json:"is_follow"
	IsFollow bool `protobuf:"varint,5,opt,name=is_follow,json=isFollow,proto3" json:"is_follow"` // true-已关注,false-未关
	// @gotags: json:"avatar"
	Avatar *string `protobuf:"bytes,6,opt,name=avatar,proto3,oneof" json:"avatar"` //用户头像
	// @gotags: json:"background_image"
	BackgroundImage *string `protobuf:"bytes,7,opt,name=background_image,json=backgroundImage,proto3,oneof" json:"background_image"` //用户个人页顶部大图
	// @gotags: json:"signature"
	Signature *string `protobuf:"bytes,8,opt,name=signature,proto3,oneof" json:"signature"` //个人简介
	// @gotags: json:"total_favorited"
	TotalFavorited *int64 `protobuf:"varint,9,opt,name=total_favorited,json=totalFavorited,proto3,oneof" json:"total_favorited"` //获赞数量
	// @gotags: json:"work_count"
	WorkCount *int64 `protobuf:"varint,10,opt,name=work_count,json=workCount,proto3,oneof" json:"work_count"` //作品数
	// @gotags: json:"favorite_count"
	FavoriteCount *int64 `protobuf:"varint,11,opt,name=favorite_count,json=favoriteCount,proto3,oneof" json:"favorite_count"` //点赞数量
	// contains filtered or unexported fields
}

调用用户信息 时返回的User

func NewDefaultUser

func NewDefaultUser() *User

func (*User) Descriptor deprecated

func (*User) Descriptor() ([]byte, []int)

Deprecated: Use User.ProtoReflect.Descriptor instead.

func (*User) GetAvatar

func (x *User) GetAvatar() string

func (*User) GetBackgroundImage

func (x *User) GetBackgroundImage() string

func (*User) GetFavoriteCount

func (x *User) GetFavoriteCount() int64

func (*User) GetFollowCount

func (x *User) GetFollowCount() int64

func (*User) GetFollowerCount

func (x *User) GetFollowerCount() int64

func (*User) GetId

func (x *User) GetId() int64

func (*User) GetIsFollow

func (x *User) GetIsFollow() bool

func (*User) GetName

func (x *User) GetName() string

func (*User) GetSignature

func (x *User) GetSignature() string

func (*User) GetTotalFavorited

func (x *User) GetTotalFavorited() int64

func (*User) GetWorkCount

func (x *User) GetWorkCount() int64

func (*User) ProtoMessage

func (*User) ProtoMessage()

func (*User) ProtoReflect

func (x *User) ProtoReflect() protoreflect.Message

func (*User) Reset

func (x *User) Reset()

func (*User) String

func (x *User) String() string

type UserInfoRequest

type UserInfoRequest struct {

	// 用户ID
	// @gotags: json:"user_id" form:"user_id" validate:"required" binding:"required"
	UserId int64 `protobuf:"varint,1,opt,name=user_id,json=userId,proto3" json:"user_id" form:"user_id" validate:"required" binding:"required"`
	// 用户鉴权Token [注:这里用 validate,是因为内部方法也必须要用到 Token,并不是所有 token 都 validate]
	// @gotags: json:"token" form:"token" binding:"required" validate:"required"
	Token string `protobuf:"bytes,2,opt,name=token,proto3" json:"token" form:"token" binding:"required" validate:"required"`
	// contains filtered or unexported fields
}

获取用户信息 的接口请求 model

func NewUserInfoRequest

func NewUserInfoRequest() *UserInfoRequest

func (*UserInfoRequest) Descriptor deprecated

func (*UserInfoRequest) Descriptor() ([]byte, []int)

Deprecated: Use UserInfoRequest.ProtoReflect.Descriptor instead.

func (*UserInfoRequest) GetToken

func (x *UserInfoRequest) GetToken() string

func (*UserInfoRequest) GetUserId

func (x *UserInfoRequest) GetUserId() int64

func (*UserInfoRequest) ProtoMessage

func (*UserInfoRequest) ProtoMessage()

func (*UserInfoRequest) ProtoReflect

func (x *UserInfoRequest) ProtoReflect() protoreflect.Message

func (*UserInfoRequest) Reset

func (x *UserInfoRequest) Reset()

func (*UserInfoRequest) String

func (x *UserInfoRequest) String() string

func (*UserInfoRequest) Validate

func (r *UserInfoRequest) Validate() error

type UserInfoResponse

type UserInfoResponse struct {

	// 用户信息
	// @gotags: json:"user"
	User *User `protobuf:"bytes,3,opt,name=user,proto3" json:"user"`
	// contains filtered or unexported fields
}

获取用户信息 的接口响应 model

func NewUserInfoResponse

func NewUserInfoResponse() *UserInfoResponse

func (*UserInfoResponse) Descriptor deprecated

func (*UserInfoResponse) Descriptor() ([]byte, []int)

Deprecated: Use UserInfoResponse.ProtoReflect.Descriptor instead.

func (*UserInfoResponse) GetUser

func (x *UserInfoResponse) GetUser() *User

func (*UserInfoResponse) ProtoMessage

func (*UserInfoResponse) ProtoMessage()

func (*UserInfoResponse) ProtoReflect

func (x *UserInfoResponse) ProtoReflect() protoreflect.Message

func (*UserInfoResponse) Reset

func (x *UserInfoResponse) Reset()

func (*UserInfoResponse) String

func (x *UserInfoResponse) String() string

type UserMapRequest

type UserMapRequest struct {

	// 用户ID 列表
	// @gotags: json:"user_ids"
	UserIds []int64 `protobuf:"varint,1,rep,packed,name=user_ids,json=userIds,proto3" json:"user_ids"`
	// 用户鉴权Token [注:这里用 validate,是因为内部方法也必须要用到 Token,并不是所有 token 都 validate]
	// @gotags: json:"token" validate:"required"
	Token string `protobuf:"bytes,2,opt,name=token,proto3" json:"token" validate:"required"`
	// contains filtered or unexported fields
}

用户IDs

func NewUserMapRequest

func NewUserMapRequest() *UserMapRequest

func (*UserMapRequest) Descriptor deprecated

func (*UserMapRequest) Descriptor() ([]byte, []int)

Deprecated: Use UserMapRequest.ProtoReflect.Descriptor instead.

func (*UserMapRequest) GetToken

func (x *UserMapRequest) GetToken() string

func (*UserMapRequest) GetUserIds

func (x *UserMapRequest) GetUserIds() []int64

func (*UserMapRequest) ProtoMessage

func (*UserMapRequest) ProtoMessage()

func (*UserMapRequest) ProtoReflect

func (x *UserMapRequest) ProtoReflect() protoreflect.Message

func (*UserMapRequest) Reset

func (x *UserMapRequest) Reset()

func (*UserMapRequest) String

func (x *UserMapRequest) String() string

func (*UserMapRequest) Validate

func (r *UserMapRequest) Validate() error

type UserMapResponse

type UserMapResponse struct {

	// 用户列表:map[userId] = User
	// @gotags: json:"user_map"
	UserMap map[int64]*User `` /* 161-byte string literal not displayed */
	// contains filtered or unexported fields
}

用户列表:map[userId] = User

func (*UserMapResponse) Descriptor deprecated

func (*UserMapResponse) Descriptor() ([]byte, []int)

Deprecated: Use UserMapResponse.ProtoReflect.Descriptor instead.

func (*UserMapResponse) GetUserMap

func (x *UserMapResponse) GetUserMap() map[int64]*User

func (*UserMapResponse) ProtoMessage

func (*UserMapResponse) ProtoMessage()

func (*UserMapResponse) ProtoReflect

func (x *UserMapResponse) ProtoReflect() protoreflect.Message

func (*UserMapResponse) Reset

func (x *UserMapResponse) Reset()

func (*UserMapResponse) String

func (x *UserMapResponse) String() string

type UserPo

type UserPo struct {

	// 用户ID
	// @gotags: json:"id" gorm:"id"
	Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id" gorm:"id"`
	// 用户名称
	// @gotags: json:"username" gorm:"username"
	Username string `protobuf:"bytes,2,opt,name=username,proto3" json:"username" gorm:"username"`
	// 用户名称
	// @gotags: json:"password" gorm:"password"
	Password string `protobuf:"bytes,3,opt,name=password,proto3" json:"password" gorm:"password"`
	// contains filtered or unexported fields
}

与数据库对应的 PO 对象

func NewDefaultUserPo

func NewDefaultUserPo() *UserPo

func NewUserPo

func NewUserPo(req *LoginAndRegisterRequest) *UserPo

func (*UserPo) CheckHash

func (u *UserPo) CheckHash(data any) bool

CheckHash 比对Hash

func (*UserPo) Descriptor deprecated

func (*UserPo) Descriptor() ([]byte, []int)

Deprecated: Use UserPo.ProtoReflect.Descriptor instead.

func (*UserPo) GetId

func (x *UserPo) GetId() int64

func (*UserPo) GetPassword

func (x *UserPo) GetPassword() string

func (*UserPo) GetUsername

func (x *UserPo) GetUsername() string

func (*UserPo) Po2vo

func (po *UserPo) Po2vo() *User

func (*UserPo) ProtoMessage

func (*UserPo) ProtoMessage()

func (*UserPo) ProtoReflect

func (x *UserPo) ProtoReflect() protoreflect.Message

func (*UserPo) Reset

func (x *UserPo) Reset()

func (*UserPo) String

func (x *UserPo) String() string

func (*UserPo) TableName

func (*UserPo) TableName() string

TableName 指明表名 -> gorm 参数映射

Directories

Path Synopsis
Package impl @Author: Ciusyan 2023/2/28
Package impl @Author: Ciusyan 2023/2/28

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL