fetdata.go 3.5 KB

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