This commit is contained in:
oneao committed 2025-11-26 17:31:56 +08:00
1 parent a57070f860
commit b9f728812d
22 files changed
+2892 -391

No files matched your search

@@ -7,8 +7,9 @@ import (
"cn/oneao/base-go/ent/userinfo"
"cn/oneao/base-go/utils/jwtx"
"cn/oneao/base-go/utils/response"
"cn/oneao/base-go/utils/routinex"
"context"
"fmt"
"github.com/cloudwego/hertz/pkg/common/hlog"
"time"
auth "cn/oneao/base-go/biz/model/auth"
@@ -27,23 +28,32 @@ func LoginDefault(ctx context.Context, c *app.RequestContext) {
c.String(consts.StatusBadRequest, err.Error())
return
}
// 假设 ctx 已有,req.Account / req.Password 已有
start := time.Now()
// 查询登录用户
loginUser, err := db.Client.UserInfo.
Query().
Where(userinfo.AccountEQ(req.Account),
userinfo.PasswordEQ(req.Password)).
Only(ctx)
duration := time.Since(start) // 耗时
fmt.Printf("登录查询: (耗时: %v)\n", duration)
if err != nil {
c.String(consts.StatusInternalServerError, "账号或密码错误")
response.Fail(c).Message("账号或密码错误").Send()
return
}
// 异步更新登录事件
routinex.Go(func() {
err := db.Client.UserInfo.
Update().
SetLastLoginTime(time.Now()).
Where(userinfo.IDEQ(loginUser.ID)).
Exec(ctx)
if err != nil {
hlog.Errorf("更新用户登录时间失败,UserID=%d, err=%v", loginUser.ID, err)
}
})
// 包装数据
token, err := jwtx.CreateAccessToken(loginUser.ID)
refreshToken, err := jwtx.CreateRefreshToken(loginUser.ID)
@@ -1,27 +0,0 @@
// Code generated by hertz generator.
package hello
import (
"context"
hello "cn/oneao/base-go/biz/model/hertz/hello"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/protocol/consts"
)
// Method1 .
// @router /hello [GET]
func Method1(ctx context.Context, c *app.RequestContext) {
var err error
var req hello.HelloReq
err = c.BindAndValidate(&req)
if err != nil {
c.String(consts.StatusBadRequest, err.Error())
return
}
resp := new(hello.HelloResp)
c.JSON(consts.StatusOK, resp)
}
@@ -1,216 +0,0 @@
// idl/hello/hello.proto
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.0
// protoc v6.33.1
// source: hello.proto
package hello
import (
_ "cn/oneao/base-go/biz/model/api"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type HelloReq struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Name string `protobuf:"bytes,1,opt,name=Name,proto3" json:"Name" query:"name"`
}
func (x *HelloReq) Reset() {
*x = HelloReq{}
if protoimpl.UnsafeEnabled {
mi := &file_hello_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *HelloReq) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*HelloReq) ProtoMessage() {}
func (x *HelloReq) ProtoReflect() protoreflect.Message {
mi := &file_hello_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use HelloReq.ProtoReflect.Descriptor instead.
func (*HelloReq) Descriptor() ([]byte, []int) {
return file_hello_proto_rawDescGZIP(), []int{0}
}
func (x *HelloReq) GetName() string {
if x != nil {
return x.Name
}
return ""
}
type HelloResp struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
RespBody string `protobuf:"bytes,1,opt,name=RespBody,proto3" form:"RespBody" json:"RespBody" query:"RespBody"`
}
func (x *HelloResp) Reset() {
*x = HelloResp{}
if protoimpl.UnsafeEnabled {
mi := &file_hello_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *HelloResp) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*HelloResp) ProtoMessage() {}
func (x *HelloResp) ProtoReflect() protoreflect.Message {
mi := &file_hello_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use HelloResp.ProtoReflect.Descriptor instead.
func (*HelloResp) Descriptor() ([]byte, []int) {
return file_hello_proto_rawDescGZIP(), []int{1}
}
func (x *HelloResp) GetRespBody() string {
if x != nil {
return x.RespBody
}
return ""
}
var File_hello_proto protoreflect.FileDescriptor
var file_hello_proto_rawDesc = []byte{
0x0a, 0x0b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x68,
0x65, 0x6c, 0x6c, 0x6f, 0x1a, 0x09, 0x61, 0x70, 0x69, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22,
0x28, 0x0a, 0x08, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x71, 0x12, 0x1c, 0x0a, 0x04, 0x4e,
0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x42, 0x08, 0xb2, 0xbb, 0x18, 0x04, 0x6e,
0x61, 0x6d, 0x65, 0x52, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x22, 0x27, 0x0a, 0x09, 0x48, 0x65, 0x6c,
0x6c, 0x6f, 0x52, 0x65, 0x73, 0x70, 0x12, 0x1a, 0x0a, 0x08, 0x52, 0x65, 0x73, 0x70, 0x42, 0x6f,
0x64, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x52, 0x65, 0x73, 0x70, 0x42, 0x6f,
0x64, 0x79, 0x32, 0x48, 0x0a, 0x0c, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x53, 0x65, 0x72, 0x76, 0x69,
0x63, 0x65, 0x12, 0x38, 0x0a, 0x07, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x31, 0x12, 0x0f, 0x2e,
0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x71, 0x1a, 0x10,
0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x73, 0x70,
0x22, 0x0a, 0xca, 0xc1, 0x18, 0x06, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x42, 0x28, 0x5a, 0x26,
0x63, 0x6e, 0x2f, 0x6f, 0x6e, 0x65, 0x61, 0x6f, 0x2f, 0x62, 0x61, 0x73, 0x65, 0x2d, 0x67, 0x6f,
0x2f, 0x62, 0x69, 0x7a, 0x2f, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x2f, 0x68, 0x65, 0x72, 0x74, 0x7a,
0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_hello_proto_rawDescOnce sync.Once
file_hello_proto_rawDescData = file_hello_proto_rawDesc
)
func file_hello_proto_rawDescGZIP() []byte {
file_hello_proto_rawDescOnce.Do(func() {
file_hello_proto_rawDescData = protoimpl.X.CompressGZIP(file_hello_proto_rawDescData)
})
return file_hello_proto_rawDescData
}
var file_hello_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_hello_proto_goTypes = []interface{}{
(*HelloReq)(nil), // 0: hello.HelloReq
(*HelloResp)(nil), // 1: hello.HelloResp
}
var file_hello_proto_depIdxs = []int32{
0, // 0: hello.HelloService.Method1:input_type -> hello.HelloReq
1, // 1: hello.HelloService.Method1:output_type -> hello.HelloResp
1, // [1:2] is the sub-list for method output_type
0, // [0:1] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_hello_proto_init() }
func file_hello_proto_init() {
if File_hello_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_hello_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*HelloReq); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_hello_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*HelloResp); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_hello_proto_rawDesc,
NumEnums: 0,
NumMessages: 2,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_hello_proto_goTypes,
DependencyIndexes: file_hello_proto_depIdxs,
MessageInfos: file_hello_proto_msgTypes,
}.Build()
File_hello_proto = out.File
file_hello_proto_rawDesc = nil
file_hello_proto_goTypes = nil
file_hello_proto_depIdxs = nil
}
@@ -1,21 +0,0 @@
// Code generated by hertz generator. DO NOT EDIT.
package hello
import (
hello "cn/oneao/base-go/biz/handler/hello"
"github.com/cloudwego/hertz/pkg/app/server"
)
/*
This file will register all the routes of the services in the master idl.
And it will update automatically when you use the "update" command for the idl.
So don't modify the contents of the file, or your code will be deleted when it is updated.
*/
// Register register routes based on the IDL 'api.${HTTP Method}' annotation.
func Register(r *server.Hertz) {
root := r.Group("/", rootMw()...)
root.GET("/hello", append(_method1Mw(), hello.Method1)...)
}
@@ -1,17 +0,0 @@
// Code generated by hertz generator.
package hello
import (
"github.com/cloudwego/hertz/pkg/app"
)
func rootMw() []app.HandlerFunc {
// your code...
return nil
}
func _method1Mw() []app.HandlerFunc {
// your code...
return nil
}
@@ -4,14 +4,12 @@ package router
import (
auth "cn/oneao/base-go/biz/router/auth"
hello "cn/oneao/base-go/biz/router/hello"
"github.com/cloudwego/hertz/pkg/app/server"
)
// GeneratedRegister registers routers generated by IDL.
func GeneratedRegister(r *server.Hertz) {
//INSERT_POINT: DO NOT DELETE THIS LINE!
hello.Register(r)
auth.Register(r)
}