Article.php 13 KB

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