Seo.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\common\model;
  3. /**
  4. *
  5. *
  6. * ----------------------------------------------------------------------------
  7. *
  8. * 数据层模型
  9. */
  10. class Seo extends BaseModel
  11. {
  12. /**
  13. * 存放SEO信息
  14. * @access private
  15. * @author csdeshang
  16. * @var obj
  17. */
  18. private $seo;
  19. /**
  20. * 取得SEO信息
  21. * @access public
  22. * @author csdeshang
  23. * @param array/string $type 类型
  24. * @return obj
  25. */
  26. public function type($type)
  27. {
  28. if (is_array($type)) { //商品分类
  29. $this->seo['seo_title'] = isset($type[1]) ? $type[1] : '';
  30. $this->seo['seo_keywords'] = isset($type[2]) ? $type[2] : '';
  31. $this->seo['seo_description'] = isset($type[3]) ? $type[3] : '';
  32. } else {
  33. $this->seo = $this->getSeo($type);
  34. }
  35. if (!is_array($this->seo))
  36. return $this;
  37. foreach ($this->seo as $key => $value) {
  38. $this->seo[$key] = str_replace(array('{sitename}'), array(config('ds_config.site_name')), $value);
  39. }
  40. return $this;
  41. }
  42. /**
  43. * 生成SEO缓存并返回
  44. * @access private
  45. * @author csdeshang
  46. * @param string $type 类型
  47. * @return array
  48. */
  49. private function getSeo($type)
  50. {
  51. $list = rkcache('seo', true);
  52. return $list[$type];
  53. }
  54. /**
  55. * 传入参数替换SEO中的标签
  56. * @access public
  57. * @author csdeshang
  58. * @param array $array 参数数组
  59. * @return obj
  60. */
  61. public function param($array = null)
  62. {
  63. if (!is_array($this->seo))
  64. return $this;
  65. if (is_array($array)) {
  66. $array_key = array_keys($array);
  67. foreach ($array_key as $k => $val) {
  68. $array_key[$k] = '{' . $val . '}';
  69. }
  70. foreach ($this->seo as $key => $value) {
  71. $this->seo[$key] = str_replace($array_key, array_values($array), $value);
  72. }
  73. }
  74. return $this;
  75. }
  76. /**
  77. * 抛出SEO信息到模板
  78. * @access public
  79. * @author csdeshang
  80. * @return type
  81. */
  82. public function show()
  83. {
  84. $this->seo['seo_title'] = preg_replace("/{.*}/siU", '', $this->seo['seo_title']);
  85. $this->seo['seo_keywords'] = preg_replace("/{.*}/siU", '', $this->seo['seo_keywords']);
  86. $this->seo['seo_description'] = preg_replace("/{.*}/siU", '', $this->seo['seo_description']);
  87. return array(
  88. 'html_title' => $this->seo['seo_title'] ? $this->seo['seo_title'] : config('ds_config.site_name'),
  89. 'seo_keywords' => $this->seo['seo_keywords'] ? $this->seo['seo_keywords'] : config('ds_config.site_name'),
  90. 'seo_description' => $this->seo['seo_description'] ? $this->seo['seo_description'] : config('ds_config.site_name'),
  91. );
  92. }
  93. }