fetdata.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package fetdata
  2. import (
  3. "html"
  4. "time"
  5. "golang-fave/engine/wrapper"
  6. "golang-fave/utils"
  7. )
  8. type FERData struct {
  9. wrap *wrapper.Wrapper
  10. is404 bool
  11. Page *Page
  12. Blog *Blog
  13. Shop *Shop
  14. }
  15. func New(wrap *wrapper.Wrapper, drow interface{}, is404 bool) *FERData {
  16. var d_Page *Page
  17. var d_Blog *Blog
  18. var d_Shop *Shop
  19. if wrap.CurrModule == "index" {
  20. if o, ok := drow.(*utils.MySql_page); ok {
  21. d_Page = &Page{wrap: wrap, object: o}
  22. }
  23. } else if wrap.CurrModule == "blog" {
  24. if len(wrap.UrlArgs) == 3 && wrap.UrlArgs[0] == "blog" && wrap.UrlArgs[1] == "category" && wrap.UrlArgs[2] != "" {
  25. if o, ok := drow.(*utils.MySql_blog_category); ok {
  26. d_Blog = &Blog{wrap: wrap, category: (&BlogCategory{wrap: wrap, object: o}).load()}
  27. d_Blog.load()
  28. }
  29. } else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "blog" && wrap.UrlArgs[1] != "" {
  30. if o, ok := drow.(*utils.MySql_blog_post); ok {
  31. d_Blog = &Blog{wrap: wrap, post: (&BlogPost{wrap: wrap, object: o}).load()}
  32. }
  33. } else {
  34. d_Blog = &Blog{wrap: wrap}
  35. d_Blog.load()
  36. }
  37. } else if wrap.CurrModule == "shop" {
  38. if len(wrap.UrlArgs) == 3 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] == "category" && wrap.UrlArgs[2] != "" {
  39. if o, ok := drow.(*utils.MySql_shop_category); ok {
  40. d_Shop = &Shop{wrap: wrap, category: (&ShopCategory{wrap: wrap, object: o}).load()}
  41. d_Shop.load()
  42. }
  43. } else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] != "" {
  44. if o, ok := drow.(*utils.MySql_shop_product); ok {
  45. d_Shop = &Shop{wrap: wrap, product: (&ShopProduct{wrap: wrap, object: o}).load()}
  46. }
  47. } else {
  48. d_Shop = &Shop{wrap: wrap}
  49. d_Shop.load()
  50. }
  51. }
  52. if d_Blog == nil {
  53. d_Blog = &Blog{wrap: wrap}
  54. }
  55. if d_Shop == nil {
  56. d_Shop = &Shop{wrap: wrap}
  57. }
  58. fer := &FERData{
  59. wrap: wrap,
  60. is404: is404,
  61. Page: d_Page,
  62. Blog: d_Blog,
  63. Shop: d_Shop,
  64. }
  65. return fer
  66. }
  67. func (this *FERData) RequestURI() string {
  68. return this.wrap.R.RequestURI
  69. }
  70. func (this *FERData) RequestURL() string {
  71. return this.wrap.R.URL.Path
  72. }
  73. func (this *FERData) RequestGET() string {
  74. return utils.ExtractGetParams(this.wrap.R.RequestURI)
  75. }
  76. func (this *FERData) Module() string {
  77. if this.is404 {
  78. return "404"
  79. }
  80. var mod string
  81. if this.wrap.CurrModule == "index" {
  82. mod = "index"
  83. } else if this.wrap.CurrModule == "blog" {
  84. if len(this.wrap.UrlArgs) == 3 && this.wrap.UrlArgs[0] == "blog" && this.wrap.UrlArgs[1] == "category" && this.wrap.UrlArgs[2] != "" {
  85. mod = "blog-category"
  86. } else if len(this.wrap.UrlArgs) == 2 && this.wrap.UrlArgs[0] == "blog" && this.wrap.UrlArgs[1] != "" {
  87. mod = "blog-post"
  88. } else {
  89. mod = "blog"
  90. }
  91. } else if this.wrap.CurrModule == "shop" {
  92. if len(this.wrap.UrlArgs) == 3 && this.wrap.UrlArgs[0] == "shop" && this.wrap.UrlArgs[1] == "category" && this.wrap.UrlArgs[2] != "" {
  93. mod = "shop-category"
  94. } else if len(this.wrap.UrlArgs) == 2 && this.wrap.UrlArgs[0] == "shop" && this.wrap.UrlArgs[1] != "" {
  95. mod = "shop-product"
  96. } else {
  97. mod = "shop"
  98. }
  99. }
  100. return mod
  101. }
  102. func (this *FERData) DateTimeUnix() int {
  103. return int(time.Now().Unix())
  104. }
  105. func (this *FERData) DateTimeFormat(format string) string {
  106. return time.Unix(int64(time.Now().Unix()), 0).Format(format)
  107. }
  108. func (this *FERData) EscapeString(str string) string {
  109. return html.EscapeString(str)
  110. }