Seo.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace app\common\model;
  3. /**
  4. * ============================================================================
  5. * DSMall多用户商城
  6. * ============================================================================
  7. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  8. * 网站地址: http://www.csdeshang.com
  9. * ----------------------------------------------------------------------------
  10. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  11. * 不允许对程序代码以任何形式任何目的的再发布。
  12. * ============================================================================
  13. * 数据层模型
  14. */
  15. class Seo extends BaseModel {
  16. /**
  17. * 存放SEO信息
  18. * @access private
  19. * @author csdeshang
  20. * @var obj
  21. */
  22. private $seo;
  23. /**
  24. * 取得SEO信息
  25. * @access public
  26. * @author csdeshang
  27. * @param array/string $type 类型
  28. * @return obj
  29. */
  30. public function type($type) {
  31. if (is_array($type)) { //商品分类
  32. $this->seo['seo_title'] = isset($type[1])?$type[1]:'';
  33. $this->seo['seo_keywords'] = isset($type[2])?$type[2]:'';
  34. $this->seo['seo_description'] = isset($type[3])?$type[3]:'';
  35. } else {
  36. $this->seo = $this->getSeo($type);
  37. }
  38. if (!is_array($this->seo))
  39. return $this;
  40. foreach ($this->seo as $key => $value) {
  41. $this->seo[$key] = str_replace(array('{sitename}'), array(config('ds_config.site_name')), $value);
  42. }
  43. return $this;
  44. }
  45. /**
  46. * 生成SEO缓存并返回
  47. * @access private
  48. * @author csdeshang
  49. * @param string $type 类型
  50. * @return array
  51. */
  52. private function getSeo($type) {
  53. $list = rkcache('seo', true);
  54. return $list[$type];
  55. }
  56. /**
  57. * 传入参数替换SEO中的标签
  58. * @access public
  59. * @author csdeshang
  60. * @param array $array 参数数组
  61. * @return obj
  62. */
  63. public function param($array = null) {
  64. if (!is_array($this->seo))
  65. return $this;
  66. if (is_array($array)) {
  67. $array_key = array_keys($array);
  68. foreach ($array_key as $k=>$val){
  69. $array_key[$k]='{'.$val.'}';
  70. }
  71. foreach ($this->seo as $key => $value) {
  72. $this->seo[$key] = str_replace($array_key, array_values($array), $value);
  73. }
  74. }
  75. return $this;
  76. }
  77. /**
  78. * 抛出SEO信息到模板
  79. * @access public
  80. * @author csdeshang
  81. * @return type
  82. */
  83. public function show() {
  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. }
  94. ?>