Templet.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\admin\controller;
  9. use app\admin\controller\base\Permissions;
  10. use app\common\model\Templet as tpModel;
  11. use app\common\service\UploadService;
  12. use file\FileHelper;
  13. use paginate\PaginateHelper;
  14. class Templet extends Permissions
  15. {
  16. public function index()
  17. {
  18. if ($this->request->isAjax()) {
  19. $name = $this->request->param('name');
  20. $data = tpModel::getTemplets(trim($name));
  21. $total = count($data);
  22. //分页
  23. $helper = new PaginateHelper($total, $this->request->param('limit'), $this->request->param('page'));
  24. $start = $helper->start() - 1;
  25. $end = $helper->end() - 1;
  26. foreach ($data as $k => $v) {
  27. if ($k > $end || $k < $start)
  28. unset($data[$k]);
  29. }
  30. return array('code' => 0, 'data' => $data, 'count' => $total);
  31. } else {
  32. $this->assign('tpPath', tpModel::getTempletDir());
  33. return $this->fetch();
  34. }
  35. }
  36. public function publish()
  37. {
  38. $rootpath = tpModel::getTempletDir();
  39. $post = $this->request->post();
  40. $filename = trim($this->request->param('filename'));//文件名
  41. $name = $this->request->param('name');//新增模板名
  42. if ($filename) {
  43. //修改操作
  44. $filePath = $rootpath . $filename . ".html";
  45. if ($this->request->isPost()) {
  46. $getfilemtime = $this->request->param('filemtime');
  47. if ($getfilemtime != filemtime($filePath)) {
  48. $this->error('文件版本不正确,请刷新后重新提交');
  49. }
  50. try {
  51. FileHelper::save($filePath, $post['content']);
  52. } catch (\Exception $e) {
  53. $this->error('修改失败.' . $e->getMessage());
  54. }
  55. $this->success('修改成功', '', filemtime($filePath));
  56. } else {
  57. if (file_exists($filePath)) {
  58. $this->assign('filename', $filename);
  59. $this->assign('filemtime', filemtime($filePath));
  60. $this->assign('content', FileHelper::read($filePath));
  61. return $this->fetch();
  62. } else {
  63. $this->error('模板格式不正确');
  64. }
  65. }
  66. } else {
  67. //新增操作
  68. if ($this->request->isPost()) {
  69. $validate = new \think\Validate([
  70. ['name|模板名称', 'require|alphaDash'],
  71. ]);
  72. if (!$validate->check($post)) {
  73. $this->error('提交失败:' . $validate->getError());
  74. }
  75. $filePath = $rootpath . $name . ".html";
  76. if (file_exists($filePath)) {
  77. $this->error('该文件已存在,请修改名称');
  78. } else {
  79. try {
  80. FileHelper::save($filePath, $post['content']);
  81. } catch (\Exception $e) {
  82. $this->error('添加失败.' . $e->getMessage());
  83. }
  84. }
  85. $this->success('添加成功', 'index');
  86. } else {
  87. return $this->fetch();
  88. }
  89. }
  90. }
  91. public function delete()
  92. {
  93. if ($this->request->isAjax()) {
  94. $fullname = $this->request->param('fullname');
  95. $rootpath = tpModel::getTempletDir();
  96. if (false == unlink($rootpath . $fullname)) {
  97. $this->error('删除失败');
  98. } else {
  99. $this->success('删除成功', 'index');
  100. }
  101. }
  102. }
  103. public function rename()
  104. {
  105. if ($this->request->isAjax()) {
  106. $oldname = $this->request->param('oldname');
  107. $newname = $this->request->param('newname');
  108. $rootpath = tpModel::getTempletDir();
  109. if (file_exists($rootpath . $newname)) {
  110. $this->error('文件已存在');
  111. }
  112. if (FileHelper::rename($rootpath . $oldname, $rootpath . $newname)) {
  113. (new \app\common\model\Catalog())->save(['catalog_templet' => $newname], ['catalog_templet' => $oldname]);
  114. (new \app\common\model\Catalog())->save(['article_templet' => $newname], ['article_templet' => $oldname]);
  115. $this->success('重命名成功', 'index');
  116. } else {
  117. $this->error('重命名失败');
  118. }
  119. }
  120. }
  121. public function upload()
  122. {
  123. $rootpath = tpModel::getTempletDir();
  124. (new UploadService())->upload2path($rootpath);
  125. }
  126. }