token 简化

This commit is contained in:
2024-12-02 11:11:40 +08:00
parent 51c8a7fe2e
commit 06070edded
5 changed files with 15 additions and 31 deletions

View File

@@ -36,6 +36,13 @@ func InitDefault(config *viper.Viper) {
} }
} }
func ParseToken(token string) (user_id int) {
if err := db.Table("web_auth").Select("user_id").Where("token = ?", token).Scan(&user_id).Error; err != nil {
fmt.Println("token解析失败", err)
}
return user_id
}
// 定时检查补全颜色字段 // 定时检查补全颜色字段
func CheckColorNullRows(offset int) { func CheckColorNullRows(offset int) {
for { for {

View File

@@ -87,9 +87,11 @@ var imageType = graphql.NewObject(graphql.ObjectConfig{
var user_id = p.Context.Value("user_id").(int) var user_id = p.Context.Value("user_id").(int)
if user_id != 0 { if user_id != 0 {
var praise int64 var praise int64
if err := db.Table("web_praise").Where("user_id = ?", user_id).Where("praise_id = ?", p.Source.(Image).ID).Where("type = ?", 4).Count(&praise); err != nil { if err := db.Table("web_praise").Where("user_id = ?", user_id).Where("praise_id = ?", p.Source.(Image).ID).Where("type = ?", 4).Count(&praise).Error; err != nil {
fmt.Println(user_id, p.Source.(Image).ID, praise, "E", err)
return false, nil return false, nil
} }
fmt.Println(user_id, p.Source.(Image).ID, praise)
if praise > 0 { if praise > 0 {
return true, nil return true, nil
} }
@@ -100,7 +102,7 @@ var imageType = graphql.NewObject(graphql.ObjectConfig{
var user_id = p.Context.Value("user_id").(int) var user_id = p.Context.Value("user_id").(int)
if user_id != 0 { if user_id != 0 {
var collect int64 var collect int64
if err := db.Table("web_collect").Where("user_id = ?", user_id).Where("collect_id = ?", p.Source.(Image).ID).Where("type = ?", 1).Count(&collect); err != nil { if err := db.Table("web_collect").Where("user_id = ?", user_id).Where("image_id = ?", p.Source.(Image).ID).Where("type = ?", 1).Count(&collect).Error; err != nil {
return false, nil return false, nil
} }
if collect > 0 { if collect > 0 {
@@ -113,7 +115,7 @@ var imageType = graphql.NewObject(graphql.ObjectConfig{
var user_id = p.Context.Value("user_id").(int) var user_id = p.Context.Value("user_id").(int)
if user_id != 0 { if user_id != 0 {
var collect_id int var collect_id int
if err := db.Table("web_collect").Where("user_id = ?", user_id).Where("image_id = ?", p.Source.(Image).ID).First(&collect_id); err != nil { if err := db.Table("web_collect").Where("user_id = ?", user_id).Where("image_id = ?", p.Source.(Image).ID).First(&collect_id).Error; err != nil {
return nil, nil return nil, nil
} }
return collect_id, nil return collect_id, nil

View File

@@ -68,26 +68,9 @@ func LogRequest(next http.Handler) http.Handler {
var user_id int var user_id int
if token := r.Header.Get("token"); token != "" { if token := r.Header.Get("token"); token != "" {
fmt.Println("token:", token) user_id = api.ParseToken(token)
rows, err := mysqlConnection.Database.Query("SELECT user_id FROM web_auth WHERE token = ? LIMIT 1", token)
if err != nil {
log.Println("查询失败:", err)
return
}
defer rows.Close()
if rows.Next() {
err = rows.Scan(&user_id)
if err != nil {
log.Println("扫描失败:", err)
return
}
}
} }
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), "user_id", user_id)))
ctx := context.WithValue(r.Context(), "user_id", user_id)
fmt.Println("user_id:", user_id)
next.ServeHTTP(w, r.WithContext(ctx))
}) })
} }

4
go.mod
View File

@@ -11,9 +11,7 @@ require (
github.com/graphql-go/graphql v0.8.1 github.com/graphql-go/graphql v0.8.1
github.com/graphql-go/handler v0.2.3 github.com/graphql-go/handler v0.2.3
github.com/hashicorp/golang-lru/v2 v2.0.7 github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/jmoiron/sqlx v1.3.5
github.com/milvus-io/milvus-sdk-go/v2 v2.2.1 github.com/milvus-io/milvus-sdk-go/v2 v2.2.1
github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c
github.com/spf13/viper v1.15.0 github.com/spf13/viper v1.15.0
github.com/thoas/go-funk v0.9.3 github.com/thoas/go-funk v0.9.3
github.com/zhenghaoz/gorse v0.5.0-alpha.1.0.20241116140254-a323b179f5e4 github.com/zhenghaoz/gorse v0.5.0-alpha.1.0.20241116140254-a323b179f5e4
@@ -43,8 +41,8 @@ require (
github.com/json-iterator/go v1.1.12 // indirect github.com/json-iterator/go v1.1.12 // indirect
github.com/lib/pq v1.10.9 // indirect github.com/lib/pq v1.10.9 // indirect
github.com/magiconair/properties v1.8.7 // indirect github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-sqlite3 v1.14.24 // indirect
github.com/milvus-io/milvus-proto/go-api v0.0.0-20230301092744-7efc6eec15fd // indirect github.com/milvus-io/milvus-proto/go-api v0.0.0-20230301092744-7efc6eec15fd // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect github.com/pelletier/go-toml/v2 v2.2.3 // indirect

6
go.sum
View File

@@ -105,8 +105,6 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
@@ -122,16 +120,12 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.1/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lib/pq v1.10.1/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/milvus-io/milvus-proto/go-api v0.0.0-20230301092744-7efc6eec15fd h1:9ilgTEqZSdEPbJKSrRGB1TIHTaF7DqVDIwn8/azcaBk= github.com/milvus-io/milvus-proto/go-api v0.0.0-20230301092744-7efc6eec15fd h1:9ilgTEqZSdEPbJKSrRGB1TIHTaF7DqVDIwn8/azcaBk=
github.com/milvus-io/milvus-proto/go-api v0.0.0-20230301092744-7efc6eec15fd/go.mod h1:148qnlmZ0Fdm1Fq+Mj/OW2uDoEP25g3mjh0vMGtkgmk= github.com/milvus-io/milvus-proto/go-api v0.0.0-20230301092744-7efc6eec15fd/go.mod h1:148qnlmZ0Fdm1Fq+Mj/OW2uDoEP25g3mjh0vMGtkgmk=
github.com/milvus-io/milvus-sdk-go/v2 v2.2.1 h1:6p/lrxZCGkw5S2p5GPWy/BUmO6mVUNfrczv98uAnhoU= github.com/milvus-io/milvus-sdk-go/v2 v2.2.1 h1:6p/lrxZCGkw5S2p5GPWy/BUmO6mVUNfrczv98uAnhoU=