Article.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2022/11/25
  6. * Time: 15:10
  7. */
  8. namespace app\admin\controller;
  9. use app\admin\controller\base\Permissions;
  10. use app\common\model\Article as articleModel;
  11. use app\common\model\ArticleCate as articleCateModel;
  12. use app\common\model\ArticleCate as cateModel;
  13. use app\common\model\Attachment as attachmentModel;
  14. use app\common\model\Catalog as cataLogModel;
  15. use app\common\model\Templet as templetModel;
  16. use app\common\service\CmsService;
  17. use file\FileHelper;
  18. use think\Db;
  19. use think\Exception;
  20. use think\Log;
  21. use think\Session;
  22. class Article extends Permissions
  23. {
  24. public function index()
  25. {
  26. $cateModel = new cateModel();
  27. $catalogModel = new cataLogModel();
  28. if ($this->request->isAjax()) {
  29. $post = $this->request->param();
  30. $where = [];
  31. if (isset($post['id']) and !empty($post['id'])) {
  32. $where['id'] = $post['id'];
  33. }
  34. if (isset($post['keywords']) and !empty($post['keywords'])) {
  35. $where['title'] = ['like', '%' . $post['keywords'] . '%'];
  36. }
  37. if (isset($post['article_cate_id']) and $post['article_cate_id'] != 0) {
  38. if ($post['article_cate_id'] == -1) {
  39. $where['article_cate_id'] = 0;
  40. } else {
  41. $where['article_cate_id'] = ['in', $cateModel->getChildsIdByPid($post['article_cate_id'])];
  42. }
  43. }
  44. if (isset($post['catalog_id']) and $post['catalog_id'] > 0) {
  45. $where['catalog_id'] = $post['catalog_id'];
  46. }
  47. if (isset($post['admin_id']) and $post['admin_id'] > 0) {
  48. $where['admin_id'] = $post['admin_id'];
  49. }
  50. if (isset($post['status']) and ($post['status'] == 1 or $post['status'] === '0')) {
  51. $where['status'] = $post['status'];
  52. }
  53. if (isset($post['istop_time']) and ($post['istop_time'] == 1 or $post['istop_time'] === '0')) {
  54. $where['istop_time'] = $post['istop_time'] == 1 ? ['<>', 0] : 0;
  55. }
  56. if (isset($post['create_time']) and !empty($post['create_time'])) {
  57. $timerang = explode(' - ', $post['create_time']);
  58. $min_time = strtotime($timerang[0]);
  59. $max_time = strtotime($timerang[1]);
  60. $where['create_time'] = [['>=', $min_time], ['<=', $max_time]];
  61. }
  62. $model = new articleModel();
  63. $count = $model->where($where)->count();
  64. $data = $model->where($where)->page($post['page']??0, $post['limit']??15)->order('create_time desc')->select();
  65. $cateIdAndName = $cateModel->column('title', 'id');
  66. $cateLogIdAndName = $catalogModel->column('title', 'id');
  67. $adminModel = new \app\admin\model\Admin();
  68. $adminIdAndName = $adminModel->column('nickname', 'id');
  69. foreach ($data as $key => $value) {
  70. $value['editor_name'] = $adminIdAndName[$value['edit_admin_id']]??"";
  71. $value['admin_name'] = $adminIdAndName[$value['admin_id']]??"";
  72. $value['article_cate'] = $cateIdAndName[$value['article_cate_id']]??"";
  73. $value['catalog'] = $cateLogIdAndName[$value['catalog_id']]??"";
  74. $value['thumb_url'] = geturl($value['thumb']);
  75. $data[$key] = $value;
  76. }
  77. return array('code' => 0, 'count' => $count, 'data' => $data);
  78. } else {
  79. //创建人
  80. $this->assign('admins', Db::name('admin')->select());
  81. //分类
  82. $this->assign('cates', $cateModel->treelist2());
  83. //栏目
  84. $this->assign('catalogs', $catalogModel->treelistForArticle());
  85. return $this->fetch();
  86. }
  87. }
  88. public function publish()
  89. {
  90. $id = $this->request->param('id', 0, 'intval');
  91. $model = new articleModel();
  92. $cateModel = new cateModel();
  93. $catalogModel = new cataLogModel();
  94. $web_config = Db::name('webconfig')->where('id', 1)->find();
  95. $post = $this->request->post();
  96. if ($this->request->isPost()) {
  97. $validate = new \think\Validate([
  98. ['title', 'require|max:50', '标题不能为空'],
  99. // ['article_cate_id', 'require', '请选择分类'],
  100. // ['content', 'require', '文章内容不能为空'],
  101. ['seo_title|SEO标题', 'max:50'],
  102. ['seo_keyword|SEO关键词', 'max:255'],
  103. ['seo_description|SEO描述', 'max:255'],
  104. ]);
  105. if (!$validate->check($post)) {
  106. $this->error('提交失败:' . $validate->getError());
  107. }
  108. if (!empty($post['article_cate_id']) && !empty($post['catalog_id'])) {
  109. $this->error('不能同时发布到,栏目和分类');
  110. }
  111. $post['uploadip'] = $this->request->ip();
  112. } else {
  113. $this->assign('web_config', $web_config);
  114. //分类
  115. $this->assign('cates', $cateModel->treelist2());
  116. //栏目
  117. $this->assign('catalogs', $catalogModel->treelistForArticle());
  118. }
  119. if ($id > 0) {
  120. $article = $model->where('id', $id)->find();
  121. if (empty($article)) {
  122. $this->error('id不正确');
  123. }
  124. if ($this->request->isPost()) {
  125. $post['edit_admin_id'] = Session::get(self::ADMIN_ID);
  126. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  127. $this->error('修改失败');
  128. } else {
  129. $this->success('修改成功');
  130. }
  131. } else {
  132. $this->assign('article', $article);
  133. $this->assign('thumb', $article->thumb);
  134. //切换编辑器
  135. $article_editor = $article->article_editor ?: $web_config['article_editor'];
  136. $this->switchEditor($article_editor, $id);
  137. return $this->fetch();
  138. }
  139. } else {
  140. //新增
  141. if ($this->request->isPost()) {
  142. $post['admin_id'] = Session::get(self::ADMIN_ID);
  143. $post['edit_admin_id'] = $post['admin_id'];
  144. if (false == $model->allowField(true)->save($post)) {
  145. $this->error('添加失败');
  146. } else {
  147. $this->success('添加成功', 'index');
  148. }
  149. } else {
  150. //随机获取封面图
  151. $attachment = new attachmentModel();
  152. $imgid = $attachment->where(['use' => ['like', '%article%'], 'fileext' => ['in', ['jpg', 'png', 'gif']], 'status' => attachmentModel::STATUS_OPEN])->orderRaw('rand()')->limit(1)->value('id');
  153. $this->assign('thumb', $imgid);
  154. //切换编辑器
  155. $article_editor = $web_config['article_editor'];
  156. $this->switchEditor($article_editor, 0);
  157. return $this->fetch();
  158. }
  159. }
  160. }
  161. private function switchEditor($article_editor, $id = 0)
  162. {
  163. if ($this->request->has('switchEditor')) {
  164. $switchEditor = $this->request->param("switchEditor");
  165. $editors = ["wangEditor", "tinymce", "ueditor", "markdown"];
  166. $index = array_search($switchEditor, $editors);//获取索引
  167. $next = ($index == count($editors) - 1) ? 0 : $index + 1;
  168. $article_editor = $editors[$next];
  169. }
  170. $this->assign('article_editor', $article_editor);
  171. $this->assign('switch_editor_url', "/admin/article/publish?switchEditor={$article_editor}&id={$id}");
  172. }
  173. private function deleteFile($id)
  174. {
  175. try {
  176. //先删除html文件
  177. $article = articleModel::get($id);
  178. if ($article->catalog_id) {
  179. $catalog = $article->catalog;
  180. $filepath = $article->getRulePath($catalog->getRealPath(), $catalog->article_rule);
  181. if (file_exists($filepath)) {
  182. unlink($filepath);//删除
  183. }
  184. } else {
  185. $catalogs = $article->catalogs();
  186. foreach ($catalogs as $catalog) {
  187. $filepath = $article->getRulePath($catalog->getRealPath(), $catalog->article_rule);
  188. if (file_exists($filepath)) {
  189. unlink($filepath);//删除
  190. }
  191. }
  192. }
  193. } catch (Exception $e) {
  194. }
  195. }
  196. public function delete()
  197. {
  198. if ($this->request->isAjax()) {
  199. $id = $this->request->param('id', 0, 'intval');
  200. $this->deleteFile($id);
  201. if (false == Db::name('article')->where('id', $id)->delete()) {
  202. $this->error('删除失败');
  203. } else {
  204. $this->success('删除成功', 'index');
  205. }
  206. }
  207. }
  208. public function deletes()
  209. {
  210. if ($this->request->isAjax()) {
  211. $post = $this->request->param();
  212. $ids = $post['ids'];
  213. foreach ($ids as $id) {
  214. $this->deleteFile($id);
  215. }
  216. $model = new articleModel();
  217. if ($model->where('id', 'in', $ids)->delete()) {
  218. Log::log("批量删除文章:" . implode(',', $ids));
  219. $this->success('删除成功');
  220. }
  221. }
  222. }
  223. public function istop_time()
  224. {
  225. if ($this->request->isPost()) {
  226. $post = $this->request->post();
  227. $status = $post['status'] == 1 ? time() : 0;
  228. if (false == Db::name('article')->where('id', $post['id'])->update(['istop_time' => $status])) {
  229. $this->error('设置失败');
  230. } else {
  231. $this->success('设置成功', 'index');
  232. }
  233. }
  234. }
  235. public function status()
  236. {
  237. if ($this->request->isPost()) {
  238. $post = $this->request->post();
  239. if (false == Db::name('article')->where('id', $post['id'])->update(['status' => $post['status']])) {
  240. $this->error('设置失败');
  241. } else {
  242. $this->success('设置成功', 'index');
  243. }
  244. }
  245. }
  246. /**
  247. * 预览
  248. * @return mixed
  249. */
  250. public function perview()
  251. {
  252. $id = $this->request->param('id', 0, 'intval');
  253. if ($id) {
  254. $article = articleModel::get($id);
  255. return (new CmsService())->perviewArticle($article);
  256. }
  257. $this->error('预览失败:id is null');
  258. }
  259. /**
  260. * 重新生成html
  261. */
  262. public function createFile()
  263. {
  264. $post = $this->request->param();
  265. $ids = $post['ids'];
  266. if ($ids) {
  267. $articles = (new articleModel())->where('id', 'in', $ids)->select();
  268. if (empty($articles)) {
  269. $this->error('articles is null');
  270. }
  271. $service = new CmsService();
  272. foreach ($articles as $article) {
  273. try {
  274. //全局对象
  275. $this->assign([
  276. 'cataLogModel' => new cataLogModel(),
  277. 'articleCateModel' => new articleCateModel(),
  278. 'articleModel' => new articleModel()
  279. ]);
  280. /** @var articleModel $article */
  281. /** @var cataLogModel $catalog */
  282. if ($article->catalog_id) {
  283. $catalog = $article->catalog;
  284. $filepath = $article->getRulePath($catalog->getRealPath(), $catalog->article_rule);
  285. $content = $this->fetch($catalog->getArticleTemplet()->getRealPath(), ['catalog' => $catalog, 'article' => $article, 'articleCate' => null]);
  286. if (ifContain($filepath, templetModel::getRootDir())) {
  287. $service->info(" ├─ 文章:" . $filepath);
  288. FileHelper::save($filepath, $content);
  289. }
  290. } else {
  291. $catalogs = $article->catalogs();
  292. $cate = $article->cate;
  293. foreach ($catalogs as $catalog) {
  294. $filepath = $article->getRulePath($catalog->getRealPath(), $catalog->article_rule);
  295. $content = $this->fetch($catalog->getArticleTemplet()->getRealPath(), ['catalog' => $catalog, 'article' => $article, 'articleCate' => $cate]);
  296. if (ifContain($filepath, templetModel::getRootDir())) {
  297. $service->info(" ├─ 文章:" . $filepath);
  298. FileHelper::save($filepath, $content);
  299. }
  300. }
  301. }
  302. } catch (\Exception $e) {
  303. $this->error('生成失败:' . $e->getMessage());
  304. }
  305. }
  306. $this->success('执行成功');
  307. }
  308. $this->error('ids is null');
  309. }
  310. }