diff --git a/config/config.go b/global/config.go similarity index 85% rename from config/config.go rename to global/config.go index b59b0f9..b27181a 100644 --- a/config/config.go +++ b/global/config.go @@ -1,4 +1,4 @@ -package config +package global type ServerConfig struct { Listen string `yaml:"listen"` @@ -11,6 +11,7 @@ type ServerConfig struct { Redis RedisConfig `yaml:"redis"` Jwt JwtConfig `yaml:"jwt"` Email EmailConfig `yaml:"email"` + Bos BosConfig `yaml:"bos"` } type MySQLConfig struct { @@ -39,3 +40,9 @@ type EmailConfig struct { Account string `yaml:"account"` Password string `yaml:"password"` } + +type BosConfig struct { + AccessKey string `yaml:"accessKey"` + SecretKey string `yaml:"secretKey"` + Domain string `yaml:"domain"` +} diff --git a/global/global.go b/global/global.go index 5966c87..feac2bc 100644 --- a/global/global.go +++ b/global/global.go @@ -3,7 +3,7 @@ package global import ( "errors" "fmt" - "nCovTrack-Backend/config" + "github.com/baidubce/bce-sdk-go/services/bos" "net/http" "github.com/gin-gonic/gin" @@ -13,12 +13,13 @@ import ( ) var ( - ServerSettings config.ServerConfig + ServerSettings ServerConfig Db *gorm.DB RootRouter *gin.RouterGroup Logger *zap.SugaredLogger HttpClient map[string]*http.Client Redis *redis.Client + BosClient *bos.Client ) func GetListenOn() string { @@ -37,7 +38,14 @@ const ( CHINA_NCOV_STATISTIC_URL = "https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5" ENV_NOLOG = "nolog" TOKEN_EXPIRE_DAYS = 15 + FACKER_HOST = "http://myhost.fallen-angle.com:5000/" REGISTER_REDIS_KEY = "register_key" CHANGEPWD_REDIS_KEY = "changepwd_key" ) + +var ( + ROLE_ID_MAP = map[int]string{ + 0: "SYSTEM", + } +) diff --git a/go.mod b/go.mod index c156a7d..2947833 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( github.com/KyleBanks/depth v1.2.1 // indirect github.com/PuerkitoBio/purell v1.1.1 // indirect github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect + github.com/baidubce/bce-sdk-go v0.9.112 // indirect github.com/fsnotify/fsnotify v1.5.1 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-openapi/jsonpointer v0.19.5 // indirect diff --git a/go.sum b/go.sum index 545fae4..bef7ffd 100644 --- a/go.sum +++ b/go.sum @@ -46,6 +46,8 @@ github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/agiledragon/gomonkey/v2 v2.3.1 h1:k+UnUY0EMNYUFUAQVETGY9uUTxjMdnUkP0ARyJS1zzs= github.com/agiledragon/gomonkey/v2 v2.3.1/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY= +github.com/baidubce/bce-sdk-go v0.9.112 h1:qHzFxG7fwGbXCv+1smcbWFhWl6iwoXDVzPn9TUtrlss= +github.com/baidubce/bce-sdk-go v0.9.112/go.mod h1:zbYJMQwE4IZuyrJiFO8tO8NbtYiKTFTbwh4eIsqjVdg= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= diff --git a/initialize/bos.go b/initialize/bos.go new file mode 100644 index 0000000..e2b349b --- /dev/null +++ b/initialize/bos.go @@ -0,0 +1,20 @@ +package initialize + +import ( + "github.com/baidubce/bce-sdk-go/services/bos" + "nCovTrack-Backend/global" +) + +func initBos() { + clientConfig := bos.BosClientConfiguration{ + global.ServerSettings.Bos.AccessKey, + global.ServerSettings.Bos.SecretKey, + global.ServerSettings.Bos.Domain, + false, + } + bosClient, err := bos.NewClientWithConfig(&clientConfig) + if err != nil { + panic(err) + } + global.BosClient = bosClient +} diff --git a/initialize/config.go b/initialize/config.go index ac58124..4d150ec 100644 --- a/initialize/config.go +++ b/initialize/config.go @@ -2,11 +2,9 @@ package initialize import ( "fmt" - "nCovTrack-Backend/config" - "nCovTrack-Backend/global" - "github.com/fatih/color" "github.com/spf13/viper" + "nCovTrack-Backend/global" ) func initConfig() { @@ -16,7 +14,7 @@ func initConfig() { if err := v.ReadInConfig(); err != nil { panic(err) } - serverConfig := config.ServerConfig{} + serverConfig := global.ServerConfig{} if err := v.Unmarshal(&serverConfig); err != nil { panic(err) } diff --git a/initialize/initialize.go b/initialize/initialize.go index 05fc1ef..8d0e9f7 100644 --- a/initialize/initialize.go +++ b/initialize/initialize.go @@ -8,6 +8,7 @@ func Initialize() *gin.Engine { initHttpClient() initMySQL() initRedis() + initBos() initCron() g := initRouter() initSwagger() diff --git a/middleware/auth.go b/middleware/auth.go index e23d4ea..fd4d0a6 100644 --- a/middleware/auth.go +++ b/middleware/auth.go @@ -27,6 +27,7 @@ func Auth() gin.HandlerFunc { c.Request.Header.Set("role", fmt.Sprint(claims["role"])) c.Request.Header.Set("email", claims["email"].(string)) c.Request.Header.Set("id", fmt.Sprint(claims["id"])) + c.Request.Header.Set("role", claims["role"].(string)) // renew token, and judge the token's iat is expired or not renewToken := utils.RenewToken(oldToken[0]) diff --git a/models/investigate.go b/models/investigate.go new file mode 100644 index 0000000..87092ea --- /dev/null +++ b/models/investigate.go @@ -0,0 +1,43 @@ +package models + +import ( + "fmt" + "time" +) + +type FakerDate time.Time + +const ( + timeFormat = "2006-01-02" +) + +func (t *FakerDate) UnmarshalJSON(data []byte) (err error) { + fmt.Println(string(data)) + newTime, err := time.ParseInLocation(`"`+timeFormat+`"`, string(data), time.Local) + *t = FakerDate(newTime) + return +} + +func (t FakerDate) MarshalJSON() ([]byte, error) { + fmt.Println(time.Time(t).Format(timeFormat)) + timeStr := fmt.Sprintf("\"%s\"", time.Time(t).Format(timeFormat)) + return []byte(timeStr), nil +} + +func (t FakerDate) String() string { + return time.Time(t).Format(timeFormat) +} + +type HotelContactRequest struct { + Name string `json:"name"` + Age int `json:"age,string"` + Sex int `json:"sex,string"` + Phone string `json:"phone"` + Address string `json:"address"` + HotelCode string `json:"hotel_code"` + HotelName string `json:"hotel_name"` + LocateCityId string `json:"locate_city_id"` + Identification string `json:"identification"` + InData FakerDate `json:"in_data"` + OutData FakerDate `json:"out_data"` +} diff --git a/router/investigate.go b/router/investigate.go new file mode 100644 index 0000000..8306398 --- /dev/null +++ b/router/investigate.go @@ -0,0 +1,17 @@ +package router + +import ( + "github.com/gin-gonic/gin" + "nCovTrack-Backend/service/investigate" + "net/http" +) + +func investigatePublicRouter(router *gin.RouterGroup) { + investigateRouter := router.Group("investigate") + { + investigateRouter.GET("/test", func(c *gin.Context) { + investigate.QueryHotelContacts() + c.JSON(http.StatusOK, nil) + }) + } +} diff --git a/router/router.go b/router/router.go index 170ad25..756e095 100644 --- a/router/router.go +++ b/router/router.go @@ -15,6 +15,7 @@ func BusiRouter() { statisticRouter(publicRouter) articlePublicRouter(publicRouter) userPublicRouter(publicRouter) + investigatePublicRouter(publicRouter) } // Private diff --git a/service/investigate/faker.go b/service/investigate/faker.go new file mode 100644 index 0000000..32848d4 --- /dev/null +++ b/service/investigate/faker.go @@ -0,0 +1,27 @@ +package investigate + +import ( + "encoding/json" + "fmt" + "nCovTrack-Backend/global" + "nCovTrack-Backend/models" + "nCovTrack-Backend/utils" +) + +func fakerGetRequest(uri string) string { + resStr := utils.GetWhioutHeader(global.FACKER_HOST + uri) + var res utils.GinResponse + _ = json.Unmarshal([]byte(resStr), &res) + dataStr, _ := json.Marshal(res.Data) + return string(dataStr) +} + +func QueryHotelContacts() { + dataStr := fakerGetRequest("query/contacts/hotel/320581199103182689") + var data []models.HotelContactRequest + err := json.Unmarshal([]byte(dataStr), &data) + if err != nil { + panic(err) + } + fmt.Println(data) +} diff --git a/service/investigate/investigate.go b/service/investigate/investigate.go new file mode 100644 index 0000000..7f35188 --- /dev/null +++ b/service/investigate/investigate.go @@ -0,0 +1 @@ +package investigate diff --git a/settings-dev.yml b/settings-dev.yml index b16d6a0..d3810f8 100644 --- a/settings-dev.yml +++ b/settings-dev.yml @@ -26,4 +26,9 @@ email: host: smtp.qq.com port: 587 account: fallen-angle@foxmail.com - password: hxrisxltxsjvieec \ No newline at end of file + password: hxrisxltxsjvieec + +bos: + accessKey: 90dbff87c5aa4bdbb0d7a29e130b2808 + secretKey: e53a672a10294abc8ecabe1ef92625b1 + domain: bj.bcebos.com diff --git a/utils/bos.go b/utils/bos.go new file mode 100644 index 0000000..b75874f --- /dev/null +++ b/utils/bos.go @@ -0,0 +1,15 @@ +package utils + +import "nCovTrack-Backend/global" + +func UploadFile() string { + etag, err := global.BosClient.PutObjectFromFile("ncovtrack", "test.jpg", "/home/fallen-angle/Pictures/Anime/pic-w-000003.jpg", nil) + if err != nil { + return "" + } + return etag +} + +func DownloadLink() string { + return global.BosClient.BasicGeneratePresignedUrl("ncovtrack", "test.jpg", 1800) +}