Article.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 控制器
  11. */
  12. class Article extends AdminControl
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/article.lang.php');
  18. }
  19. public function index()
  20. {
  21. /**
  22. * 检索条件
  23. */
  24. $condition = array();
  25. $search_ac_id = intval(input('param.search_ac_id'));
  26. if ($search_ac_id) {
  27. $condition[] = array('ac_id', '=', $search_ac_id);
  28. }
  29. $search_title = trim(input('param.search_title'));
  30. if ($search_title) {
  31. $condition[] = array('article_title', 'like', "%" . $search_title . "%");
  32. }
  33. $article_model = model('article');
  34. $article_list = $article_model->getArticleList($condition, 10);
  35. $articleclass_model = model('articleclass');
  36. /**
  37. * 整理列表内容
  38. */
  39. if (is_array($article_list)) {
  40. /**
  41. * 取文章分类
  42. */
  43. $class_list = $articleclass_model->getArticleclassList(array());
  44. $tmp_class_name = array();
  45. if (is_array($class_list)) {
  46. foreach ($class_list as $k => $v) {
  47. $tmp_class_name[$v['ac_id']] = $v['ac_name'];
  48. }
  49. }
  50. foreach ($article_list as $k => $v) {
  51. /**
  52. * 发布时间
  53. */
  54. $article_list[$k]['article_time'] = date('Y-m-d H:i:s', $v['article_time']);
  55. /**
  56. * 所属分类
  57. */
  58. if (@array_key_exists($v['ac_id'], $tmp_class_name)) {
  59. $article_list[$k]['ac_name'] = $tmp_class_name[$v['ac_id']];
  60. }
  61. }
  62. }
  63. /**
  64. * 分类列表
  65. */
  66. $parent_list = $articleclass_model->getTreeClassList(2);
  67. if (is_array($parent_list)) {
  68. $unset_sign = false;
  69. foreach ($parent_list as $k => $v) {
  70. $parent_list[$k]['ac_name'] = str_repeat("&nbsp;", $v['deep'] * 2) . $v['ac_name'];
  71. }
  72. }
  73. View::assign('article_list', $article_list);
  74. View::assign('show_page', $article_model->page_info->render());
  75. View::assign('search_title', $search_title);
  76. View::assign('search_ac_id', $search_ac_id);
  77. View::assign('parent_list', $parent_list);
  78. View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
  79. $this->setAdminCurItem('index');
  80. return View::fetch();
  81. }
  82. public function add()
  83. {
  84. if (!(request()->isPost())) {
  85. $article = [
  86. 'article_id' => 0,
  87. 'article_title' => '',
  88. 'ac_id' => input('param.ac_id'),
  89. 'article_url' => '',
  90. 'article_show' => 0,
  91. 'article_sort' => 0,
  92. 'article_content' => '',
  93. ];
  94. $articleclass_model = model('articleclass');
  95. $cate_list = $articleclass_model->getTreeClassList(2);
  96. View::assign('ac_list', $cate_list);
  97. View::assign('article', $article);
  98. //游离图片
  99. $article_pic_list = model('upload')->getUploadList(array('upload_type' => '1', 'item_id' => 0));
  100. View::assign('file_upload', $article_pic_list);
  101. $this->setAdminCurItem('add');
  102. return View::fetch('form');
  103. } else {
  104. $data = array(
  105. 'article_title' => input('post.article_title'),
  106. 'ac_id' => input('post.ac_id'),
  107. 'article_url' => input('post.article_url'),
  108. 'article_sort' => input('post.article_sort'),
  109. 'article_content' => input('post.article_content'),
  110. 'article_time' => TIMESTAMP,
  111. );
  112. $data['article_show'] = intval(input('post.article_show'));
  113. $article_validate = ds_validate('article');
  114. if (!$article_validate->scene('add')->check($data)) {
  115. $this->error($article_validate->getError());
  116. }
  117. $article_id = model('article')->addArticle($data);
  118. if ($article_id) {
  119. //更新图片信息ID
  120. $upload_model = model('upload');
  121. $file_id_array = input('post.file_id/a');
  122. if (is_array($file_id_array)) {
  123. foreach ($file_id_array as $k => $v) {
  124. $update_array = array();
  125. $update_array['item_id'] = $article_id;
  126. $upload_model->editUpload($update_array, array(array('upload_id', '=', intval($v))));
  127. unset($update_array);
  128. }
  129. }
  130. //上传文章封面
  131. if (!empty($_FILES['_pic']['name'])) {
  132. $res = ds_upload_pic(ATTACH_ARTICLE, '_pic');
  133. if ($res['code']) {
  134. $article_pic = $res['data']['file_name'];
  135. model('article')->editArticle(array('article_pic' => $article_pic), $article_id);
  136. } else {
  137. $this->error($res['msg'], (string) url('Article/edit', ['article_id' => $article_id]));
  138. }
  139. }
  140. $this->success(lang('ds_common_save_succ'), 'Article/index');
  141. } else {
  142. $this->error(lang('ds_common_save_fail'));
  143. }
  144. }
  145. }
  146. public function edit()
  147. {
  148. $art_id = intval(input('param.article_id'));
  149. if ($art_id <= 0) {
  150. $this->error(lang('param_error'));
  151. }
  152. $condition = array();
  153. $condition[] = array('article_id', '=', $art_id);
  154. $article = model('article')->getOneArticle($condition);
  155. if (!$article) {
  156. $this->error(lang('ds_no_record'));
  157. }
  158. if (!request()->isPost()) {
  159. View::assign('article', $article);
  160. $articleclass_model = model('articleclass');
  161. $cate_list = $articleclass_model->getTreeClassList(2);
  162. View::assign('ac_list', $cate_list);
  163. //附属图片
  164. $article_pic_list = model('upload')->getUploadList(array('upload_type' => '1', 'item_id' => $art_id));
  165. View::assign('file_upload', $article_pic_list);
  166. $this->setAdminCurItem('edit');
  167. return View::fetch('form');
  168. } else {
  169. $data = array(
  170. 'article_title' => input('post.article_title'),
  171. 'ac_id' => input('post.ac_id'),
  172. 'article_url' => input('post.article_url'),
  173. 'article_sort' => input('post.article_sort'),
  174. 'article_content' => input('post.article_content'),
  175. 'article_time' => TIMESTAMP,
  176. );
  177. $data['article_show'] = intval(input('post.article_show'));
  178. $article_validate = ds_validate('article');
  179. if (!$article_validate->scene('edit')->check($data)) {
  180. $this->error($article_validate->getError());
  181. }
  182. //上传文章封面
  183. if (!empty($_FILES['_pic']['name'])) {
  184. $res = ds_upload_pic(ATTACH_ARTICLE, '_pic');
  185. if ($res['code']) {
  186. $file_name = $res['data']['file_name'];
  187. //删除原图
  188. if ($article['article_pic']) {
  189. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_ARTICLE . DIRECTORY_SEPARATOR . $article['article_pic']);
  190. }
  191. $data['article_pic'] = $file_name;
  192. } else {
  193. $this->error($res['msg'], (string)url('Article/edit', ['article_id' => $art_id]));
  194. }
  195. }
  196. //验证数据 END
  197. $result = model('article')->editArticle($data, $art_id);
  198. if ($result) {
  199. $this->success(lang('ds_common_save_succ'), 'Article/index');
  200. } else {
  201. $this->error(lang('ds_common_save_fail'));
  202. }
  203. }
  204. }
  205. public function drop()
  206. {
  207. $article_id = input('param.article_id');
  208. if (empty($article_id)) {
  209. ds_json_encode(10001, lang('param_error'));
  210. }
  211. $condition = array();
  212. $condition[] = array('article_id', '=', $article_id);
  213. $article = model('article')->getOneArticle($condition);
  214. if (!$article) {
  215. ds_json_encode(10001, lang('ds_no_record'));
  216. }
  217. //删除图片
  218. if ($article['article_pic']) {
  219. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_ARTICLE . DIRECTORY_SEPARATOR . $article['article_pic']);
  220. }
  221. $article_pic_list = model('upload')->getUploadList(array('upload_type' => '1', 'item_id' => $article_id));
  222. foreach ($article_pic_list as $article_pic) {
  223. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_ARTICLE . DIRECTORY_SEPARATOR . $article_pic['file_name']);
  224. }
  225. $result = model('article')->delArticle($article_id);
  226. if ($result) {
  227. ds_json_encode(10000, lang('ds_common_op_succ'));
  228. } else {
  229. ds_json_encode(10001, lang('error'));
  230. }
  231. }
  232. /**
  233. * 文章图片上传
  234. */
  235. public function article_pic_upload()
  236. {
  237. $file_name = '';
  238. $file_object = request()->file('fileupload');
  239. if ($file_object) {
  240. $res = ds_upload_pic(ATTACH_ARTICLE, 'fileupload');
  241. if ($res['code']) {
  242. $file_name = $res['data']['file_name'];
  243. } else {
  244. echo $res['msg'];
  245. exit;
  246. }
  247. } else {
  248. echo 'error';
  249. exit;
  250. }
  251. /**
  252. * 模型实例化
  253. */
  254. $upload_model = model('upload');
  255. /**
  256. * 图片数据入库
  257. */
  258. $insert_array = array();
  259. $insert_array['file_name'] = $file_name;
  260. $insert_array['upload_type'] = '1';
  261. $insert_array['file_size'] = $_FILES['fileupload']['size'];
  262. $insert_array['item_id'] = intval(input('param.item_id'));
  263. $insert_array['upload_time'] = TIMESTAMP;
  264. $result = $upload_model->addUpload($insert_array);
  265. if ($result) {
  266. $data = array();
  267. $data['file_id'] = $result;
  268. $data['file_name'] = $file_name;
  269. $data['file_path'] = ds_get_pic(ATTACH_ARTICLE, $file_name);
  270. /**
  271. * 整理为json格式
  272. */
  273. $output = json_encode($data);
  274. echo $output;
  275. }
  276. }
  277. /**
  278. * ajax操作
  279. */
  280. public function ajax()
  281. {
  282. switch (input('param.branch')) {
  283. /**
  284. * 删除文章图片
  285. */
  286. case 'del_file_upload':
  287. if (intval(input('param.file_id')) > 0) {
  288. $upload_model = model('upload');
  289. /**
  290. * 删除图片
  291. */
  292. $file_array = $upload_model->getOneUpload(intval(input('param.file_id')));
  293. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_ARTICLE . DIRECTORY_SEPARATOR . $file_array['file_name']);
  294. /**
  295. * 删除信息
  296. */
  297. $condition = array();
  298. $condition[] = array('upload_id', '=', intval(input('param.file_id')));
  299. $upload_model->delUpload($condition);
  300. echo 'true';
  301. exit;
  302. } else {
  303. echo 'false';
  304. exit;
  305. }
  306. break;
  307. }
  308. }
  309. /**
  310. * 获取卖家栏目列表,针对控制器下的栏目
  311. */
  312. protected function getAdminItemList()
  313. {
  314. $menu_array = array(
  315. array(
  316. 'name' => 'index',
  317. 'text' => lang('ds_manage'),
  318. 'url' => (string)url('Article/index')
  319. ),
  320. );
  321. if (request()->action() == 'add' || request()->action() == 'index') {
  322. $menu_array[] = array(
  323. 'name' => 'add',
  324. 'text' => lang('ds_new'),
  325. 'url' => (string)url('Article/add')
  326. );
  327. }
  328. if (request()->action() == 'edit') {
  329. $menu_array[] = array(
  330. 'name' => 'edit',
  331. 'text' => lang('ds_edit'),
  332. 'url' => 'javascript:void(0)'
  333. );
  334. }
  335. return $menu_array;
  336. }
  337. }