fetdata.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. tmplFuncs := template.FuncMap{
  121. "plus": func(a, b int) int {
  122. return a + b
  123. },
  124. "minus": func(a, b int) int {
  125. return a - b
  126. },
  127. "multiply": func(a, b int) int {
  128. return a * b
  129. },
  130. "divide": func(a, b int) int {
  131. return a / b
  132. },
  133. "repeat": func(a string, n int) template.HTML {
  134. out := ""
  135. for i := 1; i <= n; i++ {
  136. out += a
  137. }
  138. return template.HTML(out)
  139. },
  140. }
  141. tmpl, err := template.New(block + ".html").Funcs(tmplFuncs).ParseFiles(
  142. this.wrap.DTemplate + string(os.PathSeparator) + block + ".html",
  143. )
  144. if err != nil {
  145. return template.HTML(err.Error())
  146. }
  147. var tpl bytes.Buffer
  148. err = tmpl.Execute(&tpl, consts.TmplData{
  149. System: utils.GetTmplSystemData("", ""),
  150. Data: this,
  151. })
  152. if err != nil {
  153. return template.HTML(err.Error())
  154. }
  155. return template.HTML(string(tpl.Bytes()))
  156. }
  157. func (this *FERData) CachedBlock1() template.HTML {
  158. return this.cachedBlock("cached-block-1")
  159. // return template.HTML("")
  160. }
  161. func (this *FERData) CachedBlock2() template.HTML {
  162. return this.cachedBlock("cached-block-2")
  163. // return template.HTML("")
  164. }
  165. func (this *FERData) CachedBlock3() template.HTML {
  166. return this.cachedBlock("cached-block-3")
  167. // return template.HTML("")
  168. }
  169. func (this *FERData) CachedBlock4() template.HTML {
  170. return this.cachedBlock("cached-block-4")
  171. // return template.HTML("")
  172. }
  173. func (this *FERData) CachedBlock5() template.HTML {
  174. return this.cachedBlock("cached-block-5")
  175. // return template.HTML("")
  176. }