fetdata.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package fetdata
  2. import (
  3. "bytes"
  4. "html"
  5. "html/template"
  6. "os"
  7. "time"
  8. "golang-fave/consts"
  9. "golang-fave/engine/wrapper"
  10. "golang-fave/utils"
  11. )
  12. type FERData struct {
  13. wrap *wrapper.Wrapper
  14. is404 bool
  15. Page *Page
  16. Blog *Blog
  17. Shop *Shop
  18. }
  19. func New(wrap *wrapper.Wrapper, is404 bool, drow interface{}, duser *utils.MySql_user) *FERData {
  20. var d_Page *Page
  21. var d_Blog *Blog
  22. var d_Shop *Shop
  23. var preUser *User
  24. if duser != nil {
  25. preUser = &User{wrap: wrap, object: duser}
  26. }
  27. if wrap.CurrModule == "index" {
  28. if o, ok := drow.(*utils.MySql_page); ok {
  29. d_Page = &Page{wrap: wrap, object: o, user: preUser}
  30. }
  31. } else if wrap.CurrModule == "blog" {
  32. if len(wrap.UrlArgs) == 3 && wrap.UrlArgs[0] == "blog" && wrap.UrlArgs[1] == "category" && wrap.UrlArgs[2] != "" {
  33. if o, ok := drow.(*utils.MySql_blog_category); ok {
  34. d_Blog = &Blog{wrap: wrap, category: (&BlogCategory{wrap: wrap, object: o, user: preUser}).load(nil)}
  35. d_Blog.load()
  36. }
  37. } else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "blog" && wrap.UrlArgs[1] != "" {
  38. if o, ok := drow.(*utils.MySql_blog_post); ok {
  39. d_Blog = &Blog{wrap: wrap, post: (&BlogPost{wrap: wrap, object: o, user: preUser}).load()}
  40. }
  41. } else {
  42. d_Blog = &Blog{wrap: wrap}
  43. d_Blog.load()
  44. }
  45. } else if wrap.CurrModule == "shop" {
  46. if len(wrap.UrlArgs) == 3 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] == "category" && wrap.UrlArgs[2] != "" {
  47. if o, ok := drow.(*utils.MySql_shop_category); ok {
  48. d_Shop = &Shop{wrap: wrap, category: (&ShopCategory{wrap: wrap, object: o, user: preUser}).load(nil)}
  49. d_Shop.load()
  50. }
  51. } else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] != "" {
  52. if o, ok := drow.(*utils.MySql_shop_product); ok {
  53. d_Shop = &Shop{wrap: wrap, product: (&ShopProduct{wrap: wrap, object: o, user: preUser}).load()}
  54. }
  55. } else {
  56. d_Shop = &Shop{wrap: wrap}
  57. d_Shop.load()
  58. }
  59. }
  60. if d_Blog == nil {
  61. d_Blog = &Blog{wrap: wrap}
  62. }
  63. if d_Shop == nil {
  64. d_Shop = &Shop{wrap: wrap}
  65. }
  66. fer := &FERData{
  67. wrap: wrap,
  68. is404: is404,
  69. Page: d_Page,
  70. Blog: d_Blog,
  71. Shop: d_Shop,
  72. }
  73. return fer
  74. }
  75. func (this *FERData) RequestURI() string {
  76. return this.wrap.R.RequestURI
  77. }
  78. func (this *FERData) RequestURL() string {
  79. return this.wrap.R.URL.Path
  80. }
  81. func (this *FERData) RequestGET() string {
  82. return utils.ExtractGetParams(this.wrap.R.RequestURI)
  83. }
  84. func (this *FERData) Module() string {
  85. if this.is404 {
  86. return "404"
  87. }
  88. var mod string
  89. if this.wrap.CurrModule == "index" {
  90. mod = "index"
  91. } else if this.wrap.CurrModule == "blog" {
  92. if len(this.wrap.UrlArgs) == 3 && this.wrap.UrlArgs[0] == "blog" && this.wrap.UrlArgs[1] == "category" && this.wrap.UrlArgs[2] != "" {
  93. mod = "blog-category"
  94. } else if len(this.wrap.UrlArgs) == 2 && this.wrap.UrlArgs[0] == "blog" && this.wrap.UrlArgs[1] != "" {
  95. mod = "blog-post"
  96. } else {
  97. mod = "blog"
  98. }
  99. } else if this.wrap.CurrModule == "shop" {
  100. if len(this.wrap.UrlArgs) == 3 && this.wrap.UrlArgs[0] == "shop" && this.wrap.UrlArgs[1] == "category" && this.wrap.UrlArgs[2] != "" {
  101. mod = "shop-category"
  102. } else if len(this.wrap.UrlArgs) == 2 && this.wrap.UrlArgs[0] == "shop" && this.wrap.UrlArgs[1] != "" {
  103. mod = "shop-product"
  104. } else {
  105. mod = "shop"
  106. }
  107. }
  108. return mod
  109. }
  110. func (this *FERData) DateTimeUnix() int {
  111. return int(time.Now().Unix())
  112. }
  113. func (this *FERData) DateTimeFormat(format string) string {
  114. return time.Unix(int64(time.Now().Unix()), 0).Format(format)
  115. }
  116. func (this *FERData) EscapeString(str string) string {
  117. return html.EscapeString(str)
  118. }
  119. func (this *FERData) cachedBlock(block string) template.HTML {
  120. tmpl, err := template.New(block + ".html").Funcs(utils.TemplateAdditionalFuncs()).ParseFiles(
  121. this.wrap.DTemplate + string(os.PathSeparator) + block + ".html",
  122. )
  123. if err != nil {
  124. return template.HTML(err.Error())
  125. }
  126. var tpl bytes.Buffer
  127. err = tmpl.Execute(&tpl, consts.TmplData{
  128. System: utils.GetTmplSystemData("", ""),
  129. Data: this,
  130. })
  131. if err != nil {
  132. return template.HTML(err.Error())
  133. }
  134. return template.HTML(string(tpl.Bytes()))
  135. }
  136. func (this *FERData) CachedBlock1() template.HTML {
  137. if data, ok := this.wrap.GetBlock1(); ok {
  138. return data
  139. }
  140. data := this.cachedBlock("cached-block-1")
  141. this.wrap.SetBlock1(data)
  142. return data
  143. }
  144. func (this *FERData) CachedBlock2() template.HTML {
  145. if data, ok := this.wrap.GetBlock2(); ok {
  146. return data
  147. }
  148. data := this.cachedBlock("cached-block-2")
  149. this.wrap.SetBlock2(data)
  150. return data
  151. }
  152. func (this *FERData) CachedBlock3() template.HTML {
  153. if data, ok := this.wrap.GetBlock3(); ok {
  154. return data
  155. }
  156. data := this.cachedBlock("cached-block-3")
  157. this.wrap.SetBlock3(data)
  158. return data
  159. }
  160. func (this *FERData) CachedBlock4() template.HTML {
  161. if data, ok := this.wrap.GetBlock4(); ok {
  162. return data
  163. }
  164. data := this.cachedBlock("cached-block-4")
  165. this.wrap.SetBlock4(data)
  166. return data
  167. }
  168. func (this *FERData) CachedBlock5() template.HTML {
  169. if data, ok := this.wrap.GetBlock5(); ok {
  170. return data
  171. }
  172. data := this.cachedBlock("cached-block-5")
  173. this.wrap.SetBlock5(data)
  174. return data
  175. }