common.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package tmpl
  2. import (
  3. "git.wanbits.cc/sin/flytalk/config"
  4. "git.wanbits.cc/sin/flytalk/models"
  5. "git.wanbits.cc/sin/flytalk/tools"
  6. "github.com/gin-gonic/gin"
  7. "html"
  8. "html/template"
  9. "net/http"
  10. )
  11. type CommonHtml struct {
  12. Header template.HTML
  13. Nav template.HTML
  14. Left template.HTML
  15. Bottom template.HTML
  16. Rw http.ResponseWriter
  17. }
  18. func NewRender(rw http.ResponseWriter) *CommonHtml {
  19. obj := new(CommonHtml)
  20. obj.Rw = rw
  21. header := tools.FileGetContent("html/header.html")
  22. nav := tools.FileGetContent("html/nav.html")
  23. obj.Header = template.HTML(header)
  24. obj.Nav = template.HTML(nav)
  25. return obj
  26. }
  27. func (obj *CommonHtml) SetLeft(file string) {
  28. leftStr := tools.FileGetContent("html/" + file + ".html")
  29. obj.Left = template.HTML(leftStr)
  30. }
  31. func (obj *CommonHtml) SetBottom(file string) {
  32. str := tools.FileGetContent("html/" + file + ".html")
  33. obj.Bottom = template.HTML(str)
  34. }
  35. func (obj *CommonHtml) Display(file string, data interface{}) {
  36. if data == nil {
  37. data = obj
  38. }
  39. main := tools.FileGetContent("html/" + file + ".html")
  40. t, _ := template.New(file).Parse(main)
  41. t.Execute(obj.Rw, data)
  42. }
  43. //首页
  44. func PageIndex(c *gin.Context) {
  45. if c.Request.RequestURI == "/favicon.ico" {
  46. return
  47. }
  48. lang, _ := c.Get("lang")
  49. language := config.CreateLanguage(lang.(string))
  50. about := models.FindAboutByPageLanguage("index", lang.(string))
  51. cssJs := html.UnescapeString(about.CssJs)
  52. title := about.TitleCn
  53. keywords := about.KeywordsCn
  54. desc := html.UnescapeString(about.DescCn)
  55. content := html.UnescapeString(about.HtmlCn)
  56. if lang == "en" {
  57. title = about.TitleEn
  58. keywords = about.KeywordsEn
  59. desc = html.UnescapeString(about.DescEn)
  60. content = html.UnescapeString(about.HtmlEn)
  61. }
  62. c.HTML(http.StatusOK, "index.html", gin.H{
  63. "OnlineChat": language.IndexOnlineChat,
  64. "Notice": language.Notice,
  65. "NowAsk": language.NowAsk,
  66. "LaterAsk": language.LaterAsk,
  67. "Lang": lang,
  68. "Title": title,
  69. "Keywords": keywords,
  70. "Desc": desc,
  71. "Content": template.HTML(content),
  72. "CssJs": template.HTML(cssJs),
  73. })
  74. }
  75. //登陆界面
  76. func PageMain(c *gin.Context) {
  77. nav := tools.FileGetContent("html/nav.html")
  78. c.HTML(http.StatusOK, "main.html", gin.H{
  79. "Nav": template.HTML(nav),
  80. })
  81. }
  82. //客服界面
  83. func PageChatMain(c *gin.Context) {
  84. c.HTML(http.StatusOK, "chat_main.html", nil)
  85. }