diff --git a/backend/customer/cmd/customer/wire_gen.go b/backend/customer/cmd/customer/wire_gen.go index ddcf1d1..2263642 100644 --- a/backend/customer/cmd/customer/wire_gen.go +++ b/backend/customer/cmd/customer/wire_gen.go @@ -32,7 +32,8 @@ func wireApp(confServer *conf.Server, confData *conf.Data, logger log.Logger) (* greeterUsecase := biz.NewGreeterUsecase(greeterRepo, logger) greeterService := service.NewGreeterService(greeterUsecase) grpcServer := server.NewGRPCServer(confServer, greeterService, logger) - customerService := service.NewCustomerService() + customerData := data.NewCustomerData(dataData) + customerService := service.NewCustomerService(customerData) httpServer := server.NewHTTPServer(confServer, greeterService, customerService, logger) app := newApp(logger, grpcServer, httpServer) return app, func() { diff --git a/backend/customer/configs/config.yaml b/backend/customer/configs/config.yaml index 32faa11..2e7b926 100644 --- a/backend/customer/configs/config.yaml +++ b/backend/customer/configs/config.yaml @@ -8,7 +8,7 @@ server: data: database: driver: mysql - source: root:root@tcp(127.0.0.1:3306)/test?parseTime=True&loc=Local + source: "root:123456@tcp(localhost:3306)/laomadj_customer?charset=utf8mb4&parseTime=True&loc=Local&maxConnLifetime=60s&maxIdleConns=10&maxOpenConns=100" redis: addr: 127.0.0.1:6379 read_timeout: 0.2s diff --git a/backend/customer/configs/config.yaml~ b/backend/customer/configs/config.yaml~ new file mode 100644 index 0000000..94bdedf --- /dev/null +++ b/backend/customer/configs/config.yaml~ @@ -0,0 +1,15 @@ +server: + http: + addr: 0.0.0.0:8100 + timeout: 1s + grpc: + addr: 0.0.0.0:9100 + timeout: 1s +data: + database: + driver: mysql + source: dsn := "root:123456@tcp(localhost:3306)/mydatabase?charset=utf8mb4&parseTime=True&loc=Local&maxConnLifetime=60s&maxIdleConns=10&maxOpenConns=100" + redis: + addr: 127.0.0.1:6379 + read_timeout: 0.2s + write_timeout: 0.2s diff --git a/backend/customer/go.mod b/backend/customer/go.mod index 032424f..e17b390 100644 --- a/backend/customer/go.mod +++ b/backend/customer/go.mod @@ -20,8 +20,11 @@ require ( github.com/go-logr/logr v1.4.1 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-playground/form/v4 v4.2.1 // indirect + github.com/go-sql-driver/mysql v1.7.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/gorilla/mux v1.8.1 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect github.com/kr/text v0.2.0 // indirect github.com/redis/go-redis/v9 v9.7.0 // indirect go.opentelemetry.io/otel v1.24.0 // indirect @@ -33,4 +36,6 @@ require ( golang.org/x/text v0.15.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + gorm.io/driver/mysql v1.5.7 // indirect + gorm.io/gorm v1.25.12 // indirect ) diff --git a/backend/customer/go.sum b/backend/customer/go.sum index 68f2cc4..f770ad3 100644 --- a/backend/customer/go.sum +++ b/backend/customer/go.sum @@ -32,11 +32,14 @@ github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBY github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/form/v4 v4.2.1 h1:HjdRDKO0fftVMU5epjPW2SOREcZ6/wLUzEobqUGJuPw= github.com/go-playground/form/v4 v4.2.1/go.mod h1:q1a2BY+AQUUzhl6xA/6hBetay6dEIhMHjgvJiGo6K7U= +github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE= github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -44,6 +47,10 @@ github.com/google/wire v0.6.0 h1:HBkoIh4BdSxoyo9PveV8giw7ZsaBOvzWKfcg/6MrVwI= github.com/google/wire v0.6.0/go.mod h1:F4QhpQ9EDIdJ1Mbop/NZBRB+5yrR6qg3BnctaoUk6NA= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -76,6 +83,7 @@ golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1m golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= @@ -125,6 +133,7 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= +golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc= golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/genproto/googleapis/api v0.0.0-20240528184218-531527333157 h1:7whR9kGa5LUwFtpLm2ArCEejtnxlGeLbAyjFY8sGNFw= @@ -140,3 +149,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo= +gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM= +gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= +gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= +gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= diff --git a/backend/customer/internal/biz/customer.go b/backend/customer/internal/biz/customer.go new file mode 100644 index 0000000..2c87036 --- /dev/null +++ b/backend/customer/internal/biz/customer.go @@ -0,0 +1,30 @@ +package biz + +import ( + "database/sql" + "gorm.io/gorm" +) + +// 模型 +type Customer struct { + //业务逻辑 + CustomerWork + //token部分 + CustoerToken + gorm.Model +} + +//业务逻辑部分 + +type CustomerWork struct { + Telephone string `gorm:"type:varchar(15);uniqueIndex;" json:"telephone"` + Name string `gorm:"type:varchar(255);uniqueIndex;" json:"name"` + Email string `gorm:"type:varchar(255);uniqueIndex;" json:"email"` + Wechat string `gorm:"type:varchar(255);uniqueIndex;" json:"wechat"` +} + +// token简介 +type CustoerToken struct { + Token string `gorm:"type:varchar(4095);" json:"token"` + TokenCreatAt sql.NullTime `gorm:"" json:"token_created_at"` +} diff --git a/backend/customer/internal/data/customer.go b/backend/customer/internal/data/customer.go new file mode 100644 index 0000000..43375f9 --- /dev/null +++ b/backend/customer/internal/data/customer.go @@ -0,0 +1,26 @@ +package data + +import ( + "context" + "time" +) + +// customer 中与数据操作相关的代码 +type CustomerData struct { + data *Data +} + +// New 方法 +func NewCustomerData(data *Data) *CustomerData { + return &CustomerData{data: data} +} + +// redis存储验证码 +func (cd CustomerData) SetVerifyCode(telephone, code string, ex int64) error { + + status := cd.data.Rdb.Set(context.Background(), "CVC"+telephone, code, time.Duration(ex)*time.Second) + if _, err := status.Result(); err != nil { + return err + } + return nil +} diff --git a/backend/customer/internal/data/data.go b/backend/customer/internal/data/data.go index 3015206..7bd1974 100644 --- a/backend/customer/internal/data/data.go +++ b/backend/customer/internal/data/data.go @@ -1,24 +1,60 @@ package data import ( + "customer/internal/biz" "customer/internal/conf" + "fmt" + "github.com/redis/go-redis/v9" + "gorm.io/driver/mysql" + "gorm.io/gorm" "github.com/go-kratos/kratos/v2/log" "github.com/google/wire" ) // ProviderSet is data providers. -var ProviderSet = wire.NewSet(NewData, NewGreeterRepo) +var ProviderSet = wire.NewSet(NewData, NewGreeterRepo, NewCustomerData) // Data . type Data struct { // TODO wrapped database client + Rdb *redis.Client + Mdb *gorm.DB } // NewData . func NewData(c *conf.Data, logger log.Logger) (*Data, func(), error) { + //初始化redis + //连接redis + data := &Data{} + redisURL := fmt.Sprintf("redis://%s/1?dial_timeout=%s", c.Redis.Addr, 1) + options, err := redis.ParseURL(redisURL) + if err != nil { + data.Rdb = nil + fmt.Println("初始化rdb失败") + } + data.Rdb = redis.NewClient(options) cleanup := func() { + //清理redis连接 + _ = data.Rdb.Close() log.NewHelper(logger).Info("closing the data resources") } - return &Data{}, cleanup, nil + //连接mysql + //用户名:密码啊@tcp(ip:端口)/数据库的名字 + dsn := c.Database.Source + //连接数据集 + db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) //open不会检验用户名和密码 + if err != nil { + data.Mdb = nil + log.Fatal(err) + } + data.Mdb = db + migrateTable(data.Mdb) + return data, cleanup, nil +} + +func migrateTable(db *gorm.DB) { + if err := db.AutoMigrate(&biz.Customer{}); err != nil { + log.Info("customer table migrate error, err:", err) + } } diff --git a/backend/customer/internal/service/customer.go b/backend/customer/internal/service/customer.go index 3f7b5a0..dcba484 100644 --- a/backend/customer/internal/service/customer.go +++ b/backend/customer/internal/service/customer.go @@ -3,8 +3,8 @@ package service import ( "context" "customer/api/verifyCode" + "customer/internal/data" "github.com/go-kratos/kratos/v2/transport/grpc" - "github.com/redis/go-redis/v9" "regexp" "time" @@ -13,10 +13,13 @@ import ( type CustomerService struct { pb.UnimplementedCustomerServer + CustomerService *data.CustomerData } -func NewCustomerService() *CustomerService { - return &CustomerService{} +func NewCustomerService(cd *data.CustomerData) *CustomerService { + return &CustomerService{ + CustomerService: cd, + } } func (s *CustomerService) GetVerifyCode(ctx context.Context, req *pb.GetVerifyCodeReq) (*pb.GetVerifyCodeResp, error) { @@ -46,6 +49,7 @@ func (s *CustomerService) GetVerifyCode(ctx context.Context, req *pb.GetVerifyCo }() //发送获取验证码请求 + client := verifyCode.NewVerifyCodeClient(conn) reply, err := client.GetVerifyCode(context.Background(), &verifyCode.GetVerifyCodeRequest{ @@ -58,28 +62,16 @@ func (s *CustomerService) GetVerifyCode(ctx context.Context, req *pb.GetVerifyCo Message: "验证码获取错误", }, nil } - //连接redis - options, err := redis.ParseURL("redis://192.168.233.10:6379/1?dial_timeout=1") - if err != nil { - - return &pb.GetVerifyCodeResp{ - Code: 1, - Message: "验证码连接错误(redis)", - }, nil - } - rdb := redis.NewClient(options) //设置key,customer-verify-code const life = 60 - status := rdb.Set(context.Background(), "CVC"+req.Telephone, reply.Code, life*time.Second) - if _, err := status.Result(); err != nil { - + err = s.CustomerService.SetVerifyCode(req.Telephone, reply.Code, int64(life*time.Second)) + if err != nil { return &pb.GetVerifyCodeResp{ Code: 1, Message: "验证码存储错误", }, nil } //生成响应 - return &pb.GetVerifyCodeResp{ Code: 0, VerifyCode: reply.Code,