fetdata.go 3.1 KB

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