Seo.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace app\common\model;
  3. /**
  4. * ============================================================================
  5. *
  6. * ============================================================================
  7. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  8. * 网站地址: https://www.valimart.net/
  9. * ----------------------------------------------------------------------------
  10. *
  11. * ============================================================================
  12. * 数据层模型
  13. */
  14. class Seo extends BaseModel {
  15. /**
  16. * 存放SEO信息
  17. * @access private
  18. * @author csdeshang
  19. * @var obj
  20. */
  21. private $seo;
  22. /**
  23. * 取得SEO信息
  24. * @access public
  25. * @author csdeshang
  26. * @param array/string $type 类型
  27. * @return obj
  28. */
  29. public function type($type) {
  30. if (is_array($type)) { //商品分类
  31. $this->seo['seo_title'] = isset($type[1])?$type[1]:'';
  32. $this->seo['seo_keywords'] = isset($type[2])?$type[2]:'';
  33. $this->seo['seo_description'] = isset($type[3])?$type[3]:'';
  34. } else {
  35. $this->seo = $this->getSeo($type);
  36. }
  37. if (!is_array($this->seo))
  38. return $this;
  39. foreach ($this->seo as $key => $value) {
  40. $this->seo[$key] = str_replace(array('{sitename}'), array(config('ds_config.site_name')), $value);
  41. }
  42. return $this;
  43. }
  44. /**
  45. * 生成SEO缓存并返回
  46. * @access private
  47. * @author csdeshang
  48. * @param string $type 类型
  49. * @return array
  50. */
  51. private function getSeo($type) {
  52. $list = rkcache('seo', true);
  53. return $list[$type];
  54. }
  55. /**
  56. * 传入参数替换SEO中的标签
  57. * @access public
  58. * @author csdeshang
  59. * @param array $array 参数数组
  60. * @return obj
  61. */
  62. public function param($array = null) {
  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. $this->seo['seo_title'] = preg_replace("/{.*}/siU", '', $this->seo['seo_title']);
  84. $this->seo['seo_keywords'] = preg_replace("/{.*}/siU", '', $this->seo['seo_keywords']);
  85. $this->seo['seo_description'] = preg_replace("/{.*}/siU", '', $this->seo['seo_description']);
  86. return array(
  87. 'html_title' => $this->seo['seo_title'] ? $this->seo['seo_title'] : config('ds_config.site_name'),
  88. 'seo_keywords' => $this->seo['seo_keywords'] ? $this->seo['seo_keywords'] : config('ds_config.site_name'),
  89. 'seo_description' => $this->seo['seo_description'] ? $this->seo['seo_description'] : config('ds_config.site_name'),
  90. );
  91. }
  92. }
  93. ?>