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