Article.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * lemocms
  4. * ============================================================================
  5. * 版权所有 2018-2027 lemocms,并保留所有权利。
  6. * 网站地址: https://www.lemocms.com
  7. * ----------------------------------------------------------------------------
  8. * 采用最新Thinkphp6实现
  9. * ============================================================================
  10. * Author: yuege
  11. * Date: 2019/8/26
  12. */
  13. namespace app\admin\controller;
  14. use app\common\controller\Backend;
  15. use app\common\model\ArticleCate;
  16. use think\facade\Db;
  17. use think\facade\Lang;
  18. use think\facade\Request;
  19. use think\facade\View;
  20. use lemo\helper\TreeHelper;
  21. class Article extends Backend {
  22. public function initialize()
  23. {
  24. parent::initialize(); // TODO: Change the autogenerated stub
  25. }
  26. public function index(){
  27. if(Request::isPost()){
  28. $keys = Request::post('keys','','trim');
  29. $page = Request::post('page') ? Request::post('page') : 1;
  30. $list = Db::name('article')->alias('a')
  31. ->join('article_cate ac','a.pid = ac.id','left')
  32. ->field('a.*,ac.title as cate_name')
  33. ->where('a.title|a.content','like','%'.$keys.'%')
  34. ->order('a.sort desc,a.id desc')
  35. ->paginate(['list_rows' => $this->pageSize, 'page' => $page])
  36. ->toArray();
  37. return $result = ['code'=>0,'msg'=>lang('get info success'),'data'=>$list['data'],'count'=>$list['total']];
  38. }
  39. return View::fetch();
  40. }
  41. public function add(){
  42. if(Request::isPost()) {
  43. $data = Request::post();
  44. $res = \app\common\model\Article::create($data);
  45. if ($res) {
  46. $this->success(lang('add success'));
  47. } else {
  48. $this->error(lang('add fail'));
  49. }
  50. }else{
  51. $ArticleCate = ArticleCate::where('status',1)->select()->toArray();
  52. $ArticleCate= TreeHelper::cateTree($ArticleCate);
  53. $params['name'] = 'container';
  54. $params['content'] = '';
  55. $view = [
  56. 'info' => '',
  57. 'ArticleCate' => $ArticleCate,
  58. 'title' => lang('add'),
  59. 'ueditor'=>build_ueditor($params),
  60. ];
  61. View::assign($view);
  62. return View::fetch('add');
  63. }
  64. }
  65. public function edit()
  66. {
  67. if(Request::isPost()){
  68. $data = Request::post();
  69. if(!$data['id']){
  70. $this->error(lang('invalid data'));
  71. }
  72. $res = \app\common\model\Article::update($data);
  73. if($res){
  74. $this->success(lang('edit success'));
  75. }else{
  76. $this->error(lang('edit fail'));
  77. }
  78. }else{
  79. $id = Request::get('id');
  80. $ArticleCate = ArticleCate::where('status',1)->select()->toArray();
  81. $ArticleCate= TreeHelper::cateTree($ArticleCate);
  82. $info = \app\common\model\Article::find($id);
  83. $params['name'] = 'container';
  84. $params['content'] = $info['content'];
  85. $view = [
  86. 'info' => $info,
  87. 'ArticleCate' => $ArticleCate,
  88. 'title' => lang('edit'),
  89. 'ueditor'=>build_ueditor($params),
  90. ];
  91. View::assign($view);
  92. return View::fetch('add');
  93. }
  94. }
  95. public function state()
  96. {
  97. $id = Request::post('id');
  98. if (empty($id)) {
  99. $this->error('data not exist');
  100. }
  101. $info = \app\common\model\Article::find($id);
  102. $status = $info['status'] == 1 ? 0 : 1;
  103. $info->status = $status;
  104. $info->save();
  105. $this->success(lang('edit success'));
  106. }
  107. public function delete(){
  108. if(Request::isPost()){
  109. $id = Request::post('id');
  110. \app\common\model\Article::destroy($id);
  111. $this->success('delete success');
  112. }
  113. }
  114. public function articleCate(){
  115. if(Request::isPost()){
  116. $keys = Request::post('keys','','trim');
  117. $page = Request::post('page') ? Request::post('page') : 1;
  118. $list= cache('articleCate');
  119. if(!$list) {
  120. $list = Db::name('article_cate')
  121. ->where('title','like','%'.$keys.'%')
  122. ->paginate(['list_rows' => $this->pageSize, 'page' => $page])
  123. ->toArray();
  124. foreach($list['data'] as $k=>$v){
  125. $list['data'][$k]['lay_is_open']=false;
  126. }
  127. cache('articleCate', $list, 3600);
  128. }
  129. return $result = ['code'=>0,'msg'=>lang('get info success'),'data'=>$list['data'],'count'=>$list['total']];
  130. }
  131. return View::fetch();
  132. }
  133. public function cateAdd(){
  134. if(Request::isPost()) {
  135. $data = Request::post();
  136. $res = \app\common\model\ArticleCate::create($data);
  137. if ($res) {
  138. $this->success(lang('add success'));
  139. } else {
  140. $this->error(lang('add fail'));
  141. }
  142. }else{
  143. $ArticleCate = ArticleCate::where('status',1)->select()->toArray();
  144. $ArticleCate= TreeHelper::cateTree($ArticleCate);
  145. $view = [
  146. 'info' => '',
  147. 'ArticleCate' => $ArticleCate,
  148. 'title' =>lang('add'),
  149. ];
  150. View::assign($view);
  151. return View::fetch('cate_add');
  152. }
  153. }
  154. public function cateEdit(){
  155. if(Request::isPost()){
  156. $data = Request::post();
  157. if(!$data['id']){
  158. $this->error(lang('invalid data'));
  159. }
  160. $res = \app\common\model\ArticleCate::update($data);
  161. if($res){
  162. $this->success(lang('edit success'));
  163. }else{
  164. $this->error(lang('edit fail'));
  165. }
  166. }else{
  167. $id = Request::get('id');
  168. $ArticleCate = ArticleCate::where('status',1)->select()->toArray();
  169. $ArticleCate= TreeHelper::cateTree($ArticleCate);
  170. $info = \app\common\model\ArticleCate::find($id);
  171. $view = [
  172. 'info' => $info,
  173. 'ArticleCate' => $ArticleCate,
  174. 'title' => lang('edit'),
  175. ];
  176. View::assign($view);
  177. return View::fetch('cate_add');
  178. }
  179. }
  180. public function cateState(){
  181. $data = Request::post();
  182. $id = Request::post('id');
  183. if (empty($id)) {
  184. $this->error(lang('data not exist'));
  185. }
  186. $info = \app\common\model\ArticleCate::find($id);
  187. $status = $info['status'] == 1 ? 0 : 1;
  188. $info->status = $status;
  189. $info->save();
  190. $this->success(lang('edit success'));
  191. }
  192. public function cateDel(){
  193. if(Request::isPost()){
  194. $id = Request::post('id');
  195. $child = \app\common\model\ArticleCate::where('pid',$id)->find();
  196. if($child){
  197. $this->error(lang('delete child first'));
  198. }
  199. if(\app\common\model\ArticleCate::destroy($id)){
  200. $this->success(lang('delete success'));
  201. }else{
  202. $this->error(lang('delete fail'));
  203. }
  204. }
  205. }
  206. }