方式1:Go 标准库 “html/template”, template.ParseFiles()
方式2:“github.com/gin-gonic/gin”,gin.HTML()
直接加载 login/inex.html
Handler.go
// GetIndex
// @Tags 首页
// @contact.name Allan
// @Success 200 {string} 首页
// @Router /index [get]
func (app *Config) GetIndex(c *gin.Context) {idx, err := template.ParseFiles("login/index.html")if err != nil {panic(err)}idx.Execute(c.Writer, "首页")
}
login/index.html
登录
c.HTML中的name必须与引用的tmpl文件内define定义的一致,直接使用chat/index.html不会加载内容。
Handler.go
// Chat
// @Tags 聊天
// @Description 聊天室
// @param userId query string false "用户ID"
// @param token query string false "Token"
// @contact.name Allan
// @Success 200 {string} success
// @Router /Chat [get]
func (app *Config) ChatRoom(c *gin.Context) {session:= sessions.Default(c)userId, _ := strconv.Atoi(c.Query("userId"))token := c.Query("token")if token != session.Get("loginToken") {c.JSON(-1, gin.H{"code": -1,"message": "鉴权失败!",})return}user := models.User{}user.ID = uint(userId)user.Identity = tokenc.HTML(http.StatusOK, "/chat/index.shtml", gin.H{"code": 0,"message": "success",})
}
chat/index.html
{{define "/chat/index.shtml"}}
{{template "/chat/head.shtml"}}
{{template "/chat/concat.shtml"}}{{template "/chat/group.shtml"}}{{template "/chat/profile.shtml"}}{{template "/chat/main.shtml"}}{{template "/chat/tabmenu.shtml"}}
{{template "/chat/foot.shtml"}}
{{end}}
上一篇:0311记录
下一篇:智能优化算法之蚁群算法