From 99813668eabff21f2c595f0f8d223f61ef16496f Mon Sep 17 00:00:00 2001 From: SOLECA794 <3451175152@qq.com> Date: Fri, 4 Oct 2024 19:23:31 +0800 Subject: [PATCH 01/13] Create SOLECA794 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 学号24050808 --- SOLECA794 | 1 + 1 file changed, 1 insertion(+) create mode 100644 SOLECA794 diff --git a/SOLECA794 b/SOLECA794 new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/SOLECA794 @@ -0,0 +1 @@ + From deae9130f7d34f0a543332109599f3c72c1d005d Mon Sep 17 00:00:00 2001 From: SOLECA794 <3451175152@qq.com> Date: Fri, 4 Oct 2024 21:44:34 +0800 Subject: [PATCH 02/13] Add files via upload --- ...273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" | 1 + 1 file changed, 1 insertion(+) create mode 100644 "\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" diff --git "a/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" "b/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" new file mode 100644 index 0000000..6a5b0c5 --- /dev/null +++ "b/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" @@ -0,0 +1 @@ +??? \ No newline at end of file From 582335b68c176ef22dd9ca6adaa5971e9964c00b Mon Sep 17 00:00:00 2001 From: SOLECA794 <3451175152@qq.com> Date: Fri, 4 Oct 2024 21:46:27 +0800 Subject: [PATCH 03/13] Delete SOLECA794 --- SOLECA794 | 1 - 1 file changed, 1 deletion(-) delete mode 100644 SOLECA794 diff --git a/SOLECA794 b/SOLECA794 deleted file mode 100644 index 8b13789..0000000 --- a/SOLECA794 +++ /dev/null @@ -1 +0,0 @@ - From bb437b60e1e2e5b74ad7b044c99b4f16d1d89510 Mon Sep 17 00:00:00 2001 From: SOLECA794 <3451175152@qq.com> Date: Fri, 4 Oct 2024 21:46:45 +0800 Subject: [PATCH 04/13] =?UTF-8?q?Delete=20=E6=96=B0=E5=BB=BA=20=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E6=96=87=E6=A1=A3.txt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" | 1 - 1 file changed, 1 deletion(-) delete mode 100644 "\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" diff --git "a/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" "b/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" deleted file mode 100644 index 6a5b0c5..0000000 --- "a/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" +++ /dev/null @@ -1 +0,0 @@ -??? \ No newline at end of file From 3fcdf0af86ddc5b1e23f2cabbde9e471d42a3529 Mon Sep 17 00:00:00 2001 From: SOLECA794 <3451175152@qq.com> Date: Fri, 4 Oct 2024 21:49:16 +0800 Subject: [PATCH 05/13] Create README.MD --- SOLECA794/README.MD | 1 + 1 file changed, 1 insertion(+) create mode 100644 SOLECA794/README.MD diff --git a/SOLECA794/README.MD b/SOLECA794/README.MD new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/SOLECA794/README.MD @@ -0,0 +1 @@ +1 From 15528b5ad78ff9771b5e8bfe3b14e41a76856245 Mon Sep 17 00:00:00 2001 From: SOLECA794 <3451175152@qq.com> Date: Fri, 4 Oct 2024 21:49:45 +0800 Subject: [PATCH 06/13] Update README.MD --- SOLECA794/README.MD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SOLECA794/README.MD b/SOLECA794/README.MD index d00491f..8b13789 100644 --- a/SOLECA794/README.MD +++ b/SOLECA794/README.MD @@ -1 +1 @@ -1 + From 04a8538a29cde7f070c4770474edeb4af8ce4630 Mon Sep 17 00:00:00 2001 From: SOLECA794 <3451175152@qq.com> Date: Fri, 4 Oct 2024 21:56:11 +0800 Subject: [PATCH 07/13] Add files via upload --- ...273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" | 1 + 1 file changed, 1 insertion(+) create mode 100644 "SOLECA794/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" diff --git "a/SOLECA794/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" "b/SOLECA794/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" new file mode 100644 index 0000000..6a5b0c5 --- /dev/null +++ "b/SOLECA794/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" @@ -0,0 +1 @@ +??? \ No newline at end of file From 869f0cdf8554928f1e13fa1578769b9994a4ac65 Mon Sep 17 00:00:00 2001 From: SOLECA794 <3451175152@qq.com> Date: Fri, 4 Oct 2024 22:00:12 +0800 Subject: [PATCH 08/13] =?UTF-8?q?Update=20=E6=96=B0=E5=BB=BA=20=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E6=96=87=E6=A1=A3.txt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/SOLECA794/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" "b/SOLECA794/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" index 6a5b0c5..4e47973 100644 --- "a/SOLECA794/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" +++ "b/SOLECA794/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" @@ -1 +1,2 @@ -??? \ No newline at end of file +??? +这就是pr吗??? From 8add0c43f566d44d1af34f22a3e5c664aa4bfbde Mon Sep 17 00:00:00 2001 From: SOLECA794 <3451175152@qq.com> Date: Sat, 5 Oct 2024 22:04:32 +0800 Subject: [PATCH 09/13] =?UTF-8?q?Delete=20SOLECA794/=E6=96=B0=E5=BB=BA=20?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E6=96=87=E6=A1=A3.txt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 "SOLECA794/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" diff --git "a/SOLECA794/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" "b/SOLECA794/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" deleted file mode 100644 index 4e47973..0000000 --- "a/SOLECA794/\346\226\260\345\273\272 \346\226\207\346\234\254\346\226\207\346\241\243.txt" +++ /dev/null @@ -1,2 +0,0 @@ -??? -这就是pr吗??? From 3a19e2f3970334a5468158b676185bacac3adbd5 Mon Sep 17 00:00:00 2001 From: SOLECA794 <3451175152@qq.com> Date: Sat, 5 Oct 2024 22:27:09 +0800 Subject: [PATCH 10/13] Add new HTML templates for index, showmy, and signup pages --- SOLECA794/go.mod | 41 +++++ SOLECA794/main.go | 316 ++++++++++++++++++++++++++++++++ SOLECA794/templates/home.tmpl | 112 +++++++++++ SOLECA794/templates/index.tmpl | 41 +++++ SOLECA794/templates/showmy.tmpl | 45 +++++ SOLECA794/templates/signup.tmpl | 43 +++++ 6 files changed, 598 insertions(+) create mode 100644 SOLECA794/go.mod create mode 100644 SOLECA794/main.go create mode 100644 SOLECA794/templates/home.tmpl create mode 100644 SOLECA794/templates/index.tmpl create mode 100644 SOLECA794/templates/showmy.tmpl create mode 100644 SOLECA794/templates/signup.tmpl diff --git a/SOLECA794/go.mod b/SOLECA794/go.mod new file mode 100644 index 0000000..2451df2 --- /dev/null +++ b/SOLECA794/go.mod @@ -0,0 +1,41 @@ +module awesomeProject + +go 1.23.1 + +require ( + github.com/gin-gonic/gin v1.10.0 + gorm.io/driver/mysql v1.5.7 + gorm.io/gorm v1.25.12 +) + +require ( + github.com/bytedance/sonic v1.11.6 // indirect + github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.3 // 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.20.0 // indirect + github.com/go-sql-driver/mysql v1.7.0 // indirect + github.com/goccy/go-json v0.10.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.7 // 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.2 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + golang.org/x/arch v0.8.0 // indirect + golang.org/x/crypto v0.23.0 // indirect + golang.org/x/net v0.25.0 // indirect + golang.org/x/sys v0.20.0 // indirect + golang.org/x/text v0.15.0 // indirect + google.golang.org/protobuf v1.34.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/SOLECA794/main.go b/SOLECA794/main.go new file mode 100644 index 0000000..7f1e91f --- /dev/null +++ b/SOLECA794/main.go @@ -0,0 +1,316 @@ +package main + +import ( + "fmt" + "github.com/gin-gonic/gin" + "gorm.io/driver/mysql" + "gorm.io/gorm" + "io" + "net/http" + "strconv" + "time" +) + +type User struct { + ID int + Name string + Pass string + Email string + Identy int +} +type Question struct { + ID int + Content string + From string + Time time.Time +} + +var UserNow User + +func IsLog(c *gin.Context) { + if UserNow.Name == "" { + c.Redirect(http.StatusFound, "/") + c.Abort() + } else { + c.Next() + } +} +func main() { + r := gin.Default() + + dsn := "root:794ASMIN@soleca@tcp(127.0.0.1:3306)/my_database?charset=utf8mb4&parseTime=True&loc=Local" + db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) + if err != nil { + panic("connecting filed !!!") + } + db.AutoMigrate(&User{}, &Question{}) + //重置自增主键 + //db.Exec("ALTER TABLE users AUTO_INCREMENT = 1") + + //查询某一个用户的方法 + //a := db.Where("name = ? ", "SOLECA").First(&User{}) + //if a.Error == gorm.ErrRecordNotFound { + // u0 := User{Name: "SOLECA", Pass: "123123", Identy: 0} + // db.Create(&u0) + //} + + r.LoadHTMLGlob("templates/*") + r.GET("/", func(c *gin.Context) { + UserNow.Name = "" + c.HTML(http.StatusOK, "index.tmpl", nil) + }) + r.POST("/login", func(c *gin.Context) { + username := c.PostForm("username") + password := c.PostForm("password") + var userLog User + err := db.Where("name = ? ", username).First(&userLog).Error + if err == gorm.ErrRecordNotFound { + c.HTML(http.StatusOK, "index.tmpl", gin.H{ + "message": "登录失败!你还没有注册", + "message2": "没有用户?现在注册一个▼ ▼ ▼", + "v": 0, + }) + + } else { + if password != userLog.Pass { + c.HTML(http.StatusOK, "index.tmpl", gin.H{ + "message": "您输入的密码不正确", + "v": 1, + }) + + } else { + UserNow = userLog + c.Redirect(http.StatusFound, "/home") + } + } + }) + r.GET("/signup", func(c *gin.Context) { + c.HTML(http.StatusOK, "signup.tmpl", gin.H{}) + }) + r.POST("/signup", func(c *gin.Context) { + username := c.PostForm("username") + password := c.PostForm("password") + email := c.PostForm("email") + submission := c.PostForm("submission") + var userSign User + err := db.Where("name = ? ", username).First(&userSign).Error + if err == gorm.ErrRecordNotFound { + if submission == "123321" { + userSign = User{Name: username, Pass: password, Email: email, Identy: 0} + db.Create(&userSign) + //c.SetCookie("message", "管理员注册成功!", 3600, "/", "localhost", false, true) + c.Redirect(http.StatusFound, "/") + } else { + userSign = User{Name: username, Pass: password, Email: email, Identy: 1} + db.Create(&userSign) + + c.Redirect(http.StatusFound, "/") + } + } else { + c.HTML(http.StatusOK, "signup.tmpl", gin.H{ + "text": "用户名重复!请重试", + }) + } + }) + r.GET("/reset", func(c *gin.Context) { + + }) + + //---------------------------------------------------------------------------- + + r.GET("/home", IsLog, func(c *gin.Context) { + if UserNow.Name == "" { + c.Redirect(http.StatusFound, "/") + } + v := UserNow.Identy + var questions []Question + err := db.Find(&questions).Error + if err != nil { + fmt.Println("this") + c.JSON(http.StatusBadRequest, gin.H{"error": "NO QUESTIONS"}) + } + c.HTML(http.StatusOK, "home.tmpl", gin.H{ + "Name": UserNow.Name, + "V": v, + "K": 1, + "questions": questions, + }) + }) + home := r.Group("/home") + { + home.POST("/new", IsLog, func(c *gin.Context) { + new1 := c.PostForm("new") + var NewQuestion Question = Question{Content: new1, From: UserNow.Name, Time: time.Now()} + err := db.Create(&NewQuestion).Error + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "failed"}) + } + c.Redirect(http.StatusFound, "/home/all") + }) + home.GET("/manage", IsLog, func(c *gin.Context) { + if UserNow.Identy == 0 { + fmt.Println("ok") + v := UserNow.Identy + var questionss []Question + err := db.Find(&questionss).Error + if err != nil { + fmt.Println("this") + c.JSON(http.StatusBadRequest, gin.H{"error": "NO QUESTIONS"}) + } + c.HTML(http.StatusOK, "home.tmpl", gin.H{ + "Name": UserNow.Name, + "V": v, + "K": 0, + "questions": questionss, + }) + } + }) + home.GET("/all", IsLog, func(c *gin.Context) { + v := UserNow.Identy + var questions []Question + err := db.Find(&questions).Error + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "NO QUESTIONS"}) + } + c.HTML(http.StatusOK, "home.tmpl", gin.H{ + "Name": UserNow.Name, + "V": v, + "K": 1, + "questions": questions, + }) + }) + home.GET("/my", IsLog, func(c *gin.Context) { + var questions []Question + db.Where("`from` = ?", UserNow.Name).Find(&questions) + //if err != nil { + // c.JSON(http.StatusBadRequest, gin.H{"error": "NO QUESTIONS"}) + //} + c.HTML(http.StatusOK, "showmy.tmpl", gin.H{ + "questions": questions, + }) + + }) + home.POST("home/:id/answer", IsLog, func(c *gin.Context) { + + }) + home.DELETE("/delete", IsLog, func(c *gin.Context) { + body, _ := io.ReadAll(c.Request.Body) + // 将字符串转换为数值 + receive, _ := strconv.Atoi(string(body)) + db.Where("id = ?", receive).Delete(&Question{}) + }) + } + r.Run(":8080") +} + +/* + +c.Request.URL.Path="/b" +r.HandleContext(c) + + + + +//cookie重定向 +c.SetCookie("message", "注册成功!", 3600, "/", "", false, true) +c.Redirect(http.StatusFound, "/") +router.GET("/", func(c *gin.Context) { + // 获取 cookie 中的消息 + message, err := c.Cookie("message") + if err != nil { + message = "" + } + + c.HTML(http.StatusOK, "hello.tmpl", gin.H{ + "message1": message, + }) + }) + + + + +package controllers + +import ( + "net/http" + "github.com/gin-gonic/gin" + "gorm.io/gorm" + "myapp/models" + "golang.org/x/crypto/bcrypt" +) + +var db *gorm.DB + +func init() { + var err error + db, err = gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) + if err != nil { + panic("failed to connect database") + } +} + +func ShowLoginPage(c *gin.Context) { + c.HTML(http.StatusOK, "login.html", gin.H{ + "title": "Login", + }) +} + +func PerformLogin(c *gin.Context) { + var user models.User + username := c.PostForm("username") + password := c.PostForm("password") + + if err := db.Where("username = ?", username).First(&user).Error; err != nil { + c.HTML(http.StatusUnauthorized, "login.html", gin.H{ + "ErrorTitle": "Login Failed", + "ErrorMessage": "Invalid credentials provided", + }) + return + } + + if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil { + c.HTML(http.StatusUnauthorized, "login.html", gin.H{ + "ErrorTitle": "Login Failed", + "ErrorMessage": "Invalid credentials provided", + }) + return + } + + c.HTML(http.StatusOK, "login.html", gin.H{ + "title": "Login Successful", + }) +} + + + + + + + + + + {{ .title }} + + +

{{ .title }}

+ {{ if .ErrorTitle }} +

{{ .ErrorTitle }}

+

{{ .ErrorMessage }}

+ {{ end }} +
+ + +
+ + +
+ +
+ + + + + + + +*/ diff --git a/SOLECA794/templates/home.tmpl b/SOLECA794/templates/home.tmpl new file mode 100644 index 0000000..606d7ab --- /dev/null +++ b/SOLECA794/templates/home.tmpl @@ -0,0 +1,112 @@ + + + + + ☺欢迎来到杭电问答平台! + + + + + +

☺{{.Name}},欢迎来到杭电问答平台!

+
+ + +
+

/

+ +

+

查看问题:

+
+ +
+

  

+
+ +
+
+ +
+
+
+ + +

  

+
+ + +{{if .V}}
+

权限申请:

+ +{{else}}
+ 您是管理员: +
+ +
+ +
+ +
+ +{{end}} + +
+

问题列表

+ + + \ No newline at end of file diff --git a/SOLECA794/templates/index.tmpl b/SOLECA794/templates/index.tmpl new file mode 100644 index 0000000..326da56 --- /dev/null +++ b/SOLECA794/templates/index.tmpl @@ -0,0 +1,41 @@ + + + + + ☺这是一个问答平台 + + + +

登录杭电问答平台

+

未完待续☹

+

备忘:

+

*密码错误提醒*|*忘记密码*|*未注册提示*|*注册查重*|*重置密码*|*页面间身份核验*|

+

{{ .message }}

+
+ + +
+ + +
+ +
+
+

{{ .message2 }}

+ +{{$v:=.v}} +{{if $v }} +
+

忘记密码?尝试重置

+ +
+{{ end }} + +
+

No account? Try to sign up.

+ +
+ + \ No newline at end of file diff --git a/SOLECA794/templates/showmy.tmpl b/SOLECA794/templates/showmy.tmpl new file mode 100644 index 0000000..60d455d --- /dev/null +++ b/SOLECA794/templates/showmy.tmpl @@ -0,0 +1,45 @@ + + + + + 我的问题 + + + + +

问题列表

+ + + \ No newline at end of file diff --git a/SOLECA794/templates/signup.tmpl b/SOLECA794/templates/signup.tmpl new file mode 100644 index 0000000..66ade59 --- /dev/null +++ b/SOLECA794/templates/signup.tmpl @@ -0,0 +1,43 @@ + + + + + signing up.... + + + + +

用户注册

+

备注:

+

*用户名查重*|*邮箱作为识别标志*|*社区管理员注册*

+

{{ .text }}

+
+ + +

(必填)

+
+ + +

(必填)

+
+ 邮箱: + +

(必填)

+

+

      

+ +

+ + + + +
+

{{ .message }}

+ + \ No newline at end of file From 5de7e8365282bc1f1083fca1d2f9a71dca18800b Mon Sep 17 00:00:00 2001 From: SOLECA794 <3451175152@qq.com> Date: Sat, 5 Oct 2024 22:28:54 +0800 Subject: [PATCH 11/13] Delete SOLECA794/README.MD --- SOLECA794/README.MD | 1 - 1 file changed, 1 deletion(-) delete mode 100644 SOLECA794/README.MD diff --git a/SOLECA794/README.MD b/SOLECA794/README.MD deleted file mode 100644 index 8b13789..0000000 --- a/SOLECA794/README.MD +++ /dev/null @@ -1 +0,0 @@ - From c628ee26aea8c4cb8638ad9902891fafb5473288 Mon Sep 17 00:00:00 2001 From: SOLECA794 <3451175152@qq.com> Date: Sat, 5 Oct 2024 23:41:08 +0800 Subject: [PATCH 12/13] Add new file t.txt with commit message --- SOLECA794/t.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 SOLECA794/t.txt diff --git a/SOLECA794/t.txt b/SOLECA794/t.txt new file mode 100644 index 0000000..cf72489 --- /dev/null +++ b/SOLECA794/t.txt @@ -0,0 +1 @@ +哈哈哈道爷我commit了 \ No newline at end of file From f2418df8a15ee46633f5a08ea66640fce4ae379a Mon Sep 17 00:00:00 2001 From: SOLECA794 <3451175152@qq.com> Date: Mon, 7 Oct 2024 16:26:54 +0800 Subject: [PATCH 13/13] Update signup page with additional instructions for registration --- SOLECA794/main.go | 180 ++++++++++++++++++++++++---- SOLECA794/templates/change.tmpl | 42 +++++++ SOLECA794/templates/home.tmpl | 48 +++++--- SOLECA794/templates/index.tmpl | 4 +- SOLECA794/templates/reset.tmpl | 23 ++++ SOLECA794/templates/search.tmpl | 55 +++++++++ SOLECA794/templates/showanswer.tmpl | 82 +++++++++++++ SOLECA794/templates/showmy.tmpl | 22 +++- SOLECA794/templates/signup.tmpl | 2 +- SOLECA794/templates/users.tmpl | 39 ++++++ 10 files changed, 453 insertions(+), 44 deletions(-) create mode 100644 SOLECA794/templates/change.tmpl create mode 100644 SOLECA794/templates/reset.tmpl create mode 100644 SOLECA794/templates/search.tmpl create mode 100644 SOLECA794/templates/showanswer.tmpl create mode 100644 SOLECA794/templates/users.tmpl diff --git a/SOLECA794/main.go b/SOLECA794/main.go index 7f1e91f..27d7a7b 100644 --- a/SOLECA794/main.go +++ b/SOLECA794/main.go @@ -24,6 +24,13 @@ type Question struct { From string Time time.Time } +type Answer struct { + ID int + Content string + From string + Quesid int + Agree int +} var UserNow User @@ -43,16 +50,7 @@ func main() { if err != nil { panic("connecting filed !!!") } - db.AutoMigrate(&User{}, &Question{}) - //重置自增主键 - //db.Exec("ALTER TABLE users AUTO_INCREMENT = 1") - - //查询某一个用户的方法 - //a := db.Where("name = ? ", "SOLECA").First(&User{}) - //if a.Error == gorm.ErrRecordNotFound { - // u0 := User{Name: "SOLECA", Pass: "123123", Identy: 0} - // db.Create(&u0) - //} + db.AutoMigrate(&User{}, &Question{}, &Answer{}) r.LoadHTMLGlob("templates/*") r.GET("/", func(c *gin.Context) { @@ -93,8 +91,9 @@ func main() { email := c.PostForm("email") submission := c.PostForm("submission") var userSign User + er := db.Where("email = ? ", email).First(&userSign).Error err := db.Where("name = ? ", username).First(&userSign).Error - if err == gorm.ErrRecordNotFound { + if err == gorm.ErrRecordNotFound && er == gorm.ErrRecordNotFound { if submission == "123321" { userSign = User{Name: username, Pass: password, Email: email, Identy: 0} db.Create(&userSign) @@ -107,14 +106,40 @@ func main() { c.Redirect(http.StatusFound, "/") } } else { - c.HTML(http.StatusOK, "signup.tmpl", gin.H{ - "text": "用户名重复!请重试", - }) + var text string + if er != gorm.ErrRecordNotFound { + text = "该邮箱已经注册过了,请更换其他邮箱" + c.HTML(http.StatusOK, "signup.tmpl", gin.H{ + "text": text, + }) + } else { + text = "用户名已被使用,请更换其他用户名" + c.HTML(http.StatusOK, "signup.tmpl", gin.H{ + "text": text, + }) + } + } }) - r.GET("/reset", func(c *gin.Context) { + r.GET("/reset", IsLog, func(c *gin.Context) { + c.HTML(http.StatusOK, "reset.tmpl", nil) }) + r.POST("/reset", IsLog, func(c *gin.Context) { + pass := c.PostForm("pass") + email := c.PostForm("email") + if UserNow.Email != email { + c.HTML(http.StatusOK, "reset.tmpl", gin.H{ + "message": "邮箱错误!", + }) + } else { + UserNow.Pass = pass + db.Save(&UserNow) + c.HTML(http.StatusOK, "reset.tmpl", gin.H{ + "message": "修改成功!", + }) + } + }) //---------------------------------------------------------------------------- @@ -124,16 +149,17 @@ func main() { } v := UserNow.Identy var questions []Question - err := db.Find(&questions).Error - if err != nil { - fmt.Println("this") - c.JSON(http.StatusBadRequest, gin.H{"error": "NO QUESTIONS"}) - } + db.Find(&questions) + //if err != nil { + // fmt.Println("this") + // c.JSON(http.StatusBadRequest, gin.H{"error": "NO QUESTIONS"}) + //} c.HTML(http.StatusOK, "home.tmpl", gin.H{ "Name": UserNow.Name, "V": v, "K": 1, "questions": questions, + "User": UserNow.Name, }) }) home := r.Group("/home") @@ -149,7 +175,7 @@ func main() { }) home.GET("/manage", IsLog, func(c *gin.Context) { if UserNow.Identy == 0 { - fmt.Println("ok") + v := UserNow.Identy var questionss []Question err := db.Find(&questionss).Error @@ -165,6 +191,34 @@ func main() { }) } }) + home.GET("/manage/users", IsLog, func(c *gin.Context) { + if UserNow.Identy == 0 { + var users []User + err := db.Find(&users).Error + if err != nil { + fmt.Println("this") + c.JSON(http.StatusBadRequest, gin.H{"error": "NO USERS"}) + } + c.HTML(http.StatusOK, "users.tmpl", gin.H{ + "User": users, + }) + } + }) + home.POST("/search", IsLog, func(c *gin.Context) { + username := c.PostForm("username") + var Q []Question + err := db.Where("`from` = ? ", username).Find(&Q).Error + if err == gorm.ErrRecordNotFound { + c.JSON(http.StatusBadRequest, gin.H{"error": "NOT FOUND"}) + } + c.HTML(http.StatusOK, "search.tmpl", gin.H{ + "Name": username, + "Q": Q, + "U": UserNow.Name, + "I": UserNow.Identy, + }) + + }) home.GET("/all", IsLog, func(c *gin.Context) { v := UserNow.Identy var questions []Question @@ -177,6 +231,7 @@ func main() { "V": v, "K": 1, "questions": questions, + "User": UserNow.Name, }) }) home.GET("/my", IsLog, func(c *gin.Context) { @@ -190,15 +245,94 @@ func main() { }) }) - home.POST("home/:id/answer", IsLog, func(c *gin.Context) { + home.POST("/:id/answer", IsLog, func(c *gin.Context) { + QuesId := c.Param("id") + Id, _ := strconv.Atoi(QuesId) + answer := c.PostForm("answer") + + var ans Answer = Answer{Content: answer, From: UserNow.Name, Quesid: Id, Agree: 0} + db.Create(&ans) + + c.Redirect(http.StatusFound, "/home/all") + }) + home.GET("/:id/answer", IsLog, func(c *gin.Context) { + QuesId := c.Param("id") + var ans []Answer + db.Where(" quesid = ?", QuesId).Find(&ans) + var question Question + db.Where(" id = ?", QuesId).First(&question) + fro := question.From + content := question.Content + tim := question.Time + c.HTML(http.StatusOK, "showanswer.tmpl", gin.H{ + "Content": content, + "Fro": fro, + "Ans": ans, + "Time": tim, + "User": UserNow.Name, + "Identy": UserNow.Identy, + }) + }) + home.GET("/:id/put", IsLog, func(c *gin.Context) { + QuesId := c.Param("id") + + var question Question + db.Where(" id = ?", QuesId).First(&question) + c.HTML(http.StatusOK, "change.tmpl", gin.H{ + "Content": question.Content, + "Fro": question.From, + "Time": question.Time, + "ID": question.ID, + }) }) - home.DELETE("/delete", IsLog, func(c *gin.Context) { + home.POST("/:id/put", IsLog, func(c *gin.Context) { + QuesId := c.Param("id") + change := c.PostForm("change") + var question Question + db.Where(" id = ?", QuesId).First(&question) + if change != "" { + question.Content = change + } + db.Save(&question) + c.HTML(http.StatusOK, "change.tmpl", gin.H{ + "Content": question.Content, + "Fro": question.From, + "Time": question.Time, + "ID": question.ID, + "Response": "修改成功!", + }) + }) + home.DELETE("/question/delete", IsLog, func(c *gin.Context) { body, _ := io.ReadAll(c.Request.Body) // 将字符串转换为数值 receive, _ := strconv.Atoi(string(body)) db.Where("id = ?", receive).Delete(&Question{}) }) + home.DELETE("/answer/delete", IsLog, func(c *gin.Context) { + body, _ := io.ReadAll(c.Request.Body) + // 将字符串转换为数值 + receive, _ := strconv.Atoi(string(body)) + db.Where("id = ?", receive).Delete(&Answer{}) + }) + home.DELETE("/user/delete", IsLog, func(c *gin.Context) { + body, _ := io.ReadAll(c.Request.Body) + // 将字符串转换为数值 + receive, _ := strconv.Atoi(string(body)) + db.Where("id = ?", receive).Delete(&User{}) + }) + home.POST("/answer/agree", IsLog, func(c *gin.Context) { + body, _ := io.ReadAll(c.Request.Body) + receive, _ := strconv.Atoi(string(body)) + var answer Answer + db.Where("id = ?", receive).First(&answer) + answer.Agree = answer.Agree + 1 + db.Save(&answer) + id := strconv.Itoa(answer.Quesid) + id = "/home/" + id + "/answer" + c.Redirect(http.StatusFound, id) + + }) } r.Run(":8080") } diff --git a/SOLECA794/templates/change.tmpl b/SOLECA794/templates/change.tmpl new file mode 100644 index 0000000..a1e0f98 --- /dev/null +++ b/SOLECA794/templates/change.tmpl @@ -0,0 +1,42 @@ + + + + + Answers + + + + +

▶ 问题: "{{ .Content }}"

+

|  Answered by :   < {{ .Fro }} >

+
| {{ .Time }}
+
+ +
+

+
+ + + +
+

{{ .Response }}

+ + diff --git a/SOLECA794/templates/home.tmpl b/SOLECA794/templates/home.tmpl index 606d7ab..52b3c58 100644 --- a/SOLECA794/templates/home.tmpl +++ b/SOLECA794/templates/home.tmpl @@ -2,12 +2,16 @@ + ☺欢迎来到杭电问答平台! -

☺{{.Name}},欢迎来到杭电问答平台!

+

☺ {{.Name}} ,欢迎来到杭电问答平台!

@@ -51,24 +55,29 @@
+
+ + +

  

- - +
+ +
{{if .V}}

权限申请:

- + {{else}}
您是管理员:
- +
-
+
@@ -76,19 +85,28 @@

问题列表

+{{ $u:=.User }}