diff --git "a/wonder-world/1doc_look_ME/\351\227\256\347\255\224\345\271\263\345\217\260\357\274\210\345\220\216\347\253\257\357\274\211.md" "b/wonder-world/1doc_look_ME/\351\227\256\347\255\224\345\271\263\345\217\260\357\274\210\345\220\216\347\253\257\357\274\211.md" new file mode 100644 index 0000000..e4f1174 --- /dev/null +++ "b/wonder-world/1doc_look_ME/\351\227\256\347\255\224\345\271\263\345\217\260\357\274\210\345\220\216\347\253\257\357\274\211.md" @@ -0,0 +1,88 @@ +问答平台(后端) + +1.概述 + +该后端程序实现注册登入,问题和回答的上传删除显示。 + +*** + +2.API + +(1)“/v1/register”(注册) + +name:用户名称 + +telephone:用户电话 + +password:密码 + +(2)“/v1/login”(登入) + +telephone:用户电话 + +password:用户密码 + +(3)“/problem_hall/put”(输入问题) + +title:问题标题 + +description:问题描述 + +name:提问者账号名 + +key:密匙(删除问题用密匙) + +——问题名不可重复 + +(4)“/problem_hall/delete”(删除问题) + +title:问题标题 + +key:密匙(密匙正确可删除) + +(5)“/problem_hall/hall”(读取数据发送给前端) + +first:开始跳过的数据 + +number:读取的数据数 +(6)"/problem_hall/research"(给题目读问题) +title:问题题目 + +(6)“/answer/put”(提供回答) + +description:回答内容 + +key:密匙(删除回答) + +name:回答者账号名 + +title:回答对应的问题标题 + +(7)“/answer/delete”(删除问题) + +text:问题内容 + +title:回答对应的问题标题 + +key;密匙 + +name:回答者的名字 + +(8)“/answer/hall”(显示回答数据) + +title:问题标题 + +first:开始跳过的数据 + +number:读取的数据量 +(9)检验登入状态 +在操作问题和回答时会检验是否登入 +(10)登出 +name:在输入用户名 + +*** +apifox链接:https://qsefuqgces.apifox.cn/ +*** + + + diff --git a/wonder-world/account/import.go b/wonder-world/account/import.go new file mode 100644 index 0000000..b08bad5 --- /dev/null +++ b/wonder-world/account/import.go @@ -0,0 +1,15 @@ +package account + +import "gorm.io/gorm" + +type User struct { //用户 + gorm.Model `json:"gorm_._model"` + Name string `json:"name,omitempty"` + Telephone string `json:"telephone,omitempty"` + Password string `json:"password,omitempty"` + ID int `json:"id,omitempty"` +} +type session struct { + Name string + Value string +} diff --git a/wonder-world/account/login.go b/wonder-world/account/login.go new file mode 100644 index 0000000..2d13b5b --- /dev/null +++ b/wonder-world/account/login.go @@ -0,0 +1,56 @@ +package account + +import ( + "github.com/gin-gonic/gin" + "golang.org/x/crypto/bcrypt" + "strconv" + "wonder-world/db" + "wonder-world/tool" +) + +func Login(c *gin.Context) { + db := db.Dbfrom() + telephone := c.PostForm("telephone") + password := c.PostForm("password") + var user User + db.Where("telephone = ?", telephone).First(&user) + if user.ID == 0 { + c.JSON(422, gin.H{ + "code": 422, + "message": "用户不存在", + }) + return + } + if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil { + c.JSON(422, gin.H{ + "code": 422, + "message": "密码错误", + }) + return + } + f := 1 + for f == 1 { + name := tool.Randam() + value := tool.Randam() + _, err := c.Cookie(strconv.Itoa(name)) + if err != nil { + f = -1 + c.SetCookie(user.Name, strconv.Itoa(value), 3600, "/", "http://127.0.0.1:8080", false, false) + newsession := session{ + Name: strconv.Itoa(name), + Value: strconv.Itoa(value), + } + db.Create(&newsession) + c.JSON(200, gin.H{ + "code": 200, + "message": "success", + }) + return + } + } + + c.JSON(422, gin.H{ + "code": 422, + "message": "cookie failure", + }) +} diff --git a/wonder-world/account/register.go b/wonder-world/account/register.go new file mode 100644 index 0000000..d30493b --- /dev/null +++ b/wonder-world/account/register.go @@ -0,0 +1,61 @@ +package account + +import ( + "github.com/gin-gonic/gin" + "golang.org/x/crypto/bcrypt" + "net/http" + "wonder-world/db" +) + +func Register(c *gin.Context) { + db := db.Dbfrom() + var u User + u.Name = c.PostForm("name") + u.Password = c.PostForm("password") + u.Telephone = c.PostForm("telephone") + if len(u.Name) == 0 { + c.JSON(422, gin.H{ + "code": 422, + "message": "用户名不能为空", + }) + return + } + if len(u.Telephone) != 11 { + c.JSON(422, gin.H{ + "code": 422, + "message": "手机号必须为11位", + }) + return + } + if len(u.Password) <= 6 { + c.JSON(422, gin.H{ + "code": 422, + "message": "密码不能少于6位", + }) + return + } + var user User + db.Where("telephone = ?", u.Telephone).First(&user) + if user.ID != 0 { + c.JSON(422, gin.H{ + "code": 422, + "message": "用户已存在", + }) + return + } + hasedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) + if err != nil { + c.JSON(http.StatusUnprocessableEntity, gin.H{ + "code": 500, + "message": "密码加密错误", + }) + return + } + u.Password = string(hasedPassword) + db.Create(&u) + c.JSON(200, gin.H{ + "code": 200, + "message": "success", + }) + +} diff --git a/wonder-world/answer/andelet.go b/wonder-world/answer/andelet.go new file mode 100644 index 0000000..e38c50d --- /dev/null +++ b/wonder-world/answer/andelet.go @@ -0,0 +1,58 @@ +package answer + +import ( + "fmt" + "github.com/gin-gonic/gin" + db2 "wonder-world/db" + "wonder-world/test" +) + +func Andelet(c *gin.Context) { + db := db2.Dbfrom() + f := 1 + key := c.PostForm("key") + title := c.PostForm("title") + text := c.PostForm("text") + name := c.PostForm("name") + var user Ques + db.Where("title = ?", title).First(&user) + if user.ID == 0 { + c.JSON(422, gin.H{ + "code": 422, + "message": "问题不存在", + }) + return + } + if !test.Getcookie(c, name) { + c.JSON(422, gin.H{ + "code": 422, + "message": "重登", + }) + return + } + var use []Anse + db.Where("name = ?", name).Find(&use) + fmt.Println(use) + if user.ID != 0 { + for _, answer := range use { + if answer.Text == text { + if answer.Key == key { + f = 0 + db.Delete(&answer) + break + } + } + } + if f == 1 { + c.JSON(422, gin.H{ + "code": 422, + "message": "错误", + }) + return + } + c.JSON(200, gin.H{ + "code": 200, + "message": "success", + }) + } +} diff --git a/wonder-world/answer/anhall.go b/wonder-world/answer/anhall.go new file mode 100644 index 0000000..4212907 --- /dev/null +++ b/wonder-world/answer/anhall.go @@ -0,0 +1,26 @@ +package answer + +import ( + "github.com/gin-gonic/gin" + "strconv" + db2 "wonder-world/db" +) + +func Anhall(c *gin.Context) { + db := db2.Dbfrom() + number := c.PostForm("first") + number1 := c.PostForm("number") + title := c.PostForm("title") + var num, _ = strconv.Atoi(number) + num1, _ := strconv.Atoi(number1) + var user []Anse + db.Limit(num1).Offset(num).Find(&user) + for _, answer := range user { + if answer.Title == title { + } + c.JSON(200, gin.H{ + "name": answer.Name, + "text": answer.Text, + }) + } +} diff --git a/wonder-world/answer/anput.go b/wonder-world/answer/anput.go new file mode 100644 index 0000000..cde26d7 --- /dev/null +++ b/wonder-world/answer/anput.go @@ -0,0 +1,52 @@ +package answer + +import ( + "github.com/gin-gonic/gin" + db2 "wonder-world/db" + "wonder-world/test" +) + +func Anput(c *gin.Context) { + db := db2.Dbfrom() + description := c.PostForm("description") + name := c.PostForm("name") + key := c.PostForm("key") + title := c.PostForm("title") + var user User + db.Where("name = ?", name).First(&user) + if user.ID == 0 { + c.JSON(422, gin.H{ + "code": 422, + "message": "请输入正确的用户名", + }) + return + } + if !test.Getcookie(c, name) { + c.JSON(422, gin.H{ + "code": 422, + "message": "重登", + }) + return + } + var use Ques + db.Where("title = ?", title).First(&use) + if use.ID == 0 { + c.JSON(422, gin.H{ + "code": 422, + "message": "问题不存在", + }) + return + } + newAnswer := Anse{ + Name: name, + Text: description, + Key: key, + Title: title, + } + db.Create(&newAnswer) + c.JSON(200, gin.H{ + "code": 200, + "message": "success", + }) + +} diff --git a/wonder-world/answer/import.go b/wonder-world/answer/import.go new file mode 100644 index 0000000..63147de --- /dev/null +++ b/wonder-world/answer/import.go @@ -0,0 +1,25 @@ +package answer + +import "gorm.io/gorm" + +type User struct { //用户 + gorm.Model `json:"gorm_._model"` + Name string `json:"name,omitempty"` + Telephone string `json:"telephone,omitempty"` + Password string `json:"password,omitempty"` + ID int `json:"id,omitempty"` +} +type Ques struct { //问题 + gorm.Model + Name string + Title string + Put string + Key string +} +type Anse struct { //我、回答 + Name string `json:"name"` + Text string `json:"text"` + Key string `json:"key"` + Title string `json:"title"` + gorm.Model +} diff --git a/wonder-world/db/db.go b/wonder-world/db/db.go new file mode 100644 index 0000000..685c7b5 --- /dev/null +++ b/wonder-world/db/db.go @@ -0,0 +1,18 @@ +package db + +import ( + "gorm.io/driver/mysql" + "gorm.io/gorm" +) + +var ( + db *gorm.DB + err error +) + +func Dbfrom() *gorm.DB { + dsn := "root:123789@tcp(127.0.0.1:3306)/demo?charset=utf8mb4&parseTime=True&loc=Local" //数据库登入 + db, _ = gorm.Open(mysql.Open(dsn), &gorm.Config{}) + _ = db.AutoMigrate(&User{}, &Ques{}, &Anse{}, &session{}) + return db +} diff --git a/wonder-world/db/import.go b/wonder-world/db/import.go new file mode 100644 index 0000000..71a23cf --- /dev/null +++ b/wonder-world/db/import.go @@ -0,0 +1,29 @@ +package db + +import "gorm.io/gorm" + +type User struct { //用户 + gorm.Model `json:"gorm_._model"` + Name string `json:"name,omitempty"` + Telephone string `json:"telephone,omitempty"` + Password string `json:"password,omitempty"` + ID int `json:"id,omitempty"` +} +type Ques struct { //问题 + gorm.Model + Name string + Title string + Put string + Key string +} +type Anse struct { //我、回答 + Name string `json:"name"` + Text string `json:"text"` + Key string `json:"key"` + Title string `json:"title"` + gorm.Model +} +type session struct { + Name string + Value string +} diff --git a/wonder-world/go.mod b/wonder-world/go.mod new file mode 100644 index 0000000..0d37d8e --- /dev/null +++ b/wonder-world/go.mod @@ -0,0 +1,47 @@ +module wonder-world + +go 1.23.1 + +require ( + github.com/dchest/captcha v1.0.0 + github.com/gin-contrib/sessions v1.0.1 + github.com/gin-gonic/gin v1.10.0 + golang.org/x/crypto v0.27.0 + gorm.io/driver/mysql v1.5.7 + gorm.io/gorm v1.25.12 +) + +require ( + filippo.io/edwards25519 v1.1.0 // indirect + github.com/bytedance/sonic v1.12.3 // indirect + github.com/bytedance/sonic/loader v0.2.0 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.5 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.22.1 // indirect + github.com/go-sql-driver/mysql v1.8.1 // indirect + github.com/goccy/go-json v0.10.3 // indirect + github.com/gorilla/context v1.1.2 // indirect + github.com/gorilla/securecookie v1.1.2 // indirect + github.com/gorilla/sessions v1.2.2 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.8 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.3 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + golang.org/x/arch v0.10.0 // indirect + golang.org/x/net v0.29.0 // indirect + golang.org/x/sys v0.25.0 // indirect + golang.org/x/text v0.18.0 // indirect + google.golang.org/protobuf v1.34.2 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/wonder-world/go.sum b/wonder-world/go.sum new file mode 100644 index 0000000..d0b81a3 --- /dev/null +++ b/wonder-world/go.sum @@ -0,0 +1,112 @@ +filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= +filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= +github.com/bytedance/sonic v1.12.3 h1:W2MGa7RCU1QTeYRTPE3+88mVC0yXmsRQRChiyVocVjU= +github.com/bytedance/sonic v1.12.3/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM= +github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= +github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dchest/captcha v1.0.0 h1:vw+bm/qMFvTgcjQlYVTuQBJkarm5R0YSsDKhm1HZI2o= +github.com/dchest/captcha v1.0.0/go.mod h1:7zoElIawLp7GUMLcj54K9kbw+jEyvz2K0FDdRRYhvWo= +github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= +github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4= +github.com/gin-contrib/sessions v1.0.1 h1:3hsJyNs7v7N8OtelFmYXFrulAf6zSR7nW/putcPEHxI= +github.com/gin-contrib/sessions v1.0.1/go.mod h1:ouxSFM24/OgIud5MJYQJLpy6AwxQ5EYO9yLhbtObGkM= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= +github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA= +github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= +github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= +github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= +github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= +github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gorilla/context v1.1.2 h1:WRkNAv2uoa03QNIc1A6u4O7DAGMUVoopZhkiXWA2V1o= +github.com/gorilla/context v1.1.2/go.mod h1:KDPwT9i/MeWHiLl90fuTgrt4/wPcv75vFAZLaOOcbxM= +github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA= +github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo= +github.com/gorilla/sessions v1.2.2 h1:lqzMYz6bOfvn2WriPUjNByzeXIlVzURcPmgMczkmTjY= +github.com/gorilla/sessions v1.2.2/go.mod h1:ePLdVu+jbEgHH+KWw8I1z2wqd0BAdAQh/8LRvBeoNcQ= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM= +github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= +github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +golang.org/x/arch v0.10.0 h1:S3huipmSclq3PJMNe76NGwkBR504WFkQ5dhzWzP8ZW8= +golang.org/x/arch v0.10.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= +golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= +golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= +golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= +golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo= +gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM= +gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= +gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= +gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= diff --git a/wonder-world/main.go b/wonder-world/main.go new file mode 100644 index 0000000..be1f23e --- /dev/null +++ b/wonder-world/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "wonder-world/router" +) + +func main() { + router.Run() +} diff --git a/wonder-world/question/delet.go b/wonder-world/question/delet.go new file mode 100644 index 0000000..eb67451 --- /dev/null +++ b/wonder-world/question/delet.go @@ -0,0 +1,43 @@ +package question + +import ( + "github.com/gin-gonic/gin" + "wonder-world/db" + "wonder-world/test" +) + +func De(c *gin.Context) { + db := db.Dbfrom() + title := c.PostForm("title") + key := c.PostForm("key") + var use Ques + db.Where("title = ?", title).First(&use) + if use.ID == 0 { + c.JSON(422, gin.H{ + "code": 422, + "message": "问题不存在", + }) + return + } + if use.ID != 0 { + if !test.Getcookie(c, use.Name) { + c.JSON(422, gin.H{ + "code": 422, + "message": "重登", + }) + return + } + if key != use.Key { + c.JSON(422, gin.H{ + "code": 422, + "message": "密匙错误", + }) + return + } + db.Delete(&use) + c.JSON(200, gin.H{ + "code": 200, + "message": "success", + }) + } +} diff --git a/wonder-world/question/hall.go b/wonder-world/question/hall.go new file mode 100644 index 0000000..66e9abf --- /dev/null +++ b/wonder-world/question/hall.go @@ -0,0 +1,25 @@ +package question + +import ( + "github.com/gin-gonic/gin" + "strconv" + "wonder-world/db" +) + +func Hall(c *gin.Context) { + db := db.Dbfrom() + number := c.PostForm("first") + number1 := c.PostForm("number") + var num int + num, _ = strconv.Atoi(number) + num1, _ := strconv.Atoi(number1) + var user []Ques + db.Limit(num1).Offset(num).Find(&user) //num--编号num1--数量 + for _, qusetion := range user { + c.JSON(200, gin.H{ + "title": qusetion.Title, + "put": qusetion.Put, + "name": qusetion.Name, + }) + } +} diff --git a/wonder-world/question/import.go b/wonder-world/question/import.go new file mode 100644 index 0000000..f5cade8 --- /dev/null +++ b/wonder-world/question/import.go @@ -0,0 +1,18 @@ +package question + +import "gorm.io/gorm" + +type User struct { //用户 + gorm.Model `json:"gorm_._model"` + Name string `json:"name,omitempty"` + Telephone string `json:"telephone,omitempty"` + Password string `json:"password,omitempty"` + ID int `json:"id,omitempty"` +} +type Ques struct { //问题 + gorm.Model + Name string + Title string + Put string + Key string +} diff --git a/wonder-world/question/question.go b/wonder-world/question/question.go new file mode 100644 index 0000000..8e34dd6 --- /dev/null +++ b/wonder-world/question/question.go @@ -0,0 +1,52 @@ +package question + +import ( + "github.com/gin-gonic/gin" + "wonder-world/db" + "wonder-world/test" +) + +func Question(c *gin.Context) { + db := db.Dbfrom() + title := c.PostForm("title") + description := c.PostForm("description") + name := c.PostForm("name") + key := c.PostForm("key") + var use Ques + db.Where("title = ?", title).First(&use) + if use.ID != 0 { + c.JSON(422, gin.H{ + "code": 422, + "message": "问题标题重复", + }) + return + } + var user User + db.Where("name = ?", name).First(&user) + if user.ID == 0 { + c.JSON(422, gin.H{ + "code": 422, + "message": "请输入正确的用户名", + }) + return + } + if !test.Getcookie(c, name) { + c.JSON(422, gin.H{ + "code": 422, + "message": "重登", + }) + return + } + newQuestion := Ques{ + Name: name, + Title: title, + Put: description, + Key: key, + } + db.Create(&newQuestion) + c.JSON(200, gin.H{ + "code": 200, + "message": "success", + }) + +} diff --git a/wonder-world/question/research.go b/wonder-world/question/research.go new file mode 100644 index 0000000..0082927 --- /dev/null +++ b/wonder-world/question/research.go @@ -0,0 +1,26 @@ +package question + +import ( + "github.com/gin-gonic/gin" + "wonder-world/db" +) + +func Research(c *gin.Context) { + db := db.Dbfrom() + title := c.PostForm("title") + var use Ques + db.Where("title = ?", title).First(&use) + if use.ID == 0 { + c.JSON(422, gin.H{ + "code": 422, + "message": "问题不存在", + }) + return + } + c.JSON(200, gin.H{ + "Name": use.Name, + "Title": use.Title, + "Put": use.Put, + }) + +} diff --git a/wonder-world/router/router.go b/wonder-world/router/router.go new file mode 100644 index 0000000..cf9279f --- /dev/null +++ b/wonder-world/router/router.go @@ -0,0 +1,33 @@ +package router + +import ( + "github.com/gin-gonic/gin" + "wonder-world/account" + "wonder-world/answer" + "wonder-world/question" + "wonder-world/test" +) + +func Run() { + r := gin.Default() + v := r.Group("/v1") + { + v.POST("/register", account.Register) //注册 + v.POST("/login", account.Login) //登入 + } + q := r.Group("/problem_hall") //问题 + { + q.POST("/put", question.Question) //提问 + q.POST("/delete", question.De) //删除 + q.GET("/hall", question.Hall) //大厅(输出数据) + q.GET("/research", question.Research) //寻找问题 + } + a := r.Group("/answer") //回答 + { + a.POST("/put", answer.Anput) //输入 + a.POST("/delete", answer.Andelet) //删除 + a.GET("/hall", answer.Anhall) //大厅(答案) + } + r.GET("/deletetest", test.Deletetest) //输入名字退出 + r.Run(":8080") +} diff --git a/wonder-world/test/deletetest.go b/wonder-world/test/deletetest.go new file mode 100644 index 0000000..3257d9d --- /dev/null +++ b/wonder-world/test/deletetest.go @@ -0,0 +1,34 @@ +package test + +import ( + "github.com/gin-gonic/gin" + db2 "wonder-world/db" +) + +func Deletetest(c *gin.Context) { + name := c.PostForm("name") + db := db2.Dbfrom() + var use session + cookievalue, err := c.Cookie(name) + if err != nil { + c.JSON(422, gin.H{ + "code": 422, + "message": err.Error(), + }) + return + } + db.Where("name=?", cookievalue).First(&use) + if use.Value != cookievalue { + c.JSON(422, gin.H{ + "code": 422, + "message": "cookie not exist", + }) + return + } + db.Delete(&use) + c.SetCookie(name, cookievalue, -1, "/", "", false, false) + c.JSON(200, gin.H{ + "code": 200, + "message": "退出成功", + }) +} diff --git a/wonder-world/test/test.go b/wonder-world/test/test.go new file mode 100644 index 0000000..7f2850b --- /dev/null +++ b/wonder-world/test/test.go @@ -0,0 +1,27 @@ +package test + +import ( + "github.com/gin-gonic/gin" + db2 "wonder-world/db" +) + +type session struct { + Name string + Value string +} + +func Getcookie(c *gin.Context, name string) bool { + + f := true + db := db2.Dbfrom() + var use session + cookievalue, err := c.Cookie(name) + if err != nil { + f = false + } + db.Where("name=?", cookievalue).First(&use) + if use.Value != cookievalue { + f = false + } + return f +} diff --git a/wonder-world/tool/randam.go b/wonder-world/tool/randam.go new file mode 100644 index 0000000..7510dc0 --- /dev/null +++ b/wonder-world/tool/randam.go @@ -0,0 +1,12 @@ +package tool + +import ( + "math/rand" + "time" +) + +func Randam() int { + src := rand.NewSource(time.Now().UnixNano()) + r := rand.New(src) + return r.Intn(100000) +}