├── votes ├── readmin.md ├── .gitignore ├── rpc │ ├── readmin.md │ ├── internal │ │ ├── config │ │ │ └── config.go │ │ ├── logic │ │ │ ├── incractiviviewlogic.go │ │ │ ├── error.go │ │ │ ├── getactivityinfologic.go │ │ │ ├── getenrollinfologic.go │ │ │ ├── getenrolllistlogic.go │ │ │ ├── enrolllogic.go │ │ │ └── voteslogic.go │ │ ├── svc │ │ │ └── servicecontext.go │ │ └── server │ │ │ └── votesserver.go │ ├── etc │ │ └── rpc.example.yaml │ ├── rpc.go │ ├── votes.go │ ├── votes.proto │ └── votesclient │ │ └── votes.go └── model │ ├── vars.go │ └── appvotesmodel.go ├── common ├── rpc │ ├── etc │ │ ├── .gitignore │ │ └── rpc.example.yaml │ ├── internal │ │ ├── config │ │ │ └── config.go │ │ ├── svc │ │ │ └── servicecontext.go │ │ ├── server │ │ │ └── commonserver.go │ │ └── logic │ │ │ ├── getbaseapplogic.go │ │ │ └── getappconfiglogic.go │ ├── common.proto │ ├── rpc.go │ ├── common.go │ └── commonclient │ │ └── common.go ├── .gitignore ├── model │ ├── vars.go │ └── baseappmodel.go └── readmin.md ├── user ├── .gitignore ├── model │ └── vars.go ├── readmin.md ├── api │ ├── etc │ │ └── user-api.exmaple.yaml │ ├── internal │ │ ├── config │ │ │ └── config.go │ │ ├── logic │ │ │ ├── error.go │ │ │ ├── pinglogic.go │ │ │ ├── registerlogic.go │ │ │ ├── userinfologic.go │ │ │ └── loginlogic.go │ │ ├── handler │ │ │ ├── pinghandler.go │ │ │ ├── userinfohandler.go │ │ │ ├── loginhandler.go │ │ │ ├── registerhandler.go │ │ │ └── routes.go │ │ ├── svc │ │ │ └── servicecontext.go │ │ ├── types │ │ │ └── types.go │ │ └── middleware │ │ │ └── usercheckmiddleware.go │ ├── user.go │ └── user.api └── rpc │ ├── internal │ ├── config │ │ └── config.go │ ├── logic │ │ ├── error.go │ │ ├── pinglogic.go │ │ ├── userinfologic.go │ │ ├── loginlogic.go │ │ ├── registerlogic.go │ │ └── snsloginlogic.go │ ├── svc │ │ └── servicecontext.go │ └── server │ │ └── userserver.go │ ├── etc │ └── rpc.example.yaml │ ├── user.go │ ├── rpc.go │ ├── user.proto │ └── userclient │ └── user.go ├── questions ├── .gitignore ├── model │ └── vars.go ├── rpc │ ├── internal │ │ ├── config │ │ │ └── config.go │ │ ├── logic │ │ │ ├── getquestionsgradelogic.go │ │ │ ├── getawardlistlogic.go │ │ │ ├── postconvertlogic.go │ │ │ ├── getactivitiesinfologic.go │ │ │ ├── postquestionschangelogic.go │ │ │ ├── getawardinfologic.go │ │ │ ├── getquestionslistlogic.go │ │ │ └── postturntablelogic.go │ │ ├── svc │ │ │ └── servicecontext.go │ │ └── server │ │ │ └── questionsserver.go │ ├── etc │ │ └── questions.exmple.yaml │ ├── questions.go │ ├── questions.proto │ └── questionsclient │ │ └── questions.go └── readmin.md ├── search ├── rpc │ ├── readme.md │ ├── etc │ │ └── search.example.yaml │ ├── internal │ │ ├── config │ │ │ └── config.go │ │ ├── svc │ │ │ └── servicecontext.go │ │ ├── logic │ │ │ ├── articlestorelogic.go │ │ │ ├── articlesearchlogic.go │ │ │ └── articleinitlogic.go │ │ └── server │ │ │ └── searchserver.go │ ├── search.proto │ ├── search.go │ ├── searchclient │ │ └── search.go │ └── dao │ │ └── article.go └── .gitignore ├── assets └── datacenter.jpg ├── .gitignore ├── shared ├── tools_test.go ├── cost.go ├── kes.go ├── error.go └── tools.go ├── internal ├── handler │ ├── common │ │ ├── votesverificationhandler.go │ │ ├── appinfohandler.go │ │ ├── wxtickethandler.go │ │ ├── qiuniutokenhandler.go │ │ └── snsinfohandler.go │ ├── user │ │ ├── pinghandler.go │ │ ├── userinfohandler.go │ │ ├── code2sessionhandler.go │ │ ├── wxloginhandler.go │ │ ├── registerhandler.go │ │ └── loginhandler.go │ ├── search │ │ ├── articleinithandler.go │ │ ├── articlehandler.go │ │ └── articlestorehandler.go │ ├── votes │ │ ├── enrollhandler.go │ │ ├── votehandler.go │ │ ├── activityicrviewhandler.go │ │ ├── activityinfohandler.go │ │ ├── enrollinfohandler.go │ │ └── enrolllistshandler.go │ ├── questions │ │ ├── awardinfohandler.go │ │ ├── listshandler.go │ │ ├── awardlisthandler.go │ │ ├── activitiesinfohandler.go │ │ ├── gradehandler.go │ │ ├── changehandler.go │ │ ├── lotteryhandler.go │ │ └── turntablehandler.go │ └── datacenterhandler.go ├── logic │ ├── user │ │ ├── error.go │ │ ├── pinglogic.go │ │ ├── userinfologic.go │ │ ├── registerlogic.go │ │ ├── loginlogic.go │ │ ├── wxloginlogic.go │ │ └── code2sessionlogic.go │ ├── search │ │ ├── articlelogic.go │ │ ├── articleinitlogic.go │ │ └── articlestorelogic.go │ ├── votes │ │ ├── enrollinfologic.go │ │ ├── activityinfologic.go │ │ ├── activityicrviewlogic.go │ │ ├── enrolllistslogic.go │ │ ├── votelogic.go │ │ └── enrolllogic.go │ ├── questions │ │ ├── lotterylogic.go │ │ ├── listslogic.go │ │ ├── changelogic.go │ │ ├── awardinfologic.go │ │ ├── awardlistlogic.go │ │ ├── turntablelogic.go │ │ ├── gradelogic.go │ │ └── activitiesinfologic.go │ ├── common │ │ ├── votesverificationlogic.go │ │ ├── appinfologic.go │ │ ├── qiuniutokenlogic.go │ │ ├── snsinfologic.go │ │ └── wxticketlogic.go │ └── datacenterlogic.go ├── config │ └── config.go ├── middleware │ ├── greetmiddleware1middleware.go │ ├── greetmiddleware2middleware.go │ ├── admincheckmiddleware.go │ ├── usercheckmiddleware.go │ └── corsmiddleware.go └── svc │ └── servicecontext.go ├── etc └── datacenter-api.example.yaml ├── search.api ├── server.sh ├── restart.sh ├── questions.api ├── datacenter.go ├── datacenter.api ├── votes.api ├── user.api ├── README.md └── go.mod /votes/readmin.md: -------------------------------------------------------------------------------- 1 | ### 投票服务 -------------------------------------------------------------------------------- /common/rpc/etc/.gitignore: -------------------------------------------------------------------------------- 1 | rpc.yaml -------------------------------------------------------------------------------- /common/.gitignore: -------------------------------------------------------------------------------- 1 | rpc/etc/rpc.yaml 2 | rpc/nohnohup.out 3 | rpc/common-server -------------------------------------------------------------------------------- /user/.gitignore: -------------------------------------------------------------------------------- 1 | rpc/etc/rpc.yaml 2 | rpc/nohnohup.out 3 | rpc/user-server 4 | 5 | -------------------------------------------------------------------------------- /questions/.gitignore: -------------------------------------------------------------------------------- 1 | rpc/etc/questions.yaml 2 | rpc/nohnohup.out 3 | rpc/questions-server -------------------------------------------------------------------------------- /search/rpc/readme.md: -------------------------------------------------------------------------------- 1 | # 搜索服务 2 | ``` 3 | goctl rpc proto -src search.proto -dir . 4 | ``` -------------------------------------------------------------------------------- /votes/.gitignore: -------------------------------------------------------------------------------- 1 | ./votes-server 2 | 3 | rpc/etc/rpc.yaml 4 | rpc/nohnohup.out 5 | rpc/votes-server -------------------------------------------------------------------------------- /assets/datacenter.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackluo2012/datacenter/HEAD/assets/datacenter.jpg -------------------------------------------------------------------------------- /search/.gitignore: -------------------------------------------------------------------------------- 1 | ./search-server 2 | 3 | rpc/etc/search.yaml 4 | rpc/nohnohup.out 5 | rpc/search-server -------------------------------------------------------------------------------- /votes/rpc/readmin.md: -------------------------------------------------------------------------------- 1 | ``` 2 | goctl rpc template -o=votes.proto 3 | goctl rpc proto -src votes.proto -dir . 4 | ``` -------------------------------------------------------------------------------- /common/model/vars.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | import "github.com/zeromicro/go-zero/core/stores/sqlx" 4 | 5 | var ErrNotFound = sqlx.ErrNotFound 6 | -------------------------------------------------------------------------------- /user/model/vars.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | import "github.com/zeromicro/go-zero/core/stores/sqlx" 4 | 5 | var ErrNotFound = sqlx.ErrNotFound 6 | -------------------------------------------------------------------------------- /votes/model/vars.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | import "github.com/zeromicro/go-zero/core/stores/sqlx" 4 | 5 | var ErrNotFound = sqlx.ErrNotFound 6 | -------------------------------------------------------------------------------- /questions/model/vars.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | import "github.com/zeromicro/go-zero/core/stores/sqlx" 4 | 5 | var ErrNotFound = sqlx.ErrNotFound 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | common/rpc/nohup.out 2 | datacenter-server 3 | nohup.out 4 | .DS_Store 5 | datacenter-api.yaml 6 | user/api/etc/user-api.yaml 7 | mysql8 8 | redis 9 | etcd 10 | elasticsearch -------------------------------------------------------------------------------- /user/readmin.md: -------------------------------------------------------------------------------- 1 | ### 用户服务,含登陆 2 | 3 | 4 | ### 创建api 文件 5 | ```shell 生成接口 6 | goctl api -o user.api 7 | ``` 8 | ### 生成user api服务 9 | ``` 10 | goctl api go -api user.api -dir . 11 | 12 | goctl rpc proto -src user.proto -dir . -------------------------------------------------------------------------------- /search/rpc/etc/search.example.yaml: -------------------------------------------------------------------------------- 1 | Name: search.rpc 2 | ListenOn: 127.0.0.1:8083 3 | Etcd: 4 | Hosts: 5 | - 127.0.0.1:2379 6 | Key: search.rpc 7 | Esconfig: 8 | Urls: 9 | - http://127.0.0.1:9200 10 | User: elastic 11 | Password: 密码 -------------------------------------------------------------------------------- /common/readmin.md: -------------------------------------------------------------------------------- 1 | ## 公共服务 2 | 3 | ``` 4 | goctl model mysql datasource -url="root:admin@tcp(127.0.0.1:3306)/datacenter" -table="base_app" -dir ./model -c 5 | 6 | goctl model mysql datasource -url="root:admin@tcp(127.0.0.1:3306)/datacenter" -table="app_config" -dir ./model -c 7 | 8 | ``` -------------------------------------------------------------------------------- /shared/tools_test.go: -------------------------------------------------------------------------------- 1 | package shared 2 | 3 | import ( 4 | "net/url" 5 | "testing" 6 | ) 7 | 8 | func TestHttpPostForm(t *testing.T) { 9 | body, err := HttpPostForm("http://xxxxxxxxxx", url.Values{}) 10 | if err != nil { 11 | t.Error(err) 12 | } 13 | t.Error(string(body)) 14 | } 15 | -------------------------------------------------------------------------------- /search/rpc/internal/config/config.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import "github.com/zeromicro/go-zero/zrpc" 4 | 5 | type EserverConfig struct { 6 | Urls []string 7 | User string 8 | Password string 9 | } 10 | 11 | type Config struct { 12 | zrpc.RpcServerConf 13 | Esconfig EserverConfig 14 | } 15 | -------------------------------------------------------------------------------- /shared/cost.go: -------------------------------------------------------------------------------- 1 | package shared 2 | 3 | const ( 4 | QiuniuPtyId int64 = 7 //七 牛的是7 5 | //cdn 地址 6 | CDN_SSO_Qiuniu string = "https://xxxxxxxxx/" 7 | //微信 Noncestr 8 | Noncestr string = "e2323232" 9 | 10 | //投票锁的 时长 11 | VotesUidAuidLockExpire int = 1 12 | //用户操作加锁 13 | UidAuidLockExpire int = 2 14 | ) 15 | -------------------------------------------------------------------------------- /votes/rpc/internal/config/config.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "github.com/zeromicro/go-zero/core/stores/cache" 5 | "github.com/zeromicro/go-zero/zrpc" 6 | ) 7 | 8 | type Config struct { 9 | zrpc.RpcServerConf 10 | Mysql struct { 11 | DataSource string 12 | } 13 | CacheRedis cache.ClusterConf 14 | } 15 | -------------------------------------------------------------------------------- /common/rpc/internal/config/config.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "github.com/zeromicro/go-zero/core/stores/cache" 5 | "github.com/zeromicro/go-zero/zrpc" 6 | ) 7 | 8 | type Config struct { 9 | zrpc.RpcServerConf 10 | Mysql struct { 11 | DataSource string 12 | } 13 | CacheRedis cache.ClusterConf 14 | } 15 | -------------------------------------------------------------------------------- /questions/rpc/internal/config/config.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "github.com/zeromicro/go-zero/core/stores/cache" 5 | "github.com/zeromicro/go-zero/zrpc" 6 | ) 7 | 8 | type Config struct { 9 | zrpc.RpcServerConf 10 | Mysql struct { 11 | DataSource string 12 | } 13 | CacheRedis cache.ClusterConf 14 | } 15 | -------------------------------------------------------------------------------- /votes/rpc/etc/rpc.example.yaml: -------------------------------------------------------------------------------- 1 | Name: votes.rpc 2 | ListenOn: 127.0.0.1:8082 3 | Etcd: 4 | Hosts: 5 | - 127.0.0.1:2379 6 | Key: votes.rpc 7 | Mysql: 8 | DataSource: root:admin@tcp(127.0.0.1:3306)/datacenter?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai 9 | CacheRedis: 10 | - Host: 127.0.0.1:6379 11 | Pass: admin 12 | Type: node -------------------------------------------------------------------------------- /common/rpc/etc/rpc.example.yaml: -------------------------------------------------------------------------------- 1 | Name: common.rpc 2 | ListenOn: 127.0.0.1:8081 3 | Mysql: 4 | DataSource: root:admin@tcp(127.0.0.1:3306)/datacenter?charset=utf8&parseTime=true&loc=Asia%2FShanghai 5 | CacheRedis: 6 | - Host: 127.0.0.1:6379 7 | Pass: admin 8 | Type: node 9 | Etcd: 10 | Hosts: 11 | - 127.0.0.1:2379 12 | Key: common.rpc 13 | -------------------------------------------------------------------------------- /questions/rpc/etc/questions.exmple.yaml: -------------------------------------------------------------------------------- 1 | Name: questions.rpc 2 | ListenOn: 127.0.0.1:8084 3 | Etcd: 4 | Hosts: 5 | - 127.0.0.1:2379 6 | Key: questions.rpc 7 | Mysql: 8 | DataSource: root:admin@tcp(127.0.0.1:3306)/datacenter?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai 9 | CacheRedis: 10 | - Host: 127.0.0.1:6379 11 | Pass: admin 12 | Type: node -------------------------------------------------------------------------------- /user/api/etc/user-api.exmaple.yaml: -------------------------------------------------------------------------------- 1 | Name: user-api 2 | Host: 0.0.0.0 3 | Port: 8888 4 | Mysql: 5 | DataSource: root:admin@tcp(127.0.0.1:3306)/datacenter?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai 6 | CacheRedis: 7 | - Host: 127.0.0.1:6379 8 | Pass: 9 | Type: node 10 | 11 | Auth: 12 | AccessSecret: xxxxxxxxxxxxxx 13 | AccessExpire: 86400 -------------------------------------------------------------------------------- /internal/handler/common/votesverificationhandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | "datacenter/internal/svc" 7 | ) 8 | 9 | func VotesVerificationHandler(ctx *svc.ServiceContext) http.HandlerFunc { 10 | return func(w http.ResponseWriter, r *http.Request) { 11 | 12 | w.Write([]byte("NT04cqknJe0em3mT")) 13 | return 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /user/rpc/internal/config/config.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "github.com/zeromicro/go-zero/core/stores/cache" 5 | "github.com/zeromicro/go-zero/zrpc" 6 | ) 7 | 8 | type Config struct { 9 | zrpc.RpcServerConf 10 | 11 | Mysql struct { 12 | DataSource string 13 | } 14 | CacheRedis cache.ClusterConf 15 | CommonRpc zrpc.RpcClientConf 16 | } 17 | -------------------------------------------------------------------------------- /user/api/internal/config/config.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "github.com/zeromicro/go-zero/core/stores/cache" 5 | "github.com/zeromicro/go-zero/rest" 6 | ) 7 | 8 | type Config struct { 9 | rest.RestConf 10 | Mysql struct { 11 | DataSource string 12 | } 13 | CacheRedis cache.ClusterConf 14 | 15 | Auth struct { 16 | AccessSecret string 17 | AccessExpire int64 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /shared/kes.go: -------------------------------------------------------------------------------- 1 | package shared 2 | 3 | import "fmt" 4 | 5 | var ( 6 | VoteUidAuidLockKey = "vote.lock#%v#%v#" 7 | UidAuidLockKey = "user.lock#%v#%v#" 8 | ) 9 | 10 | func GetUidAuidLockKey(uid, auid int64) string { 11 | return fmt.Sprintf(UidAuidLockKey, uid, auid) 12 | } 13 | 14 | func GetVoteUidAuidLockKey(uid, auid int64) string { 15 | return fmt.Sprintf(VoteUidAuidLockKey, uid, auid) 16 | } 17 | -------------------------------------------------------------------------------- /internal/logic/user/error.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "datacenter/shared" 5 | ) 6 | 7 | var ( 8 | errorDuplicateUsername = shared.NewDefaultError("用户名已经注册") 9 | errorDuplicateMobile = shared.NewDefaultError("手机号已经被占用") 10 | errorUsernameUnRegister = shared.NewDefaultError("用户未注册") 11 | errorIncorrectPassword = shared.NewDefaultError("用户密码错误") 12 | errorUserNotFound = shared.NewDefaultError("用户不存在") 13 | ) 14 | -------------------------------------------------------------------------------- /user/api/internal/logic/error.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "datacenter/shared" 5 | ) 6 | 7 | var ( 8 | errorDuplicateUsername = shared.NewDefaultError("用户名已经注册") 9 | errorDuplicateMobile = shared.NewDefaultError("手机号已经被占用") 10 | errorUsernameUnRegister = shared.NewDefaultError("用户未注册") 11 | errorIncorrectPassword = shared.NewDefaultError("用户密码错误") 12 | errorUserNotFound = shared.NewDefaultError("用户不存在") 13 | ) 14 | -------------------------------------------------------------------------------- /user/rpc/etc/rpc.example.yaml: -------------------------------------------------------------------------------- 1 | Name: user.rpc 2 | ListenOn: 127.0.0.1:8080 3 | Etcd: 4 | Hosts: 5 | - 127.0.0.1:2379 6 | Key: user.rpc 7 | Mysql: 8 | DataSource: root:admin@tcp(127.0.0.1:3306)/datacenter?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai 9 | CacheRedis: 10 | - Host: 127.0.0.1:6379 11 | Pass: admin 12 | Type: node 13 | CommonRpc: 14 | Etcd: 15 | Hosts: 16 | - 127.0.0.1:2379 17 | Key: common.rpc 18 | -------------------------------------------------------------------------------- /user/rpc/internal/logic/error.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "datacenter/shared" 5 | ) 6 | 7 | var ( 8 | errorDuplicateUsername = shared.NewDefaultError("用户名已经注册") 9 | errorDuplicateMobile = shared.NewDefaultError("手机号已经被占用") 10 | errorUsernameUnRegister = shared.NewDefaultError("用户未注册") 11 | errorIncorrectPassword = shared.NewDefaultError("用户密码错误") 12 | errorUserNotFound = shared.NewDefaultError("用户不存在") 13 | ) 14 | -------------------------------------------------------------------------------- /internal/handler/user/pinghandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/user" 7 | "datacenter/internal/svc" 8 | 9 | "github.com/zeromicro/go-zero/rest/httpx" 10 | ) 11 | 12 | func PingHandler(ctx *svc.ServiceContext) http.HandlerFunc { 13 | return func(w http.ResponseWriter, r *http.Request) { 14 | 15 | l := logic.NewPingLogic(r.Context(), ctx) 16 | err := l.Ping() 17 | if err != nil { 18 | httpx.Error(w, err) 19 | } else { 20 | httpx.Ok(w) 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /user/api/internal/handler/pinghandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | "datacenter/user/api/internal/logic" 7 | "datacenter/user/api/internal/svc" 8 | "github.com/zeromicro/go-zero/rest/httpx" 9 | ) 10 | 11 | func pingHandler(ctx *svc.ServiceContext) http.HandlerFunc { 12 | return func(w http.ResponseWriter, r *http.Request) { 13 | 14 | l := logic.NewPingLogic(r.Context(), ctx) 15 | err := l.Ping() 16 | if err != nil { 17 | httpx.Error(w, err) 18 | } else { 19 | httpx.Ok(w) 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /internal/config/config.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "github.com/zeromicro/go-zero/core/stores/cache" 5 | "github.com/zeromicro/go-zero/rest" 6 | "github.com/zeromicro/go-zero/zrpc" 7 | ) 8 | 9 | type Config struct { 10 | rest.RestConf 11 | 12 | Auth struct { 13 | AccessSecret string 14 | AccessExpire int64 15 | } 16 | UserRpc zrpc.RpcClientConf 17 | CommonRpc zrpc.RpcClientConf 18 | VotesRpc zrpc.RpcClientConf 19 | SearchRpc zrpc.RpcClientConf 20 | QuestionsRpc zrpc.RpcClientConf 21 | 22 | CacheRedis cache.ClusterConf 23 | } 24 | -------------------------------------------------------------------------------- /internal/middleware/greetmiddleware1middleware.go: -------------------------------------------------------------------------------- 1 | package middleware 2 | 3 | import "net/http" 4 | 5 | type GreetMiddleware1Middleware struct { 6 | } 7 | 8 | func NewGreetMiddleware1Middleware() *GreetMiddleware1Middleware { 9 | return &GreetMiddleware1Middleware{} 10 | } 11 | 12 | func (m *GreetMiddleware1Middleware) Handle(next http.HandlerFunc) http.HandlerFunc { 13 | return func(w http.ResponseWriter, r *http.Request) { 14 | // TODO generate middleware implement function, delete after code implementation 15 | 16 | // Passthrough to next handler if need 17 | next(w, r) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /internal/middleware/greetmiddleware2middleware.go: -------------------------------------------------------------------------------- 1 | package middleware 2 | 3 | import "net/http" 4 | 5 | type GreetMiddleware2Middleware struct { 6 | } 7 | 8 | func NewGreetMiddleware2Middleware() *GreetMiddleware2Middleware { 9 | return &GreetMiddleware2Middleware{} 10 | } 11 | 12 | func (m *GreetMiddleware2Middleware) Handle(next http.HandlerFunc) http.HandlerFunc { 13 | return func(w http.ResponseWriter, r *http.Request) { 14 | // TODO generate middleware implement function, delete after code implementation 15 | 16 | // Passthrough to next handler if need 17 | next(w, r) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /internal/logic/user/pinglogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "github.com/zeromicro/go-zero/core/logx" 8 | ) 9 | 10 | type PingLogic struct { 11 | logx.Logger 12 | ctx context.Context 13 | svcCtx *svc.ServiceContext 14 | } 15 | 16 | func NewPingLogic(ctx context.Context, svcCtx *svc.ServiceContext) PingLogic { 17 | return PingLogic{ 18 | Logger: logx.WithContext(ctx), 19 | ctx: ctx, 20 | svcCtx: svcCtx, 21 | } 22 | } 23 | 24 | func (l *PingLogic) Ping() error { 25 | // todo: add your logic here and delete this line 26 | 27 | return nil 28 | } 29 | -------------------------------------------------------------------------------- /user/api/internal/handler/userinfohandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | "datacenter/user/api/internal/logic" 7 | "datacenter/user/api/internal/svc" 8 | 9 | "github.com/zeromicro/go-zero/rest/httpx" 10 | ) 11 | 12 | func userInfoHandler(ctx *svc.ServiceContext) http.HandlerFunc { 13 | return func(w http.ResponseWriter, r *http.Request) { 14 | 15 | userId := r.Header.Get("x-user-id") 16 | l := logic.NewUserInfoLogic(r.Context(), ctx) 17 | resp, err := l.UserInfo(userId) 18 | if err != nil { 19 | httpx.Error(w, err) 20 | } else { 21 | httpx.OkJson(w, resp) 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /user/api/internal/logic/pinglogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/user/api/internal/svc" 7 | "github.com/zeromicro/go-zero/core/logx" 8 | ) 9 | 10 | type PingLogic struct { 11 | logx.Logger 12 | ctx context.Context 13 | svcCtx *svc.ServiceContext 14 | } 15 | 16 | func NewPingLogic(ctx context.Context, svcCtx *svc.ServiceContext) PingLogic { 17 | return PingLogic{ 18 | Logger: logx.WithContext(ctx), 19 | ctx: ctx, 20 | svcCtx: svcCtx, 21 | } 22 | } 23 | 24 | func (l *PingLogic) Ping() error { 25 | // todo: add your logic here and delete this line 26 | 27 | return nil 28 | } 29 | -------------------------------------------------------------------------------- /internal/handler/search/articleinithandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/search" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/shared" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func ArticleInitHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.SearchReq 17 | l := logic.NewArticleInitLogic(r.Context(), ctx) 18 | resp, err := l.ArticleInit(req) 19 | if err != nil { 20 | httpx.Error(w, err) 21 | } else { 22 | shared.OkJson(w, resp) 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /search/rpc/search.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package search; 4 | 5 | message LimitReq{ 6 | int64 offset=1; 7 | int64 size=2; 8 | } 9 | message SearchReq{ 10 | string Keyword=1; 11 | LimitReq Limit=2; 12 | } 13 | message ArticleResp{ 14 | repeated ArticleReq List=1; 15 | } 16 | message ArticleReq { 17 | string NewsId = 1; 18 | string NewsTitle = 2; 19 | string ImageUrl = 3; 20 | } 21 | 22 | message Request{ 23 | bool Once=1; 24 | } 25 | 26 | message Response{ 27 | } 28 | 29 | service Search { 30 | rpc ArticleInit(Request) returns(Response); 31 | rpc ArticleStore(ArticleReq) returns(Response); 32 | rpc ArticleSearch(SearchReq) returns(ArticleResp); 33 | } 34 | -------------------------------------------------------------------------------- /common/rpc/common.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package common; 4 | 5 | 6 | message BaseAppReq{ 7 | int64 beid=1; 8 | } 9 | 10 | message BaseAppResp{ 11 | int64 beid=1; 12 | string logo=2; 13 | string sname=3; 14 | int64 isclose=4; 15 | string fullwebsite=5; 16 | } 17 | 18 | //请求的api 19 | message AppConfigReq { 20 | int64 beid=1; 21 | int64 ptyid=2; 22 | } 23 | 24 | //返回的值 25 | message AppConfigResp { 26 | int64 id=1; 27 | int64 beid=2; 28 | int64 ptyid=3; 29 | string appid=4; 30 | string appsecret=5; 31 | string title=6; 32 | } 33 | 34 | service Common { 35 | rpc GetAppConfig(AppConfigReq) returns(AppConfigResp); 36 | rpc GetBaseApp(BaseAppReq) returns(BaseAppResp); 37 | } 38 | -------------------------------------------------------------------------------- /common/rpc/internal/svc/servicecontext.go: -------------------------------------------------------------------------------- 1 | package svc 2 | 3 | import ( 4 | "datacenter/common/model" 5 | "datacenter/common/rpc/internal/config" 6 | 7 | "github.com/zeromicro/go-zero/core/stores/sqlx" 8 | ) 9 | 10 | type ServiceContext struct { 11 | c config.Config 12 | AppConfigModel model.AppConfigModel 13 | BaseAppModel model.BaseAppModel 14 | } 15 | 16 | func NewServiceContext(c config.Config) *ServiceContext { 17 | conn := sqlx.NewMysql(c.Mysql.DataSource) 18 | apm := model.NewAppConfigModel(conn, c.CacheRedis) 19 | bam := model.NewBaseAppModel(conn, c.CacheRedis) 20 | return &ServiceContext{ 21 | c: c, 22 | AppConfigModel: apm, 23 | BaseAppModel: bam, 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /internal/logic/search/articlelogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/search/rpc/searchclient" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type ArticleLogic struct { 13 | logx.Logger 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) ArticleLogic { 19 | return ArticleLogic{ 20 | Logger: logx.WithContext(ctx), 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | } 24 | } 25 | 26 | func (l *ArticleLogic) Article(req searchclient.SearchReq) (*searchclient.ArticleResp, error) { 27 | return l.svcCtx.SearchRpc.ArticleSearch(l.ctx, &req) 28 | } 29 | -------------------------------------------------------------------------------- /user/rpc/internal/logic/pinglogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/user/rpc/internal/svc" 7 | "datacenter/user/rpc/user" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type PingLogic struct { 13 | ctx context.Context 14 | svcCtx *svc.ServiceContext 15 | logx.Logger 16 | } 17 | 18 | func NewPingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PingLogic { 19 | return &PingLogic{ 20 | ctx: ctx, 21 | svcCtx: svcCtx, 22 | Logger: logx.WithContext(ctx), 23 | } 24 | } 25 | 26 | func (l *PingLogic) Ping(in *user.Request) (*user.Response, error) { 27 | // todo: add your logic here and delete this line 28 | 29 | return &user.Response{}, nil 30 | } 31 | -------------------------------------------------------------------------------- /internal/logic/votes/enrollinfologic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/votes/rpc/votes" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type EnrollInfoLogic struct { 13 | logx.Logger 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewEnrollInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) EnrollInfoLogic { 19 | return EnrollInfoLogic{ 20 | Logger: logx.WithContext(ctx), 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | } 24 | } 25 | 26 | func (l *EnrollInfoLogic) EnrollInfo(in *votes.EnrollInfoReq) (*votes.EnrollResp, error) { 27 | 28 | return l.svcCtx.VotesRpc.GetEnrollInfo(l.ctx, in) 29 | } 30 | -------------------------------------------------------------------------------- /internal/handler/user/userinfohandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/user" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | 10 | "github.com/zeromicro/go-zero/rest/httpx" 11 | ) 12 | 13 | func UserInfoHandler(ctx *svc.ServiceContext) http.HandlerFunc { 14 | return func(w http.ResponseWriter, r *http.Request) { 15 | var req types.UserReq 16 | if err := httpx.Parse(r, &req); err != nil { 17 | httpx.Error(w, err) 18 | return 19 | } 20 | 21 | l := logic.NewUserInfoLogic(r.Context(), ctx) 22 | resp, err := l.UserInfo(req) 23 | if err != nil { 24 | httpx.Error(w, err) 25 | } else { 26 | httpx.OkJson(w, resp) 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /internal/logic/questions/lotterylogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/questions/rpc/questionsclient" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type LotteryLogic struct { 13 | logx.Logger 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewLotteryLogic(ctx context.Context, svcCtx *svc.ServiceContext) LotteryLogic { 19 | return LotteryLogic{ 20 | Logger: logx.WithContext(ctx), 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | } 24 | } 25 | 26 | func (l *LotteryLogic) Lottery(in questionsclient.ConvertReq) (*questionsclient.ConvertResp, error) { 27 | return l.svcCtx.QuestionsRpc.PostConvert(l.ctx, &in) 28 | } 29 | -------------------------------------------------------------------------------- /internal/middleware/admincheckmiddleware.go: -------------------------------------------------------------------------------- 1 | package middleware 2 | 3 | import ( 4 | "errors" 5 | "net/http" 6 | ) 7 | 8 | var ( 9 | AdminError = errors.New("非法的用户请求") 10 | ) 11 | 12 | type AdminCheckMiddleware struct { 13 | } 14 | 15 | func NewAdminCheckMiddleware() *AdminCheckMiddleware { 16 | return &AdminCheckMiddleware{} 17 | } 18 | 19 | func (m *AdminCheckMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc { 20 | return func(w http.ResponseWriter, r *http.Request) { 21 | // user, pass, _ := r.BasicAuth() 22 | // logx.Info("user, pass", user, pass) 23 | // if user == "xxx" && pass == "xxxxx" { 24 | // next(w, r) 25 | // } else { 26 | // httpx.Error(w, AdminError) 27 | // } 28 | next(w, r) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /internal/handler/user/code2sessionhandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | 7 | logic "datacenter/internal/logic/user" 8 | "datacenter/internal/svc" 9 | "datacenter/shared" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func Code2SessionHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | 17 | query := r.URL.Query() 18 | code := query.Get("code") 19 | 20 | l := logic.NewCode2SessionLogic(r.Context(), ctx) 21 | 22 | fmt.Println("请求的code 是", code) 23 | resp, err := l.Code2Session(1, 1, code) 24 | if err != nil { 25 | httpx.Error(w, err) 26 | } else { 27 | shared.OkJson(w, resp) 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /internal/logic/questions/listslogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/questions/rpc/questionsclient" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type ListsLogic struct { 13 | logx.Logger 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewListsLogic(ctx context.Context, svcCtx *svc.ServiceContext) ListsLogic { 19 | return ListsLogic{ 20 | Logger: logx.WithContext(ctx), 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | } 24 | } 25 | 26 | func (l *ListsLogic) Lists(in questionsclient.ActivitiesReq) (*questionsclient.QuestionsListResp, error) { 27 | return l.svcCtx.QuestionsRpc.GetQuestionsList(l.ctx, &in) 28 | 29 | } 30 | -------------------------------------------------------------------------------- /internal/logic/votes/activityinfologic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/votes/rpc/votes" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type ActivityInfoLogic struct { 13 | logx.Logger 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewActivityInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) ActivityInfoLogic { 19 | return ActivityInfoLogic{ 20 | Logger: logx.WithContext(ctx), 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | } 24 | } 25 | 26 | func (l *ActivityInfoLogic) ActivityInfo(req votes.ActInfoReq) (*votes.ActInfoResp, error) { 27 | 28 | return l.svcCtx.VotesRpc.GetActivityInfo(l.ctx, &req) 29 | } 30 | -------------------------------------------------------------------------------- /user/api/internal/handler/loginhandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | "datacenter/user/api/internal/logic" 7 | "datacenter/user/api/internal/svc" 8 | "datacenter/user/api/internal/types" 9 | 10 | "github.com/zeromicro/go-zero/rest/httpx" 11 | ) 12 | 13 | func loginHandler(ctx *svc.ServiceContext) http.HandlerFunc { 14 | return func(w http.ResponseWriter, r *http.Request) { 15 | var req types.LoginReq 16 | if err := httpx.Parse(r, &req); err != nil { 17 | httpx.Error(w, err) 18 | return 19 | } 20 | 21 | l := logic.NewLoginLogic(r.Context(), ctx) 22 | resp, err := l.Login(req) 23 | if err != nil { 24 | httpx.Error(w, err) 25 | } else { 26 | httpx.OkJson(w, resp) 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /user/api/internal/handler/registerhandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | "datacenter/user/api/internal/logic" 7 | "datacenter/user/api/internal/svc" 8 | "datacenter/user/api/internal/types" 9 | 10 | "github.com/zeromicro/go-zero/rest/httpx" 11 | ) 12 | 13 | func registerHandler(ctx *svc.ServiceContext) http.HandlerFunc { 14 | return func(w http.ResponseWriter, r *http.Request) { 15 | var req types.RegisterReq 16 | if err := httpx.Parse(r, &req); err != nil { 17 | httpx.Error(w, err) 18 | return 19 | } 20 | 21 | l := logic.NewRegisterLogic(r.Context(), ctx) 22 | err := l.Register(req) 23 | if err != nil { 24 | httpx.Error(w, err) 25 | } else { 26 | httpx.Ok(w) 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /internal/handler/user/wxloginhandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | "datacenter/internal/logic/user" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/shared" 10 | "github.com/zeromicro/go-zero/rest/httpx" 11 | ) 12 | 13 | func WxloginHandler(ctx *svc.ServiceContext) http.HandlerFunc { 14 | return func(w http.ResponseWriter, r *http.Request) { 15 | var req types.WxLoginReq 16 | if err := httpx.Parse(r, &req); err != nil { 17 | httpx.Error(w, err) 18 | return 19 | } 20 | 21 | l := logic.NewWxloginLogic(r.Context(), ctx) 22 | resp, err := l.Wxlogin(req) 23 | if err != nil { 24 | httpx.Error(w, err) 25 | } else { 26 | shared.OkJson(w, resp) 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /internal/logic/questions/changelogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/questions/rpc/questions" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type ChangeLogic struct { 13 | logx.Logger 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewChangeLogic(ctx context.Context, svcCtx *svc.ServiceContext) ChangeLogic { 19 | return ChangeLogic{ 20 | Logger: logx.WithContext(ctx), 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | } 24 | } 25 | 26 | func (l *ChangeLogic) Change(in questions.QuestionsAnswerReq) (*questions.QuestionsAnswerResp, error) { 27 | 28 | return l.svcCtx.QuestionsRpc.PostQuestionsChange(l.ctx, &in) 29 | 30 | } 31 | -------------------------------------------------------------------------------- /internal/handler/common/appinfohandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/common" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/shared" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func AppInfoHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.Beid 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.Error(w, err) 19 | return 20 | } 21 | 22 | l := logic.NewAppInfoLogic(r.Context(), ctx) 23 | resp, err := l.AppInfo(req) 24 | if err != nil { 25 | httpx.Error(w, err) 26 | } else { 27 | shared.OkJson(w, resp) 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /internal/logic/questions/awardinfologic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/internal/types" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type AwardInfoLogic struct { 13 | logx.Logger 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewAwardInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) AwardInfoLogic { 19 | return AwardInfoLogic{ 20 | Logger: logx.WithContext(ctx), 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | } 24 | } 25 | 26 | func (l *AwardInfoLogic) AwardInfo(req types.Actid) (*types.ActivityResp, error) { 27 | // todo: add your logic here and delete this line 28 | 29 | return &types.ActivityResp{}, nil 30 | } 31 | -------------------------------------------------------------------------------- /internal/logic/votes/activityicrviewlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/votes/rpc/votes" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type ActivityIcrViewLogic struct { 13 | logx.Logger 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewActivityIcrViewLogic(ctx context.Context, svcCtx *svc.ServiceContext) ActivityIcrViewLogic { 19 | return ActivityIcrViewLogic{ 20 | Logger: logx.WithContext(ctx), 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | } 24 | } 25 | 26 | func (l *ActivityIcrViewLogic) ActivityIcrView(req votes.ActInfoReq) (*votes.ActInfoResp, error) { 27 | return l.svcCtx.VotesRpc.IncrActiviView(l.ctx, &req) 28 | } 29 | -------------------------------------------------------------------------------- /user/api/internal/svc/servicecontext.go: -------------------------------------------------------------------------------- 1 | package svc 2 | 3 | import ( 4 | "datacenter/user/api/internal/config" 5 | "datacenter/user/api/internal/middleware" 6 | "datacenter/user/model" 7 | 8 | "github.com/zeromicro/go-zero/core/stores/sqlx" 9 | "github.com/zeromicro/go-zero/rest" 10 | ) 11 | 12 | type ServiceContext struct { 13 | Config config.Config 14 | UserModel *model.BaseMemberModel 15 | UserCheck rest.Middleware 16 | } 17 | 18 | func NewServiceContext(c config.Config) *ServiceContext { 19 | conn := sqlx.NewMysql(c.Mysql.DataSource) 20 | um := model.NewBaseMemberModel(conn, c.CacheRedis) 21 | 22 | return &ServiceContext{ 23 | Config: c, 24 | UserModel: um, 25 | UserCheck: middleware.NewUserCheckMiddleware().Handle, 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /etc/datacenter-api.example.yaml: -------------------------------------------------------------------------------- 1 | Name: datacenter-api 2 | Log: 3 | Mode: console 4 | Host: 0.0.0.0 5 | Port: 8887 6 | Auth: 7 | AccessSecret: ad87xxxxxxx5d54b7d 8 | AccessExpire: 86400 9 | CacheRedis: 10 | - Host: 127.0.0.1:6379 11 | Pass: admin 12 | Type: node 13 | UserRpc: 14 | Etcd: 15 | Hosts: 16 | - 127.0.0.1:2379 17 | Key: user.rpc 18 | CommonRpc: 19 | Etcd: 20 | Hosts: 21 | - 127.0.0.1:2379 22 | Key: common.rpc 23 | VotesRpc: 24 | Etcd: 25 | Hosts: 26 | - 127.0.0.1:2379 27 | Key: votes.rpc 28 | SearchRpc: 29 | Etcd: 30 | Hosts: 31 | - 127.0.0.1:2379 32 | Key: search.rpc 33 | QuestionsRpc: 34 | Etcd: 35 | Hosts: 36 | - 127.0.0.1:2379 37 | Key: questions.rpc -------------------------------------------------------------------------------- /internal/handler/common/wxtickethandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/common" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/shared" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func WxTicketHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.SnsReq 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.Error(w, err) 19 | return 20 | } 21 | 22 | l := logic.NewWxTicketLogic(r.Context(), ctx) 23 | resp, err := l.WxTicket(req) 24 | if err != nil { 25 | httpx.Error(w, err) 26 | } else { 27 | shared.OkJson(w, resp) 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /internal/handler/user/registerhandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/user" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/shared" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func RegisterHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.RegisterReq 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.Error(w, err) 19 | return 20 | } 21 | 22 | l := logic.NewRegisterLogic(r.Context(), ctx) 23 | resp, err := l.Register(req) 24 | if err != nil { 25 | httpx.Error(w, err) 26 | } else { 27 | shared.OkJson(w, resp) 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /internal/handler/votes/enrollhandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/votes" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/shared" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func EnrollHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | 17 | var req types.EnrollReq 18 | 19 | if err := httpx.Parse(r, &req); err != nil { 20 | httpx.Error(w, err) 21 | return 22 | } 23 | l := logic.NewEnrollLogic(r.Context(), ctx) 24 | resp, err := l.Enroll(req) 25 | if err != nil { 26 | httpx.Error(w, err) 27 | } else { 28 | shared.OkJson(w, resp) 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /internal/logic/questions/awardlistlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/questions/rpc/questionsclient" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type AwardListLogic struct { 13 | logx.Logger 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewAwardListLogic(ctx context.Context, svcCtx *svc.ServiceContext) AwardListLogic { 19 | return AwardListLogic{ 20 | Logger: logx.WithContext(ctx), 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | } 24 | } 25 | 26 | func (l *AwardListLogic) AwardList(in questionsclient.ActivitiesReq) (*questionsclient.AwardListResp, error) { 27 | 28 | return l.svcCtx.QuestionsRpc.GetAwardList(l.ctx, &in) 29 | 30 | } 31 | -------------------------------------------------------------------------------- /internal/logic/questions/turntablelogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/questions/rpc/questionsclient" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type TurntableLogic struct { 13 | logx.Logger 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewTurntableLogic(ctx context.Context, svcCtx *svc.ServiceContext) TurntableLogic { 19 | return TurntableLogic{ 20 | Logger: logx.WithContext(ctx), 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | } 24 | } 25 | 26 | func (l *TurntableLogic) Turntable(req questionsclient.TurnTableReq) (*questionsclient.AwardInfoResp, error) { 27 | 28 | return l.svcCtx.QuestionsRpc.PostTurnTable(l.ctx, &req) 29 | } 30 | -------------------------------------------------------------------------------- /internal/handler/common/qiuniutokenhandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/common" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/shared" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func QiuniuTokenHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.Beid 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.Error(w, err) 19 | return 20 | } 21 | 22 | l := logic.NewQiuniuTokenLogic(r.Context(), ctx) 23 | resp, err := l.QiuniuToken(req) 24 | if err != nil { 25 | httpx.Error(w, err) 26 | } else { 27 | shared.OkJson(w, resp) 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /internal/handler/questions/awardinfohandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/questions" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/shared" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func AwardInfoHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.Actid 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.Error(w, err) 19 | return 20 | } 21 | 22 | l := logic.NewAwardInfoLogic(r.Context(), ctx) 23 | resp, err := l.AwardInfo(req) 24 | if err != nil { 25 | httpx.Error(w, err) 26 | } else { 27 | shared.OkJson(w, resp) 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /internal/handler/votes/votehandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/votes" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/shared" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func VoteHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.VoteReq 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.Error(w, err) 19 | return 20 | } 21 | //获取用户的信息 22 | 23 | l := logic.NewVoteLogic(r.Context(), ctx) 24 | //获取用户的 25 | resp, err := l.Vote(req) 26 | if err != nil { 27 | httpx.Error(w, err) 28 | return 29 | } else { 30 | shared.OkJson(w, resp) 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /internal/handler/datacenterhandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | "datacenter/internal/logic" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func datacenterHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.Request 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.Error(w, err) 19 | return 20 | } 21 | logx.Info("121212121") 22 | l := logic.NewDatacenterLogic(r.Context(), ctx) 23 | resp, err := l.Datacenter(req) 24 | if err != nil { 25 | httpx.Error(w, err) 26 | } else { 27 | httpx.OkJson(w, resp) 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /internal/logic/common/votesverificationlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/internal/types" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type VotesVerificationLogic struct { 13 | logx.Logger 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewVotesVerificationLogic(ctx context.Context, svcCtx *svc.ServiceContext) VotesVerificationLogic { 19 | return VotesVerificationLogic{ 20 | Logger: logx.WithContext(ctx), 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | } 24 | } 25 | 26 | func (l *VotesVerificationLogic) VotesVerification(req types.SnsReq) (*types.SnsResp, error) { 27 | // todo: add your logic here and delete this line 28 | 29 | return &types.SnsResp{}, nil 30 | } 31 | -------------------------------------------------------------------------------- /search.api: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | type SearchReq struct { 6 | Keyword string `json:"keyword"` 7 | Page string `json:"page"` 8 | Size string `json:"size"` 9 | } 10 | type SearchResp struct { 11 | Data []ArticleReq `json:"data"` 12 | } 13 | 14 | type ArticleReq struct{ 15 | NewsId string `json:"NewsId"` 16 | NewsTitle string `json:"NewsTitle"` 17 | ImageUrl string `json:"ImageUrl"` 18 | } 19 | 20 | 21 | @server( 22 | group: search 23 | middleware: Admincheck 24 | ) 25 | service datacenter-api { 26 | @doc( 27 | summary: "搜索" 28 | ) 29 | @handler article 30 | get /search/article (SearchReq) returns (SearchResp) 31 | @handler articleInit 32 | get /search/articel/init (SearchReq) returns (SearchResp) 33 | @handler articleStore 34 | post /search/articel/store (ArticleReq) returns (ArticleReq) 35 | } 36 | -------------------------------------------------------------------------------- /internal/handler/questions/listshandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/questions" 7 | "datacenter/internal/svc" 8 | "datacenter/questions/rpc/questionsclient" 9 | "datacenter/shared" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func ListsHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | query := r.URL.Query() 17 | actid := shared.StrToInt64(query.Get("actid")) 18 | activitReq := questionsclient.ActivitiesReq{ 19 | Actid: actid, 20 | } 21 | 22 | l := logic.NewListsLogic(r.Context(), ctx) 23 | resp, err := l.Lists(activitReq) 24 | if err != nil { 25 | httpx.Error(w, err) 26 | } else { 27 | shared.OkJson(w, resp) 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /internal/handler/votes/activityicrviewhandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/votes" 7 | "datacenter/internal/svc" 8 | "datacenter/shared" 9 | "datacenter/votes/rpc/votes" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func ActivityIcrViewHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | query := r.URL.Query() 17 | actid := shared.StrToInt64(query.Get("actid")) 18 | activitReq := votes.ActInfoReq{ 19 | Actid: actid, 20 | } 21 | 22 | l := logic.NewActivityIcrViewLogic(r.Context(), ctx) 23 | resp, err := l.ActivityIcrView(activitReq) 24 | if err != nil { 25 | httpx.Error(w, err) 26 | } else { 27 | shared.OkJson(w, resp) 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /internal/handler/votes/activityinfohandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/votes" 7 | "datacenter/internal/svc" 8 | "datacenter/shared" 9 | "datacenter/votes/rpc/votes" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func ActivityInfoHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | query := r.URL.Query() 17 | 18 | actid := shared.StrToInt64(query.Get("actid")) 19 | activitReq := votes.ActInfoReq{ 20 | Actid: actid, 21 | } 22 | 23 | l := logic.NewActivityInfoLogic(r.Context(), ctx) 24 | 25 | resp, err := l.ActivityInfo(activitReq) 26 | if err != nil { 27 | httpx.Error(w, err) 28 | } else { 29 | shared.OkJson(w, resp) 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /internal/logic/search/articleinitlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/internal/types" 8 | "datacenter/search/rpc/search" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type ArticleInitLogic struct { 14 | logx.Logger 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | } 18 | 19 | func NewArticleInitLogic(ctx context.Context, svcCtx *svc.ServiceContext) ArticleInitLogic { 20 | return ArticleInitLogic{ 21 | Logger: logx.WithContext(ctx), 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | } 25 | } 26 | 27 | func (l *ArticleInitLogic) ArticleInit(req types.SearchReq) (*types.SearchResp, error) { 28 | _, err := l.svcCtx.SearchRpc.ArticleInit(l.ctx, &search.Request{}) 29 | return &types.SearchResp{}, err 30 | } 31 | -------------------------------------------------------------------------------- /votes/rpc/internal/logic/incractiviviewlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/votes/rpc/internal/svc" 7 | "datacenter/votes/rpc/votes" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type IncrActiviViewLogic struct { 13 | ctx context.Context 14 | svcCtx *svc.ServiceContext 15 | logx.Logger 16 | } 17 | 18 | func NewIncrActiviViewLogic(ctx context.Context, svcCtx *svc.ServiceContext) *IncrActiviViewLogic { 19 | return &IncrActiviViewLogic{ 20 | ctx: ctx, 21 | svcCtx: svcCtx, 22 | Logger: logx.WithContext(ctx), 23 | } 24 | } 25 | 26 | // 增加活动的爆光量 27 | func (l *IncrActiviViewLogic) IncrActiviView(in *votes.ActInfoReq) (*votes.ActInfoResp, error) { 28 | err := l.svcCtx.AppVotesActivityModel.IncrView(in.Actid) 29 | return &votes.ActInfoResp{}, err 30 | } 31 | -------------------------------------------------------------------------------- /internal/handler/questions/awardlisthandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/questions" 7 | "datacenter/internal/svc" 8 | "datacenter/questions/rpc/questionsclient" 9 | "datacenter/shared" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func AwardListHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | query := r.URL.Query() 17 | actid := shared.StrToInt64(query.Get("actid")) 18 | activitReq := questionsclient.ActivitiesReq{ 19 | Actid: actid, 20 | } 21 | 22 | l := logic.NewAwardListLogic(r.Context(), ctx) 23 | resp, err := l.AwardList(activitReq) 24 | if err != nil { 25 | httpx.Error(w, err) 26 | } else { 27 | shared.OkJson(w, resp) 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /internal/logic/questions/gradelogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/questions/rpc/questions" 8 | "datacenter/questions/rpc/questionsclient" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type GradeLogic struct { 14 | logx.Logger 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | } 18 | 19 | func NewGradeLogic(ctx context.Context, svcCtx *svc.ServiceContext) GradeLogic { 20 | return GradeLogic{ 21 | Logger: logx.WithContext(ctx), 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | } 25 | } 26 | 27 | func (l *GradeLogic) Grade(in questionsclient.GradeReq) (*questions.QuestionsAnswerResp, error) { 28 | // todo: add your logic here and delete this line 29 | return l.svcCtx.QuestionsRpc.GetQuestionsGrade(l.ctx, &in) 30 | 31 | } 32 | -------------------------------------------------------------------------------- /internal/logic/questions/activitiesinfologic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/questions/rpc/questionsclient" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type ActivitiesInfoLogic struct { 13 | logx.Logger 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewActivitiesInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) ActivitiesInfoLogic { 19 | return ActivitiesInfoLogic{ 20 | Logger: logx.WithContext(ctx), 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | } 24 | } 25 | 26 | /** 27 | * 获取 活动信息 28 | */ 29 | func (l *ActivitiesInfoLogic) ActivitiesInfo(req questionsclient.ActivitiesReq) (*questionsclient.ActInfoResp, error) { 30 | 31 | return l.svcCtx.QuestionsRpc.GetActivitiesInfo(l.ctx, &req) 32 | 33 | } 34 | -------------------------------------------------------------------------------- /search/rpc/internal/svc/servicecontext.go: -------------------------------------------------------------------------------- 1 | package svc 2 | 3 | import ( 4 | "datacenter/search/rpc/dao" 5 | "datacenter/search/rpc/internal/config" 6 | "strings" 7 | 8 | "github.com/olivere/elastic/v7" 9 | ) 10 | 11 | type ServiceContext struct { 12 | c config.Config 13 | Esconn *elastic.Client 14 | ArticeEs *dao.ArticelES 15 | } 16 | 17 | func NewServiceContext(c config.Config) *ServiceContext { 18 | 19 | client, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(strings.Join(c.Esconfig.Urls, ",")), elastic.SetBasicAuth(c.Esconfig.User, c.Esconfig.Password)) 20 | if err != nil { 21 | panic(err) 22 | } 23 | ArticeEs, err := dao.NewArticelES(client) 24 | if err != nil { 25 | panic(err) 26 | } 27 | return &ServiceContext{ 28 | c: c, 29 | Esconn: client, 30 | ArticeEs: ArticeEs, 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /internal/handler/questions/activitiesinfohandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/questions" 7 | "datacenter/internal/svc" 8 | "datacenter/questions/rpc/questionsclient" 9 | "datacenter/shared" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func ActivitiesInfoHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | query := r.URL.Query() 17 | actid := shared.StrToInt64(query.Get("actid")) 18 | activitReq := questionsclient.ActivitiesReq{ 19 | Actid: actid, 20 | } 21 | 22 | l := logic.NewActivitiesInfoLogic(r.Context(), ctx) 23 | resp, err := l.ActivitiesInfo(activitReq) 24 | if err != nil { 25 | httpx.Error(w, err) 26 | } else { 27 | shared.OkJson(w, resp) 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /search/rpc/internal/logic/articlestorelogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/search/rpc/internal/svc" 7 | "datacenter/search/rpc/search" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type ArticleStoreLogic struct { 13 | ctx context.Context 14 | svcCtx *svc.ServiceContext 15 | logx.Logger 16 | } 17 | 18 | func NewArticleStoreLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ArticleStoreLogic { 19 | return &ArticleStoreLogic{ 20 | ctx: ctx, 21 | svcCtx: svcCtx, 22 | Logger: logx.WithContext(ctx), 23 | } 24 | } 25 | 26 | func (l *ArticleStoreLogic) ArticleStore(in *search.ArticleReq) (*search.Response, error) { 27 | id, err := l.svcCtx.ArticeEs.Index(in) 28 | if err != nil { 29 | return nil, err 30 | } 31 | logx.Info(id) 32 | return &search.Response{}, nil 33 | } 34 | -------------------------------------------------------------------------------- /votes/rpc/internal/logic/error.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import "errors" 4 | 5 | var ( 6 | ActivityDoesNotExist = errors.New("活动不存在") 7 | ActivityEnd = errors.New("活动已结束") 8 | ActivityDoesNotOpen = errors.New("活动未开启") 9 | RegistrationActivityDoesNotEnroll = errors.New("活动还未开始报名") 10 | ActivityDoesNotStart = errors.New("活动还未开始") 11 | RegistrationActivityDoesNotEnrollEND = errors.New("报名活动未开启") 12 | EnrollFalt = errors.New("报名失败") 13 | YouHaveSignedUp = errors.New("你已经报名") 14 | ActivityDoesNotEnroll = errors.New("活动还未开始投票") 15 | VotesLock = errors.New("当前投票处理中,请稍后重试") 16 | VotesFailt = errors.New("投票失败") 17 | 18 | EnrollNotExist = errors.New("投票的作品不存在") 19 | ) 20 | -------------------------------------------------------------------------------- /search/rpc/internal/logic/articlesearchlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/search/rpc/internal/svc" 7 | "datacenter/search/rpc/search" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type ArticleSearchLogic struct { 13 | ctx context.Context 14 | svcCtx *svc.ServiceContext 15 | logx.Logger 16 | } 17 | 18 | func NewArticleSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ArticleSearchLogic { 19 | return &ArticleSearchLogic{ 20 | ctx: ctx, 21 | svcCtx: svcCtx, 22 | Logger: logx.WithContext(ctx), 23 | } 24 | } 25 | 26 | func (l *ArticleSearchLogic) ArticleSearch(in *search.SearchReq) (*search.ArticleResp, error) { 27 | 28 | list, err := l.svcCtx.ArticeEs.Search(in) 29 | if err != nil { 30 | return nil, err 31 | } 32 | 33 | return &search.ArticleResp{List: list}, nil 34 | } 35 | -------------------------------------------------------------------------------- /internal/handler/user/loginhandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/user" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/shared" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func LoginHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.LoginReq 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.Error(w, err) 19 | return 20 | } 21 | ptyid := shared.StrToInt64(r.Header.Get("ptyid")) 22 | beid := shared.StrToInt64(r.Header.Get("beid")) 23 | 24 | l := logic.NewLoginLogic(r.Context(), ctx) 25 | resp, err := l.Login(req, beid, ptyid) 26 | if err != nil { 27 | httpx.Error(w, err) 28 | } else { 29 | shared.OkJson(w, resp) 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /internal/handler/votes/enrollinfohandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/votes" 7 | "datacenter/internal/svc" 8 | "datacenter/shared" 9 | "datacenter/votes/rpc/votes" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func EnrollInfoHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | 17 | query := r.URL.Query() 18 | aeid := shared.StrToInt64(query.Get("aeid")) 19 | actid := shared.StrToInt64(query.Get("actid")) 20 | 21 | eReq := &votes.EnrollInfoReq{ 22 | Actid: actid, 23 | Aeid: aeid, 24 | } 25 | 26 | l := logic.NewEnrollInfoLogic(r.Context(), ctx) 27 | resp, err := l.EnrollInfo(eReq) 28 | if err != nil { 29 | httpx.Error(w, err) 30 | } else { 31 | shared.OkJson(w, resp) 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /internal/logic/search/articlestorelogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/internal/types" 8 | "datacenter/search/rpc/searchclient" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type ArticleStoreLogic struct { 14 | logx.Logger 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | } 18 | 19 | func NewArticleStoreLogic(ctx context.Context, svcCtx *svc.ServiceContext) ArticleStoreLogic { 20 | return ArticleStoreLogic{ 21 | Logger: logx.WithContext(ctx), 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | } 25 | } 26 | 27 | func (l *ArticleStoreLogic) ArticleStore(req searchclient.ArticleReq) (*types.ArticleReq, error) { 28 | _, err := l.svcCtx.SearchRpc.ArticleStore(l.ctx, &req) 29 | if err != nil { 30 | return nil, err 31 | } 32 | 33 | return &types.ArticleReq{}, nil 34 | } 35 | -------------------------------------------------------------------------------- /user/api/user.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | 7 | "datacenter/shared" 8 | "datacenter/user/api/internal/config" 9 | "datacenter/user/api/internal/handler" 10 | "datacenter/user/api/internal/svc" 11 | 12 | "github.com/zeromicro/go-zero/core/conf" 13 | "github.com/zeromicro/go-zero/rest" 14 | 15 | "github.com/zeromicro/go-zero/rest/httpx" 16 | ) 17 | 18 | var configFile = flag.String("f", "etc/user-api.yaml", "the config file") 19 | 20 | func main() { 21 | flag.Parse() 22 | 23 | var c config.Config 24 | conf.MustLoad(*configFile, &c) 25 | 26 | ctx := svc.NewServiceContext(c) 27 | server := rest.MustNewServer(c.RestConf) 28 | defer server.Stop() 29 | 30 | httpx.SetErrorHandler(shared.ErrorHandler) 31 | handler.RegisterHandlers(server, ctx) 32 | 33 | fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) 34 | server.Start() 35 | } 36 | -------------------------------------------------------------------------------- /internal/handler/search/articlehandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/search" 7 | "datacenter/internal/svc" 8 | "datacenter/search/rpc/searchclient" 9 | "datacenter/shared" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func ArticleHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | query := r.URL.Query() 17 | 18 | offset, size := shared.ToLimitOffset(query.Get("page"), query.Get("size")) 19 | req := searchclient.SearchReq{ 20 | Limit: &searchclient.LimitReq{ 21 | Offset: offset, 22 | Size: size, 23 | }, 24 | Keyword: query.Get("keyword"), 25 | } 26 | l := logic.NewArticleLogic(r.Context(), ctx) 27 | resp, err := l.Article(req) 28 | if err != nil { 29 | httpx.Error(w, err) 30 | } else { 31 | shared.OkJson(w, resp) 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /common/rpc/internal/server/commonserver.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: common.proto 3 | 4 | package server 5 | 6 | import ( 7 | "context" 8 | 9 | "datacenter/common/rpc/common" 10 | "datacenter/common/rpc/internal/logic" 11 | "datacenter/common/rpc/internal/svc" 12 | ) 13 | 14 | type CommonServer struct { 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewCommonServer(svcCtx *svc.ServiceContext) *CommonServer { 19 | return &CommonServer{ 20 | svcCtx: svcCtx, 21 | } 22 | } 23 | 24 | func (s *CommonServer) GetAppConfig(ctx context.Context, in *common.AppConfigReq) (*common.AppConfigResp, error) { 25 | l := logic.NewGetAppConfigLogic(ctx, s.svcCtx) 26 | return l.GetAppConfig(in) 27 | } 28 | 29 | func (s *CommonServer) GetBaseApp(ctx context.Context, in *common.BaseAppReq) (*common.BaseAppResp, error) { 30 | l := logic.NewGetBaseAppLogic(ctx, s.svcCtx) 31 | return l.GetBaseApp(in) 32 | } 33 | -------------------------------------------------------------------------------- /shared/error.go: -------------------------------------------------------------------------------- 1 | package shared 2 | 3 | import ( 4 | "errors" 5 | "net/http" 6 | ) 7 | 8 | const ( 9 | defaultCode = 1001 10 | ) 11 | 12 | var ( 13 | ErrNotFound = errors.New("data not find") 14 | ErrorUserNotFound = errors.New("用户不存在") 15 | ErrorNoRequiredParameters = errors.New("必要参数不能为空") 16 | ErrorUserOperation = errors.New("用户正在操作中,请稍后重试") 17 | ) 18 | 19 | type ( 20 | CodeError struct { 21 | Code int `json:"code"` 22 | Msg string `json:"msg"` 23 | } 24 | ) 25 | 26 | func ErrorHandler(err error) (int, interface{}) { 27 | return http.StatusConflict, CodeError{ 28 | Code: -1, 29 | Msg: err.Error(), 30 | } 31 | } 32 | 33 | func NewCodeError(code int, msg string) error { 34 | return &CodeError{Code: code, Msg: msg} 35 | } 36 | 37 | func NewDefaultError(msg string) error { 38 | return NewCodeError(defaultCode, msg) 39 | } 40 | 41 | func (e *CodeError) Error() string { 42 | return e.Msg 43 | } 44 | -------------------------------------------------------------------------------- /user/rpc/user.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: user.proto 3 | 4 | package main 5 | 6 | import ( 7 | "flag" 8 | "fmt" 9 | 10 | "datacenter/user/rpc/internal/config" 11 | "datacenter/user/rpc/internal/server" 12 | "datacenter/user/rpc/internal/svc" 13 | "datacenter/user/rpc/user" 14 | 15 | "github.com/zeromicro/go-zero/core/conf" 16 | "github.com/zeromicro/go-zero/zrpc" 17 | "google.golang.org/grpc" 18 | ) 19 | 20 | var configFile = flag.String("f", "etc/user.yaml", "the config file") 21 | 22 | func main() { 23 | flag.Parse() 24 | 25 | var c config.Config 26 | conf.MustLoad(*configFile, &c) 27 | ctx := svc.NewServiceContext(c) 28 | srv := server.NewUserServer(ctx) 29 | 30 | s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { 31 | user.RegisterUserServer(grpcServer, srv) 32 | }) 33 | defer s.Stop() 34 | 35 | fmt.Printf("Starting rpc server at %s...\n", c.ListenOn) 36 | s.Start() 37 | } 38 | -------------------------------------------------------------------------------- /internal/handler/search/articlestorehandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/search" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/search/rpc/searchclient" 10 | "datacenter/shared" 11 | 12 | "github.com/zeromicro/go-zero/rest/httpx" 13 | ) 14 | 15 | func ArticleStoreHandler(ctx *svc.ServiceContext) http.HandlerFunc { 16 | return func(w http.ResponseWriter, r *http.Request) { 17 | var req types.ArticleReq 18 | if err := httpx.Parse(r, &req); err != nil { 19 | httpx.Error(w, err) 20 | return 21 | } 22 | sreq := searchclient.ArticleReq{ 23 | ImageUrl: req.ImageUrl, 24 | NewsId: req.NewsId, 25 | NewsTitle: req.NewsTitle, 26 | } 27 | l := logic.NewArticleStoreLogic(r.Context(), ctx) 28 | resp, err := l.ArticleStore(sreq) 29 | if err != nil { 30 | httpx.Error(w, err) 31 | } else { 32 | shared.OkJson(w, resp) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /votes/rpc/rpc.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: votes.proto 3 | 4 | package main 5 | 6 | import ( 7 | "flag" 8 | "fmt" 9 | 10 | "datacenter/votes/rpc/internal/config" 11 | "datacenter/votes/rpc/internal/server" 12 | "datacenter/votes/rpc/internal/svc" 13 | "datacenter/votes/rpc/votes" 14 | 15 | "github.com/zeromicro/go-zero/core/conf" 16 | "github.com/zeromicro/go-zero/zrpc" 17 | "google.golang.org/grpc" 18 | ) 19 | 20 | var configFile = flag.String("f", "etc/rpc.yaml", "the config file") 21 | 22 | func main() { 23 | flag.Parse() 24 | 25 | var c config.Config 26 | conf.MustLoad(*configFile, &c) 27 | ctx := svc.NewServiceContext(c) 28 | srv := server.NewVotesServer(ctx) 29 | 30 | s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { 31 | votes.RegisterVotesServer(grpcServer, srv) 32 | }) 33 | defer s.Stop() 34 | 35 | fmt.Printf("Starting rpc server at %s...\n", c.ListenOn) 36 | s.Start() 37 | } 38 | -------------------------------------------------------------------------------- /votes/rpc/votes.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: votes.proto 3 | 4 | package main 5 | 6 | import ( 7 | "flag" 8 | "fmt" 9 | 10 | "datacenter/votes/rpc/internal/config" 11 | "datacenter/votes/rpc/internal/server" 12 | "datacenter/votes/rpc/internal/svc" 13 | "datacenter/votes/rpc/votes" 14 | 15 | "github.com/zeromicro/go-zero/core/conf" 16 | "github.com/zeromicro/go-zero/zrpc" 17 | "google.golang.org/grpc" 18 | ) 19 | 20 | var configFile = flag.String("f", "etc/votes.yaml", "the config file") 21 | 22 | func main() { 23 | flag.Parse() 24 | 25 | var c config.Config 26 | conf.MustLoad(*configFile, &c) 27 | ctx := svc.NewServiceContext(c) 28 | srv := server.NewVotesServer(ctx) 29 | 30 | s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { 31 | votes.RegisterVotesServer(grpcServer, srv) 32 | }) 33 | defer s.Stop() 34 | 35 | fmt.Printf("Starting rpc server at %s...\n", c.ListenOn) 36 | s.Start() 37 | } 38 | -------------------------------------------------------------------------------- /common/rpc/rpc.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: common.proto 3 | 4 | package main 5 | 6 | import ( 7 | "flag" 8 | "fmt" 9 | 10 | "datacenter/common/rpc/common" 11 | "datacenter/common/rpc/internal/config" 12 | "datacenter/common/rpc/internal/server" 13 | "datacenter/common/rpc/internal/svc" 14 | 15 | "github.com/zeromicro/go-zero/core/conf" 16 | "github.com/zeromicro/go-zero/zrpc" 17 | "google.golang.org/grpc" 18 | ) 19 | 20 | var configFile = flag.String("f", "etc/rpc.yaml", "the config file") 21 | 22 | func main() { 23 | flag.Parse() 24 | 25 | var c config.Config 26 | conf.MustLoad(*configFile, &c) 27 | ctx := svc.NewServiceContext(c) 28 | srv := server.NewCommonServer(ctx) 29 | 30 | s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { 31 | common.RegisterCommonServer(grpcServer, srv) 32 | }) 33 | defer s.Stop() 34 | 35 | fmt.Printf("Starting rpc server at %s...\n", c.ListenOn) 36 | s.Start() 37 | } 38 | -------------------------------------------------------------------------------- /internal/logic/votes/enrolllistslogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/votes/rpc/votes" 8 | "datacenter/votes/rpc/votesclient" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type EnrollListsLogic struct { 14 | logx.Logger 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | } 18 | 19 | func NewEnrollListsLogic(ctx context.Context, svcCtx *svc.ServiceContext) EnrollListsLogic { 20 | return EnrollListsLogic{ 21 | Logger: logx.WithContext(ctx), 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | } 25 | } 26 | 27 | func (l *EnrollListsLogic) EnrollLists(req votesclient.ActidReq) ([]*votes.EnrollResp, error) { 28 | 29 | // todo: add your logic here and delete this line 30 | 31 | repl, err := l.svcCtx.VotesRpc.GetEnrollList(l.ctx, &req) 32 | if err != nil { 33 | return nil, err 34 | } 35 | logx.Info("GetEnrollList===", repl) 36 | 37 | return repl.Data, nil 38 | } 39 | -------------------------------------------------------------------------------- /common/rpc/common.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: common.proto 3 | 4 | package main 5 | 6 | import ( 7 | "flag" 8 | "fmt" 9 | 10 | "datacenter/common/rpc/common" 11 | "datacenter/common/rpc/internal/config" 12 | "datacenter/common/rpc/internal/server" 13 | "datacenter/common/rpc/internal/svc" 14 | 15 | "github.com/zeromicro/go-zero/core/conf" 16 | "github.com/zeromicro/go-zero/zrpc" 17 | "google.golang.org/grpc" 18 | ) 19 | 20 | var configFile = flag.String("f", "etc/common.yaml", "the config file") 21 | 22 | func main() { 23 | flag.Parse() 24 | 25 | var c config.Config 26 | conf.MustLoad(*configFile, &c) 27 | ctx := svc.NewServiceContext(c) 28 | srv := server.NewCommonServer(ctx) 29 | 30 | s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { 31 | common.RegisterCommonServer(grpcServer, srv) 32 | }) 33 | defer s.Stop() 34 | 35 | fmt.Printf("Starting rpc server at %s...\n", c.ListenOn) 36 | s.Start() 37 | } 38 | -------------------------------------------------------------------------------- /common/rpc/internal/logic/getbaseapplogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/common/rpc/common" 7 | "datacenter/common/rpc/internal/svc" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type GetBaseAppLogic struct { 13 | ctx context.Context 14 | svcCtx *svc.ServiceContext 15 | logx.Logger 16 | } 17 | 18 | func NewGetBaseAppLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBaseAppLogic { 19 | return &GetBaseAppLogic{ 20 | ctx: ctx, 21 | svcCtx: svcCtx, 22 | Logger: logx.WithContext(ctx), 23 | } 24 | } 25 | 26 | func (l *GetBaseAppLogic) GetBaseApp(in *common.BaseAppReq) (*common.BaseAppResp, error) { 27 | repl, err := l.svcCtx.BaseAppModel.FindOne(in.Beid) 28 | if err != nil { 29 | return nil, err 30 | } 31 | 32 | return &common.BaseAppResp{ 33 | Beid: repl.Id, 34 | Logo: repl.Logo, 35 | Fullwebsite: repl.Fullwebsite, 36 | Isclose: repl.Isclose, 37 | }, nil 38 | } 39 | -------------------------------------------------------------------------------- /questions/rpc/questions.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: questions.proto 3 | 4 | package main 5 | 6 | import ( 7 | "flag" 8 | "fmt" 9 | 10 | "datacenter/questions/rpc/internal/config" 11 | "datacenter/questions/rpc/internal/server" 12 | "datacenter/questions/rpc/internal/svc" 13 | "datacenter/questions/rpc/questions" 14 | 15 | "github.com/zeromicro/go-zero/core/conf" 16 | "github.com/zeromicro/go-zero/zrpc" 17 | "google.golang.org/grpc" 18 | ) 19 | 20 | var configFile = flag.String("f", "etc/questions.yaml", "the config file") 21 | 22 | func main() { 23 | flag.Parse() 24 | 25 | var c config.Config 26 | conf.MustLoad(*configFile, &c) 27 | ctx := svc.NewServiceContext(c) 28 | srv := server.NewQuestionsServer(ctx) 29 | 30 | s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { 31 | questions.RegisterQuestionsServer(grpcServer, srv) 32 | }) 33 | defer s.Stop() 34 | 35 | fmt.Printf("Starting rpc server at %s...\n", c.ListenOn) 36 | s.Start() 37 | } 38 | -------------------------------------------------------------------------------- /internal/handler/questions/gradehandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "net/http" 7 | 8 | logic "datacenter/internal/logic/questions" 9 | "datacenter/internal/svc" 10 | "datacenter/questions/rpc/questionsclient" 11 | "datacenter/shared" 12 | 13 | "github.com/zeromicro/go-zero/rest/httpx" 14 | ) 15 | 16 | func GradeHandler(ctx *svc.ServiceContext) http.HandlerFunc { 17 | return func(w http.ResponseWriter, r *http.Request) { 18 | query := r.URL.Query() 19 | actid := shared.StrToInt64(query.Get("actid")) 20 | uid, _ := json.Number(fmt.Sprintf("%v", r.Context().Value("uid"))).Int64() 21 | auid, _ := json.Number(fmt.Sprintf("%v", r.Context().Value("auid"))).Int64() 22 | in := questionsclient.GradeReq{ 23 | Actid: actid, 24 | Uid: uid, 25 | Auid: auid, 26 | } 27 | l := logic.NewGradeLogic(r.Context(), ctx) 28 | resp, err := l.Grade(in) 29 | if err != nil { 30 | httpx.Error(w, err) 31 | } else { 32 | shared.OkJson(w, resp) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /questions/readmin.md: -------------------------------------------------------------------------------- 1 | ### 问答抽奖服务 2 | 3 | ``` 4 | goctl rpc template -o rpc/questions.proto 5 | goctl rpc proto -src questions.proto -dir . 6 | 7 | 8 | goctl model mysql datasource -url="root:admin@tcp(127.0.0.1:3306)/datacenter" -table="app_questions" -dir ./model -c 9 | goctl model mysql datasource -url="root:admin@tcp(127.0.0.1:3306)/datacenter" -table="app_questions_activities" -dir ./model -c 10 | goctl model mysql datasource -url="root:admin@tcp(127.0.0.1:3306)/datacenter" -table="app_questions_answers" -dir ./model -c 11 | goctl model mysql datasource -url="root:admin@tcp(127.0.0.1:3306)/datacenter" -table="app_questions_awards" -dir ./model -c 12 | goctl model mysql datasource -url="root:admin@tcp(127.0.0.1:3306)/datacenter" -table="app_questions_converts" -dir ./model -c 13 | goctl model mysql datasource -url="root:admin@tcp(127.0.0.1:3306)/datacenter" -table="app_questions_lotteries" -dir ./model -c 14 | goctl model mysql datasource -url="root:admin@tcp(127.0.0.1:3306)/datacenter" -table="app_questions_tests" -dir ./model -c 15 | 16 | ``` -------------------------------------------------------------------------------- /user/rpc/internal/svc/servicecontext.go: -------------------------------------------------------------------------------- 1 | package svc 2 | 3 | import ( 4 | "datacenter/common/rpc/commonclient" 5 | "datacenter/user/model" 6 | "datacenter/user/rpc/internal/config" 7 | 8 | "github.com/zeromicro/go-zero/core/stores/sqlx" 9 | "github.com/zeromicro/go-zero/zrpc" 10 | ) 11 | 12 | type ServiceContext struct { 13 | c config.Config 14 | UserModel *model.BaseMemberModel 15 | AppUserModel *model.AppUserModel 16 | CommonRpc commonclient.Common 17 | } 18 | 19 | func NewServiceContext(c config.Config) *ServiceContext { 20 | conn := sqlx.NewMysql(c.Mysql.DataSource) 21 | um := model.NewBaseMemberModel(conn, c.CacheRedis) 22 | aup := model.NewAppUserModel(conn, c.CacheRedis) 23 | //调用核心的公共配置 后面再来做难 24 | comm := commonclient.NewCommon(zrpc.MustNewClient(c.CommonRpc)) 25 | //userclient.NewUser(zrpc.MustNewClient(c.UserRpc, zrpc.WithUnaryClientInterceptor(timeInterceptor))) 26 | 27 | return &ServiceContext{ 28 | c: c, 29 | UserModel: um, 30 | CommonRpc: comm, 31 | AppUserModel: aup, 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /internal/logic/user/userinfologic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/internal/types" 8 | "datacenter/user/rpc/userclient" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type UserInfoLogic struct { 14 | logx.Logger 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | } 18 | 19 | func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) UserInfoLogic { 20 | return UserInfoLogic{ 21 | Logger: logx.WithContext(ctx), 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | } 25 | } 26 | 27 | func (l *UserInfoLogic) UserInfo(req types.UserReq) (*types.UserReply, error) { 28 | reply, err := l.svcCtx.UserRpc.UserInfo(l.ctx, &userclient.UserReq{}) 29 | if err != nil { 30 | return nil, err 31 | } 32 | return &types.UserReply{ 33 | Auid: reply.Auid, 34 | Beid: reply.Beid, 35 | Ptyid: reply.Ptyid, 36 | Uid: reply.Uid, 37 | Username: reply.Username, 38 | Mobile: reply.Mobile, 39 | Avator: reply.Avator, 40 | }, nil 41 | } 42 | -------------------------------------------------------------------------------- /questions/rpc/internal/logic/getquestionsgradelogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/questions/rpc/internal/svc" 7 | "datacenter/questions/rpc/questions" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type GetQuestionsGradeLogic struct { 13 | ctx context.Context 14 | svcCtx *svc.ServiceContext 15 | logx.Logger 16 | } 17 | 18 | func NewGetQuestionsGradeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetQuestionsGradeLogic { 19 | return &GetQuestionsGradeLogic{ 20 | ctx: ctx, 21 | svcCtx: svcCtx, 22 | Logger: logx.WithContext(ctx), 23 | } 24 | } 25 | 26 | // 获取得分 27 | func (l *GetQuestionsGradeLogic) GetQuestionsGrade(in *questions.GradeReq) (*questions.QuestionsAnswerResp, error) { 28 | // todo: add your logic here and delete this line 29 | answers, err := l.svcCtx.QuestionsAnswersModel.FindOne(in) 30 | if err != nil { 31 | return nil, err 32 | } 33 | return &questions.QuestionsAnswerResp{ 34 | AnswerId: answers.Id, 35 | Score: answers.Score, 36 | }, nil 37 | } 38 | -------------------------------------------------------------------------------- /user/rpc/internal/logic/userinfologic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/user/model" 7 | "datacenter/user/rpc/internal/svc" 8 | "datacenter/user/rpc/user" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type UserInfoLogic struct { 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | logx.Logger 17 | } 18 | 19 | func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoLogic { 20 | return &UserInfoLogic{ 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | Logger: logx.WithContext(ctx), 24 | } 25 | } 26 | 27 | func (l *UserInfoLogic) UserInfo(in *user.UserReq) (*user.UserReply, error) { 28 | 29 | // 忽略逻辑校验 30 | userInfo, err := l.svcCtx.UserModel.FindOne(in.Uid) 31 | switch err { 32 | case nil: 33 | return &user.UserReply{ 34 | Uid: userInfo.Id, 35 | Username: userInfo.Username, 36 | Mobile: userInfo.Mobile, 37 | }, nil 38 | case model.ErrNotFound: 39 | return nil, errorUsernameUnRegister 40 | default: 41 | return nil, err 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /user/api/internal/logic/registerlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | "time" 6 | 7 | "datacenter/user/api/internal/svc" 8 | "datacenter/user/api/internal/types" 9 | 10 | "datacenter/user/model" 11 | 12 | "github.com/zeromicro/go-zero/core/logx" 13 | ) 14 | 15 | type RegisterLogic struct { 16 | logx.Logger 17 | ctx context.Context 18 | svcCtx *svc.ServiceContext 19 | } 20 | 21 | func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) RegisterLogic { 22 | return RegisterLogic{ 23 | Logger: logx.WithContext(ctx), 24 | ctx: ctx, 25 | svcCtx: svcCtx, 26 | } 27 | } 28 | 29 | func (l *RegisterLogic) Register(req types.RegisterReq) error { 30 | 31 | _, err := l.svcCtx.UserModel.FindOneByMobile(req.Mobile) 32 | if err == nil { 33 | return errorDuplicateMobile 34 | } 35 | 36 | // 处理加盐算法 37 | 38 | _, err = l.svcCtx.UserModel.Insert(model.BaseMember{ 39 | Username: req.Username, 40 | Mobile: req.Mobile, 41 | Password: req.Password, 42 | DeletedAt: time.Unix(0, 0), 43 | }) 44 | return err 45 | 46 | } 47 | -------------------------------------------------------------------------------- /common/rpc/internal/logic/getappconfiglogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/common/rpc/common" 7 | "datacenter/common/rpc/internal/svc" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type GetAppConfigLogic struct { 13 | ctx context.Context 14 | svcCtx *svc.ServiceContext 15 | logx.Logger 16 | } 17 | 18 | func NewGetAppConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAppConfigLogic { 19 | return &GetAppConfigLogic{ 20 | ctx: ctx, 21 | svcCtx: svcCtx, 22 | Logger: logx.WithContext(ctx), 23 | } 24 | } 25 | 26 | func (l *GetAppConfigLogic) GetAppConfig(in *common.AppConfigReq) (*common.AppConfigResp, error) { 27 | appConfig, err := l.svcCtx.AppConfigModel.FindOneByAppid(in.Beid, in.Ptyid) 28 | if err != nil { 29 | return nil, err 30 | } 31 | 32 | return &common.AppConfigResp{ 33 | Id: appConfig.Id, 34 | Beid: appConfig.Beid, 35 | Ptyid: appConfig.Ptyid, 36 | Appid: appConfig.Appid, 37 | Appsecret: appConfig.Appsecret, 38 | Title: appConfig.Title, 39 | }, nil 40 | } 41 | -------------------------------------------------------------------------------- /questions/rpc/internal/logic/getawardlistlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/questions/rpc/internal/svc" 7 | "datacenter/questions/rpc/questions" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type GetAwardListLogic struct { 13 | ctx context.Context 14 | svcCtx *svc.ServiceContext 15 | logx.Logger 16 | } 17 | 18 | func NewGetAwardListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAwardListLogic { 19 | return &GetAwardListLogic{ 20 | ctx: ctx, 21 | svcCtx: svcCtx, 22 | Logger: logx.WithContext(ctx), 23 | } 24 | } 25 | 26 | // 获取 问答奖品列表 27 | func (l *GetAwardListLogic) GetAwardList(in *questions.ActivitiesReq) (*questions.AwardListResp, error) { 28 | lists, err := l.svcCtx.QuestionsAwardsModel.Find(in.Actid) 29 | if err != nil { 30 | return nil, err 31 | } 32 | list := make([]*questions.AwardInfoResp, 0) 33 | if len(lists) > 0 { 34 | for _, award := range lists { 35 | list = append(list, Awards2AwardInfoResp(&award)) 36 | } 37 | } 38 | return &questions.AwardListResp{ 39 | Data: list, 40 | }, nil 41 | } 42 | -------------------------------------------------------------------------------- /user/api/internal/handler/routes.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT. 2 | package handler 3 | 4 | import ( 5 | "net/http" 6 | 7 | "datacenter/user/api/internal/svc" 8 | 9 | "github.com/zeromicro/go-zero/rest" 10 | ) 11 | 12 | func RegisterHandlers(engine *rest.Server, serverCtx *svc.ServiceContext) { 13 | engine.AddRoutes( 14 | []rest.Route{ 15 | { 16 | Method: http.MethodPost, 17 | Path: "/user/ping", 18 | Handler: pingHandler(serverCtx), 19 | }, 20 | { 21 | Method: http.MethodPost, 22 | Path: "/user/register", 23 | Handler: registerHandler(serverCtx), 24 | }, 25 | { 26 | Method: http.MethodPost, 27 | Path: "/user/login", 28 | Handler: loginHandler(serverCtx), 29 | }, 30 | }, 31 | ) 32 | 33 | engine.AddRoutes( 34 | rest.WithMiddlewares( 35 | []rest.Middleware{serverCtx.UserCheck}, 36 | []rest.Route{ 37 | { 38 | Method: http.MethodGet, 39 | Path: "/user/info", 40 | Handler: userInfoHandler(serverCtx), 41 | }, 42 | }..., 43 | ), 44 | rest.WithJwt(serverCtx.Config.Auth.AccessSecret), 45 | ) 46 | } 47 | -------------------------------------------------------------------------------- /votes/rpc/internal/svc/servicecontext.go: -------------------------------------------------------------------------------- 1 | package svc 2 | 3 | import ( 4 | "datacenter/votes/model" 5 | "datacenter/votes/rpc/internal/config" 6 | 7 | "github.com/zeromicro/go-zero/core/stores/redis" 8 | "github.com/zeromicro/go-zero/core/stores/sqlx" 9 | ) 10 | 11 | type ServiceContext struct { 12 | c config.Config 13 | AppEnrollModel model.AppEnrollModel 14 | AppVotesActivityModel model.AppVotesActivityModel 15 | AppVotesModel model.AppVotesModel 16 | RedisConn *redis.Redis 17 | } 18 | 19 | func NewServiceContext(c config.Config) *ServiceContext { 20 | conn := sqlx.NewMysql(c.Mysql.DataSource) 21 | aem := model.NewAppEnrollModel(conn, c.CacheRedis) 22 | avm := model.NewAppVotesModel(conn) 23 | avam := model.NewAppVotesActivityModel(conn, c.CacheRedis) 24 | rconn := redis.NewRedis(c.CacheRedis[0].Host, c.CacheRedis[0].Type, c.CacheRedis[0].Pass) 25 | return &ServiceContext{ 26 | c: c, 27 | AppEnrollModel: aem, 28 | AppVotesModel: avm, 29 | AppVotesActivityModel: avam, 30 | RedisConn: rconn, 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /internal/handler/votes/enrolllistshandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/votes" 7 | "datacenter/internal/svc" 8 | "datacenter/shared" 9 | "datacenter/votes/rpc/votesclient" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func EnrollListsHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | query := r.URL.Query() 17 | 18 | actid := shared.StrToInt64(query.Get("actid")) 19 | offset, size := shared.ToLimitOffset(query.Get("page"), query.Get("size")) 20 | 21 | if actid == 0 { 22 | httpx.Error(w, shared.ErrorNoRequiredParameters) 23 | return 24 | } 25 | actreq := votesclient.ActidReq{ 26 | Actid: actid, 27 | Keyword: query.Get("keyword"), 28 | Limit: &votesclient.LimitReq{ 29 | Offset: offset, 30 | Size: size, 31 | }, 32 | } 33 | l := logic.NewEnrollListsLogic(r.Context(), ctx) 34 | resp, err := l.EnrollLists(actreq) 35 | if err != nil { 36 | httpx.Error(w, err) 37 | } else { 38 | shared.OkJson(w, resp) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /search/rpc/internal/server/searchserver.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: search.proto 3 | 4 | package server 5 | 6 | import ( 7 | "context" 8 | 9 | "datacenter/search/rpc/internal/logic" 10 | "datacenter/search/rpc/internal/svc" 11 | "datacenter/search/rpc/search" 12 | ) 13 | 14 | type SearchServer struct { 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewSearchServer(svcCtx *svc.ServiceContext) *SearchServer { 19 | return &SearchServer{ 20 | svcCtx: svcCtx, 21 | } 22 | } 23 | 24 | func (s *SearchServer) ArticleInit(ctx context.Context, in *search.Request) (*search.Response, error) { 25 | l := logic.NewArticleInitLogic(ctx, s.svcCtx) 26 | return l.ArticleInit(in) 27 | } 28 | 29 | func (s *SearchServer) ArticleStore(ctx context.Context, in *search.ArticleReq) (*search.Response, error) { 30 | l := logic.NewArticleStoreLogic(ctx, s.svcCtx) 31 | return l.ArticleStore(in) 32 | } 33 | 34 | func (s *SearchServer) ArticleSearch(ctx context.Context, in *search.SearchReq) (*search.ArticleResp, error) { 35 | l := logic.NewArticleSearchLogic(ctx, s.svcCtx) 36 | return l.ArticleSearch(in) 37 | } 38 | -------------------------------------------------------------------------------- /user/api/internal/types/types.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT. 2 | package types 3 | 4 | type JwtToken struct { 5 | AccessToken string `json:"accessToken,omitempty"` 6 | AccessExpire int64 `json:"accessExpire,omitempty"` 7 | RefreshAfter int64 `json:"refreshAfter,omitempty"` 8 | } 9 | 10 | type LoginReq struct { 11 | Username string `json:"username"` 12 | Mobile string `json:"mobile"` 13 | Password string `json:"password"` 14 | Smscode string `json:"smscode"` //短信码 15 | } 16 | 17 | type RegisterReq struct { 18 | Username string `json:"username"` 19 | Mobile string `json:"mobile"` //基本一个手机号码就完事 20 | Password string `json:"password"` 21 | Smscode string `json:"smscode"` //短信码 22 | } 23 | 24 | type UserReply struct { 25 | Auid int64 `json:"auid"` 26 | Uid int64 `json:"uid"` 27 | Beid int64 `json:"beid"` //应用id 28 | Ptyid int64 `json:"ptyid"` //对应平台 29 | Username string `json:"username"` 30 | Mobile string `json:"mobile"` 31 | Nickname string `json:"nickname"` 32 | Openid string `json:"openid"` 33 | Avator string `json:"avator"` 34 | Gender string `json:"gender"` 35 | JwtToken 36 | } 37 | -------------------------------------------------------------------------------- /user/rpc/internal/logic/loginlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/user/model" 7 | "datacenter/user/rpc/internal/svc" 8 | "datacenter/user/rpc/user" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type LoginLogic struct { 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | logx.Logger 17 | } 18 | 19 | func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic { 20 | return &LoginLogic{ 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | Logger: logx.WithContext(ctx), 24 | } 25 | } 26 | 27 | func (l *LoginLogic) Login(in *user.LoginReq) (*user.UserReply, error) { 28 | 29 | // 忽略逻辑校验 30 | userInfo, err := l.svcCtx.UserModel.FindOneByMobile(in.Mobile) 31 | switch err { 32 | case nil: 33 | if userInfo.Password != in.Password { 34 | return nil, errorIncorrectPassword 35 | } 36 | return &user.UserReply{ 37 | Uid: userInfo.Id, 38 | Username: userInfo.Username, 39 | Mobile: userInfo.Mobile, 40 | }, nil 41 | case model.ErrNotFound: 42 | return nil, errorUsernameUnRegister 43 | default: 44 | return nil, err 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /internal/logic/common/appinfologic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/svc" 7 | "datacenter/internal/types" 8 | "datacenter/shared" 9 | 10 | "datacenter/common/model" 11 | "datacenter/common/rpc/common" 12 | 13 | "github.com/zeromicro/go-zero/core/logx" 14 | ) 15 | 16 | type AppInfoLogic struct { 17 | logx.Logger 18 | ctx context.Context 19 | svcCtx *svc.ServiceContext 20 | } 21 | 22 | func NewAppInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) AppInfoLogic { 23 | return AppInfoLogic{ 24 | Logger: logx.WithContext(ctx), 25 | ctx: ctx, 26 | svcCtx: svcCtx, 27 | } 28 | } 29 | 30 | func (l *AppInfoLogic) AppInfo(req types.Beid) (appconfig *common.BaseAppResp, err error) { 31 | 32 | //检查 缓存中是否有值 33 | err = l.svcCtx.Cache.Get(model.GetcacheBaseAppIdPrefix(req.Beid), appconfig) 34 | if err != nil && err == shared.ErrNotFound { 35 | appconfig, err = l.svcCtx.CommonRpc.GetBaseApp(l.ctx, &common.BaseAppReq{ 36 | Beid: req.Beid, 37 | }) 38 | if err != nil { 39 | return 40 | } 41 | err = l.svcCtx.Cache.Set(model.GetcacheBaseAppIdPrefix(req.Beid), appconfig) 42 | } 43 | 44 | return 45 | } 46 | -------------------------------------------------------------------------------- /user/api/internal/logic/userinfologic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | "strconv" 6 | 7 | "datacenter/user/api/internal/svc" 8 | "datacenter/user/api/internal/types" 9 | "datacenter/user/model" 10 | 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type UserInfoLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) UserInfoLogic { 21 | return UserInfoLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *UserInfoLogic) UserInfo(userId string) (*types.UserReply, error) { 29 | 30 | userInt, err := strconv.ParseInt(userId, 10, 64) 31 | if err != nil { 32 | return nil, err 33 | } 34 | 35 | userInfo, err := l.svcCtx.UserModel.FindOne(userInt) 36 | switch err { 37 | case nil: 38 | return &types.UserReply{ 39 | Uid: userInfo.Id, 40 | Username: userInfo.Username, 41 | Mobile: userInfo.Mobile, 42 | }, nil 43 | case model.ErrNotFound: 44 | return nil, errorUserNotFound 45 | default: 46 | return nil, err 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /user/api/internal/middleware/usercheckmiddleware.go: -------------------------------------------------------------------------------- 1 | package middleware 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "fmt" 7 | "net/http" 8 | 9 | "github.com/zeromicro/go-zero/rest/httpx" 10 | ) 11 | 12 | var ( 13 | errorUserInfo = errors.New("用户信息获取失败") 14 | authDeny = errors.New("用户信息不一致") 15 | ) 16 | 17 | const ( 18 | userKey = `x-user-id` 19 | ) 20 | 21 | type UserCheckMiddleware struct { 22 | } 23 | 24 | func NewUserCheckMiddleware() *UserCheckMiddleware { 25 | return &UserCheckMiddleware{} 26 | } 27 | 28 | func (m *UserCheckMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc { 29 | return func(w http.ResponseWriter, r *http.Request) { 30 | userId := r.Header.Get(userKey) 31 | jwtUserId := r.Context().Value("userId") 32 | 33 | userInt, err := json.Number(userId).Int64() 34 | if err != nil { 35 | httpx.Error(w, errorUserInfo) 36 | return 37 | } 38 | 39 | jwtInt, err := json.Number(fmt.Sprintf("%v", jwtUserId)).Int64() 40 | if err != nil { 41 | httpx.Error(w, errorUserInfo) 42 | return 43 | } 44 | 45 | if jwtInt != userInt { 46 | httpx.Error(w, authDeny) 47 | return 48 | } 49 | 50 | next(w, r) 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /user/rpc/internal/logic/registerlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/user/rpc/internal/svc" 7 | "datacenter/user/rpc/user" 8 | 9 | "datacenter/user/model" 10 | "time" 11 | 12 | "github.com/zeromicro/go-zero/core/logx" 13 | ) 14 | 15 | type RegisterLogic struct { 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | logx.Logger 19 | } 20 | 21 | func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic { 22 | return &RegisterLogic{ 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | Logger: logx.WithContext(ctx), 26 | } 27 | } 28 | 29 | func (l *RegisterLogic) Register(in *user.RegisterReq) (*user.UserReply, error) { 30 | 31 | _, err := l.svcCtx.UserModel.FindOneByMobile(in.Mobile) 32 | if err == nil { 33 | return nil, errorDuplicateMobile 34 | } 35 | 36 | // 处理加盐算法 37 | res, err := l.svcCtx.UserModel.Insert(model.BaseMember{ 38 | Mobile: in.Mobile, 39 | Password: in.Password, 40 | DeletedAt: time.Unix(0, 0), 41 | }) 42 | if err != nil { 43 | return nil, err 44 | } 45 | uid, err := res.LastInsertId() 46 | if err != nil { 47 | return nil, err 48 | } 49 | return &user.UserReply{ 50 | Uid: uid, 51 | Mobile: in.Mobile, 52 | }, nil 53 | } 54 | -------------------------------------------------------------------------------- /internal/logic/common/qiuniutokenlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/common/rpc/commonclient" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/shared" 10 | 11 | "github.com/qiniu/api.v7/v7/auth" 12 | "github.com/qiniu/api.v7/v7/storage" 13 | "github.com/zeromicro/go-zero/core/logx" 14 | ) 15 | 16 | type QiuniuTokenLogic struct { 17 | logx.Logger 18 | ctx context.Context 19 | svcCtx *svc.ServiceContext 20 | } 21 | 22 | func NewQiuniuTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) QiuniuTokenLogic { 23 | return QiuniuTokenLogic{ 24 | Logger: logx.WithContext(ctx), 25 | ctx: ctx, 26 | svcCtx: svcCtx, 27 | } 28 | } 29 | 30 | func (l *QiuniuTokenLogic) QiuniuToken(req types.Beid) (*types.Token, error) { 31 | repl, err := l.svcCtx.CommonRpc.GetAppConfig(l.ctx, &commonclient.AppConfigReq{ 32 | Beid: req.Beid, 33 | Ptyid: shared.QiuniuPtyId, 34 | }) 35 | 36 | if err != nil { 37 | return nil, err 38 | } 39 | //请求七牛 40 | // 简单上传凭证 41 | putPolicy := storage.PutPolicy{ 42 | Scope: "tanzi-datacenter", 43 | } 44 | mac := auth.New(repl.Appid, repl.Appsecret) 45 | 46 | upToken := putPolicy.UploadToken(mac) 47 | 48 | return &types.Token{ 49 | Token: upToken, 50 | }, nil 51 | } 52 | -------------------------------------------------------------------------------- /questions/rpc/internal/logic/postconvertlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | "database/sql" 6 | 7 | "datacenter/questions/model" 8 | "datacenter/questions/rpc/internal/svc" 9 | "datacenter/questions/rpc/questions" 10 | 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type PostConvertLogic struct { 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | logx.Logger 18 | } 19 | 20 | func NewPostConvertLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PostConvertLogic { 21 | return &PostConvertLogic{ 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | Logger: logx.WithContext(ctx), 25 | } 26 | } 27 | 28 | // 填写中奖记录 29 | func (l *PostConvertLogic) PostConvert(in *questions.ConvertReq) (*questions.ConvertResp, error) { 30 | result, err := l.svcCtx.QuestionsConvertsModel.Insert(model.AppQuestionsConverts{ 31 | LotteryId: in.LotteryId, 32 | Username: in.Username, 33 | Ptyid: in.Ptyid, 34 | Beid: in.Beid, 35 | Uid: in.Uid, 36 | Auid: in.Auid, 37 | Phone: sql.NullString{String: in.Phone, Valid: true}, 38 | }) 39 | if err != nil { 40 | return nil, err 41 | } 42 | id, err := result.LastInsertId() 43 | return &questions.ConvertResp{ 44 | Id: id, 45 | LotteryId: in.LotteryId, 46 | }, err 47 | } 48 | -------------------------------------------------------------------------------- /internal/logic/common/snsinfologic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/common/rpc/common" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/shared" 10 | 11 | "datacenter/common/model" 12 | 13 | "github.com/zeromicro/go-zero/core/logx" 14 | ) 15 | 16 | type SnsInfoLogic struct { 17 | logx.Logger 18 | ctx context.Context 19 | svcCtx *svc.ServiceContext 20 | } 21 | 22 | func NewSnsInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) SnsInfoLogic { 23 | return SnsInfoLogic{ 24 | Logger: logx.WithContext(ctx), 25 | ctx: ctx, 26 | svcCtx: svcCtx, 27 | } 28 | } 29 | 30 | func (l *SnsInfoLogic) SnsInfo(req types.SnsReq) (appConfigresp *common.AppConfigResp, err error) { 31 | 32 | //检查 缓存中是否有数据 33 | err = l.svcCtx.Cache.Get(model.GetCacheAppConfigIdPtyidPrefix(req.Beid.Beid, req.Ptyid), appConfigresp) 34 | if err != nil && err == shared.ErrNotFound { 35 | //直接请求 并返回 36 | appConfigresp, err = l.svcCtx.CommonRpc.GetAppConfig(l.ctx, &common.AppConfigReq{ 37 | Beid: req.Beid.Beid, 38 | Ptyid: req.Ptyid, 39 | }) 40 | if err != nil { 41 | return 42 | } 43 | //缓存数据 44 | err = l.svcCtx.Cache.Set(model.GetCacheAppConfigIdPtyidPrefix(req.Beid.Beid, req.Ptyid), appConfigresp) 45 | } 46 | return 47 | } 48 | -------------------------------------------------------------------------------- /common/rpc/commonclient/common.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: common.proto 3 | 4 | //go:generate mockgen -destination ./common_mock.go -package commonclient -source $GOFILE 5 | 6 | package commonclient 7 | 8 | import ( 9 | "context" 10 | 11 | "datacenter/common/rpc/common" 12 | 13 | "github.com/zeromicro/go-zero/zrpc" 14 | ) 15 | 16 | type ( 17 | BaseAppReq = common.BaseAppReq 18 | BaseAppResp = common.BaseAppResp 19 | AppConfigReq = common.AppConfigReq 20 | AppConfigResp = common.AppConfigResp 21 | 22 | Common interface { 23 | GetAppConfig(ctx context.Context, in *AppConfigReq) (*AppConfigResp, error) 24 | GetBaseApp(ctx context.Context, in *BaseAppReq) (*BaseAppResp, error) 25 | } 26 | 27 | defaultCommon struct { 28 | cli zrpc.Client 29 | } 30 | ) 31 | 32 | func NewCommon(cli zrpc.Client) Common { 33 | return &defaultCommon{ 34 | cli: cli, 35 | } 36 | } 37 | 38 | func (m *defaultCommon) GetAppConfig(ctx context.Context, in *AppConfigReq) (*AppConfigResp, error) { 39 | client := common.NewCommonClient(m.cli.Conn()) 40 | return client.GetAppConfig(ctx, in) 41 | } 42 | 43 | func (m *defaultCommon) GetBaseApp(ctx context.Context, in *BaseAppReq) (*BaseAppResp, error) { 44 | client := common.NewCommonClient(m.cli.Conn()) 45 | return client.GetBaseApp(ctx, in) 46 | } 47 | -------------------------------------------------------------------------------- /internal/handler/questions/changehandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "net/http" 7 | 8 | logic "datacenter/internal/logic/questions" 9 | "datacenter/internal/svc" 10 | "datacenter/internal/types" 11 | "datacenter/questions/rpc/questions" 12 | "datacenter/shared" 13 | 14 | "github.com/zeromicro/go-zero/rest/httpx" 15 | ) 16 | 17 | func ChangeHandler(ctx *svc.ServiceContext) http.HandlerFunc { 18 | return func(w http.ResponseWriter, r *http.Request) { 19 | 20 | var req types.AnswerReq 21 | 22 | if err := httpx.Parse(r, &req); err != nil { 23 | httpx.Error(w, err) 24 | return 25 | } 26 | uid, _ := json.Number(fmt.Sprintf("%v", r.Context().Value("uid"))).Int64() 27 | ptyid, _ := json.Number(fmt.Sprintf("%v", r.Context().Value("ptyid"))).Int64() 28 | beid, _ := json.Number(fmt.Sprintf("%v", r.Context().Value("beid"))).Int64() 29 | 30 | in := questions.QuestionsAnswerReq{ 31 | Uid: uid, 32 | Ptyid: ptyid, 33 | Beid: beid, 34 | ActivityId: req.ActivityId, 35 | Answers: req.Answers, 36 | Score: req.Score, 37 | } 38 | 39 | l := logic.NewChangeLogic(r.Context(), ctx) 40 | resp, err := l.Change(in) 41 | if err != nil { 42 | httpx.Error(w, err) 43 | } else { 44 | shared.OkJson(w, resp) 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /internal/logic/votes/votelogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | "encoding/json" 6 | "fmt" 7 | 8 | "datacenter/internal/svc" 9 | "datacenter/internal/types" 10 | "datacenter/shared" 11 | "datacenter/votes/rpc/votesclient" 12 | 13 | "github.com/zeromicro/go-zero/core/logx" 14 | ) 15 | 16 | type VoteLogic struct { 17 | logx.Logger 18 | ctx context.Context 19 | svcCtx *svc.ServiceContext 20 | } 21 | 22 | func NewVoteLogic(ctx context.Context, svcCtx *svc.ServiceContext) VoteLogic { 23 | return VoteLogic{ 24 | Logger: logx.WithContext(ctx), 25 | ctx: ctx, 26 | svcCtx: svcCtx, 27 | } 28 | } 29 | 30 | func (l *VoteLogic) Vote(req types.VoteReq) (*votesclient.VotesResp, error) { 31 | auid, _ := json.Number(fmt.Sprintf("%v", l.ctx.Value("auid"))).Int64() 32 | uid, _ := json.Number(fmt.Sprintf("%v", l.ctx.Value("uid"))).Int64() 33 | ptyid, _ := json.Number(fmt.Sprintf("%v", l.ctx.Value("ptyid"))).Int64() 34 | beid, _ := json.Number(fmt.Sprintf("%v", l.ctx.Value("beid"))).Int64() 35 | if auid == 0 && uid == 0 { 36 | return nil, shared.ErrorUserNotFound 37 | } 38 | return l.svcCtx.VotesRpc.Votes(l.ctx, &votesclient.VotesReq{ 39 | Aeid: req.Aeid, 40 | Actid: req.Actid.Actid, 41 | Auid: auid, 42 | Uid: uid, 43 | Ptyid: ptyid, 44 | Beid: beid, 45 | Ip: shared.GetLocalIP(), 46 | }) 47 | } 48 | -------------------------------------------------------------------------------- /votes/rpc/internal/logic/getactivityinfologic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/votes/rpc/internal/svc" 7 | "datacenter/votes/rpc/votes" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type GetActivityInfoLogic struct { 13 | ctx context.Context 14 | svcCtx *svc.ServiceContext 15 | logx.Logger 16 | } 17 | 18 | func NewGetActivityInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetActivityInfoLogic { 19 | return &GetActivityInfoLogic{ 20 | ctx: ctx, 21 | svcCtx: svcCtx, 22 | Logger: logx.WithContext(ctx), 23 | } 24 | } 25 | 26 | // 获取 活动信息 27 | func (l *GetActivityInfoLogic) GetActivityInfo(in *votes.ActInfoReq) (*votes.ActInfoResp, error) { 28 | activity, err := l.svcCtx.AppVotesActivityModel.FindOne(in.Actid) 29 | if err != nil { 30 | return nil, err 31 | } 32 | list := &votes.ActInfoResp{ 33 | Actid: activity.Actid, 34 | Beid: activity.Beid, 35 | Descr: activity.Descr, 36 | EndDate: activity.EndDate, 37 | StartDate: activity.StartDate, 38 | EnrollDate: activity.EnrollDate, 39 | Type: activity.Type, 40 | Title: activity.Title, 41 | Viewcount: activity.Viewcount, 42 | Votecount: activity.Votecount, 43 | Enrollcount: activity.Enrollcount, 44 | Num: activity.Num, 45 | } 46 | return list, nil 47 | } 48 | -------------------------------------------------------------------------------- /search/rpc/search.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: search.proto 3 | 4 | package main 5 | 6 | import ( 7 | "context" 8 | "flag" 9 | "fmt" 10 | "time" 11 | 12 | "datacenter/search/rpc/internal/config" 13 | // searchlogic "datacenter/search/rpc/internal/logic" 14 | "datacenter/search/rpc/internal/server" 15 | "datacenter/search/rpc/internal/svc" 16 | "datacenter/search/rpc/search" 17 | 18 | "github.com/zeromicro/go-zero/core/conf" 19 | "github.com/zeromicro/go-zero/core/logx" 20 | "github.com/zeromicro/go-zero/zrpc" 21 | "google.golang.org/grpc" 22 | ) 23 | 24 | var configFile = flag.String("f", "etc/search.yaml", "the config file") 25 | 26 | func main() { 27 | flag.Parse() 28 | 29 | var c config.Config 30 | conf.MustLoad(*configFile, &c) 31 | ctx := svc.NewServiceContext(c) 32 | srv := server.NewSearchServer(ctx) 33 | 34 | s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { 35 | search.RegisterSearchServer(grpcServer, srv) 36 | }) 37 | // 后台定时搜索服务 38 | go func(srv *server.SearchServer) { 39 | for { 40 | srv.ArticleInit(context.Background(), &search.Request{ 41 | Once: true, 42 | }) 43 | logx.Info("定时计划。。。") 44 | time.Sleep(10 * time.Second) 45 | } 46 | }(srv) 47 | 48 | defer s.Stop() 49 | 50 | fmt.Printf("Starting rpc server at %s...\n", c.ListenOn) 51 | s.Start() 52 | } 53 | -------------------------------------------------------------------------------- /questions/rpc/internal/logic/getactivitiesinfologic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/questions/rpc/internal/svc" 7 | "datacenter/questions/rpc/questions" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type GetActivitiesInfoLogic struct { 13 | ctx context.Context 14 | svcCtx *svc.ServiceContext 15 | logx.Logger 16 | } 17 | 18 | func NewGetActivitiesInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetActivitiesInfoLogic { 19 | return &GetActivitiesInfoLogic{ 20 | ctx: ctx, 21 | svcCtx: svcCtx, 22 | Logger: logx.WithContext(ctx), 23 | } 24 | } 25 | 26 | // 获取 问答抽奖活动信息 27 | func (l *GetActivitiesInfoLogic) GetActivitiesInfo(in *questions.ActivitiesReq) (*questions.ActInfoResp, error) { 28 | // todo: add your logic here and delete this line 29 | activits, err := l.svcCtx.QuestionsActivitiesModel.FindOne(in.Actid) 30 | if err != nil { 31 | return nil, err 32 | } 33 | 34 | return &questions.ActInfoResp{ 35 | Id: activits.Id, 36 | Beid: activits.Beid, 37 | Ptyid: activits.Ptyid, 38 | StartDate: activits.StartDate, 39 | EndDate: activits.EndDate, 40 | Title: activits.Title, 41 | Rule: activits.Rule, 42 | GetScore: int64(activits.GetScore), 43 | Des: activits.Des, 44 | Header: activits.Header, 45 | Image: activits.Image, 46 | }, nil 47 | } 48 | -------------------------------------------------------------------------------- /questions/rpc/internal/logic/postquestionschangelogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/questions/model" 7 | "datacenter/questions/rpc/internal/svc" 8 | "datacenter/questions/rpc/questions" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type PostQuestionsChangeLogic struct { 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | logx.Logger 17 | } 18 | 19 | func NewPostQuestionsChangeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PostQuestionsChangeLogic { 20 | return &PostQuestionsChangeLogic{ 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | Logger: logx.WithContext(ctx), 24 | } 25 | } 26 | 27 | // 提交 答案 28 | func (l *PostQuestionsChangeLogic) PostQuestionsChange(in *questions.QuestionsAnswerReq) (*questions.QuestionsAnswerResp, error) { 29 | logx.Info("in=", in) 30 | result, err := l.svcCtx.QuestionsAnswersModel.Insert(model.AppQuestionsAnswers{ 31 | Beid: in.Beid, 32 | Ptyid: in.Ptyid, 33 | Answers: in.Answers, 34 | Score: in.Score, 35 | ActivityId: in.ActivityId, 36 | Uid: in.Uid, 37 | Auid: in.Auid, 38 | }) 39 | if err != nil { 40 | return nil, err 41 | } 42 | id, err := result.LastInsertId() 43 | if err != nil { 44 | return nil, err 45 | } 46 | return &questions.QuestionsAnswerResp{ 47 | Score: in.Score, 48 | AnswerId: id, 49 | }, nil 50 | } 51 | -------------------------------------------------------------------------------- /internal/handler/questions/lotteryhandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "net/http" 7 | 8 | logic "datacenter/internal/logic/questions" 9 | "datacenter/internal/svc" 10 | "datacenter/internal/types" 11 | "datacenter/questions/rpc/questionsclient" 12 | "datacenter/shared" 13 | 14 | "github.com/zeromicro/go-zero/rest/httpx" 15 | ) 16 | 17 | func LotteryHandler(ctx *svc.ServiceContext) http.HandlerFunc { 18 | return func(w http.ResponseWriter, r *http.Request) { 19 | var req types.AwardConvertReq 20 | if err := httpx.Parse(r, &req); err != nil { 21 | httpx.Error(w, err) 22 | return 23 | } 24 | uid, _ := json.Number(fmt.Sprintf("%v", r.Context().Value("uid"))).Int64() 25 | auid, _ := json.Number(fmt.Sprintf("%v", r.Context().Value("auid"))).Int64() 26 | ptyid, _ := json.Number(fmt.Sprintf("%v", r.Context().Value("ptyid"))).Int64() 27 | beid, _ := json.Number(fmt.Sprintf("%v", r.Context().Value("beid"))).Int64() 28 | l := logic.NewLotteryLogic(r.Context(), ctx) 29 | resp, err := l.Lottery(questionsclient.ConvertReq{ 30 | Uid: uid, 31 | Auid: auid, 32 | LotteryId: req.LotteryId, 33 | Username: req.UserName, 34 | Phone: req.Phone, 35 | Beid: beid, 36 | Ptyid: ptyid, 37 | }) 38 | if err != nil { 39 | httpx.Error(w, err) 40 | } else { 41 | shared.OkJson(w, resp) 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /user/rpc/internal/server/userserver.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: user.proto 3 | 4 | package server 5 | 6 | import ( 7 | "context" 8 | 9 | "datacenter/user/rpc/internal/logic" 10 | "datacenter/user/rpc/internal/svc" 11 | "datacenter/user/rpc/user" 12 | ) 13 | 14 | type UserServer struct { 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewUserServer(svcCtx *svc.ServiceContext) *UserServer { 19 | return &UserServer{ 20 | svcCtx: svcCtx, 21 | } 22 | } 23 | 24 | func (s *UserServer) Ping(ctx context.Context, in *user.Request) (*user.Response, error) { 25 | l := logic.NewPingLogic(ctx, s.svcCtx) 26 | return l.Ping(in) 27 | } 28 | 29 | func (s *UserServer) Register(ctx context.Context, in *user.RegisterReq) (*user.UserReply, error) { 30 | l := logic.NewRegisterLogic(ctx, s.svcCtx) 31 | return l.Register(in) 32 | } 33 | 34 | func (s *UserServer) Login(ctx context.Context, in *user.LoginReq) (*user.UserReply, error) { 35 | l := logic.NewLoginLogic(ctx, s.svcCtx) 36 | return l.Login(in) 37 | } 38 | 39 | func (s *UserServer) UserInfo(ctx context.Context, in *user.UserReq) (*user.UserReply, error) { 40 | l := logic.NewUserInfoLogic(ctx, s.svcCtx) 41 | return l.UserInfo(in) 42 | } 43 | 44 | func (s *UserServer) SnsLogin(ctx context.Context, in *user.AppConfigReq) (*user.AppUserResp, error) { 45 | l := logic.NewSnsLoginLogic(ctx, s.svcCtx) 46 | return l.SnsLogin(in) 47 | } 48 | -------------------------------------------------------------------------------- /internal/handler/questions/turntablehandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "net/http" 7 | 8 | logic "datacenter/internal/logic/questions" 9 | "datacenter/internal/svc" 10 | "datacenter/internal/types" 11 | "datacenter/questions/rpc/questionsclient" 12 | "datacenter/shared" 13 | 14 | "github.com/zeromicro/go-zero/rest/httpx" 15 | ) 16 | 17 | func TurntableHandler(ctx *svc.ServiceContext) http.HandlerFunc { 18 | return func(w http.ResponseWriter, r *http.Request) { 19 | var req types.QuestionsAwardReq 20 | if err := httpx.Parse(r, &req); err != nil { 21 | httpx.Error(w, err) 22 | return 23 | } 24 | 25 | uid, _ := json.Number(fmt.Sprintf("%v", r.Context().Value("uid"))).Int64() 26 | auid, _ := json.Number(fmt.Sprintf("%v", r.Context().Value("auid"))).Int64() 27 | ptyid, _ := json.Number(fmt.Sprintf("%v", r.Context().Value("ptyid"))).Int64() 28 | beid, _ := json.Number(fmt.Sprintf("%v", r.Context().Value("beid"))).Int64() 29 | 30 | in := questionsclient.TurnTableReq{ 31 | ActivityId: req.ActivityId, 32 | Uid: uid, 33 | Auid: auid, 34 | AnswerId: req.AnswerId, 35 | Beid: beid, 36 | Ptyid: ptyid, 37 | } 38 | 39 | l := logic.NewTurntableLogic(r.Context(), ctx) 40 | resp, err := l.Turntable(in) 41 | if err != nil { 42 | httpx.Error(w, err) 43 | } else { 44 | shared.OkJson(w, resp) 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /internal/middleware/usercheckmiddleware.go: -------------------------------------------------------------------------------- 1 | package middleware 2 | 3 | import ( 4 | "errors" 5 | "net/http" 6 | ) 7 | 8 | var ( 9 | errorUserInfo = errors.New("用户信息获取失败") 10 | authDeny = errors.New("用户信息不一致") 11 | ) 12 | 13 | const ( 14 | userKey = `x-user-id` 15 | ) 16 | 17 | type UserCheckMiddleware struct { 18 | } 19 | 20 | func NewUserCheckMiddleware() *UserCheckMiddleware { 21 | return &UserCheckMiddleware{} 22 | } 23 | 24 | func (m *UserCheckMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc { 25 | return func(w http.ResponseWriter, r *http.Request) { 26 | /* 27 | userId := r.Header.Get(userKey) 28 | jwtUserId := r.Context().Value("userId") 29 | logx.Info("r.Context()========", r.Context()) 30 | logx.Info("uid====", r.Context().Value("uid")) 31 | userInt, err := json.Number(userId).Int64() 32 | if err != nil { 33 | httpx.Error(w, errorUserInfo) 34 | return 35 | } 36 | 37 | jwtInt, err := json.Number(fmt.Sprintf("%v", jwtUserId)).Int64() 38 | if err != nil { 39 | httpx.Error(w, errorUserInfo) 40 | return 41 | } 42 | 43 | if jwtInt != userInt { 44 | httpx.Error(w, authDeny) 45 | return 46 | } 47 | */ 48 | 49 | next(w, r) 50 | } 51 | } 52 | 53 | /** 54 | * 后台 接口更新 55 | */ 56 | func (m *UserCheckMiddleware) AdminHandle(next http.HandlerFunc) http.HandlerFunc { 57 | return func(w http.ResponseWriter, r *http.Request) { 58 | next(w, r) 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /user/rpc/rpc.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: user.proto 3 | 4 | package main 5 | 6 | import ( 7 | "context" 8 | "datacenter/user/rpc/internal/config" 9 | "datacenter/user/rpc/internal/server" 10 | "datacenter/user/rpc/internal/svc" 11 | "datacenter/user/rpc/user" 12 | "flag" 13 | "fmt" 14 | 15 | "github.com/zeromicro/go-zero/core/conf" 16 | "github.com/zeromicro/go-zero/core/logx" 17 | "github.com/zeromicro/go-zero/zrpc" 18 | "golang.org/x/time/rate" 19 | "google.golang.org/grpc" 20 | ) 21 | 22 | var configFile = flag.String("f", "etc/rpc.yaml", "the config file") 23 | var limiter = rate.NewLimiter(rate.Limit(100), 100) 24 | 25 | func main() { 26 | flag.Parse() 27 | 28 | var c config.Config 29 | conf.MustLoad(*configFile, &c) 30 | ctx := svc.NewServiceContext(c) 31 | srv := server.NewUserServer(ctx) 32 | 33 | s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { 34 | user.RegisterUserServer(grpcServer, srv) 35 | }) 36 | defer s.Stop() 37 | s.AddUnaryInterceptors(authInterceptor) 38 | 39 | fmt.Printf("Starting rpc server at %s...\n", c.ListenOn) 40 | s.Start() 41 | } 42 | 43 | //拦截验证器 44 | func authInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { 45 | 46 | if !limiter.Allow() { 47 | fmt.Println("限流了") 48 | return nil, nil 49 | } 50 | logx.Info("好玩") 51 | return handler(ctx, req) 52 | } 53 | -------------------------------------------------------------------------------- /internal/logic/user/registerlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/logic" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/user/rpc/userclient" 10 | 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type RegisterLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | datacenterLogic logic.DatacenterLogic 19 | } 20 | 21 | func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) RegisterLogic { 22 | return RegisterLogic{ 23 | Logger: logx.WithContext(ctx), 24 | ctx: ctx, 25 | svcCtx: svcCtx, 26 | datacenterLogic: logic.NewDatacenterLogic(ctx, svcCtx), 27 | } 28 | } 29 | 30 | func (l *RegisterLogic) Register(req types.RegisterReq) (*types.UserReply, error) { 31 | reply, err := l.svcCtx.UserRpc.Register(l.ctx, &userclient.RegisterReq{ 32 | Mobile: req.Mobile, 33 | Password: req.Password, 34 | Smscode: req.Smscode, 35 | }) 36 | if err != nil { 37 | return nil, err 38 | } 39 | jwttoken, err := l.datacenterLogic.GetJwtToken(types.AppUser{Uid: reply.Uid}) 40 | if err != nil { 41 | return nil, err 42 | } 43 | return &types.UserReply{ 44 | Auid: reply.Auid, 45 | Beid: reply.Beid, 46 | Ptyid: reply.Ptyid, 47 | Uid: reply.Uid, 48 | Username: reply.Username, 49 | Mobile: reply.Mobile, 50 | Avator: reply.Avator, 51 | JwtToken: jwttoken, 52 | }, nil 53 | } 54 | -------------------------------------------------------------------------------- /internal/logic/user/loginlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/logic" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/user/rpc/userclient" 10 | 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type LoginLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | datacenterLogic logic.DatacenterLogic 19 | } 20 | 21 | func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) LoginLogic { 22 | return LoginLogic{ 23 | Logger: logx.WithContext(ctx), 24 | ctx: ctx, 25 | svcCtx: svcCtx, 26 | datacenterLogic: logic.NewDatacenterLogic(ctx, svcCtx), 27 | } 28 | } 29 | 30 | func (l *LoginLogic) Login(req types.LoginReq, beid, ptyid int64) (*types.UserReply, error) { 31 | 32 | reply, err := l.svcCtx.UserRpc.Login(l.ctx, &userclient.LoginReq{ 33 | Mobile: req.Mobile, 34 | Type: req.Type, 35 | Password: req.Password, 36 | }) 37 | if err != nil { 38 | return nil, err 39 | } 40 | jwttoken, err := l.datacenterLogic.GetJwtToken(types.AppUser{Uid: reply.Uid, Ptyid: ptyid, Beid: beid}) 41 | if err != nil { 42 | return nil, err 43 | } 44 | return &types.UserReply{ 45 | Auid: reply.Auid, 46 | Beid: beid, 47 | Ptyid: ptyid, 48 | Uid: reply.Uid, 49 | Username: reply.Username, 50 | Mobile: reply.Mobile, 51 | Avator: reply.Avator, 52 | JwtToken: jwttoken, 53 | }, nil 54 | } 55 | -------------------------------------------------------------------------------- /internal/logic/user/wxloginlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/logic" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/user/rpc/userclient" 10 | 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type WxloginLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | datacenter logic.DatacenterLogic 19 | } 20 | 21 | func NewWxloginLogic(ctx context.Context, svcCtx *svc.ServiceContext) WxloginLogic { 22 | return WxloginLogic{ 23 | Logger: logx.WithContext(ctx), 24 | ctx: ctx, 25 | svcCtx: svcCtx, 26 | datacenter: logic.NewDatacenterLogic(ctx, svcCtx), 27 | } 28 | } 29 | 30 | func (l *WxloginLogic) Wxlogin(req types.WxLoginReq) (*types.LoginAppUser, error) { 31 | 32 | reply, err := l.svcCtx.UserRpc.SnsLogin(l.ctx, &userclient.AppConfigReq{Beid: req.Beid, Ptyid: req.Ptyid, Code: req.Code}) 33 | if err != nil { 34 | return nil, err 35 | } 36 | token, err := l.datacenter.GetJwtToken(types.AppUser{ 37 | Auid: reply.Auid, 38 | Beid: reply.Beid, 39 | Ptyid: reply.Ptyid, 40 | Nickname: reply.Nickname, 41 | Openid: reply.Openid, 42 | }) 43 | if err != nil { 44 | return nil, err 45 | } 46 | return &types.LoginAppUser{ 47 | Auid: reply.Auid, 48 | Beid: reply.Beid, 49 | Ptyid: reply.Ptyid, 50 | Nickname: reply.Nickname, 51 | Openid: reply.Openid, 52 | Avator: reply.Avator, 53 | JwtToken: token, 54 | }, nil 55 | } 56 | -------------------------------------------------------------------------------- /internal/handler/common/snsinfohandler.go: -------------------------------------------------------------------------------- 1 | package handler 2 | 3 | import ( 4 | "net/http" 5 | 6 | logic "datacenter/internal/logic/common" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/shared" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func SnsInfoHandler(ctx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | 17 | // req.Beid.Beid = shared.StrToInt64(r.Header.Get("beid")) 18 | // req.Ptyid = shared.StrToInt64(r.Header.Get("ptyid")) 19 | var req types.SnsReq 20 | if err := httpx.Parse(r, &req); err != nil { 21 | httpx.Error(w, err) 22 | return 23 | } 24 | 25 | //req.BackUrl = r.URL.Query().Get("backurl") 26 | if req.Ptyid == 0 || req.Beid.Beid == 0 { 27 | httpx.Error(w, shared.ErrorNoRequiredParameters) 28 | return 29 | } 30 | //站点的sns 31 | lsns := logic.NewSnsInfoLogic(r.Context(), ctx) 32 | //站点配置的 33 | lapp := logic.NewAppInfoLogic(r.Context(), ctx) 34 | app, err := lapp.AppInfo(req.Beid) 35 | if err != nil { 36 | httpx.Error(w, err) 37 | return 38 | } 39 | resp, err := lsns.SnsInfo(req) 40 | if err != nil { 41 | httpx.Error(w, err) 42 | return 43 | } 44 | app.Fullwebsite = app.Fullwebsite + req.BackUrl 45 | shared.OkJson(w, types.SnsResp{ 46 | Beid: req.Beid, 47 | Ptyid: resp.Ptyid, 48 | Appid: resp.Appid, 49 | Title: resp.Title, 50 | LoginUrl: shared.GetWxLoginUrl(resp.Appid, app.Fullwebsite), 51 | }) 52 | 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /internal/middleware/corsmiddleware.go: -------------------------------------------------------------------------------- 1 | package middleware 2 | 3 | import "net/http" 4 | 5 | // CorsMiddleware 跨域请求处理中间件 6 | type CorsMiddleware struct { 7 | } 8 | 9 | // NewCorsMiddleware 新建跨域请求处理中间件 10 | func NewCorsMiddleware() *CorsMiddleware { 11 | return &CorsMiddleware{} 12 | } 13 | 14 | // Handle 跨域请求处理 15 | func (m *CorsMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc { 16 | return func(w http.ResponseWriter, r *http.Request) { 17 | setHeader(w) 18 | 19 | // 放行所有 OPTIONS 方法 20 | if r.Method == "OPTIONS" { 21 | w.WriteHeader(http.StatusNoContent) 22 | return 23 | } 24 | 25 | // 处理请求 26 | next(w, r) 27 | } 28 | } 29 | 30 | // Handler 跨域请求处理器 31 | func (m *CorsMiddleware) Handler() http.Handler { 32 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 33 | setHeader(w) 34 | 35 | if r.Method == "OPTIONS" { 36 | w.WriteHeader(http.StatusNoContent) 37 | } else { 38 | w.WriteHeader(http.StatusNotFound) 39 | } 40 | }) 41 | } 42 | 43 | // setHeader 设置响应头 44 | func setHeader(w http.ResponseWriter) { 45 | w.Header().Set("Access-Control-Allow-Origin", "*") 46 | w.Header().Set("Access-Control-Allow-Headers", "Content-Type, X-CSRF-Token, authorization, beid, ptyid") 47 | w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH") 48 | w.Header().Set("Access-Control-Expose-Headers", "Content-Length, Content-Type, Access-Control-Allow-Origin, Access-Control-Allow-Headers") 49 | w.Header().Set("Access-Control-Allow-Credentials", "true") 50 | } 51 | -------------------------------------------------------------------------------- /questions/rpc/internal/logic/getawardinfologic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/questions/model" 7 | "datacenter/questions/rpc/internal/svc" 8 | "datacenter/questions/rpc/questions" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type GetAwardInfoLogic struct { 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | logx.Logger 17 | } 18 | 19 | /** 20 | * 数据格式转换 21 | */ 22 | func Awards2AwardInfoResp(awards *model.AppQuestionsAwards) *questions.AwardInfoResp { 23 | return &questions.AwardInfoResp{ 24 | Id: awards.Id, 25 | Beid: awards.Beid, 26 | Ptyid: awards.Ptyid, 27 | ActivityId: awards.ActivityId, 28 | StartProbability: awards.StartProbability, 29 | EndProbability: awards.EndProbability, 30 | Des: awards.Des, 31 | Title: awards.Title.String, 32 | Header: awards.Header, 33 | Image: awards.Image.String, 34 | } 35 | } 36 | 37 | func NewGetAwardInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAwardInfoLogic { 38 | return &GetAwardInfoLogic{ 39 | ctx: ctx, 40 | svcCtx: svcCtx, 41 | Logger: logx.WithContext(ctx), 42 | } 43 | } 44 | 45 | // 获取 问答奖品信息 46 | func (l *GetAwardInfoLogic) GetAwardInfo(in *questions.ActivitiesReq) (*questions.AwardInfoResp, error) { 47 | AwardInfo, err := l.svcCtx.QuestionsAwardsModel.FindOne(in.Actid) 48 | if err != nil { 49 | return nil, err 50 | } 51 | return Awards2AwardInfoResp(AwardInfo), nil 52 | } 53 | -------------------------------------------------------------------------------- /internal/logic/user/code2sessionlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/internal/logic" 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | "datacenter/user/rpc/userclient" 10 | 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type Code2SessionLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | datacenter logic.DatacenterLogic 19 | } 20 | 21 | func NewCode2SessionLogic(ctx context.Context, svcCtx *svc.ServiceContext) Code2SessionLogic { 22 | return Code2SessionLogic{ 23 | Logger: logx.WithContext(ctx), 24 | ctx: ctx, 25 | svcCtx: svcCtx, 26 | datacenter: logic.NewDatacenterLogic(ctx, svcCtx), 27 | } 28 | } 29 | 30 | func (l *Code2SessionLogic) Code2Session(beid, ptyid int64, code string) (lp *types.LoginAppUser, err error) { 31 | 32 | reply, err := l.svcCtx.UserRpc.SnsLogin(l.ctx, &userclient.AppConfigReq{Beid: beid, Ptyid: ptyid, Code: code}) 33 | if err != nil { 34 | return 35 | } 36 | token, err := l.datacenter.GetJwtToken(types.AppUser{ 37 | Auid: reply.Auid, 38 | Beid: reply.Beid, 39 | Ptyid: reply.Ptyid, 40 | Nickname: reply.Nickname, 41 | Openid: reply.Openid, 42 | }) 43 | if err != nil { 44 | return 45 | } 46 | return &types.LoginAppUser{ 47 | Auid: reply.Auid, 48 | Beid: reply.Beid, 49 | Ptyid: reply.Ptyid, 50 | Nickname: reply.Nickname, 51 | Openid: reply.Openid, 52 | Avator: reply.Avator, 53 | JwtToken: token, 54 | }, nil 55 | } 56 | -------------------------------------------------------------------------------- /questions/rpc/internal/logic/getquestionslistlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/questions/model" 7 | "datacenter/questions/rpc/internal/svc" 8 | "datacenter/questions/rpc/questions" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type GetQuestionsListLogic struct { 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | logx.Logger 17 | } 18 | 19 | func Questions2QuestionsInfoResp(q *model.AppQuestions) *questions.QuestionsResp { 20 | return &questions.QuestionsResp{ 21 | Id: q.Id, 22 | Beid: q.Beid, 23 | Ptyid: q.Ptyid, 24 | ActivityId: q.ActivityId, 25 | Options: q.Options, 26 | Corrent: q.Corrent, 27 | Question: q.Question, 28 | Status: q.Status, 29 | } 30 | } 31 | 32 | func NewGetQuestionsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetQuestionsListLogic { 33 | return &GetQuestionsListLogic{ 34 | ctx: ctx, 35 | svcCtx: svcCtx, 36 | Logger: logx.WithContext(ctx), 37 | } 38 | } 39 | 40 | // 获取 问题列表 41 | func (l *GetQuestionsListLogic) GetQuestionsList(in *questions.ActivitiesReq) (*questions.QuestionsListResp, error) { 42 | lists, err := l.svcCtx.QuestionsModel.Find(in.Actid) 43 | if err != nil { 44 | return nil, err 45 | } 46 | list := make([]*questions.QuestionsResp, 0) 47 | if len(lists) > 0 { 48 | for _, questions := range lists { 49 | list = append(list, Questions2QuestionsInfoResp(&questions)) 50 | } 51 | } 52 | return &questions.QuestionsListResp{ 53 | Data: list, 54 | }, nil 55 | } 56 | -------------------------------------------------------------------------------- /votes/rpc/internal/logic/getenrollinfologic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | "encoding/json" 6 | 7 | "datacenter/votes/rpc/internal/svc" 8 | "datacenter/votes/rpc/votes" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type GetEnrollInfoLogic struct { 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | logx.Logger 17 | } 18 | 19 | func NewGetEnrollInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetEnrollInfoLogic { 20 | return &GetEnrollInfoLogic{ 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | Logger: logx.WithContext(ctx), 24 | } 25 | } 26 | 27 | // 获取作品信息 28 | func (l *GetEnrollInfoLogic) GetEnrollInfo(in *votes.EnrollInfoReq) (*votes.EnrollResp, error) { 29 | vote, err := l.svcCtx.AppEnrollModel.FindOne(in.Aeid) 30 | if err != nil { 31 | logx.Info("EnrollNotExist==", EnrollNotExist) 32 | return nil, EnrollNotExist 33 | } 34 | image := make([]string, 0) 35 | err = json.Unmarshal([]byte(vote.Images), &image) 36 | if err != nil { 37 | return nil, err 38 | } 39 | //增加 浏览量和 40 | l.svcCtx.AppEnrollModel.IncrView(in.Aeid) 41 | l.svcCtx.AppVotesActivityModel.IncrView(in.Actid) 42 | return &votes.EnrollResp{ 43 | Ptyid: vote.Ptyid, 44 | Uid: vote.Uid, 45 | Beid: vote.Beid, 46 | Auid: vote.Auid, 47 | Actid: vote.Actid, 48 | Name: vote.Name, 49 | Images: image, 50 | Descr: vote.Descr, 51 | Aeid: vote.Aeid, 52 | Address: vote.Address, 53 | Viewcount: vote.Viewcount, 54 | Votecount: vote.Votecount, 55 | }, nil 56 | } 57 | -------------------------------------------------------------------------------- /server.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ## author by jackluo 3 | ## Email net.webjoy@gmail.com 4 | ## wechat 13228191831 5 | ##### 启动mysql 服务 6 | docker kill mysql 7 | docker rm mysql 8 | docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=admin -v $(pwd)/mysql8:/var/lib/mysql -d mysql:8.0.21 9 | 10 | ##### 启动redis 服务 11 | docker stop redis 12 | docker rm redis 13 | echo "Start Redis Service..." 14 | docker run --name redis -d \ 15 | --publish 6379:6379 \ 16 | --env 'REDIS_PASSWORD=admin' \ 17 | --volume $(pwd)/redis:/var/lib/redis \ 18 | sameersbn/redis:latest 19 | 20 | ### 启动 etcd 服务 21 | #!/bin/bash 22 | rm -rf $(pwd)/etcd && mkdir -p $(pwd)/etcd && \ 23 | docker stop etcd && \ 24 | docker rm etcd || true && \ 25 | docker run -d \ 26 | -p 2379:2379 \ 27 | -p 2380:2380 \ 28 | --mount type=bind,source=$(pwd)/etcd,destination=/etcd-data \ 29 | --name etcd \ 30 | quay.io/coreos/etcd \ 31 | /usr/local/bin/etcd \ 32 | --name s1 \ 33 | --data-dir /etcd-data \ 34 | --listen-client-urls http://0.0.0.0:2379 \ 35 | --advertise-client-urls http://0.0.0.0:2379 \ 36 | --listen-peer-urls http://0.0.0.0:2380 \ 37 | --initial-advertise-peer-urls http://0.0.0.0:2380 \ 38 | --initial-cluster s1=http://0.0.0.0:2380 \ 39 | --initial-cluster-token tkn \ 40 | --initial-cluster-state new 41 | 42 | 43 | #### 启动elasticsearch 服务 44 | docker stop es 45 | docker rm es 46 | 47 | docker run -d --name es \ 48 | -p 9200:9200 -p 9300:9300 \ 49 | -e "discovery.type=single-node" \ 50 | -v $(pwd)/elasticsearch:/usr/share/elasticsearch/data \ 51 | spencezhou/elasticsearch:7.6.2 -------------------------------------------------------------------------------- /search/rpc/searchclient/search.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: search.proto 3 | 4 | //go:generate mockgen -destination ./search_mock.go -package searchclient -source $GOFILE 5 | 6 | package searchclient 7 | 8 | import ( 9 | "context" 10 | 11 | "datacenter/search/rpc/search" 12 | 13 | "github.com/zeromicro/go-zero/zrpc" 14 | ) 15 | 16 | type ( 17 | LimitReq = search.LimitReq 18 | SearchReq = search.SearchReq 19 | ArticleResp = search.ArticleResp 20 | ArticleReq = search.ArticleReq 21 | Request = search.Request 22 | Response = search.Response 23 | 24 | Search interface { 25 | ArticleInit(ctx context.Context, in *Request) (*Response, error) 26 | ArticleStore(ctx context.Context, in *ArticleReq) (*Response, error) 27 | ArticleSearch(ctx context.Context, in *SearchReq) (*ArticleResp, error) 28 | } 29 | 30 | defaultSearch struct { 31 | cli zrpc.Client 32 | } 33 | ) 34 | 35 | func NewSearch(cli zrpc.Client) Search { 36 | return &defaultSearch{ 37 | cli: cli, 38 | } 39 | } 40 | 41 | func (m *defaultSearch) ArticleInit(ctx context.Context, in *Request) (*Response, error) { 42 | client := search.NewSearchClient(m.cli.Conn()) 43 | return client.ArticleInit(ctx, in) 44 | } 45 | 46 | func (m *defaultSearch) ArticleStore(ctx context.Context, in *ArticleReq) (*Response, error) { 47 | client := search.NewSearchClient(m.cli.Conn()) 48 | return client.ArticleStore(ctx, in) 49 | } 50 | 51 | func (m *defaultSearch) ArticleSearch(ctx context.Context, in *SearchReq) (*ArticleResp, error) { 52 | client := search.NewSearchClient(m.cli.Conn()) 53 | return client.ArticleSearch(ctx, in) 54 | } 55 | -------------------------------------------------------------------------------- /votes/rpc/internal/logic/getenrolllistlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | "encoding/json" 6 | 7 | "datacenter/votes/rpc/internal/svc" 8 | "datacenter/votes/rpc/votes" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type GetEnrollListLogic struct { 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | logx.Logger 17 | } 18 | 19 | func NewGetEnrollListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetEnrollListLogic { 20 | return &GetEnrollListLogic{ 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | Logger: logx.WithContext(ctx), 24 | } 25 | } 26 | 27 | // 获取 作品列表 28 | func (l *GetEnrollListLogic) GetEnrollList(in *votes.ActidReq) (*votes.EnrollListResp, error) { 29 | //这里可以控制走哪里 30 | list, err := l.svcCtx.AppEnrollModel.Find(in) 31 | if err != nil { 32 | return nil, err 33 | } 34 | 35 | lists := make([]*votes.EnrollResp, 0) 36 | for _, vote := range list { 37 | images := make([]string, 0) 38 | err := json.Unmarshal([]byte(vote.Images), &images) 39 | if err != nil { 40 | return nil, err 41 | } 42 | enrollResp := votes.EnrollResp{ 43 | Ptyid: vote.Ptyid, 44 | Uid: vote.Uid, 45 | Beid: vote.Beid, 46 | Auid: vote.Auid, 47 | Aeid: vote.Aeid, 48 | Actid: vote.Actid, 49 | Name: vote.Name, 50 | Descr: vote.Descr, 51 | Images: images, 52 | Address: vote.Address, 53 | Viewcount: vote.Viewcount, 54 | Votecount: vote.Votecount, 55 | } 56 | logx.Info("enrollResp.Aeid===", enrollResp.Aeid) 57 | lists = append(lists, &enrollResp) 58 | } 59 | 60 | return &votes.EnrollListResp{ 61 | Data: lists, 62 | }, nil 63 | } 64 | -------------------------------------------------------------------------------- /user/api/user.api: -------------------------------------------------------------------------------- 1 | info( 2 | title: "user api"// TODO: add title 3 | desc: "用户系统"// TODO: add description 4 | author: "jackluo" 5 | email: "net.webjoy@gmail.com" 6 | ) 7 | //注册请求 8 | type RegisterReq struct { 9 | // TODO: add members here and delete this comment 10 | Username string `json:"username"` 11 | Mobile string `json:"mobile"` //基本一个手机号码就完事 12 | Password string `json:"password"` 13 | Smscode string `json:"smscode"` //短信码 14 | } 15 | //登陆请求 16 | type LoginReq struct{ 17 | Username string `json:"username"` 18 | Mobile string `json:"mobile"` 19 | Password string `json:"password"` 20 | Smscode string `json:"smscode"` //短信码 21 | } 22 | //返回用户信息 23 | type UserReply struct { 24 | auid int64 `json:"auid"` 25 | Uid int64 `json:"uid"` 26 | Beid int64 `json:"beid"` //应用id 27 | Ptyid int64 `json:"ptyid"` //对应平台 28 | Username string `json:"username"` 29 | Mobile string `json:"mobile"` 30 | Nickname string `json:"nickname"` 31 | Openid string `json:"openid"` 32 | Avator string `json:"avator"` 33 | Gender string `json:"gender"` 34 | JwtToken 35 | } 36 | 37 | type JwtToken struct { 38 | AccessToken string `json:"accessToken,omitempty"` 39 | AccessExpire int64 `json:"accessExpire,omitempty"` 40 | RefreshAfter int64 `json:"refreshAfter,omitempty"` 41 | } 42 | 43 | service user-api { 44 | @handler ping 45 | post /user/ping () 46 | 47 | @handler register 48 | post /user/register (RegisterReq) 49 | 50 | @handler login 51 | post /user/login (LoginReq) returns (UserReply) 52 | } 53 | @server( 54 | jwt: Auth 55 | middleware: Usercheck 56 | ) 57 | service user-api { 58 | @handler userInfo 59 | get /user/info () returns (UserReply) 60 | } -------------------------------------------------------------------------------- /internal/logic/datacenterlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | "time" 6 | 7 | "datacenter/internal/svc" 8 | "datacenter/internal/types" 9 | 10 | "github.com/dgrijalva/jwt-go" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type DatacenterLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewDatacenterLogic(ctx context.Context, svcCtx *svc.ServiceContext) DatacenterLogic { 21 | return DatacenterLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *DatacenterLogic) Datacenter(req types.Request) (*types.Response, error) { 29 | // todo: add your logic here and delete this line 30 | // 在这里加入业务逻辑,我们用打印日志来代表业务逻辑 31 | l.Infof("name: %v", req.Name) 32 | 33 | return &types.Response{Message: "Hello Jack"}, nil 34 | } 35 | 36 | func (l *DatacenterLogic) GetJwtToken(ap types.AppUser) (types.JwtToken, error) { 37 | iat := time.Now().Unix() 38 | accessExpire := l.svcCtx.Config.Auth.AccessExpire 39 | 40 | claims := make(jwt.MapClaims) 41 | claims["exp"] = iat + l.svcCtx.Config.Auth.AccessExpire 42 | claims["iat"] = iat 43 | claims["uid"] = ap.Uid 44 | claims["auid"] = ap.Auid 45 | claims["beid"] = ap.Beid 46 | claims["ptyid"] = ap.Ptyid 47 | claims["openid"] = ap.Openid 48 | token := jwt.New(jwt.SigningMethodHS256) 49 | token.Claims = claims 50 | jwtstr, err := token.SignedString([]byte(l.svcCtx.Config.Auth.AccessSecret)) 51 | if err != nil { 52 | return types.JwtToken{}, err 53 | } 54 | return types.JwtToken{ 55 | AccessToken: jwtstr, 56 | AccessExpire: iat + accessExpire, 57 | RefreshAfter: iat + accessExpire/2, 58 | }, nil 59 | } 60 | -------------------------------------------------------------------------------- /questions/rpc/internal/svc/servicecontext.go: -------------------------------------------------------------------------------- 1 | package svc 2 | 3 | import ( 4 | "datacenter/questions/model" 5 | "datacenter/questions/rpc/internal/config" 6 | 7 | "github.com/zeromicro/go-zero/core/stores/redis" 8 | "github.com/zeromicro/go-zero/core/stores/sqlx" 9 | ) 10 | 11 | type ServiceContext struct { 12 | Config config.Config 13 | RedisConn *redis.Redis 14 | QuestionsActivitiesModel model.AppQuestionsActivitiesModel 15 | QuestionsAwardsModel model.AppQuestionsAwardsModel 16 | QuestionsModel model.AppQuestionsModel 17 | QuestionsAnswersModel model.AppQuestionsAnswersModel 18 | QuestionsLotteriesModel model.AppQuestionsLotteriesModel 19 | QuestionsConvertsModel model.AppQuestionsConvertsModel 20 | } 21 | 22 | func NewServiceContext(c config.Config) *ServiceContext { 23 | rconn := redis.NewRedis(c.CacheRedis[0].Host, c.CacheRedis[0].Type, c.CacheRedis[0].Pass) 24 | mconn := sqlx.NewMysql(c.Mysql.DataSource) 25 | qam := model.NewAppQuestionsActivitiesModel(mconn, c.CacheRedis) 26 | qawardsm := model.NewAppQuestionsAwardsModel(mconn, c.CacheRedis) 27 | qm := model.NewAppQuestionsModel(mconn, c.CacheRedis) 28 | qanswersm := model.NewAppQuestionsAnswersModel(mconn, c.CacheRedis) 29 | qlm := model.NewAppQuestionsLotteriesModel(mconn, c.CacheRedis) 30 | qcm := model.NewAppQuestionsConvertsModel(mconn, c.CacheRedis) 31 | return &ServiceContext{ 32 | Config: c, 33 | RedisConn: rconn, 34 | QuestionsActivitiesModel: qam, 35 | QuestionsAwardsModel: qawardsm, 36 | QuestionsModel: qm, 37 | QuestionsAnswersModel: qanswersm, 38 | QuestionsLotteriesModel: qlm, 39 | QuestionsConvertsModel: qcm, 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /restart.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ## author by jackluo 3 | ## Email net.webjoy@gmail.com 4 | ## wechat 13228191831 5 | 6 | getewayPath=$(pwd) #网关服务 7 | userPath=$(pwd)/user/rpc #用户服务 8 | commonPath=$(pwd)/common/rpc #公共服务 9 | votesPath=$(pwd)/votes/rpc #投票服务 10 | configPath=/etc/rpc.yaml #配置文件 11 | geteWayCnf=/etc/datacenter-api.yaml 12 | UserRpc=user-server #定义网关服务 13 | CommonRpc=common-server #定义公共服务 14 | VotesRpc=votes-server #定义投票服务 15 | geteWayApi=datacenter-server #定义网关服务 16 | 17 | 18 | RpcServer(){ 19 | mydir=$1 20 | myserver=$2 21 | mycnf=$3 22 | cd ${mydir} 23 | go build -o ${myserver} $mydir/rpc.go 24 | kill -9 $(ps -ef|grep "${myserver}"|awk '{print $2}') >/dev/null 2>&1 25 | nohup ${mydir}/${myserver} -f ${mydir}${mycnf} & 26 | } 27 | RpcServerPlus(){ 28 | mydir=$1/$2/rpc 29 | myserver=${2}-server 30 | mycnf=/etc/${2}.yaml 31 | cd ${mydir} 32 | go build -o ${myserver} $mydir/$2.go 33 | kill -9 $(ps -ef|grep "${myserver}"|awk '{print $2}') >/dev/null 2>&1 34 | nohup ${mydir}/${myserver} -f ${mydir}${mycnf} & 35 | } 36 | 37 | StartServer(){ 38 | mydir=$1 39 | myserver=$2 40 | mycnf=$3 41 | cd ${mydir} 42 | go build -o ${myserver} $mydir/datacenter.go 43 | kill -9 $(ps -ef|grep "${myserver}"|awk '{print $2}') >/dev/null 2>&1 44 | nohup ${mydir}/${myserver} -f ${mydir}${mycnf} & 45 | } 46 | #公共服务 47 | RpcServer ${commonPath} ${CommonRpc} ${configPath} 48 | #用户服务 49 | RpcServer ${userPath} ${UserRpc} ${configPath} 50 | #投票服务 51 | RpcServer ${votesPath} ${VotesRpc} ${configPath} 52 | #搜索服务 53 | RpcServerPlus ${getewayPath} search 54 | #问答抽奖服务 55 | RpcServerPlus ${getewayPath} questions 56 | #Api服务 57 | StartServer ${getewayPath} ${geteWayApi} ${geteWayCnf} 58 | 59 | 60 | -------------------------------------------------------------------------------- /questions.api: -------------------------------------------------------------------------------- 1 | // 问答 抽奖 开始 2 | @server( 3 | group: questions 4 | ) 5 | service datacenter-api { 6 | @doc( 7 | summary: "获取活动的信息" 8 | ) 9 | @handler activitiesInfo 10 | get /questions/activities/info (Actid) returns (ActivityResp) 11 | @doc( 12 | summary: "获取奖品信息" 13 | ) 14 | @handler awardInfo 15 | get /questions/award/info (Actid) returns (ActivityResp) 16 | 17 | @handler awardList 18 | get /questions/award/list (Actid) returns (ActivityResp) 19 | 20 | } 21 | type AnswerReq struct { 22 | ActivityId int64 `json:"actid"` 23 | Answers string `json:"answers"` 24 | Score string `json:"score"` 25 | } 26 | type QuestionsAwardReq struct { 27 | ActivityId int64 `json:"actid"` 28 | AnswerId int64 `json:"answerid"` 29 | } 30 | type AnswerResp struct { 31 | Answers string `json:"answers"` 32 | Score string `json:"score"` 33 | } 34 | type AwardConvertReq struct { 35 | UserName string `json:"username"` 36 | Phone string `json:"phone"` 37 | LotteryId int64 `json:"lotteryid"` 38 | } 39 | 40 | 41 | @server( 42 | jwt: Auth 43 | group: questions 44 | middleware: Usercheck 45 | ) 46 | service datacenter-api { 47 | @doc( 48 | summary: "获取题目" 49 | ) 50 | @handler lists 51 | get /questions/lists (VoteReq) returns (AnswerResp) 52 | @doc( 53 | summary: "提交答案" 54 | ) 55 | @handler change 56 | post /questions/change (AnswerReq) returns (VoteResp) 57 | 58 | @doc( 59 | summary: "获取分数" 60 | ) 61 | @handler grade 62 | get /questions/grade (VoteReq) returns (VoteResp) 63 | 64 | @doc( 65 | summary: "开始转盘" 66 | ) 67 | @handler turntable 68 | post /questions/lottery/turntable (EnrollReq) returns (EnrollResp) 69 | @doc( 70 | summary: "填写中奖信息人" 71 | ) 72 | @handler lottery 73 | post /questions/lottery/convert (AwardConvertReq) returns (EnrollResp) 74 | } 75 | 76 | 77 | // 问答 抽奖 结束 -------------------------------------------------------------------------------- /votes/rpc/internal/server/votesserver.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: votes.proto 3 | 4 | package server 5 | 6 | import ( 7 | "context" 8 | 9 | "datacenter/votes/rpc/internal/logic" 10 | "datacenter/votes/rpc/internal/svc" 11 | "datacenter/votes/rpc/votes" 12 | ) 13 | 14 | type VotesServer struct { 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewVotesServer(svcCtx *svc.ServiceContext) *VotesServer { 19 | return &VotesServer{ 20 | svcCtx: svcCtx, 21 | } 22 | } 23 | 24 | // 获取作品信息 25 | func (s *VotesServer) GetEnrollInfo(ctx context.Context, in *votes.EnrollInfoReq) (*votes.EnrollResp, error) { 26 | l := logic.NewGetEnrollInfoLogic(ctx, s.svcCtx) 27 | return l.GetEnrollInfo(in) 28 | } 29 | 30 | // 获取 作品列表 31 | func (s *VotesServer) GetEnrollList(ctx context.Context, in *votes.ActidReq) (*votes.EnrollListResp, error) { 32 | l := logic.NewGetEnrollListLogic(ctx, s.svcCtx) 33 | return l.GetEnrollList(in) 34 | } 35 | 36 | // 获取 活动信息 37 | func (s *VotesServer) GetActivityInfo(ctx context.Context, in *votes.ActInfoReq) (*votes.ActInfoResp, error) { 38 | l := logic.NewGetActivityInfoLogic(ctx, s.svcCtx) 39 | return l.GetActivityInfo(in) 40 | } 41 | 42 | // 报名 43 | func (s *VotesServer) Enroll(ctx context.Context, in *votes.EnrollReq) (*votes.EnrollResp, error) { 44 | l := logic.NewEnrollLogic(ctx, s.svcCtx) 45 | return l.Enroll(in) 46 | } 47 | 48 | // 投票 49 | func (s *VotesServer) Votes(ctx context.Context, in *votes.VotesReq) (*votes.VotesResp, error) { 50 | l := logic.NewVotesLogic(ctx, s.svcCtx) 51 | return l.Votes(in) 52 | } 53 | 54 | // 增加活动的爆光量 55 | func (s *VotesServer) IncrActiviView(ctx context.Context, in *votes.ActInfoReq) (*votes.ActInfoResp, error) { 56 | l := logic.NewIncrActiviViewLogic(ctx, s.svcCtx) 57 | return l.IncrActiviView(in) 58 | } 59 | -------------------------------------------------------------------------------- /datacenter.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "net/http" 7 | "strings" 8 | 9 | "datacenter/internal/config" 10 | "datacenter/internal/handler" 11 | "datacenter/internal/middleware" 12 | "datacenter/internal/svc" 13 | "datacenter/shared" 14 | 15 | "github.com/zeromicro/go-zero/core/conf" 16 | "github.com/zeromicro/go-zero/rest" 17 | "github.com/zeromicro/go-zero/rest/httpx" 18 | ) 19 | 20 | var configFile = flag.String("f", "etc/datacenter-api.yaml", "the config file") 21 | 22 | func dirhandler(patern, filedir string) http.HandlerFunc { 23 | return func(w http.ResponseWriter, req *http.Request) { 24 | handler := http.StripPrefix(patern, http.FileServer(http.Dir(filedir))) 25 | handler.ServeHTTP(w, req) 26 | 27 | } 28 | } 29 | 30 | func staticFileHandler(engine *rest.Server) { 31 | //这里注册 32 | dirlevel := []string{":1", ":2", ":3", ":4", ":5", ":6", ":7", ":8"} 33 | patern := "/static/" 34 | dirpath := "./assets/" 35 | for i := 1; i < len(dirlevel); i++ { 36 | path := "/" + strings.Join(dirlevel[:i], "/") 37 | //最后生成 /asset 38 | engine.AddRoute( 39 | rest.Route{ 40 | Method: http.MethodGet, 41 | Path: path, 42 | Handler: dirhandler(patern, dirpath), 43 | }) 44 | } 45 | 46 | } 47 | 48 | func main() { 49 | flag.Parse() 50 | 51 | var c config.Config 52 | conf.MustLoad(*configFile, &c) 53 | 54 | ctx := svc.NewServiceContext(c) 55 | server := rest.MustNewServer(c.RestConf, rest.WithNotAllowedHandler(middleware.NewCorsMiddleware().Handler())) 56 | defer server.Stop() 57 | server.Use(middleware.NewCorsMiddleware().Handle) 58 | //静太文件处理 59 | staticFileHandler(server) 60 | // 设置错误处理函数 61 | httpx.SetErrorHandler(shared.ErrorHandler) 62 | 63 | handler.RegisterHandlers(server, ctx) 64 | 65 | fmt.Printf("Starting server xxx at %s:%d...\n", c.Host, c.Port) 66 | server.Start() 67 | } 68 | -------------------------------------------------------------------------------- /internal/logic/votes/enrolllogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | "encoding/json" 6 | "fmt" 7 | 8 | "datacenter/internal/svc" 9 | "datacenter/internal/types" 10 | "datacenter/shared" 11 | "datacenter/votes/rpc/votesclient" 12 | 13 | "github.com/zeromicro/go-zero/core/logx" 14 | ) 15 | 16 | type EnrollLogic struct { 17 | logx.Logger 18 | ctx context.Context 19 | svcCtx *svc.ServiceContext 20 | } 21 | 22 | func NewEnrollLogic(ctx context.Context, svcCtx *svc.ServiceContext) EnrollLogic { 23 | return EnrollLogic{ 24 | Logger: logx.WithContext(ctx), 25 | ctx: ctx, 26 | svcCtx: svcCtx, 27 | } 28 | } 29 | 30 | func (l *EnrollLogic) Enroll(req types.EnrollReq) (*types.EnrollResp, error) { 31 | //获取用户的值 32 | 33 | images := make([]string, 0) 34 | for _, val := range req.Images { 35 | images = append(images, shared.CDN_SSO_Qiuniu+val) 36 | } 37 | 38 | jsonStu, err := json.Marshal(images) 39 | if err != nil { 40 | return nil, err 41 | } 42 | auid, _ := json.Number(fmt.Sprintf("%v", l.ctx.Value("auid"))).Int64() 43 | uid, _ := json.Number(fmt.Sprintf("%v", l.ctx.Value("uid"))).Int64() 44 | ptyid, _ := json.Number(fmt.Sprintf("%v", l.ctx.Value("ptyid"))).Int64() 45 | beid, _ := json.Number(fmt.Sprintf("%v", l.ctx.Value("beid"))).Int64() 46 | if auid == 0 && uid == 0 { 47 | return nil, shared.ErrorUserNotFound 48 | } 49 | logx.Info("ptyid=", ptyid, "beid=", beid) 50 | 51 | relp, err := l.svcCtx.VotesRpc.Enroll(l.ctx, &votesclient.EnrollReq{ 52 | Auid: auid, 53 | Uid: uid, 54 | Ptyid: ptyid, 55 | Beid: beid, 56 | Name: req.Name, 57 | Address: req.Address, 58 | Descr: req.Descr, 59 | Images: string(jsonStu), 60 | Actid: req.Actid.Actid, 61 | }) 62 | if err != nil { 63 | return nil, err 64 | } 65 | return &types.EnrollResp{ 66 | Aeid: relp.Aeid, 67 | }, nil 68 | } 69 | -------------------------------------------------------------------------------- /datacenter.api: -------------------------------------------------------------------------------- 1 | info( 2 | title: "中台系统"// TODO: add title 3 | desc: "中台系统"// TODO: add description 4 | author: "jackluo" 5 | email: "net.webjoy@gmail.com" 6 | ) 7 | 8 | import "user.api" 9 | import "votes.api" 10 | import "search.api" 11 | import "questions.api" 12 | 13 | //获取 应用信息 14 | type Beid { 15 | Beid int64 `json:"beid"` 16 | } 17 | type Token { 18 | Token string `json:"token"` 19 | } 20 | type WxTicket { 21 | Ticket string `json:"ticket"` 22 | } 23 | type Application { 24 | Sname string `json:"Sname"` //名称 25 | Logo string `json:"logo"` // login 26 | Isclose int64 `json:"isclose"` //是否关闭 27 | Fullwebsite string `json:"fullwebsite"` // 全站名称 28 | } 29 | type SnsReq { 30 | Beid 31 | Ptyid int64 `json:"ptyid"` //对应平台 32 | BackUrl string `json:"back_url"` //登陆返回的地址 33 | } 34 | type SnsResp { 35 | Beid 36 | Ptyid int64 `json:"ptyid"` //对应平台 37 | Appid string `json:"appid"` //sns 平台的id 38 | Title string `json:"title"` //名称 39 | LoginUrl string `json:"login_url"` //微信登陆的地址 40 | } 41 | 42 | type WxShareResp { 43 | Appid string `json:"appid"` 44 | Timestamp int64 `json:"timestamp"` 45 | Noncestr string `json:"noncestr"` 46 | Signature string `json:"signature"` 47 | } 48 | 49 | @server( 50 | group: common 51 | ) 52 | service datacenter-api { 53 | @doc( 54 | summary: "获取站点的信息" 55 | ) 56 | @handler appInfo 57 | get /common/appinfo (Beid) returns (Application) 58 | @doc( 59 | summary: "获取站点的社交属性信息" 60 | ) 61 | @handler snsInfo 62 | post /common/snsinfo (SnsReq) returns (SnsResp) 63 | 64 | //获取分享的 65 | @handler wxTicket 66 | post /common/wx/ticket (SnsReq) returns (WxShareResp) 67 | 68 | } 69 | 70 | //上传需要登陆 71 | @server( 72 | jwt: Auth 73 | group: common 74 | ) 75 | service datacenter-api { 76 | @doc( 77 | summary: "七牛上传凭证" 78 | ) 79 | @handler qiuniuToken 80 | post /common/qiuniu/token (Beid) returns (Token) 81 | } -------------------------------------------------------------------------------- /user/api/internal/logic/loginlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | "time" 6 | 7 | "datacenter/user/api/internal/svc" 8 | "datacenter/user/api/internal/types" 9 | "datacenter/user/model" 10 | 11 | "github.com/dgrijalva/jwt-go" 12 | "github.com/zeromicro/go-zero/core/logx" 13 | ) 14 | 15 | type LoginLogic struct { 16 | logx.Logger 17 | ctx context.Context 18 | svcCtx *svc.ServiceContext 19 | } 20 | 21 | func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) LoginLogic { 22 | return LoginLogic{ 23 | Logger: logx.WithContext(ctx), 24 | ctx: ctx, 25 | svcCtx: svcCtx, 26 | } 27 | } 28 | 29 | func (l *LoginLogic) Login(req types.LoginReq) (*types.UserReply, error) { 30 | // 忽略逻辑校验 31 | userInfo, err := l.svcCtx.UserModel.FindOneByMobile(req.Mobile) 32 | switch err { 33 | case nil: 34 | if userInfo.Password != req.Password { 35 | return nil, errorIncorrectPassword 36 | } 37 | now := time.Now().Unix() 38 | accessExpire := l.svcCtx.Config.Auth.AccessExpire 39 | jwtToken, err := l.getJwtToken(l.svcCtx.Config.Auth.AccessSecret, now, accessExpire, userInfo.Id) 40 | if err != nil { 41 | return nil, err 42 | } 43 | return &types.UserReply{ 44 | Uid: userInfo.Id, 45 | Username: userInfo.Username, 46 | Mobile: userInfo.Mobile, 47 | JwtToken: types.JwtToken{ 48 | AccessToken: jwtToken, 49 | AccessExpire: now + accessExpire, 50 | RefreshAfter: now + accessExpire/2, 51 | }, 52 | }, nil 53 | case model.ErrNotFound: 54 | return nil, errorUsernameUnRegister 55 | default: 56 | return nil, err 57 | } 58 | } 59 | 60 | func (l *LoginLogic) getJwtToken(secretKey string, iat, seconds int64, userId int64) (string, error) { 61 | claims := make(jwt.MapClaims) 62 | claims["exp"] = iat + seconds 63 | claims["iat"] = iat 64 | claims["userId"] = userId 65 | token := jwt.New(jwt.SigningMethodHS256) 66 | token.Claims = claims 67 | return token.SignedString([]byte(secretKey)) 68 | } 69 | -------------------------------------------------------------------------------- /user/rpc/user.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package user; 4 | 5 | message Request { 6 | string ping = 1; 7 | } 8 | 9 | //注册请求 10 | message RegisterReq { 11 | string mobile=1; 12 | string password=2; 13 | string smscode=3; 14 | } 15 | 16 | //登陆请求 17 | message LoginReq { 18 | string mobile=1; 19 | int64 type=2; 20 | string password=3; 21 | } 22 | 23 | //返回用户信息 24 | message UserReply { 25 | int64 auid=1; 26 | int64 uid=2; 27 | int64 beid=3; 28 | int64 ptyid=4; 29 | string username=5; 30 | string mobile=6; 31 | string nickname=7; 32 | string openid=8; 33 | string avator=9; 34 | JwtToken jwttoken=10; 35 | } 36 | 37 | //返回用户信息 38 | message UserReq { 39 | int64 auid=1; 40 | int64 uid=2; 41 | int64 beid=3; 42 | int64 ptyid=4; 43 | } 44 | 45 | 46 | message JwtToken { 47 | string accessToken=1; 48 | int64 accessExpire=2; 49 | int64 refreshAfter=3; 50 | } 51 | //请求的api 52 | message AppConfigReq { 53 | int64 beid=1; 54 | int64 ptyid=2; 55 | string code=3; 56 | } 57 | 58 | //返回用户的信息 59 | message AppUserResp { 60 | int64 auid=1; 61 | int64 beid=2; 62 | int64 ptyid=3; 63 | string nickname=4; 64 | string avator=5; 65 | string openid=6; 66 | string province=7; 67 | string city=8; 68 | string country=9; 69 | int64 sex=10; 70 | string unionid=11; 71 | } 72 | 73 | message AppUserLoginResp { 74 | int64 auid=1; 75 | int64 beid=2; 76 | int64 ptyid=3; 77 | string nickname=4; 78 | string avator=5; 79 | string openid=6; 80 | string province=7; 81 | string city=8; 82 | string country=9; 83 | string sex=10; 84 | JwtToken jwttoken=11; 85 | } 86 | 87 | 88 | //返回的值 89 | message AppConfigResp { 90 | int64 id=1; 91 | int64 beid=2; 92 | int64 ptyid=3; 93 | string appid=4; 94 | string appsecret=5; 95 | string title=6; 96 | } 97 | 98 | message Response { 99 | string pong = 1; 100 | } 101 | 102 | service User { 103 | rpc Ping(Request) returns(Response); 104 | rpc Register(RegisterReq) returns(UserReply); 105 | rpc Login(LoginReq) returns(UserReply); 106 | rpc UserInfo(UserReq) returns (UserReply); 107 | rpc SnsLogin(AppConfigReq) returns (AppUserResp); 108 | } 109 | -------------------------------------------------------------------------------- /votes/rpc/internal/logic/enrolllogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | "time" 6 | 7 | "datacenter/votes/model" 8 | "datacenter/votes/rpc/internal/svc" 9 | "datacenter/votes/rpc/votes" 10 | 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type EnrollLogic struct { 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | logx.Logger 18 | } 19 | 20 | func NewEnrollLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EnrollLogic { 21 | return &EnrollLogic{ 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | Logger: logx.WithContext(ctx), 25 | } 26 | } 27 | 28 | func (l *EnrollLogic) Enroll(in *votes.EnrollReq) (*votes.EnrollResp, error) { 29 | 30 | //检查 用户是否重复报名 31 | count, err := l.svcCtx.AppEnrollModel.GetActIdOrAcidExist(in.Actid, in.Uid, in.Auid) 32 | if err != nil { 33 | return nil, YouHaveSignedUp 34 | } 35 | if count > 0 { 36 | //return nil, YouHaveSignedUp 37 | } 38 | // 获取 投票活动的信息 39 | activit, err := l.svcCtx.AppVotesActivityModel.FindOne(in.Actid) 40 | if err != nil { 41 | return nil, ActivityDoesNotExist 42 | } 43 | // 检查 活动的状态 44 | if activit.Status != 1 { 45 | return nil, ActivityDoesNotOpen 46 | } 47 | //检查 活动是否开始报名 48 | nowtime := time.Now().Unix() 49 | if activit.StartDate > nowtime { 50 | return nil, ActivityDoesNotStart 51 | } 52 | //时间是否过了 53 | if activit.EnrollDate < nowtime { 54 | //return nil, RegistrationActivityDoesNotEnrollEND 55 | } 56 | //活动结束 57 | if activit.EndDate < nowtime { 58 | return nil, ActivityEnd 59 | } 60 | resl, err := l.svcCtx.AppEnrollModel.Insert(model.AppEnroll{ 61 | Ptyid: in.Ptyid, 62 | Beid: in.Beid, 63 | Actid: in.Actid, 64 | Uid: in.Uid, 65 | Auid: in.Auid, 66 | Name: in.Name, 67 | Status: 1, 68 | Address: in.Address, 69 | Descr: in.Descr, 70 | Images: in.Images, 71 | }) 72 | if err != nil { 73 | return nil, EnrollFalt 74 | } 75 | id, err := resl.LastInsertId() 76 | if err != nil { 77 | return nil, EnrollFalt 78 | } 79 | // 报名成功了 活动报名 +1 80 | l.svcCtx.AppVotesActivityModel.IncrEnroll(in.Actid) 81 | 82 | return &votes.EnrollResp{ 83 | Aeid: id, 84 | Ptyid: in.Ptyid, 85 | Beid: in.Beid, 86 | Actid: in.Actid, 87 | Auid: in.Auid, 88 | }, nil 89 | } 90 | -------------------------------------------------------------------------------- /user/rpc/userclient/user.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: user.proto 3 | 4 | //go:generate mockgen -destination ./user_mock.go -package userclient -source $GOFILE 5 | 6 | package userclient 7 | 8 | import ( 9 | "context" 10 | 11 | "datacenter/user/rpc/user" 12 | 13 | "github.com/zeromicro/go-zero/zrpc" 14 | ) 15 | 16 | type ( 17 | Request = user.Request 18 | LoginReq = user.LoginReq 19 | UserReply = user.UserReply 20 | UserReq = user.UserReq 21 | JwtToken = user.JwtToken 22 | AppConfigReq = user.AppConfigReq 23 | AppUserLoginResp = user.AppUserLoginResp 24 | AppConfigResp = user.AppConfigResp 25 | Response = user.Response 26 | RegisterReq = user.RegisterReq 27 | AppUserResp = user.AppUserResp 28 | 29 | User interface { 30 | Ping(ctx context.Context, in *Request) (*Response, error) 31 | Register(ctx context.Context, in *RegisterReq) (*UserReply, error) 32 | Login(ctx context.Context, in *LoginReq) (*UserReply, error) 33 | UserInfo(ctx context.Context, in *UserReq) (*UserReply, error) 34 | SnsLogin(ctx context.Context, in *AppConfigReq) (*AppUserResp, error) 35 | } 36 | 37 | defaultUser struct { 38 | cli zrpc.Client 39 | } 40 | ) 41 | 42 | func NewUser(cli zrpc.Client) User { 43 | return &defaultUser{ 44 | cli: cli, 45 | } 46 | } 47 | 48 | func (m *defaultUser) Ping(ctx context.Context, in *Request) (*Response, error) { 49 | client := user.NewUserClient(m.cli.Conn()) 50 | return client.Ping(ctx, in) 51 | } 52 | 53 | func (m *defaultUser) Register(ctx context.Context, in *RegisterReq) (*UserReply, error) { 54 | client := user.NewUserClient(m.cli.Conn()) 55 | return client.Register(ctx, in) 56 | } 57 | 58 | func (m *defaultUser) Login(ctx context.Context, in *LoginReq) (*UserReply, error) { 59 | client := user.NewUserClient(m.cli.Conn()) 60 | return client.Login(ctx, in) 61 | } 62 | 63 | func (m *defaultUser) UserInfo(ctx context.Context, in *UserReq) (*UserReply, error) { 64 | client := user.NewUserClient(m.cli.Conn()) 65 | return client.UserInfo(ctx, in) 66 | } 67 | 68 | func (m *defaultUser) SnsLogin(ctx context.Context, in *AppConfigReq) (*AppUserResp, error) { 69 | client := user.NewUserClient(m.cli.Conn()) 70 | return client.SnsLogin(ctx, in) 71 | } 72 | -------------------------------------------------------------------------------- /votes/rpc/votes.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package votes; 4 | 5 | message ActidReq{ 6 | int64 actid=1; 7 | LimitReq limit=2; 8 | string keyword=3; 9 | } 10 | message ActInfoReq{ 11 | int64 actid=1; 12 | } 13 | message LimitReq{ 14 | int64 offset=1; 15 | int64 size=2; 16 | } 17 | 18 | message EnrollInfoReq{ 19 | int64 aeid=1; 20 | int64 actid=2; 21 | } 22 | //报名 请求 23 | message EnrollReq { 24 | int64 auid=1; 25 | int64 beid=2; 26 | int64 ptyid=3; 27 | int64 uid=4; 28 | string name=5; 29 | string address=6; 30 | string images=7; 31 | string descr=8; 32 | int64 actid=9; 33 | } 34 | //报名返回 35 | message EnrollResp { 36 | int64 auid=1; 37 | int64 beid=2; 38 | int64 ptyid=3; 39 | int64 uid=4; 40 | string name=5; 41 | string address=6; 42 | repeated string images=7; 43 | string descr=8; 44 | int64 votecount=9; 45 | int64 viewcount=10; 46 | int64 aeid=11; 47 | int64 actid=12; 48 | } 49 | //返回列表 50 | message EnrollListResp{ 51 | repeated EnrollResp data=1; 52 | } 53 | 54 | //投票 55 | message VotesReq { 56 | int64 auid=1; 57 | int64 beid=2; 58 | int64 ptyid=3; 59 | int64 uid=4; 60 | int64 actid=5; 61 | int64 aeid=6; 62 | string ip=7; 63 | } 64 | //返回票数 65 | message VotesResp { 66 | int64 auid=1; 67 | int64 beid=2; 68 | int64 ptyid=3; 69 | int64 uid=4; 70 | int64 avid=5; 71 | int64 aeid=6; 72 | int64 votecount=7; 73 | int64 viewcount=8; 74 | } 75 | //返回活动信息 76 | message ActInfoResp{ 77 | int64 actid=1; 78 | int64 beid=2; 79 | int64 ptyid=3; 80 | string title=4; 81 | string descr=5; 82 | int64 status=6; 83 | int64 start_date=7; 84 | int64 enroll_date=8; 85 | int64 end_date=9; 86 | int64 votecount=10; 87 | int64 viewcount=11; 88 | int64 type=12; 89 | int64 num=13; 90 | int64 enrollcount=14; 91 | } 92 | 93 | 94 | service Votes { 95 | //获取作品信息 96 | rpc GetEnrollInfo(EnrollInfoReq) returns(EnrollResp); 97 | //获取 作品列表 98 | rpc GetEnrollList(ActidReq) returns(EnrollListResp); 99 | //获取 活动信息 100 | rpc GetActivityInfo(ActInfoReq) returns(ActInfoResp); 101 | //报名 102 | rpc Enroll(EnrollReq) returns(EnrollResp); 103 | //投票 104 | rpc Votes(VotesReq) returns(VotesResp); 105 | //增加活动的爆光量 106 | rpc IncrActiviView(ActInfoReq) returns(ActInfoResp); 107 | } 108 | -------------------------------------------------------------------------------- /search/rpc/internal/logic/articleinitlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | "datacenter/search/rpc/internal/svc" 6 | "datacenter/search/rpc/search" 7 | "datacenter/search/rpc/searchclient" 8 | "datacenter/shared" 9 | "encoding/json" 10 | "net/url" 11 | "sync" 12 | 13 | "github.com/zeromicro/go-zero/core/logx" 14 | ) 15 | 16 | //数据初始化模板事例 17 | //一些数据结构 18 | type PageData struct { 19 | ResultMsg string 20 | Data Data 21 | } 22 | type Data struct { 23 | Header []searchclient.ArticleReq 24 | Normal []searchclient.ArticleReq 25 | TimeStamp int64 26 | Page int64 27 | ViewTimes int64 28 | } 29 | 30 | type ArticleInitLogic struct { 31 | ctx context.Context 32 | lock *sync.Mutex 33 | svcCtx *svc.ServiceContext 34 | logx.Logger 35 | } 36 | 37 | func NewArticleInitLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ArticleInitLogic { 38 | return &ArticleInitLogic{ 39 | ctx: ctx, 40 | svcCtx: svcCtx, 41 | lock: new(sync.Mutex), 42 | Logger: logx.WithContext(ctx), 43 | } 44 | } 45 | 46 | //这个是个递归 47 | func (l *ArticleInitLogic) ArticleRecursion(gourl string, form url.Values, once bool) error { 48 | body, err := shared.HttpPostForm(gourl, form) 49 | if err != nil { 50 | return err 51 | } 52 | var data PageData 53 | err = json.Unmarshal(body, &data) 54 | if err != nil { 55 | return err 56 | } 57 | if len(data.Data.Header) > 0 { 58 | for _, art := range data.Data.Header { 59 | l.svcCtx.ArticeEs.Index(&art) 60 | } 61 | } 62 | if len(data.Data.Normal) > 0 { 63 | for _, art := range data.Data.Normal { 64 | id, err := l.svcCtx.ArticeEs.Index(&art) 65 | if err != nil { 66 | logx.Info("err==", err) 67 | } 68 | logx.Info("id=", id) 69 | } 70 | if !once { 71 | err = l.ArticleRecursion(gourl, url.Values{"TimeStamp": []string{shared.Int64ToStr(data.Data.TimeStamp)}, "Page": []string{shared.Int64ToStr(data.Data.Page)}, "ViewTimes": []string{shared.Int64ToStr(data.Data.ViewTimes)}}, once) 72 | } 73 | } 74 | return err 75 | } 76 | 77 | /** 78 | * 初始化抓取数据 79 | */ 80 | func (l *ArticleInitLogic) ArticleInit(in *search.Request) (*search.Response, error) { 81 | //开始锁定 82 | l.lock.Lock() 83 | //err := l.ArticleRecursion("http://xxxxxxxxxxx", url.Values{}, in.Once) 84 | l.lock.Unlock() 85 | // if err != nil { 86 | // return nil, err 87 | // } 88 | return &search.Response{}, nil 89 | } 90 | -------------------------------------------------------------------------------- /shared/tools.go: -------------------------------------------------------------------------------- 1 | package shared 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "math/rand" 7 | "net" 8 | "net/http" 9 | "net/url" 10 | "strconv" 11 | "time" 12 | 13 | "github.com/zeromicro/go-zero/rest/httpx" 14 | ) 15 | 16 | type ( 17 | Returns struct { 18 | Code int `json:"code"` 19 | Msg string `json:"msg"` 20 | Data interface{} `json:"data"` 21 | } 22 | ) 23 | 24 | func OkJson(w http.ResponseWriter, v interface{}) { 25 | 26 | httpx.OkJson(w, Returns{ 27 | Code: 0, 28 | Data: v, 29 | }) 30 | } 31 | 32 | func GetDate() string { 33 | return time.Now().Format("2006-01-02") 34 | } 35 | 36 | /** 37 | * 获取 微信登陆的url 38 | */ 39 | 40 | func GetWxLoginUrl(appid, fillwebsite string) string { 41 | return fmt.Sprintf(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo`, appid, fillwebsite) 42 | } 43 | 44 | //字符串转int 45 | 46 | func StrToInt64(str string) (id int64) { 47 | id, _ = strconv.ParseInt(str, 10, 64) 48 | return 49 | } 50 | 51 | func GetLocalIP() string { 52 | addrs, err := net.InterfaceAddrs() 53 | if err != nil { 54 | return "" 55 | } 56 | for _, address := range addrs { 57 | // 检查ip地址判断是否回环地址 58 | if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { 59 | if ipnet.IP.To4() != nil { 60 | return ipnet.IP.String() 61 | } 62 | } 63 | } 64 | return "" 65 | } 66 | 67 | func Int64ToStr(id int64) (str string) { 68 | str = fmt.Sprintf("%d", id) 69 | return 70 | } 71 | 72 | /** 73 | * 字符 转换 74 | */ 75 | func ToLimitOffset(pageIn, pagesizeIn string) (offset int64, limit int64) { 76 | page := StrToInt64(pageIn) 77 | limit = StrToInt64(pagesizeIn) 78 | if page <= 0 { 79 | page = 1 80 | } 81 | offset = limit * (page - 1) 82 | if limit == 0 { 83 | limit = 10 84 | } 85 | return 86 | } 87 | 88 | func HttpPostForm(gourl string, form url.Values) ([]byte, error) { 89 | resp, err := http.PostForm(gourl, form) 90 | if err != nil { 91 | return []byte{}, err 92 | } 93 | defer resp.Body.Close() 94 | return ioutil.ReadAll(resp.Body) 95 | } 96 | 97 | // 得到一个随机数 98 | func RandInt(max int) int { 99 | r := rand.New(rand.NewSource(time.Now().UnixNano())) 100 | 101 | if max < 1 { 102 | return r.Int() 103 | } else { 104 | return r.Intn(max) 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /questions/rpc/internal/server/questionsserver.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: questions.proto 3 | 4 | package server 5 | 6 | import ( 7 | "context" 8 | 9 | "datacenter/questions/rpc/internal/logic" 10 | "datacenter/questions/rpc/internal/svc" 11 | "datacenter/questions/rpc/questions" 12 | ) 13 | 14 | type QuestionsServer struct { 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewQuestionsServer(svcCtx *svc.ServiceContext) *QuestionsServer { 19 | return &QuestionsServer{ 20 | svcCtx: svcCtx, 21 | } 22 | } 23 | 24 | // 获取 问答抽奖活动信息 25 | func (s *QuestionsServer) GetActivitiesInfo(ctx context.Context, in *questions.ActivitiesReq) (*questions.ActInfoResp, error) { 26 | l := logic.NewGetActivitiesInfoLogic(ctx, s.svcCtx) 27 | return l.GetActivitiesInfo(in) 28 | } 29 | 30 | // 获取 问答奖品信息 31 | func (s *QuestionsServer) GetAwardInfo(ctx context.Context, in *questions.ActivitiesReq) (*questions.AwardInfoResp, error) { 32 | l := logic.NewGetAwardInfoLogic(ctx, s.svcCtx) 33 | return l.GetAwardInfo(in) 34 | } 35 | 36 | // 获取 问答奖品列表 37 | func (s *QuestionsServer) GetAwardList(ctx context.Context, in *questions.ActivitiesReq) (*questions.AwardListResp, error) { 38 | l := logic.NewGetAwardListLogic(ctx, s.svcCtx) 39 | return l.GetAwardList(in) 40 | } 41 | 42 | // 获取 问题列表 43 | func (s *QuestionsServer) GetQuestionsList(ctx context.Context, in *questions.ActivitiesReq) (*questions.QuestionsListResp, error) { 44 | l := logic.NewGetQuestionsListLogic(ctx, s.svcCtx) 45 | return l.GetQuestionsList(in) 46 | } 47 | 48 | // 提交 答案 49 | func (s *QuestionsServer) PostQuestionsChange(ctx context.Context, in *questions.QuestionsAnswerReq) (*questions.QuestionsAnswerResp, error) { 50 | l := logic.NewPostQuestionsChangeLogic(ctx, s.svcCtx) 51 | return l.PostQuestionsChange(in) 52 | } 53 | 54 | // 获取得分 55 | func (s *QuestionsServer) GetQuestionsGrade(ctx context.Context, in *questions.GradeReq) (*questions.QuestionsAnswerResp, error) { 56 | l := logic.NewGetQuestionsGradeLogic(ctx, s.svcCtx) 57 | return l.GetQuestionsGrade(in) 58 | } 59 | 60 | // 抽奖 61 | func (s *QuestionsServer) PostTurnTable(ctx context.Context, in *questions.TurnTableReq) (*questions.AwardInfoResp, error) { 62 | l := logic.NewPostTurnTableLogic(ctx, s.svcCtx) 63 | return l.PostTurnTable(in) 64 | } 65 | 66 | // 填写中奖记录 67 | func (s *QuestionsServer) PostConvert(ctx context.Context, in *questions.ConvertReq) (*questions.ConvertResp, error) { 68 | l := logic.NewPostConvertLogic(ctx, s.svcCtx) 69 | return l.PostConvert(in) 70 | } 71 | -------------------------------------------------------------------------------- /votes.api: -------------------------------------------------------------------------------- 1 | 2 | 3 | // 投票活动api 4 | 5 | 6 | type Actid struct { 7 | Actid int64 `json:"actid"` //活动id 8 | } 9 | 10 | type VoteReq struct { 11 | Aeid int64 `json:"aeid"` // 作品id 12 | Actid 13 | } 14 | type VoteResp struct { 15 | VoteReq 16 | Votecount int64 `json:"votecount"` //投票票数 17 | Viewcount int64 `json:"viewcount"` //浏览数 18 | } 19 | 20 | 21 | // 活动返回的参数 22 | 23 | type ActivityResp struct { 24 | Actid int64 `json:"actid"` 25 | Title string `json:"title"` //活动名称 26 | Descr string `json:"descr"` //活动描述 27 | StartDate int64 `json:"start_date"` //活动时间 28 | EnrollDate int64 `json:"enroll_date"` //投票时间 29 | EndDate int64 `json:"end_date"` //活动结束时间 30 | Votecount int64 `json:"votecount"` //当前活动的总票数 31 | Viewcount int64 `json:"viewcount"` //当前活动的总浏览数 32 | Type int64 `json:"type"` //投票方式 33 | Num int64 `json:"num"` //投票几票 34 | } 35 | //报名 36 | 37 | 38 | type EnrollReq struct { 39 | Actid 40 | Name string `json:"name"` // 名称 41 | Address string `json:"address"` //地址 42 | Images []string `json:"images"` //作品图片 43 | Descr string `json:"descr"` // 作品描述 44 | } 45 | // 作品返回 46 | 47 | type EnrollResp struct { 48 | Actid 49 | Aeid int64 `json:"aeid"` // 作品id 50 | Name string `json:"name"` // 名称 51 | Address string `json:"address"` //地址 52 | Images []string `json:"images"` //作品图片 53 | Descr string `json:"descr"` // 作品描述 54 | Votecount int64 `json:"votecount"` //当前活动的总票数 55 | Viewcount int64 `json:"viewcount"` //当前活动的总浏览数 56 | 57 | } 58 | 59 | 60 | @server( 61 | group: votes 62 | ) 63 | service datacenter-api { 64 | @doc( 65 | summary: "获取活动的信息" 66 | ) 67 | @handler activityInfo 68 | get /votes/activity/info (Actid) returns (ActivityResp) 69 | @doc( 70 | summary: "活动访问+1" 71 | ) 72 | @handler activityIcrView 73 | get /votes/activity/view (Actid) returns (ActivityResp) 74 | @doc( 75 | summary: "获取报名的投票作品信息" 76 | ) 77 | @handler enrollInfo 78 | get /votes/enroll/info (VoteReq) returns (EnrollResp) 79 | @doc( 80 | summary: "获取报名的投票作品列表" 81 | ) 82 | @handler enrollLists 83 | get /votes/enroll/lists (Actid) returns(EnrollResp) 84 | } 85 | 86 | @server( 87 | jwt: Auth 88 | group: votes 89 | middleware: Usercheck 90 | ) 91 | service datacenter-api { 92 | @doc( 93 | summary: "投票" 94 | ) 95 | @handler vote 96 | post /votes/vote (VoteReq) returns (VoteResp) 97 | @handler enroll 98 | post /votes/enroll (EnrollReq) returns (EnrollResp) 99 | } -------------------------------------------------------------------------------- /internal/logic/common/wxticketlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | "time" 6 | 7 | "datacenter/common/rpc/common" 8 | "datacenter/internal/svc" 9 | "datacenter/internal/types" 10 | "datacenter/shared" 11 | 12 | "github.com/chanxuehong/wechat/mp/core" 13 | "github.com/chanxuehong/wechat/mp/jssdk" 14 | "github.com/zeromicro/go-zero/core/logx" 15 | ) 16 | 17 | type WxTicketLogic struct { 18 | logx.Logger 19 | ctx context.Context 20 | svcCtx *svc.ServiceContext 21 | } 22 | 23 | func NewWxTicketLogic(ctx context.Context, svcCtx *svc.ServiceContext) WxTicketLogic { 24 | return WxTicketLogic{ 25 | Logger: logx.WithContext(ctx), 26 | ctx: ctx, 27 | svcCtx: svcCtx, 28 | } 29 | } 30 | 31 | var ( 32 | accessTokenServer core.AccessTokenServer 33 | wechatClient *core.Client 34 | cardTicketServer *jssdk.DefaultTicketServer 35 | ) 36 | 37 | func (l *WxTicketLogic) WxTicket(req types.SnsReq) (*types.WxShareResp, error) { 38 | // todo: add your logic here and delete this line 39 | //= core.NewDefaultAccessTokenServer(wxAppId, wxAppSecret, nil) 40 | appconf, err := l.svcCtx.CommonRpc.GetAppConfig(l.ctx, &common.AppConfigReq{ 41 | Beid: req.Beid.Beid, 42 | Ptyid: req.Ptyid, 43 | }) 44 | if err != nil { 45 | return nil, err 46 | } 47 | var ticketoken = "" 48 | //如果有 49 | l.svcCtx.Cache.Get("ticket_token", &ticketoken) 50 | if ticketoken == "" { 51 | //拿到微信操作的 52 | accessTokenServer = core.NewDefaultAccessTokenServer(appconf.Appid, appconf.Appsecret, nil) 53 | wechatClient = core.NewClient(accessTokenServer, nil) 54 | 55 | cardTicketServer = jssdk.NewDefaultTicketServer(wechatClient) 56 | 57 | ticketoken, err = cardTicketServer.Ticket() 58 | if err != nil { 59 | return nil, err 60 | } 61 | l.svcCtx.Cache.SetWithExpire("ticket_token", ticketoken, time.Duration(3600)*time.Second) 62 | } 63 | 64 | // jsapiTicket := "sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg" 65 | // nonceStr := "Wm3WZYTPz0wzccnW" 66 | // timestamp := "1414587457" 67 | // url := "http://mp.weixin.qq.com?params=value#xxxx" 68 | 69 | // wantSignature := "0f9de62fce790f9a083d5c99e95740ceb90c27ed" 70 | 71 | // haveSignature := WXConfigSign(jsapiTicket, nonceStr, timestamp, url) 72 | 73 | wxticket := &types.WxShareResp{ 74 | Appid: appconf.Appid, 75 | Noncestr: shared.Noncestr, 76 | Timestamp: time.Now().Unix(), 77 | } 78 | wxticket.Signature = jssdk.WXConfigSign(ticketoken, wxticket.Noncestr, shared.Int64ToStr(wxticket.Timestamp), req.BackUrl) 79 | 80 | return wxticket, nil 81 | } 82 | -------------------------------------------------------------------------------- /user/rpc/internal/logic/snsloginlogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/common/rpc/commonclient" 7 | "datacenter/user/model" 8 | "datacenter/user/rpc/internal/svc" 9 | "datacenter/user/rpc/user" 10 | 11 | mpoauth2 "github.com/chanxuehong/wechat/mp/oauth2" 12 | "github.com/chanxuehong/wechat/oauth2" 13 | "github.com/zeromicro/go-zero/core/logx" 14 | ) 15 | 16 | type SnsLoginLogic struct { 17 | ctx context.Context 18 | svcCtx *svc.ServiceContext 19 | logx.Logger 20 | } 21 | 22 | func NewSnsLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SnsLoginLogic { 23 | return &SnsLoginLogic{ 24 | ctx: ctx, 25 | svcCtx: svcCtx, 26 | Logger: logx.WithContext(ctx), 27 | } 28 | } 29 | 30 | func (l *SnsLoginLogic) SnsLogin(in *user.AppConfigReq) (apr *user.AppUserResp, err error) { 31 | 32 | //先取配置文件信息 33 | reply, err := l.svcCtx.CommonRpc.GetAppConfig(l.ctx, &commonclient.AppConfigReq{Beid: in.Beid, Ptyid: in.Ptyid}) 34 | if err != nil { 35 | return 36 | } 37 | //调用微信第三方登陆 38 | //mpoauth2.GetSessionWithClient() 39 | oauth2Endpoint := mpoauth2.NewEndpoint(reply.Appid, reply.Appsecret) 40 | oauth2Client := oauth2.Client{ 41 | Endpoint: oauth2Endpoint, 42 | } 43 | 44 | token, err := oauth2Client.ExchangeToken(in.Code) 45 | if err != nil { 46 | return 47 | } 48 | 49 | // 检查 用户是否已存在 50 | appuser, err := l.svcCtx.AppUserModel.FindOneByOpenid(in.Beid, in.Ptyid, token.OpenId) 51 | 52 | if err != nil && err != model.ErrNotFound { 53 | // logx.Info("appuser==xxxxx=====xxxxxx", appuser, err) 54 | return 55 | } 56 | if err == model.ErrNotFound { 57 | userinfo, errox := mpoauth2.GetUserInfo(token.AccessToken, token.OpenId, "", nil) 58 | if errox != nil { 59 | // 处理一般错误信息 60 | return 61 | } 62 | appuser = &model.AppUser{ 63 | Ptyid: in.Ptyid, 64 | Beid: in.Beid, 65 | Openid: userinfo.OpenId, 66 | Nickname: userinfo.Nickname, 67 | UnionId: userinfo.UnionId, 68 | Avator: userinfo.HeadImageURL, 69 | Province: userinfo.Province, 70 | Sex: int64(userinfo.Sex), 71 | City: userinfo.City, 72 | Country: userinfo.City, 73 | } 74 | res, errox := l.svcCtx.AppUserModel.Insert(*appuser) 75 | if errox != nil { 76 | return nil, errox 77 | } 78 | appuser.Auid, errox = res.LastInsertId() 79 | if errox != nil { 80 | return nil, errox 81 | } 82 | err = nil 83 | } 84 | return &user.AppUserResp{ 85 | Ptyid: appuser.Ptyid, 86 | Beid: appuser.Beid, 87 | Nickname: appuser.Nickname, 88 | Openid: appuser.Openid, 89 | Auid: appuser.Auid, 90 | Avator: appuser.Avator, 91 | Province: appuser.Province, 92 | Sex: appuser.Sex, 93 | City: appuser.City, 94 | Country: appuser.City, 95 | }, nil 96 | } 97 | -------------------------------------------------------------------------------- /votes/rpc/votesclient/votes.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: votes.proto 3 | 4 | //go:generate mockgen -destination ./votes_mock.go -package votesclient -source $GOFILE 5 | 6 | package votesclient 7 | 8 | import ( 9 | "context" 10 | 11 | "datacenter/votes/rpc/votes" 12 | 13 | "github.com/zeromicro/go-zero/zrpc" 14 | ) 15 | 16 | type ( 17 | ActInfoReq = votes.ActInfoReq 18 | EnrollInfoReq = votes.EnrollInfoReq 19 | EnrollReq = votes.EnrollReq 20 | EnrollResp = votes.EnrollResp 21 | VotesReq = votes.VotesReq 22 | ActInfoResp = votes.ActInfoResp 23 | ActidReq = votes.ActidReq 24 | EnrollListResp = votes.EnrollListResp 25 | VotesResp = votes.VotesResp 26 | LimitReq = votes.LimitReq 27 | 28 | Votes interface { 29 | // 获取作品信息 30 | GetEnrollInfo(ctx context.Context, in *EnrollInfoReq) (*EnrollResp, error) 31 | // 获取 作品列表 32 | GetEnrollList(ctx context.Context, in *ActidReq) (*EnrollListResp, error) 33 | // 获取 活动信息 34 | GetActivityInfo(ctx context.Context, in *ActInfoReq) (*ActInfoResp, error) 35 | // 报名 36 | Enroll(ctx context.Context, in *EnrollReq) (*EnrollResp, error) 37 | // 投票 38 | Votes(ctx context.Context, in *VotesReq) (*VotesResp, error) 39 | // 增加活动的爆光量 40 | IncrActiviView(ctx context.Context, in *ActInfoReq) (*ActInfoResp, error) 41 | } 42 | 43 | defaultVotes struct { 44 | cli zrpc.Client 45 | } 46 | ) 47 | 48 | func NewVotes(cli zrpc.Client) Votes { 49 | return &defaultVotes{ 50 | cli: cli, 51 | } 52 | } 53 | 54 | // 获取作品信息 55 | func (m *defaultVotes) GetEnrollInfo(ctx context.Context, in *EnrollInfoReq) (*EnrollResp, error) { 56 | client := votes.NewVotesClient(m.cli.Conn()) 57 | return client.GetEnrollInfo(ctx, in) 58 | } 59 | 60 | // 获取 作品列表 61 | func (m *defaultVotes) GetEnrollList(ctx context.Context, in *ActidReq) (*EnrollListResp, error) { 62 | client := votes.NewVotesClient(m.cli.Conn()) 63 | return client.GetEnrollList(ctx, in) 64 | } 65 | 66 | // 获取 活动信息 67 | func (m *defaultVotes) GetActivityInfo(ctx context.Context, in *ActInfoReq) (*ActInfoResp, error) { 68 | client := votes.NewVotesClient(m.cli.Conn()) 69 | return client.GetActivityInfo(ctx, in) 70 | } 71 | 72 | // 报名 73 | func (m *defaultVotes) Enroll(ctx context.Context, in *EnrollReq) (*EnrollResp, error) { 74 | client := votes.NewVotesClient(m.cli.Conn()) 75 | return client.Enroll(ctx, in) 76 | } 77 | 78 | // 投票 79 | func (m *defaultVotes) Votes(ctx context.Context, in *VotesReq) (*VotesResp, error) { 80 | client := votes.NewVotesClient(m.cli.Conn()) 81 | return client.Votes(ctx, in) 82 | } 83 | 84 | // 增加活动的爆光量 85 | func (m *defaultVotes) IncrActiviView(ctx context.Context, in *ActInfoReq) (*ActInfoResp, error) { 86 | client := votes.NewVotesClient(m.cli.Conn()) 87 | return client.IncrActiviView(ctx, in) 88 | } 89 | -------------------------------------------------------------------------------- /user.api: -------------------------------------------------------------------------------- 1 | 2 | //注册请求 3 | type RegisterReq struct { 4 | // TODO: add members here and delete this comment 5 | Mobile string `json:"mobile"` //基本一个手机号码就完事 6 | Password string `json:"password"` 7 | Smscode string `json:"smscode"` //短信码 8 | } 9 | //登陆请求 10 | type LoginReq struct{ 11 | Mobile string `json:"mobile"` 12 | Type int64 `json:"type"` //1.密码登陆,2.短信登陆 13 | Password string `json:"password"` 14 | } 15 | //微信登陆 16 | type WxLoginReq struct { 17 | Beid int64 `json:"beid"` //应用id 18 | Code string `json:"code"` //微信登陆密钥 19 | Ptyid int64 `json:"ptyid"` //对应平台 20 | } 21 | 22 | //返回用户信息 23 | type UserReply struct { 24 | Auid int64 `json:"auid"` 25 | Uid int64 `json:"uid"` 26 | Beid int64 `json:"beid"` //应用id 27 | Ptyid int64 `json:"ptyid"` //对应平台 28 | Username string `json:"username"` 29 | Mobile string `json:"mobile"` 30 | Nickname string `json:"nickname"` 31 | Openid string `json:"openid"` 32 | Avator string `json:"avator"` 33 | JwtToken 34 | } 35 | //返回APPUser 36 | type AppUser struct{ 37 | Uid int64 `json:"uid"` 38 | Auid int64 `json:"auid"` 39 | Beid int64 `json:"beid"` //应用id 40 | Ptyid int64 `json:"ptyid"` //对应平台 41 | Nickname string `json:"nickname"` 42 | Openid string `json:"openid"` 43 | Avator string `json:"avator"` 44 | } 45 | 46 | type LoginAppUser struct{ 47 | Uid int64 `json:"uid"` 48 | Auid int64 `json:"auid"` 49 | Beid int64 `json:"beid"` //应用id 50 | Ptyid int64 `json:"ptyid"` //对应平台 51 | Nickname string `json:"nickname"` 52 | Openid string `json:"openid"` 53 | Avator string `json:"avator"` 54 | JwtToken 55 | } 56 | 57 | type JwtToken struct { 58 | AccessToken string `json:"access_token,omitempty"` 59 | AccessExpire int64 `json:"access_expire,omitempty"` 60 | RefreshAfter int64 `json:"refresh_after,omitempty"` 61 | } 62 | 63 | type UserReq struct{ 64 | Auid int64 `json:"auid"` 65 | Uid int64 `json:"uid"` 66 | Beid int64 `json:"beid"` //应用id 67 | Ptyid int64 `json:"ptyid"` //对应平台 68 | } 69 | 70 | type Request { 71 | Name string `path:"name,options=you|me"` 72 | } 73 | type Response { 74 | Message string `json:"message"` 75 | } 76 | 77 | 78 | @server( 79 | group: user 80 | ) 81 | service datacenter-api { 82 | @handler ping 83 | post /user/ping () 84 | 85 | @handler register 86 | post /user/register (RegisterReq) returns (UserReply) 87 | 88 | @handler login 89 | post /user/login (LoginReq) returns (UserReply) 90 | 91 | @handler wxlogin 92 | post /user/wx/login (WxLoginReq) returns (LoginAppUser) 93 | 94 | @handler code2Session 95 | get /user/wx/login () returns (LoginAppUser) 96 | } 97 | @server( 98 | jwt: Auth 99 | group: user 100 | middleware: Usercheck 101 | ) 102 | service datacenter-api { 103 | @handler userInfo 104 | get /user/dc/info (UserReq) returns (UserReply) 105 | } -------------------------------------------------------------------------------- /questions/rpc/questions.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package questions; 4 | 5 | message Request { 6 | string ping = 1; 7 | } 8 | 9 | message Response { 10 | string pong = 1; 11 | } 12 | message ActivitiesReq { 13 | int64 actid=1; 14 | } 15 | 16 | //返回问答抽奖活动信息 17 | message ActInfoResp{ 18 | int64 id=1; 19 | int64 beid=2; 20 | int64 ptyid=3; 21 | string title=4; 22 | string descr=5; 23 | int64 status=6; 24 | int64 start_date=7; 25 | int64 end_date=8; 26 | int64 get_score=9; 27 | string header=10; 28 | string des=11; 29 | string image=12; 30 | string rule=13; 31 | } 32 | message AwardInfoResp{ 33 | int64 id=1; 34 | int64 beid=2; 35 | int64 ptyid=3; 36 | string title=4; 37 | string des=5; 38 | int64 number=6; 39 | int64 start_probability=7; 40 | int64 end_probability=8; 41 | int64 ActivityId=9; 42 | string header=10; 43 | string image=11; 44 | } 45 | message AwardListResp{ 46 | repeated AwardInfoResp data=1; 47 | } 48 | 49 | message questionsResp { 50 | int64 id=1; 51 | int64 beid=2; 52 | int64 ptyid=3; 53 | string Options=4; 54 | string Corrent=5; 55 | int64 Status=6; 56 | string Question=7; 57 | int64 ActivityId=8; 58 | } 59 | message QuestionsListResp{ 60 | repeated questionsResp data=1; 61 | } 62 | //答案提交 63 | message QuestionsAnswerReq{ 64 | int64 beid=1; 65 | int64 ptyid=2; 66 | int64 ActivityId=3; 67 | int64 Uid=4; 68 | string answers=5; 69 | string score=6; 70 | int64 Auid=7; 71 | } 72 | //问答返回 73 | message QuestionsAnswerResp{ 74 | int64 answer_id=1; 75 | string score=2; 76 | } 77 | //得分 78 | message GradeReq { 79 | int64 actid=1; 80 | int64 uid=2; 81 | int64 Auid=3; 82 | } 83 | 84 | //抽奖 85 | message TurnTableReq{ 86 | int64 beid=1; 87 | int64 ptyid=2; 88 | int64 ActivityId=3; 89 | int64 Uid=4; 90 | int64 Auid=5; 91 | int64 answer_id=6; 92 | } 93 | 94 | //兑奖 95 | message ConvertReq{ 96 | int64 beid=1; 97 | int64 ptyid=2; 98 | int64 ActivityId=3; 99 | int64 Uid=4; 100 | int64 Auid=5; 101 | int64 LotteryId=6; 102 | string username=7; 103 | string phone=8; 104 | } 105 | message ConvertResp{ 106 | int64 Id=1; 107 | int64 LotteryId=2; 108 | int64 ActivityId=3; 109 | } 110 | 111 | service Questions { 112 | //获取 问答抽奖活动信息 113 | rpc GetActivitiesInfo(ActivitiesReq) returns(ActInfoResp); 114 | //获取 问答奖品信息 115 | rpc GetAwardInfo(ActivitiesReq) returns(AwardInfoResp); 116 | //获取 问答奖品列表 117 | rpc GetAwardList(ActivitiesReq) returns(AwardListResp); 118 | //获取 问题列表 119 | rpc GetQuestionsList(ActivitiesReq) returns(QuestionsListResp); 120 | // 提交 答案 121 | rpc PostQuestionsChange(QuestionsAnswerReq) returns(QuestionsAnswerResp); 122 | //获取得分 123 | rpc GetQuestionsGrade(GradeReq) returns(QuestionsAnswerResp); 124 | //抽奖 125 | rpc PostTurnTable(TurnTableReq) returns (AwardInfoResp); 126 | 127 | //填写中奖记录 128 | rpc PostConvert(ConvertReq) returns (ConvertResp); 129 | } 130 | -------------------------------------------------------------------------------- /votes/rpc/internal/logic/voteslogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | "errors" 6 | "fmt" 7 | "time" 8 | 9 | "datacenter/shared" 10 | "datacenter/votes/model" 11 | "datacenter/votes/rpc/internal/svc" 12 | "datacenter/votes/rpc/votes" 13 | 14 | "github.com/zeromicro/go-zero/core/logx" 15 | "github.com/zeromicro/go-zero/core/stores/redis" 16 | ) 17 | 18 | type VotesLogic struct { 19 | ctx context.Context 20 | svcCtx *svc.ServiceContext 21 | logx.Logger 22 | } 23 | 24 | func NewVotesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VotesLogic { 25 | return &VotesLogic{ 26 | ctx: ctx, 27 | svcCtx: svcCtx, 28 | Logger: logx.WithContext(ctx), 29 | } 30 | } 31 | 32 | func (l *VotesLogic) Votes(in *votes.VotesReq) (*votes.VotesResp, error) { 33 | //获取 活动信息 34 | //加锁 35 | redisLock := redis.NewRedisLock(l.svcCtx.RedisConn, shared.GetVoteUidAuidLockKey(in.Uid, in.Auid)) 36 | // 2. 可选操作,设置 redislock 过期时间 37 | redisLock.SetExpire(shared.VotesUidAuidLockExpire) 38 | if ok, err := redisLock.Acquire(); !ok || err != nil { 39 | return nil, VotesLock 40 | } 41 | // 3. 释放锁 42 | defer redisLock.Release() 43 | // 获取 投票活动的信息 44 | activit, err := l.svcCtx.AppVotesActivityModel.FindOne(in.Actid) 45 | if err != nil { 46 | return nil, ActivityDoesNotExist 47 | } 48 | // 检查 活动的状态 49 | if activit.Status != 1 { 50 | return nil, ActivityDoesNotOpen 51 | } 52 | //检查 活动是否可以投票 53 | nowtime := time.Now().Unix() 54 | if activit.StartDate > nowtime { 55 | return nil, ActivityDoesNotEnroll 56 | } 57 | //活动结束 58 | if activit.EndDate < nowtime { 59 | return nil, ActivityEnd 60 | } 61 | //判断投票方式 1.按次数投票,2.按天数投票 62 | if activit.Type == 2 { 63 | count, err := l.svcCtx.AppVotesModel.FindByDaysWithVotes(in.Actid, in.Uid, in.Auid, shared.GetDate()) 64 | if err != nil { 65 | return nil, VotesFailt 66 | } 67 | if count >= activit.Num { 68 | return nil, errors.New(fmt.Sprintf("每天只能投%d票", activit.Num)) 69 | } 70 | } else { 71 | count, err := l.svcCtx.AppVotesModel.FindByNumWithVotes(in.Actid, in.Uid, in.Auid) 72 | if err != nil { 73 | return nil, VotesFailt 74 | } 75 | if count >= activit.Num { 76 | return nil, errors.New(fmt.Sprintf("每人只能投%d票", activit.Num)) 77 | } 78 | } 79 | //检查 作品的状态 80 | appenroll, err := l.svcCtx.AppEnrollModel.FindOne(in.Aeid) 81 | if err != nil { 82 | return nil, EnrollNotExist 83 | } 84 | if appenroll.Status != 1 { 85 | return nil, EnrollNotExist 86 | } 87 | //写入投票表 88 | _, err = l.svcCtx.AppVotesModel.Insert(model.AppVotes{ 89 | Actid: in.Actid, 90 | Ptyid: in.Ptyid, 91 | Aeid: in.Aeid, 92 | Uid: in.Uid, 93 | Auid: in.Auid, 94 | Beid: in.Beid, 95 | Ip: in.Ip, 96 | }) 97 | if err != nil { 98 | return nil, err 99 | } 100 | // 更新投票和浏览 101 | l.svcCtx.AppEnrollModel.IncrVotes(in.Aeid) 102 | l.svcCtx.AppEnrollModel.IncrView(in.Aeid) 103 | // 更新总 投票和总浏览 104 | l.svcCtx.AppVotesActivityModel.IncrVotes(in.Actid) 105 | l.svcCtx.AppVotesActivityModel.IncrView(in.Actid) 106 | return &votes.VotesResp{ 107 | Aeid: in.Aeid, 108 | Uid: in.Uid, 109 | Auid: in.Auid, 110 | Beid: in.Beid, 111 | }, err 112 | } 113 | -------------------------------------------------------------------------------- /search/rpc/dao/article.go: -------------------------------------------------------------------------------- 1 | package dao 2 | 3 | import ( 4 | "context" 5 | "datacenter/search/rpc/searchclient" 6 | "fmt" 7 | "reflect" 8 | 9 | "github.com/olivere/elastic/v7" 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | const ( 14 | author = "tanzi" 15 | project = "article" 16 | mappingTpl = ` 17 | { 18 | "settings":{ 19 | "number_of_shards":1, 20 | "number_of_replicas":0 21 | }, 22 | "mappings":{ 23 | "properties" : { 24 | "ImageUrl" : { 25 | "type" : "keyword" 26 | }, 27 | "NewsId" : { 28 | "type" : "keyword" 29 | }, 30 | "NewsTitle" : { 31 | "type" : "text", 32 | "analyzer" : "ik_max_word", 33 | "search_analyzer" : "ik_smart", 34 | "fields" : { 35 | "keyword" : { 36 | "type" : "keyword", 37 | "ignore_above" : 256 38 | } 39 | } 40 | } 41 | } 42 | } 43 | } 44 | }` 45 | esRetryLimit = 3 //bulk 错误重试机制 46 | ) 47 | 48 | type ArticelES struct { 49 | index string 50 | mapping string 51 | client *elastic.Client 52 | } 53 | 54 | func NewArticelES(client *elastic.Client) (*ArticelES, error) { 55 | index := fmt.Sprintf("%s_%s", author, project) 56 | articelEs := &ArticelES{ 57 | client: client, 58 | index: index, 59 | mapping: mappingTpl, 60 | } 61 | //创建表 62 | exists, err := articelEs.client.IndexExists(articelEs.index).Do(context.Background()) 63 | if err != nil { 64 | logx.Info(err) 65 | return nil, err 66 | } 67 | if !exists { 68 | _, err := articelEs.client.CreateIndex(articelEs.index).Body(articelEs.mapping).Do(context.Background()) 69 | if err != nil { 70 | logx.Info("userEs init failed err is %s\n", err) 71 | return nil, err 72 | } 73 | } 74 | return articelEs, nil 75 | } 76 | 77 | //索引 78 | func (aes *ArticelES) Index(art *searchclient.ArticleReq) (string, error) { 79 | res, err := aes.client.Index().Index(aes.index).Id(art.NewsId).BodyJson(art).Do(context.Background()) 80 | if err != nil { 81 | return "", err 82 | } 83 | return res.Id, err 84 | } 85 | 86 | //得到值 87 | 88 | func (aes *ArticelES) Search(search *searchclient.SearchReq) ([]*searchclient.ArticleReq, error) { 89 | searcharticleLists := make([]*searchclient.ArticleReq, 0) 90 | //termQuery := elastic.NewTermQuery("NewsTitle", search.Keyword) 91 | matchQuery := elastic.NewMatchQuery("NewsTitle", search.Keyword) 92 | 93 | searchResult, err := aes.client.Search(). 94 | Index(aes.index). // search in index "twitter" 95 | //Source("NewsId,NewsTitle"). 96 | //Source([]string{"NewsId", "NewsTitle"}). 97 | Query(matchQuery). // specify the query 98 | Sort("NewsId", false). 99 | From(int(search.Limit.Offset)). 100 | Size(int(search.Limit.Size)). // take documents 0-9 101 | Pretty(true). // pretty print request and response JSON 102 | Do(context.Background()) // execute 103 | 104 | if err != nil { 105 | return searcharticleLists, err 106 | } 107 | var articel searchclient.ArticleReq 108 | for _, item := range searchResult.Each(reflect.TypeOf(articel)) { 109 | t, ok := item.(searchclient.ArticleReq) 110 | if ok { 111 | searcharticleLists = append(searcharticleLists, &t) 112 | } 113 | } 114 | 115 | return searcharticleLists, nil 116 | } 117 | -------------------------------------------------------------------------------- /internal/svc/servicecontext.go: -------------------------------------------------------------------------------- 1 | package svc 2 | 3 | import ( 4 | "context" 5 | "datacenter/common/rpc/commonclient" 6 | "datacenter/internal/config" 7 | "datacenter/internal/middleware" 8 | "datacenter/questions/rpc/questionsclient" 9 | "datacenter/search/rpc/searchclient" 10 | "datacenter/shared" 11 | "datacenter/user/rpc/userclient" 12 | "datacenter/votes/rpc/votesclient" 13 | "fmt" 14 | "net/http" 15 | "time" 16 | 17 | "github.com/zeromicro/go-zero/core/logx" 18 | "github.com/zeromicro/go-zero/core/stores/cache" 19 | "github.com/zeromicro/go-zero/core/stores/redis" 20 | "github.com/zeromicro/go-zero/core/syncx" 21 | "github.com/zeromicro/go-zero/rest" 22 | "github.com/zeromicro/go-zero/zrpc" 23 | "google.golang.org/grpc" 24 | ) 25 | 26 | type ServiceContext struct { 27 | Config config.Config 28 | GreetMiddleware1 rest.Middleware 29 | GreetMiddleware2 rest.Middleware 30 | Usercheck rest.Middleware 31 | Admincheck rest.Middleware 32 | UserRpc userclient.User //用户 33 | CommonRpc commonclient.Common //公共 34 | VotesRpc votesclient.Votes //投票 35 | SearchRpc searchclient.Search //搜索 36 | QuestionsRpc questionsclient.Questions //问答抽奖 37 | Cache cache.Cache 38 | RedisConn *redis.Redis 39 | } 40 | 41 | func timeInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { 42 | stime := time.Now() 43 | err := invoker(ctx, method, req, reply, cc, opts...) 44 | if err != nil { 45 | return err 46 | } 47 | 48 | fmt.Printf("调用 %s 方法 耗时: %v\n", method, time.Now().Sub(stime)) 49 | return nil 50 | } 51 | func NewServiceContext(c config.Config) *ServiceContext { 52 | 53 | ur := userclient.NewUser(zrpc.MustNewClient(c.UserRpc, zrpc.WithUnaryClientInterceptor(timeInterceptor))) 54 | cr := commonclient.NewCommon(zrpc.MustNewClient(c.CommonRpc, zrpc.WithUnaryClientInterceptor(timeInterceptor))) 55 | vr := votesclient.NewVotes(zrpc.MustNewClient(c.VotesRpc, zrpc.WithUnaryClientInterceptor(timeInterceptor))) 56 | sr := searchclient.NewSearch(zrpc.MustNewClient(c.SearchRpc, zrpc.WithUnaryClientInterceptor(timeInterceptor))) 57 | qr := questionsclient.NewQuestions(zrpc.MustNewClient(c.QuestionsRpc, zrpc.WithUnaryClientInterceptor(timeInterceptor))) 58 | //缓存 59 | ca := cache.New(c.CacheRedis, syncx.NewSingleFlight(), cache.NewStat("dc"), shared.ErrNotFound) 60 | rcon := redis.NewRedis(c.CacheRedis[0].Host, c.CacheRedis[0].Type, c.CacheRedis[0].Pass) 61 | return &ServiceContext{ 62 | Config: c, 63 | GreetMiddleware1: greetMiddleware1, 64 | GreetMiddleware2: greetMiddleware2, 65 | Usercheck: middleware.NewUserCheckMiddleware().Handle, 66 | Admincheck: middleware.NewAdminCheckMiddleware().Handle, 67 | UserRpc: ur, 68 | CommonRpc: cr, 69 | VotesRpc: vr, 70 | SearchRpc: sr, 71 | QuestionsRpc: qr, 72 | Cache: ca, 73 | RedisConn: rcon, 74 | } 75 | } 76 | func greetMiddleware1(next http.HandlerFunc) http.HandlerFunc { 77 | return func(w http.ResponseWriter, r *http.Request) { 78 | logx.Info("greetMiddleware1 request ... ") 79 | next(w, r) 80 | logx.Info("greetMiddleware1 reponse ... ") 81 | } 82 | } 83 | 84 | func greetMiddleware2(next http.HandlerFunc) http.HandlerFunc { 85 | return func(w http.ResponseWriter, r *http.Request) { 86 | logx.Info("greetMiddleware2 request ... ") 87 | next(w, r) 88 | logx.Info("greetMiddleware2 reponse ... ") 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /questions/rpc/internal/logic/postturntablelogic.go: -------------------------------------------------------------------------------- 1 | package logic 2 | 3 | import ( 4 | "context" 5 | 6 | "datacenter/questions/model" 7 | "datacenter/questions/rpc/internal/svc" 8 | "datacenter/questions/rpc/questions" 9 | "datacenter/shared" 10 | 11 | "github.com/zeromicro/go-zero/core/logx" 12 | "github.com/zeromicro/go-zero/core/stores/redis" 13 | ) 14 | 15 | type PostTurnTableLogic struct { 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | logx.Logger 19 | } 20 | 21 | func NewPostTurnTableLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PostTurnTableLogic { 22 | return &PostTurnTableLogic{ 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | Logger: logx.WithContext(ctx), 26 | } 27 | } 28 | 29 | // 抽奖 30 | func (l *PostTurnTableLogic) PostTurnTable(in *questions.TurnTableReq) (*questions.AwardInfoResp, error) { 31 | 32 | //1. 加锁 33 | redisLock := redis.NewRedisLock(l.svcCtx.RedisConn, shared.GetUidAuidLockKey(in.Uid, in.Auid)) 34 | // 2. 可选操作,设置 redislock 过期时间 35 | redisLock.SetExpire(shared.UidAuidLockExpire) 36 | if ok, err := redisLock.Acquire(); !ok || err != nil { 37 | return nil, shared.ErrorUserOperation 38 | } 39 | // 3. 释放锁 40 | defer redisLock.Release() 41 | 42 | //1. 检查用户是否已经抽过奖了 43 | 44 | //2. 获取抽奖的随机编码 45 | prizeCode := shared.RandInt(10000) 46 | 47 | //3.匹配是否中奖了 48 | awards, err := l.prize(in.ActivityId, int64(prizeCode)) 49 | if err != nil { 50 | return nil, err 51 | } 52 | //如果是虚拟高品 53 | if awards.IsLottery == 0 { 54 | goto doDefault 55 | } 56 | err = l.svcCtx.QuestionsLotteriesModel.TurnTable(model.AppQuestionsLotteries{ 57 | ActivityId: in.ActivityId, 58 | Uid: in.Uid, 59 | Auid: in.Auid, 60 | Ptyid: in.Ptyid, 61 | Beid: in.Beid, 62 | AwardId: awards.Id, 63 | IsWinning: awards.IsLottery, 64 | }) 65 | //如果因为库存不足,各种问题报错,就随便给一个 66 | if err != nil { 67 | goto doDefault 68 | } 69 | 70 | doDefault: 71 | { 72 | //随机写一个奖品 73 | awards, err = l.defaultPrize(in.ActivityId) 74 | l.svcCtx.QuestionsLotteriesModel.Insert(model.AppQuestionsLotteries{ 75 | ActivityId: in.ActivityId, 76 | Uid: in.Uid, 77 | Auid: in.Auid, 78 | Ptyid: in.Ptyid, 79 | Beid: in.Beid, 80 | AwardId: awards.Id, 81 | IsWinning: awards.IsLottery, 82 | }) 83 | } 84 | 85 | return &questions.AwardInfoResp{ 86 | Id: awards.Id, 87 | Beid: awards.Beid, 88 | Ptyid: awards.Ptyid, 89 | ActivityId: awards.ActivityId, 90 | }, nil 91 | } 92 | 93 | //检查 奖品信息 94 | func (l *PostTurnTableLogic) prize(actid, prizeCode int64) (*model.AppQuestionsAwards, error) { 95 | var prizeGift *model.AppQuestionsAwards 96 | 97 | giftList, err := l.svcCtx.QuestionsAwardsModel.Find(actid) 98 | if err != nil { 99 | return nil, err 100 | } 101 | 102 | for _, gift := range giftList { 103 | if gift.StartProbability <= prizeCode && gift.EndProbability >= prizeCode { 104 | prizeGift = &gift 105 | break 106 | } 107 | } 108 | return prizeGift, nil 109 | } 110 | 111 | /** 112 | * 随机拿一个奖品吧 113 | */ 114 | func (l *PostTurnTableLogic) defaultPrize(actid int64) (*model.AppQuestionsAwards, error) { 115 | 116 | giftList, err := l.svcCtx.QuestionsAwardsModel.Find(actid) 117 | if err != nil { 118 | return nil, err 119 | } 120 | noLottery := make([]model.AppQuestionsAwards, 0) 121 | for _, gift := range giftList { 122 | // 中奖编码区间满足条件,说明可以中奖 123 | if gift.IsLottery == 0 { 124 | noLottery = append(noLottery, gift) 125 | } 126 | } 127 | return &noLottery[shared.RandInt(len(noLottery))], nil 128 | 129 | } 130 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 基于go-zero 框架写的一个数据中台中心 2 | 3 | ### 详细介绍 [我是如何用go-zero实现一个中台系统的](https://www.cnblogs.com/jackluo/p/14148518.html) 4 | 5 | ## 架构图 6 | ![中台系统](https://img2020.cnblogs.com/blog/203395/202012/203395-20201217094615171-335437652.jpg "中台架构") 7 | 8 | ### 已完成的功能列表 9 | - [x] 微信公众号登陆 10 | - [x] 七牛上传获取token 11 | - [x] 投票 12 | - [x] 报名 13 | - [x] 报名列表 14 | - [x] 投票 15 | - [x] 抽奖问答 16 | - [x] 活动信息 17 | - [x] 问答列表 18 | - [x] 提交答案 19 | - [x] 获取得分 20 | - [x] 抽奖 21 | - [x] 填写中奖人信息 22 | - [x] 搜索 23 | - [x] 基于elasticsearch 24 | 25 | ### 未完成的 26 | - [ ] 微信支付宝登陆 27 | - [ ] 微信支付宝支付 28 | 29 | ## 如何运行 30 | 31 | ### 先启动mysql redis etcd 服务 32 | ```shell 33 | sh server.sh 34 | ``` 35 | ### 输出如下 就显示 成功了 36 | ``` 37 | mysql 38 | mysql 39 | 8d5d4b381ab7abe8947f532422255cd172f214ab4a6b0533da1619259e1cc4a5 40 | redis 41 | redis 42 | Start Redis Service... 43 | 1fc187a9d82f0942dd60cac76c723a5bc531e1b67424384d04e7a69dad1362f0 44 | etcd 45 | etcd 46 | 98f88d81e1e218d4d53c608e5a68cd70254df221bdb34c9beab37a7473971ba0 47 | es 48 | es 49 | 04b37b58f10411fa8ab5894c917266cc7bc7f9a96988908fff9b3734e6259ad4 50 | ``` 51 | ### 需要注意的是 elasticsearch 第一次用的时候,需要初始化密码 执行下面的操作 52 | ```bash 53 | ➜ ~ docker exec -it es /bin/bash 54 | [root@04b37b58f104 elasticsearch]# sh /usr/share/elasticsearch/bin/elasticsearch-setup-passwords auto 55 | Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user. 56 | The passwords will be randomly generated and printed to the console. 57 | Please confirm that you would like to continue [y/N]y 58 | 59 | 60 | Changed password for user apm_system 61 | PASSWORD apm_system = iKVpVyFFTC8qEXvJILi2 62 | 63 | Changed password for user kibana 64 | PASSWORD kibana = DnDgQRRgkuyV8YqTrxbk 65 | 66 | Changed password for user logstash_system 67 | PASSWORD logstash_system = aqjVEdMsG7P2CXm9sQNk 68 | 69 | Changed password for user beats_system 70 | PASSWORD beats_system = Oleo1gQhli6tGaWuHz96 71 | 72 | Changed password for user remote_monitoring_user 73 | PASSWORD remote_monitoring_user = rX9CsBLM2c3ow9sH6Iud 74 | 75 | Changed password for user elastic 76 | PASSWORD elastic = yi4cxxdiz86pRKOoTAcm 77 | ``` 78 | ### 看到上面最后行 79 | ``` 80 | Changed password for user elastic 81 | PASSWORD elastic = yi4cxxdiz86pRKOoTAcm 82 | ``` 83 | ### 得到elastic 的帐号: elastic ,密码: yi4cxxdiz86pRKOoTAcm 将这个填入search/rpc/search.yaml 文件中 84 | 85 | 86 | 接着导入 sql.sql到 mysql数据中 ,如果有工具自行导入,下面仅参考 87 | ``` 88 | mysql -uroot -padmin 89 | mysql > set name utf8 90 | mysql > create databse datacenter 91 | mysql > use datacenter 92 | mysql > source sql.sql 93 | ``` 94 | ### 然后分别把配置文件 ,文件下面分别对应了一个rpc.example.yaml的文件,复制,基本就没有问题 95 | 96 | ``` 97 | vi etc/datacenter-api.yaml #网关配置 98 | vi user/rpc/etc/rpc.yaml #用户信息配置 99 | vi common/rpc/etc/rpc.yaml #公共配置 100 | vi votes/rpc/etc/rpc.yaml #投票配置 101 | vi search/rpc/etc/search.yaml #搜索配置 102 | vi questions/rpc/etc/questions.yaml #抽奖配置 103 | ``` 104 | ### 然后启动 服务 ,应该我们要启动 105 | ``` 106 | sh restart.sh 107 | ``` 108 | ### 输出如下 109 | ``` 110 | ➜ datacenter.bak git:(master) ✗ sh restart.sh 111 | appending output to nohup.out 112 | appending output to nohup.out 113 | appending output to nohup.out 114 | appending output to nohup.out 115 | appending output to nohup.out 116 | appending output to nohup.out 117 | ``` 118 | ### 可以分别查看是否启动成功 119 | ``` 120 | tail -F nohup.out #网关的服务 121 | tail -F user/rpc/nohup.out #用户的rpc服务 122 | tail -F common/rpc/nohup.out #公共的 123 | tail -F votes/rpc/nohup.out #投票的 124 | tail -F search/rpc/nohup.out #搜索的 125 | tail -F questions/rpc/nohup.out #问答抽奖 126 | ``` 127 | 128 | ### 在postman 导入数据 数据中台中心.postman_collection.json 就可以很愉快的玩耍了 -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module datacenter 2 | 3 | go 1.17 4 | 5 | require ( 6 | github.com/chanxuehong/wechat v0.0.0-20201110083048-0180211b69fd 7 | github.com/dgrijalva/jwt-go v3.2.0+incompatible 8 | github.com/golang/protobuf v1.5.2 9 | github.com/olivere/elastic/v7 v7.0.28 10 | github.com/qiniu/api.v7/v7 v7.8.2 11 | golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 12 | google.golang.org/grpc v1.43.0 13 | google.golang.org/protobuf v1.27.1 14 | ) 15 | 16 | require ( 17 | github.com/go-logr/stdr v1.2.2 // indirect 18 | github.com/golang-jwt/jwt/v4 v4.2.0 // indirect 19 | github.com/openzipkin/zipkin-go v0.4.0 // indirect 20 | go.opentelemetry.io/otel/exporters/zipkin v1.3.0 // indirect 21 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect 22 | ) 23 | 24 | require ( 25 | github.com/beorn7/perks v1.0.1 // indirect 26 | github.com/cespare/xxhash/v2 v2.1.1 // indirect 27 | github.com/chanxuehong/rand v0.0.0-20201110082127-2f19a1bdd973 // indirect 28 | github.com/chanxuehong/util v0.0.0-20200304121633-ca8141845b13 // indirect 29 | github.com/coreos/go-semver v0.3.0 // indirect 30 | github.com/coreos/go-systemd/v22 v22.3.2 // indirect 31 | github.com/davecgh/go-spew v1.1.1 // indirect 32 | github.com/go-logr/logr v1.2.2 // indirect 33 | github.com/go-redis/redis v6.15.9+incompatible // indirect 34 | github.com/go-sql-driver/mysql v1.6.0 // indirect 35 | github.com/gogo/protobuf v1.3.2 // indirect 36 | github.com/golang/mock v1.6.0 // indirect 37 | github.com/google/go-cmp v0.5.6 // indirect 38 | github.com/google/gofuzz v1.1.0 // indirect 39 | github.com/google/uuid v1.3.0 // indirect 40 | github.com/googleapis/gnostic v0.4.1 // indirect 41 | github.com/gookit/color v1.3.6 // indirect 42 | github.com/hashicorp/golang-lru v0.5.1 // indirect 43 | github.com/josharian/intern v1.0.0 // indirect 44 | github.com/json-iterator/go v1.1.11 // indirect 45 | github.com/justinas/alice v1.2.0 // indirect 46 | github.com/mailru/easyjson v0.7.7 // indirect 47 | github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect 48 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect 49 | github.com/modern-go/reflect2 v1.0.1 // indirect 50 | github.com/pkg/errors v0.9.1 // indirect 51 | github.com/prometheus/client_golang v1.11.0 // indirect 52 | github.com/prometheus/client_model v0.2.0 // indirect 53 | github.com/prometheus/common v0.26.0 // indirect 54 | github.com/prometheus/procfs v0.6.0 // indirect 55 | github.com/spaolacci/murmur3 v1.1.0 // indirect 56 | github.com/zeromicro/go-zero v1.3.0 57 | go.etcd.io/etcd/api/v3 v3.5.1 // indirect 58 | go.etcd.io/etcd/client/pkg/v3 v3.5.1 // indirect 59 | go.etcd.io/etcd/client/v3 v3.5.1 // indirect 60 | go.opentelemetry.io/otel v1.3.0 // indirect 61 | go.opentelemetry.io/otel/exporters/jaeger v1.3.0 // indirect 62 | go.opentelemetry.io/otel/sdk v1.3.0 // indirect 63 | go.opentelemetry.io/otel/trace v1.3.0 // indirect 64 | go.uber.org/atomic v1.7.0 // indirect 65 | go.uber.org/automaxprocs v1.4.0 // indirect 66 | go.uber.org/multierr v1.6.0 // indirect 67 | go.uber.org/zap v1.17.0 // indirect 68 | golang.org/x/crypto v0.0.0-20210920023735-84f357641f63 // indirect 69 | golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d // indirect 70 | golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect 71 | golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect 72 | golang.org/x/sys v0.0.0-20220111092808-5a964db01320 // indirect 73 | golang.org/x/text v0.3.7 // indirect 74 | google.golang.org/appengine v1.6.5 // indirect 75 | google.golang.org/genproto v0.0.0-20220112215332-a9c7c0acf9f2 // indirect 76 | gopkg.in/inf.v0 v0.9.1 // indirect 77 | gopkg.in/yaml.v2 v2.4.0 // indirect 78 | k8s.io/api v0.20.12 // indirect 79 | k8s.io/apimachinery v0.20.12 // indirect 80 | k8s.io/client-go v0.20.12 // indirect 81 | k8s.io/klog/v2 v2.40.1 // indirect 82 | k8s.io/utils v0.0.0-20201110183641-67b214c5f920 // indirect 83 | sigs.k8s.io/structured-merge-diff/v4 v4.1.2 // indirect 84 | sigs.k8s.io/yaml v1.2.0 // indirect 85 | ) 86 | -------------------------------------------------------------------------------- /questions/rpc/questionsclient/questions.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT! 2 | // Source: questions.proto 3 | 4 | //go:generate mockgen -destination ./questions_mock.go -package questionsclient -source $GOFILE 5 | 6 | package questionsclient 7 | 8 | import ( 9 | "context" 10 | 11 | "datacenter/questions/rpc/questions" 12 | 13 | "github.com/zeromicro/go-zero/zrpc" 14 | ) 15 | 16 | type ( 17 | Request = questions.Request 18 | ConvertResp = questions.ConvertResp 19 | ActInfoResp = questions.ActInfoResp 20 | AwardInfoResp = questions.AwardInfoResp 21 | QuestionsListResp = questions.QuestionsListResp 22 | ConvertReq = questions.ConvertReq 23 | ActivitiesReq = questions.ActivitiesReq 24 | QuestionsAnswerReq = questions.QuestionsAnswerReq 25 | QuestionsAnswerResp = questions.QuestionsAnswerResp 26 | Response = questions.Response 27 | AwardListResp = questions.AwardListResp 28 | QuestionsResp = questions.QuestionsResp 29 | GradeReq = questions.GradeReq 30 | TurnTableReq = questions.TurnTableReq 31 | 32 | Questions interface { 33 | // 获取 问答抽奖活动信息 34 | GetActivitiesInfo(ctx context.Context, in *ActivitiesReq) (*ActInfoResp, error) 35 | // 获取 问答奖品信息 36 | GetAwardInfo(ctx context.Context, in *ActivitiesReq) (*AwardInfoResp, error) 37 | // 获取 问答奖品列表 38 | GetAwardList(ctx context.Context, in *ActivitiesReq) (*AwardListResp, error) 39 | // 获取 问题列表 40 | GetQuestionsList(ctx context.Context, in *ActivitiesReq) (*QuestionsListResp, error) 41 | // 提交 答案 42 | PostQuestionsChange(ctx context.Context, in *QuestionsAnswerReq) (*QuestionsAnswerResp, error) 43 | // 获取得分 44 | GetQuestionsGrade(ctx context.Context, in *GradeReq) (*QuestionsAnswerResp, error) 45 | // 抽奖 46 | PostTurnTable(ctx context.Context, in *TurnTableReq) (*AwardInfoResp, error) 47 | // 填写中奖记录 48 | PostConvert(ctx context.Context, in *ConvertReq) (*ConvertResp, error) 49 | } 50 | 51 | defaultQuestions struct { 52 | cli zrpc.Client 53 | } 54 | ) 55 | 56 | func NewQuestions(cli zrpc.Client) Questions { 57 | return &defaultQuestions{ 58 | cli: cli, 59 | } 60 | } 61 | 62 | // 获取 问答抽奖活动信息 63 | func (m *defaultQuestions) GetActivitiesInfo(ctx context.Context, in *ActivitiesReq) (*ActInfoResp, error) { 64 | client := questions.NewQuestionsClient(m.cli.Conn()) 65 | return client.GetActivitiesInfo(ctx, in) 66 | } 67 | 68 | // 获取 问答奖品信息 69 | func (m *defaultQuestions) GetAwardInfo(ctx context.Context, in *ActivitiesReq) (*AwardInfoResp, error) { 70 | client := questions.NewQuestionsClient(m.cli.Conn()) 71 | return client.GetAwardInfo(ctx, in) 72 | } 73 | 74 | // 获取 问答奖品列表 75 | func (m *defaultQuestions) GetAwardList(ctx context.Context, in *ActivitiesReq) (*AwardListResp, error) { 76 | client := questions.NewQuestionsClient(m.cli.Conn()) 77 | return client.GetAwardList(ctx, in) 78 | } 79 | 80 | // 获取 问题列表 81 | func (m *defaultQuestions) GetQuestionsList(ctx context.Context, in *ActivitiesReq) (*QuestionsListResp, error) { 82 | client := questions.NewQuestionsClient(m.cli.Conn()) 83 | return client.GetQuestionsList(ctx, in) 84 | } 85 | 86 | // 提交 答案 87 | func (m *defaultQuestions) PostQuestionsChange(ctx context.Context, in *QuestionsAnswerReq) (*QuestionsAnswerResp, error) { 88 | client := questions.NewQuestionsClient(m.cli.Conn()) 89 | return client.PostQuestionsChange(ctx, in) 90 | } 91 | 92 | // 获取得分 93 | func (m *defaultQuestions) GetQuestionsGrade(ctx context.Context, in *GradeReq) (*QuestionsAnswerResp, error) { 94 | client := questions.NewQuestionsClient(m.cli.Conn()) 95 | return client.GetQuestionsGrade(ctx, in) 96 | } 97 | 98 | // 抽奖 99 | func (m *defaultQuestions) PostTurnTable(ctx context.Context, in *TurnTableReq) (*AwardInfoResp, error) { 100 | client := questions.NewQuestionsClient(m.cli.Conn()) 101 | return client.PostTurnTable(ctx, in) 102 | } 103 | 104 | // 填写中奖记录 105 | func (m *defaultQuestions) PostConvert(ctx context.Context, in *ConvertReq) (*ConvertResp, error) { 106 | client := questions.NewQuestionsClient(m.cli.Conn()) 107 | return client.PostConvert(ctx, in) 108 | } 109 | -------------------------------------------------------------------------------- /votes/model/appvotesmodel.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | import ( 4 | "database/sql" 5 | "fmt" 6 | "strings" 7 | "time" 8 | 9 | "github.com/zeromicro/go-zero/core/stores/builder" 10 | "github.com/zeromicro/go-zero/core/stores/sqlc" 11 | "github.com/zeromicro/go-zero/core/stores/sqlx" 12 | "github.com/zeromicro/go-zero/core/stringx" 13 | ) 14 | 15 | var ( 16 | appVotesFieldNames = builder.RawFieldNames(&AppVotes{}) 17 | appVotesRows = strings.Join(appVotesFieldNames, ",") 18 | appVotesRowsExpectAutoSet = strings.Join(stringx.Remove(appVotesFieldNames, "avid", "create_time", "update_time"), ",") 19 | appVotesRowsWithPlaceHolder = strings.Join(stringx.Remove(appVotesFieldNames, "avid", "create_time", "update_time"), "=?,") + "=?" 20 | ) 21 | 22 | type ( 23 | AppVotesModel interface { 24 | Insert(data AppVotes) (sql.Result, error) 25 | FindOne(avid int64) (*AppVotes, error) 26 | Update(data AppVotes) error 27 | Delete(avid int64) error 28 | FindByNumWithVotes(aeid, uid, auid int64) (int64, error) 29 | FindByDaysWithVotes(actid, uid, auid int64, date string) (int64, error) 30 | } 31 | 32 | defaultAppVotesModel struct { 33 | conn sqlx.SqlConn 34 | table string 35 | } 36 | 37 | AppVotes struct { 38 | Actid int64 `db:"actid"` // 投票活动的id 39 | Aeid int64 `db:"aeid"` // 投票的id 40 | Auid int64 `db:"auid"` // 中台表appuser的id 41 | Beid int64 `db:"beid"` // 对应的平台 42 | CreateTime time.Time `db:"create_time"` 43 | Ip string `db:"ip"` // 投票人IP 44 | Ptyid int64 `db:"ptyid"` // 平台id: 1.微信公众号,2.微信小程序,3.支付宝 45 | Uid int64 `db:"uid"` // 中台表用户的id 46 | UpdateTime time.Time `db:"update_time"` 47 | Avid int64 `db:"avid"` // 投票人序号:自增 48 | } 49 | ) 50 | 51 | func NewAppVotesModel(conn sqlx.SqlConn) AppVotesModel { 52 | return &defaultAppVotesModel{ 53 | conn: conn, 54 | table: "app_votes", 55 | } 56 | } 57 | 58 | /** 59 | * 获取 按天数投票 60 | * aeid 活动的id 61 | * uid 用户的id 62 | * auid snsid 63 | * date 64 | */ 65 | 66 | func (m *defaultAppVotesModel) FindByDaysWithVotes(actid, uid, auid int64, date string) (int64, error) { 67 | query := fmt.Sprintf("select count(*) as count from %s where actid = ? AND uid = ? AND auid = ? AND create_time like ? limit 1", m.table) 68 | var count int64 69 | err := m.conn.QueryRowPartial(&count, query, actid, uid, auid, date+"%") 70 | if err != nil { 71 | return 0, err 72 | } 73 | return count, nil 74 | } 75 | 76 | /** 77 | * 获取 按次数投票 78 | * aeid 活动的id 79 | * uid 用户的id 80 | * auid snsid 81 | */ 82 | func (m *defaultAppVotesModel) FindByNumWithVotes(actid, uid, auid int64) (int64, error) { 83 | query := fmt.Sprintf("select count(*) as count from %s where actid = ? AND uid = ? AND auid = ? limit 1", appVotesRows, m.table) 84 | var count int64 85 | err := m.conn.QueryRowPartial(&count, query, actid, uid, auid) 86 | if err != nil { 87 | return 0, err 88 | } 89 | return count, nil 90 | } 91 | 92 | func (m *defaultAppVotesModel) Insert(data AppVotes) (sql.Result, error) { 93 | query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?)", m.table, appVotesRowsExpectAutoSet) 94 | ret, err := m.conn.Exec(query, data.Actid, data.Aeid, data.Auid, data.Beid, data.Ip, data.Ptyid, data.Uid) 95 | return ret, err 96 | } 97 | 98 | func (m *defaultAppVotesModel) FindOne(avid int64) (*AppVotes, error) { 99 | query := fmt.Sprintf("select %s from %s where avid = ? limit 1", appVotesRows, m.table) 100 | var resp AppVotes 101 | err := m.conn.QueryRow(&resp, query, avid) 102 | switch err { 103 | case nil: 104 | return &resp, nil 105 | case sqlc.ErrNotFound: 106 | return nil, ErrNotFound 107 | default: 108 | return nil, err 109 | } 110 | } 111 | 112 | func (m *defaultAppVotesModel) Update(data AppVotes) error { 113 | query := fmt.Sprintf("update %s set %s where avid = ?", m.table, appVotesRowsWithPlaceHolder) 114 | _, err := m.conn.Exec(query, data.Actid, data.Aeid, data.Auid, data.Beid, data.Ip, data.Ptyid, data.Uid, data.Avid) 115 | return err 116 | } 117 | 118 | func (m *defaultAppVotesModel) Delete(avid int64) error { 119 | query := fmt.Sprintf("delete from %s where avid = ?", m.table) 120 | _, err := m.conn.Exec(query, avid) 121 | return err 122 | } 123 | -------------------------------------------------------------------------------- /common/model/baseappmodel.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | import ( 4 | "database/sql" 5 | "fmt" 6 | "strings" 7 | "time" 8 | 9 | "github.com/zeromicro/go-zero/core/stores/builder" 10 | "github.com/zeromicro/go-zero/core/stores/cache" 11 | "github.com/zeromicro/go-zero/core/stores/sqlc" 12 | "github.com/zeromicro/go-zero/core/stores/sqlx" 13 | "github.com/zeromicro/go-zero/core/stringx" 14 | ) 15 | 16 | var ( 17 | baseAppFieldNames = builder.RawFieldNames(&BaseApp{}) 18 | baseAppRows = strings.Join(baseAppFieldNames, ",") 19 | baseAppRowsExpectAutoSet = strings.Join(stringx.Remove(baseAppFieldNames, "id", "create_time", "update_time"), ",") 20 | baseAppRowsWithPlaceHolder = strings.Join(stringx.Remove(baseAppFieldNames, "id", "create_time", "update_time"), "=?,") + "=?" 21 | 22 | cacheBaseAppIdPrefix = "cache#BaseApp#id#" 23 | ) 24 | 25 | type ( 26 | BaseAppModel interface { 27 | Insert(data BaseApp) (sql.Result, error) 28 | FindOne(id int64) (*BaseApp, error) 29 | Update(data BaseApp) error 30 | Delete(id int64) error 31 | } 32 | 33 | defaultBaseAppModel struct { 34 | sqlc.CachedConn 35 | table string 36 | } 37 | 38 | BaseApp struct { 39 | CreateBy string `db:"create_by"` 40 | CreatedAt time.Time `db:"created_at"` 41 | Fullwebsite string `db:"fullwebsite"` // 完整的域名 42 | Isclose int64 `db:"isclose"` // 站点是否关闭 43 | Logo string `db:"logo"` // 应用login 44 | Sname string `db:"sname"` // 应用名称 45 | UpdateBy string `db:"update_by"` 46 | UpdatedAt time.Time `db:"updated_at"` 47 | Website string `db:"website"` // 站点名称 48 | Id int64 `db:"id"` 49 | } 50 | ) 51 | 52 | func NewBaseAppModel(conn sqlx.SqlConn, c cache.CacheConf) BaseAppModel { 53 | return &defaultBaseAppModel{ 54 | CachedConn: sqlc.NewConn(conn, c), 55 | table: "base_app", 56 | } 57 | } 58 | 59 | func (m *defaultBaseAppModel) Insert(data BaseApp) (sql.Result, error) { 60 | query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, baseAppRowsExpectAutoSet) 61 | ret, err := m.ExecNoCache(query, data.CreateBy, data.CreatedAt, data.Fullwebsite, data.Isclose, data.Logo, data.Sname, data.UpdateBy, data.UpdatedAt, data.Website) 62 | 63 | return ret, err 64 | } 65 | 66 | func GetcacheBaseAppIdPrefix(id int64) string { 67 | return fmt.Sprintf("%s%v", cacheBaseAppIdPrefix, id) 68 | } 69 | 70 | func (m *defaultBaseAppModel) FindOne(id int64) (*BaseApp, error) { 71 | baseAppIdKey := fmt.Sprintf("%s%v", cacheBaseAppIdPrefix, id) 72 | var resp BaseApp 73 | err := m.QueryRow(&resp, baseAppIdKey, func(conn sqlx.SqlConn, v interface{}) error { 74 | query := fmt.Sprintf("select %s from %s where id = ? limit 1", baseAppRows, m.table) 75 | return conn.QueryRow(v, query, id) 76 | }) 77 | switch err { 78 | case nil: 79 | return &resp, nil 80 | case sqlc.ErrNotFound: 81 | return nil, ErrNotFound 82 | default: 83 | return nil, err 84 | } 85 | } 86 | 87 | func (m *defaultBaseAppModel) Update(data BaseApp) error { 88 | baseAppIdKey := fmt.Sprintf("%s%v", cacheBaseAppIdPrefix, data.Id) 89 | _, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) { 90 | query := fmt.Sprintf("update %s set %s where id = ?", m.table, baseAppRowsWithPlaceHolder) 91 | return conn.Exec(query, data.CreateBy, data.CreatedAt, data.Fullwebsite, data.Isclose, data.Logo, data.Sname, data.UpdateBy, data.UpdatedAt, data.Website, data.Id) 92 | }, baseAppIdKey) 93 | return err 94 | } 95 | 96 | func (m *defaultBaseAppModel) Delete(id int64) error { 97 | 98 | baseAppIdKey := fmt.Sprintf("%s%v", cacheBaseAppIdPrefix, id) 99 | _, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) { 100 | query := fmt.Sprintf("delete from %s where id = ?", m.table) 101 | return conn.Exec(query, id) 102 | }, baseAppIdKey) 103 | return err 104 | } 105 | 106 | func (m *defaultBaseAppModel) formatPrimary(primary interface{}) string { 107 | return fmt.Sprintf("%s%v", cacheBaseAppIdPrefix, primary) 108 | } 109 | 110 | func (m *defaultBaseAppModel) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error { 111 | query := fmt.Sprintf("select %s from %s where id = ? limit 1", baseAppRows, m.table) 112 | return conn.QueryRow(v, query, primary) 113 | } 114 | --------------------------------------------------------------------------------