package controllers
import (
"encoding/json"
"fmt"
_"net/http"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
article "./article"
login "./login"
jwt "./token"
)
type Message struct {
name string
}
func HomeGet(c *gin.Context) {
// isLogin:=GetSession(c)
// fmt.Println("",isLogin)
// tag:=c.Query("tag")
// page,_:=strconv.Atoi(c.Query("page"))
// // 这里是为了如果是复制链接过来,要给他显示请求的内容
// if len(tag)>0 &&page>=1{
// fmt.Println("两个都要",page,tag)
// }else if len(tag)>0 && page<=0{
// fmt.Println("只有tag",tag)
// }else if len(tag)<=0 && page>=1{
// fmt.Println("只有page",page)
// }else{
// fmt.Println("返回默认首页")
// }
c.HTML(200, "index.html", gin.H{
})
}
func Register(c *gin.Context) {
// 返回html
c.HTML(200, "register.html", gin.H{
"title": "注册页面",
},
)
}
// 注册路由
func RegisterPost(c *gin.Context) {
login.Login(c)
}
// 没有匹配到路由
func NoResponse(c *gin.Context) {
c.JSON(404, gin.H{
"status": 404,
"error": "这里还没有开发出来",
})
// 需要加日志
}
// 登陆页面
func LoginGet(c *gin.Context) {
c.HTML(200, "login.html", gin.H{
"title": "登陆",
})
}
func LoginPOST(c *gin.Context) {
username := c.PostForm("username")
password := c.PostForm("password")
fmt.Println(username, password)
if username != "1" {
// 添加session
session := sessions.Default(c)
session.Set("loginUser", username)
// sessionValue:=session.Get("loginUser")
// 把这个session和名字联系在一起
session.Save()
c.JSON(200, gin.H{
"code": 200,
"message": session,
})
return
} else {
c.JSON(200, gin.H{
"code": 404,
"message": "登陆失败",
})
return
}
}
// 检查是否登陆
func GetSession(c *gin.Context) bool {
session := sessions.Default(c)
loginUser := session.Get("loginUser")
fmt.Println(session, loginUser)
if loginUser != nil {
return true
} else {
return false
}
}
// 退出
func Exit(c *gin.Context) {
fmt.Print("开始退出了")
session := sessions.Default(c)
session.Delete("loginUser")
session.Save()
fmt.Println("delete session", session.Get("loginUser"))
// c.Redirect(http.StatusMovedPermanently, "/")
c.JSON(200, gin.H{
"code": 404,
"message": session,
})
}
// 写文章:必须登陆
func Write(c *gin.Context) {
c.HTML(200, "write.html", "")
}
// 提交文章
func PostWrite(c *gin.Context) {
article.Add(c)
}
// 删除文章
func Delete(c *gin.Context) {
title := c.PostForm("title")
article.Delete(title)
}
type articles struct {
Data []article.Article
}
// 首页获取一些文章
func GetArticles(c *gin.Context) {
re, ok := article.QueryArticles()
var ree articles = articles{re}
if ok == nil {
result, err := json.Marshal(ree)
if err == nil {
c.JSON(200, gin.H{
"code": 200,
"data": string(result),
})
} else {
c.JSON(200, gin.H{
"code": 404,
"data": "",
})
}
}
}
func GetArticle(c *gin.Context) {
id := c.Param("id")
re, ok := article.QueryArticle(id)
if ok {
fmt.Println(re, "找到了")
// j,err:=json.Marshal(re)
// if err!=nil{
// fmt.Println(err,"转换发生了报错")
// }else{
// c.JSON(200,gin.H{
// "code":"200",
// "data":string(j),
// // 还需要转换下为字符串,先在切换完的是切片[]byte
// })
// }
// 这里是返回json的操作
// 返回页面模板并且添加数据的操作
c.HTML(200, "article.html", gin.H{
"Title": re.Title,
"Content": re.Content,
})
} else {
c.JSON(200, gin.H{
"code": "404",
"data": "没有找到",
})
}
}
func GetToken(c *gin.Context) {
jwt.GetToken(c)
}
其他
1 .简单的函数都直接写在了controller.go里面
2 .其实还可以细分,和文章有关的写在article里面
3 .