Templet.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. namespace app\common\model;
  9. use file\FileHelper;
  10. class Templet
  11. {
  12. //模板目录
  13. const DEFAULT_TEMPLET_PATH = ROOT_PATH . "app" . DS . 'common' . DS . 'view' . DS . 'template' . DS;
  14. //上传文件目录
  15. const UPLOAD_PATH = ROOT_PATH . "public" . DS . "uploads" . DS;
  16. //默认HTML目录
  17. const DEFAULT_STATIC_PATH = self::UPLOAD_PATH . 'q1464674022' . DS;
  18. private $name;
  19. /**
  20. * 设置文件名,并返回模板对象
  21. * @param $name
  22. * @return $this
  23. * @throws \Exception
  24. */
  25. public function setName($name)
  26. {
  27. $this->name = trim($name);
  28. $filepath = $this->getRealPath();
  29. if (!file_exists($filepath)) {
  30. throw new \Exception($filepath . '模板文件不存在');
  31. }
  32. return $this;
  33. }
  34. /**
  35. * 模板文件路径
  36. * @return string
  37. */
  38. public function getRealPath()
  39. {
  40. return self::getTempletDir() . $this->name . '.html';
  41. }
  42. /**
  43. * 读取模板内容
  44. * @return mixed
  45. */
  46. public function getContent()
  47. {
  48. return FileHelper::read($this->getRealPath());
  49. }
  50. /**
  51. * 静态化目录,生成静态页的根目录 ,需要写权限
  52. * @return string
  53. */
  54. public static function getRootDir()
  55. {
  56. $path = Webconfig::getValue('article_static_path');
  57. return empty($path) ? self::DEFAULT_STATIC_PATH : appendEndDS($path);
  58. }
  59. /**
  60. * 模板目录
  61. * @return string
  62. */
  63. public static function getTempletDir()
  64. {
  65. $path = Webconfig::getValue('article_templet_path');
  66. return empty($path) ? self::DEFAULT_TEMPLET_PATH : appendEndDS($path);
  67. }
  68. /**
  69. * 获取所有模板信息
  70. * @param string $name [过滤文件名]
  71. * @return array
  72. */
  73. public static function getTemplets($name = "")
  74. {
  75. //定义要查看的目录
  76. $rootpath = self::getTempletDir();
  77. $files = [];
  78. if (!file_exists($rootpath)) {
  79. return $files;
  80. }
  81. //遍历目录
  82. $handle = opendir($rootpath);
  83. while (false !== ($f = readdir($handle))) {
  84. //过滤点
  85. if ($f == "." || $f == "..") {
  86. continue;
  87. }
  88. //拼路径
  89. $filepath = $rootpath . $f;
  90. //防止中文乱码
  91. // $filename = iconv("gb2312", "utf-8", $f);
  92. $filename = $f;
  93. $info = pathinfo($filepath);
  94. $ext = $info['extension']??'';
  95. $filename = str_replace("." . $ext, "", $filename);
  96. //过滤查询
  97. if (isset($name) and !empty($name)) {
  98. if (!ifContain($filename, $name)) {
  99. continue;
  100. }
  101. }
  102. //过滤文件夹
  103. if (filetype($filepath) == "dir") {
  104. continue;
  105. }
  106. $files[] = [
  107. "filename" => $filename,//文件名
  108. "fileext" => $ext,//扩展名
  109. "filetype" => filetype($filepath),//文件or文件夹
  110. "filesize" => filesize($filepath),
  111. "create_time" => date('Y-m-d H:i:s', filectime($filepath)),
  112. "update_time" => date('Y-m-d H:i:s', filemtime($filepath)),
  113. ];
  114. }
  115. IS_WIN ? asort($files) : sort($files);
  116. return $files;
  117. }
  118. }