Templet.php 3.4 KB

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