diff --git a/oa/errs/errors.go b/oa/errs/errors.go index 229084b..aba206f 100644 --- a/oa/errs/errors.go +++ b/oa/errs/errors.go @@ -9,24 +9,31 @@ package errs import ( "fmt" + "net/http" ) -type CodeMsg struct { +type CodeErr struct { Code int Msg string } -func (c *CodeMsg) Error() string { +func (c *CodeErr) Error() string { return fmt.Sprintf("code: %d, msg: %s", c.Code, c.Msg) } +func (c *CodeErr) WithErr(e error) error { + c.Msg = fmt.Sprintf("%s: %s", c.Msg, e.Error()) + return c +} + // New creates a new CodeMsg. -func New(code int, msg string) error { - return &CodeMsg{Code: code, Msg: msg} +func New(code int, msg string) *CodeErr { + return &CodeErr{Code: code, Msg: msg} } var ( AuthFailed = New(401, "auth failed") AuthExpired = New(401, "auth expired") AuthInvalid = New(401, "auth invalid") + ArgsInvalid = New(http.StatusUnprocessableEntity, "args invalid") ) diff --git a/oa/errs/response.go b/oa/errs/response.go index 1ef4a27..76cb2ef 100644 --- a/oa/errs/response.go +++ b/oa/errs/response.go @@ -8,7 +8,6 @@ package errs import ( - "fmt" "net/http" "github.com/go-sql-driver/mysql" @@ -17,23 +16,21 @@ import ( func Response(w http.ResponseWriter, resp interface{}, err error) { if err != nil { - httpx.Error(w, err) + code := http.StatusInternalServerError + msg := err.Error() + switch e := err.(type) { + case *CodeErr: + code = e.Code + msg = e.Msg + case *mysql.MySQLError: + code = http.StatusUnprocessableEntity + msg = e.Message + } + w.WriteHeader(code) + w.Write([]byte(msg)) } else if resp != nil { httpx.OkJson(w, resp) } else { httpx.Ok(w) } } - -func ErrorHandler(err error) (int, any) { - switch e := err.(type) { - case *CodeMsg: - return e.Code, e.Msg - case *mysql.MySQLError: - fmt.Printf("\nerror: %v| %v\n", e.SQLState, e.Number) - return http.StatusUnprocessableEntity, e.Message - default: - fmt.Printf("\nerror: %T| %v\n", err, err) - return http.StatusInternalServerError, err.Error() - } -} diff --git a/oa/go.mod b/oa/go.mod index ddb90b8..6da8ca4 100644 --- a/oa/go.mod +++ b/oa/go.mod @@ -3,8 +3,10 @@ module oa go 1.21.5 require ( + github.com/go-sql-driver/mysql v1.8.1 + github.com/google/uuid v1.6.0 + github.com/veypi/utils v0.3.6 github.com/zeromicro/go-zero v1.7.0 - github.com/zeromicro/x v0.0.0-20240408115609-8224c482b07e ) require ( @@ -12,13 +14,10 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cenkalti/backoff/v4 v4.3.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect - github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/fatih/color v1.17.0 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect - github.com/go-sql-driver/mysql v1.8.1 // indirect github.com/golang-jwt/jwt/v4 v4.5.0 // indirect - github.com/google/uuid v1.6.0 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect @@ -28,7 +27,6 @@ require ( github.com/prometheus/client_model v0.5.0 // indirect github.com/prometheus/common v0.48.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect - github.com/redis/go-redis/v9 v9.6.1 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect go.opentelemetry.io/otel v1.24.0 // indirect go.opentelemetry.io/otel/exporters/jaeger v1.17.0 // indirect diff --git a/oa/go.sum b/oa/go.sum index bcb3054..64819f2 100644 --- a/oa/go.sum +++ b/oa/go.sum @@ -1,26 +1,19 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU= github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU= -github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302 h1:uvdUDbHQHO85qeSydJtItA4T55Pw6BtAejd0APRJOCE= -github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc= -github.com/alicebob/miniredis/v2 v2.33.0 h1:uvTF0EDeu9RLnUEG27Db5I68ESoIxTiXbNUiji6lZrA= -github.com/alicebob/miniredis/v2 v2.33.0/go.mod h1:MhP4a3EU7aENRi9aO+tHfTBZicLqQevyi/DJpoj6mi0= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= -github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= -github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= -github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= -github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4= github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= @@ -40,6 +33,7 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1 github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k= github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw= github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI= +github.com/kardianos/service v1.2.2/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM= 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= @@ -53,6 +47,7 @@ github.com/openzipkin/zipkin-go v0.4.3 h1:9EGwpqkgnwdEIJ+Od7QVSEIH+ocmm5nPat0G7s github.com/openzipkin/zipkin-go v0.4.3/go.mod h1:M9wCJZFWCo2RiY+o1eBCEMe0Dp2S5LDHcMZmk3RmK7c= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g= @@ -65,10 +60,13 @@ github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSz github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc= github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= -github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4= -github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= +github.com/rs/zerolog v1.17.2 h1:RMRHFw2+wF7LO0QqtELQwo8hqSmqISyCJeFeAAuWcRo= +github.com/rs/zerolog v1.17.2/go.mod h1:9nvC1axdVrAHcu/s9taAVfBuIdTZLVQmKQyvrUjF5+I= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -81,12 +79,12 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M= -github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw= +github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= +github.com/veypi/utils v0.3.6 h1:czLbFXzDwMGe03RJ2NxenKhtXVSMknVKkx5tTQQfKJA= +github.com/veypi/utils v0.3.6/go.mod h1:ElH1LPatrmw7d+GPFNk7UcnSOdNFajV3P44mi6wBXh8= +github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= github.com/zeromicro/go-zero v1.7.0 h1:B+y7tUVlo3qVQ6F0I0R9bi+Dq4I1QdO9ZB+dz1r0p1s= github.com/zeromicro/go-zero v1.7.0/go.mod h1:ypW4PzQI+jUrMcNJDDQ+7YW+pE+tMua9Xj/pmtmS1Dc= -github.com/zeromicro/x v0.0.0-20240408115609-8224c482b07e h1:F5waakzloTfbJg2lcO1xvrzO6ssn7jQ38lXIDBz+nbQ= -github.com/zeromicro/x v0.0.0-20240408115609-8224c482b07e/go.mod h1:5TP11tc1RHPCi5C/KDL0kIB0KgJAb9FB3ChpT/qM/jA= go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo= go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo= go.opentelemetry.io/otel/exporters/jaeger v1.17.0 h1:D7UpUy2Xc2wsi1Ras6V40q806WM07rqoCWzXu7Sqy+4= @@ -113,14 +111,22 @@ go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8= go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/genproto/googleapis/api v0.0.0-20240711142825-46eb208f015d h1:kHjw/5UfflP/L5EbledDrcG4C2597RtymmGRZvHiCuY= google.golang.org/genproto/googleapis/api v0.0.0-20240711142825-46eb208f015d/go.mod h1:mw8MG/Qz5wfgYr6VqVCiZcHe/GJEfI+oGGDCohaVgB0= google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 h1:BwIjyKYGsK9dMCBOorzRri8MQwmi7mT9rGHsCEinZkA= @@ -134,6 +140,9 @@ 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/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY= gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0= +gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8= +gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/oa/internal/handler/user/reghandler.go b/oa/internal/handler/user/reghandler.go index fe5d281..04c5868 100644 --- a/oa/internal/handler/user/reghandler.go +++ b/oa/internal/handler/user/reghandler.go @@ -19,7 +19,6 @@ func RegHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { errs.Response(w, nil, err) return } - l := user.NewRegLogic(r.Context(), svcCtx) err := l.Reg(&req) errs.Response(w, nil, err) diff --git a/oa/internal/logic/user/reglogic.go b/oa/internal/logic/user/reglogic.go index eeca781..4861fc5 100644 --- a/oa/internal/logic/user/reglogic.go +++ b/oa/internal/logic/user/reglogic.go @@ -2,14 +2,18 @@ package user import ( "context" + "fmt" + "math/rand" "strings" "time" + "oa/errs" "oa/internal/svc" "oa/internal/types" "oa/models" "github.com/google/uuid" + "github.com/veypi/utils" "github.com/zeromicro/go-zero/core/logx" ) @@ -30,14 +34,23 @@ func NewRegLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegLogic { func (l *RegLogic) Reg(req *types.RegReq) error { // todo: add your logic here and delete this line + r := rand.New(rand.NewSource(time.Now().UnixNano())) m := models.NewUserModel(l.svcCtx.Sqlx()) u := &models.User{ Id: strings.ReplaceAll(uuid.New().String(), "-", ""), Created: time.Now(), Updated: time.Now(), Username: req.Username, + RealCode: utils.RandSeq(32), + Icon: fmt.Sprintf("/media/icon/default/%04d.jpg", r.Intn(230)), + Space: 300, + } + var err error + u.CheckCode, err = utils.AesEncrypt(u.RealCode, []byte(req.Pwd)) + if err != nil { + return errs.ArgsInvalid.WithErr(err) } l.Infof("user: %v", u.Id) - _, e := m.Insert(l.ctx, u) - return e + _, err = m.Insert(l.ctx, u) + return err } diff --git a/oa/main.go b/oa/main.go index 0fbcf8e..d9356a2 100644 --- a/oa/main.go +++ b/oa/main.go @@ -4,14 +4,12 @@ import ( "flag" "fmt" - "oa/errs" "oa/internal/config" "oa/internal/handler" "oa/internal/svc" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" - "github.com/zeromicro/go-zero/rest/httpx" ) var configFile = flag.String("f", "etc/main.yaml", "the config file") @@ -27,7 +25,6 @@ func main() { ctx := svc.NewServiceContext(c) handler.RegisterHandlers(server, ctx) - httpx.SetErrorHandler(errs.ErrorHandler) fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) server.Start() diff --git a/oa/models/appmodel_gen.go b/oa/models/appmodel_gen.go index ef0a5c6..fa4e4b3 100755 --- a/oa/models/appmodel_gen.go +++ b/oa/models/appmodel_gen.go @@ -40,12 +40,12 @@ type ( Updated time.Time `db:"updated"` Key string `db:"_key"` Name string `db:"name"` - Icon sql.NullString `db:"icon"` + Icon string `db:"icon"` Des sql.NullString `db:"des"` UserCount int64 `db:"user_count"` Hide int64 `db:"hide"` JoinMethod int64 `db:"join_method"` - RoleId sql.NullString `db:"role_id"` + RoleId string `db:"role_id"` Host string `db:"host"` Redirect string `db:"redirect"` Status int64 `db:"status"` // 状态(0:ok,1:disabled) diff --git a/oa/models/resourcemodel_gen.go b/oa/models/resourcemodel_gen.go index 7bdebce..ab4d7ba 100755 --- a/oa/models/resourcemodel_gen.go +++ b/oa/models/resourcemodel_gen.go @@ -36,12 +36,12 @@ type ( } Resource struct { - Id int64 `db:"id"` - Created time.Time `db:"created"` - Updated time.Time `db:"updated"` - AppId string `db:"app_id"` - Name string `db:"name"` - Des sql.NullString `db:"des"` + Id int64 `db:"id"` + Created time.Time `db:"created"` + Updated time.Time `db:"updated"` + AppId string `db:"app_id"` + Name string `db:"name"` + Des string `db:"des"` } ) diff --git a/oa/models/rolemodel_gen.go b/oa/models/rolemodel_gen.go index 7fb3ab6..a15d24a 100755 --- a/oa/models/rolemodel_gen.go +++ b/oa/models/rolemodel_gen.go @@ -35,13 +35,13 @@ type ( } Role struct { - Id string `db:"id"` - Created time.Time `db:"created"` - Updated time.Time `db:"updated"` - AppId string `db:"app_id"` - Name string `db:"name"` - Des sql.NullString `db:"des"` - UserCount int64 `db:"user_count"` + Id string `db:"id"` + Created time.Time `db:"created"` + Updated time.Time `db:"updated"` + AppId string `db:"app_id"` + Name string `db:"name"` + Des string `db:"des"` + UserCount int64 `db:"user_count"` } ) diff --git a/oa/models/usermodel_gen.go b/oa/models/usermodel_gen.go index 404d99c..3f884cc 100755 --- a/oa/models/usermodel_gen.go +++ b/oa/models/usermodel_gen.go @@ -25,8 +25,6 @@ type ( userModel interface { Insert(ctx context.Context, data *User) (sql.Result, error) FindOne(ctx context.Context, id string) (*User, error) - FindOneByEmail(ctx context.Context, email sql.NullString) (*User, error) - FindOneByPhone(ctx context.Context, phone sql.NullString) (*User, error) FindOneByUsername(ctx context.Context, username string) (*User, error) Update(ctx context.Context, data *User) error Delete(ctx context.Context, id string) error @@ -38,19 +36,19 @@ type ( } User struct { - Id string `db:"id"` // User UUID - Created time.Time `db:"created"` - Updated time.Time `db:"updated"` - Username string `db:"username"` - Nickname sql.NullString `db:"nickname"` - Email sql.NullString `db:"email"` - Phone sql.NullString `db:"phone"` - Icon sql.NullString `db:"icon"` - RealCode sql.NullString `db:"_real_code"` - CheckCode sql.NullString `db:"_check_code"` - Status int64 `db:"status"` // 状态(0:ok,1:disabled) - Used int64 `db:"used"` - Space int64 `db:"space"` + Id string `db:"id"` // User UUID + Created time.Time `db:"created"` + Updated time.Time `db:"updated"` + Username string `db:"username"` + Nickname string `db:"nickname"` + Email string `db:"email"` + Phone string `db:"phone"` + Icon string `db:"icon"` + RealCode string `db:"_real_code"` + CheckCode string `db:"_check_code"` + Status int64 `db:"status"` // 状态(0:ok,1:disabled) + Used int64 `db:"used"` + Space int64 `db:"space"` } ) @@ -81,34 +79,6 @@ func (m *defaultUserModel) FindOne(ctx context.Context, id string) (*User, error } } -func (m *defaultUserModel) FindOneByEmail(ctx context.Context, email sql.NullString) (*User, error) { - var resp User - query := fmt.Sprintf("select %s from %s where `email` = ? limit 1", userRows, m.table) - err := m.conn.QueryRowCtx(ctx, &resp, query, email) - switch err { - case nil: - return &resp, nil - case sqlx.ErrNotFound: - return nil, ErrNotFound - default: - return nil, err - } -} - -func (m *defaultUserModel) FindOneByPhone(ctx context.Context, phone sql.NullString) (*User, error) { - var resp User - query := fmt.Sprintf("select %s from %s where `phone` = ? limit 1", userRows, m.table) - err := m.conn.QueryRowCtx(ctx, &resp, query, phone) - switch err { - case nil: - return &resp, nil - case sqlx.ErrNotFound: - return nil, ErrNotFound - default: - return nil, err - } -} - func (m *defaultUserModel) FindOneByUsername(ctx context.Context, username string) (*User, error) { var resp User query := fmt.Sprintf("select %s from %s where `username` = ? limit 1", userRows, m.table) diff --git a/oa/protoc/sql/20240801061157_base.sql b/oa/protoc/sql/20240801061157_base.sql index 05d360c..532e6ee 100644 --- a/oa/protoc/sql/20240801061157_base.sql +++ b/oa/protoc/sql/20240801061157_base.sql @@ -5,12 +5,12 @@ CREATE TABLE IF NOT EXISTS `user` `updated` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL, `username` varchar(255) NOT NULL UNIQUE, - `nickname` varchar(255), - `email` varchar(255) UNIQUE, - `phone` varchar(255) UNIQUE, - `icon` varchar(255), - `_real_code` varchar(32), - `_check_code` binary(48), + `nickname` varchar(255) NOT NULL, + `email` varchar(255) NOT NULL, + `phone` varchar(255) NOT NULL, + `icon` varchar(255) NOT NULL DEFAULT "", + `_real_code` varchar(32) NOT NULL, + `_check_code` varchar(64) NOT NULL, `status` int NOT NULL COMMENT '状态(0:ok,1:disabled)' DEFAULT 0, `used` int NOT NULL DEFAULT 0, @@ -27,13 +27,13 @@ CREATE TABLE IF NOT EXISTS `app` `_key` varchar(32) NOT NULL, `name` varchar(255) NOT NULL, - `icon` varchar(255), + `icon` varchar(255) NOT NULL DEFAULT "", `des` TEXT, `user_count` int NOT NULL DEFAULT 0, `hide` tinyint(1) NOT NULL DEFAULT 0, `join_method` int NOT NULL DEFAULT 0, - `role_id` varchar(32), + `role_id` varchar(32) NOT NULL, `host` varchar(255) NOT NULL DEFAULT '', `redirect` varchar(255) NOT NULL DEFAULT '', `status` int NOT NULL COMMENT '状态(0:ok,1:disabled)' DEFAULT 0, @@ -62,7 +62,7 @@ CREATE TABLE IF NOT EXISTS `role` `app_id` varchar(32) NOT NULL, `name` varchar(255) NOT NULL, - `des` varchar(255), + `des` varchar(255) NOT NULL DEFAULT '', `user_count` int NOT NULL DEFAULT 0, PRIMARY KEY (`id`) USING BTREE @@ -90,7 +90,7 @@ CREATE TABLE IF NOT EXISTS `resource` `app_id` varchar(32) NOT NULL, `name` varchar(32) NOT NULL, - `des` varchar(255), + `des` varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`), @@ -134,7 +134,7 @@ CREATE TABLE IF NOT EXISTS `token` -INSERT INTO `app` (`id`, `name`, `_key`, `role_id`) +INSERT INTO `app` (`id`, `name`, `_key`, `role_id` ) VALUES ('FR9P5t8debxc11aFF', 'oa', 'AMpjwQHwVjGsb1WC4WG6', '1lytMwQL4uiNd0vsc'); INSERT INTO `resource` (`app_id`, `name`)