Seo.php 3.1 KB

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