seo.model.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * SEO
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class seoModel extends Model{
  11. /**
  12. * 存放SEO信息
  13. *
  14. * @var obj
  15. */
  16. private $seo;
  17. public function __construct(){
  18. parent::__construct('seo');
  19. }
  20. /**
  21. * 取得SEO信息
  22. *
  23. * @param array/string $type
  24. * @return obj
  25. */
  26. public function type($type){
  27. if (is_array($type)){ //商品分类
  28. $this->seo['title'] = $type[1];
  29. $this->seo['keywords'] = $type[2];
  30. $this->seo['description'] = $type[3];
  31. }else{
  32. $this->seo = $this->getSeo($type);
  33. }
  34. if (!is_array($this->seo)) return $this;
  35. foreach ($this->seo as $key=>$value) {
  36. $this->seo[$key] = str_replace(['{sitename}'], [C('site_name')],$value);
  37. }
  38. return $this;
  39. }
  40. /**
  41. * 生成SEO缓存并返回
  42. *
  43. * @param string $type
  44. * @return array
  45. */
  46. private function getSeo($type){
  47. $list = rkcache('seo',true);
  48. return $list[$type];
  49. }
  50. /**
  51. * 传入参数替换SEO中的标签
  52. *
  53. * @param array $array
  54. * @return obj
  55. */
  56. public function param($array = null)
  57. {
  58. if (!is_array($this->seo)) return $this;
  59. if (is_array($array)){
  60. $array_key = array_keys($array);
  61. array_walk($array_key, [__CLASS__,'addTag']);
  62. foreach ($this->seo as $key=>$value) {
  63. $this->seo[$key] = str_replace($array_key,array_values($array),$value);
  64. }
  65. }
  66. return $this;
  67. }
  68. /**
  69. * 抛出SEO信息到模板
  70. *
  71. */
  72. public function show(){
  73. $this->seo['title'] = preg_replace("/{.*}/siU",'',$this->seo['title']);
  74. $this->seo['keywords'] = preg_replace("/{.*}/siU",'',$this->seo['keywords']);
  75. $this->seo['description'] = preg_replace("/{.*}/siU",'',$this->seo['description']);
  76. Tpl::output('html_title',$this->seo['title'] ? $this->seo['title'] : C('site_name'));
  77. Tpl::output('seo_keywords',$this->seo['keywords'] ? $this->seo['keywords'] : C('site_name'));
  78. Tpl::output('seo_description',$this->seo['description'] ? $this->seo['description'] : C('site_name'));
  79. }
  80. private function addTag(&$key){
  81. $key ='{'.$key.'}';
  82. }
  83. }