Article.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. * ============================================================================
  7. * DSMall多用户商城
  8. * ============================================================================
  9. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  10. * 网站地址: http://www.csdeshang.com
  11. * ----------------------------------------------------------------------------
  12. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  13. * 不允许对程序代码以任何形式任何目的的再发布。
  14. * ============================================================================
  15. * 控制器
  16. */
  17. class Article extends AdminControl {
  18. public function initialize() {
  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. $condition = array();
  27. $search_ac_id = intval(input('param.search_ac_id'));
  28. if ($search_ac_id) {
  29. $condition[]=array('ac_id','=',$search_ac_id);
  30. }
  31. $search_title = trim(input('param.search_title'));
  32. if ($search_title) {
  33. $condition[]=array('article_title','like', "%" . $search_title . "%");
  34. }
  35. $article_model = model('article');
  36. $article_list = $article_model->getArticleList($condition, 10);
  37. $articleclass_model = model('articleclass');
  38. /**
  39. * 整理列表内容
  40. */
  41. if (is_array($article_list)) {
  42. /**
  43. * 取文章分类
  44. */
  45. $class_list = $articleclass_model->getArticleclassList(array());
  46. $tmp_class_name = array();
  47. if (is_array($class_list)) {
  48. foreach ($class_list as $k => $v) {
  49. $tmp_class_name[$v['ac_id']] = $v['ac_name'];
  50. }
  51. }
  52. foreach ($article_list as $k => $v) {
  53. /**
  54. * 发布时间
  55. */
  56. $article_list[$k]['article_time'] = date('Y-m-d H:i:s', $v['article_time']);
  57. /**
  58. * 所属分类
  59. */
  60. if (@array_key_exists($v['ac_id'], $tmp_class_name)) {
  61. $article_list[$k]['ac_name'] = $tmp_class_name[$v['ac_id']];
  62. }
  63. }
  64. }
  65. /**
  66. * 分类列表
  67. */
  68. $parent_list = $articleclass_model->getTreeClassList(2);
  69. if (is_array($parent_list)) {
  70. $unset_sign = false;
  71. foreach ($parent_list as $k => $v) {
  72. $parent_list[$k]['ac_name'] = str_repeat("&nbsp;", $v['deep'] * 2) . $v['ac_name'];
  73. }
  74. }
  75. View::assign('article_list', $article_list);
  76. View::assign('show_page', $article_model->page_info->render());
  77. View::assign('search_title', $search_title);
  78. View::assign('search_ac_id', $search_ac_id);
  79. View::assign('parent_list', $parent_list);
  80. View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
  81. $this->setAdminCurItem('index');
  82. return View::fetch();
  83. }
  84. public function add() {
  85. if (!(request()->isPost())) {
  86. $article = [
  87. 'article_id' => 0,
  88. 'article_title' => '',
  89. 'ac_id' => input('param.ac_id'),
  90. 'article_url' => '',
  91. 'article_show' => 0,
  92. 'article_sort' => 0,
  93. 'article_content' => '',
  94. ];
  95. $articleclass_model = model('articleclass');
  96. $cate_list = $articleclass_model->getTreeClassList(2);
  97. View::assign('ac_list', $cate_list);
  98. View::assign('article', $article);
  99. //游离图片
  100. $article_pic_list = model('upload')->getUploadList(array('upload_type' => '1', 'item_id' => 0));
  101. View::assign('file_upload', $article_pic_list);
  102. $this->setAdminCurItem('add');
  103. return View::fetch('form');
  104. } else {
  105. $data = array(
  106. 'article_title' => input('post.article_title'),
  107. 'ac_id' => input('post.ac_id'),
  108. 'article_url' => input('post.article_url'),
  109. 'article_sort' => input('post.article_sort'),
  110. 'article_content' => input('post.article_content'),
  111. 'article_time' => TIMESTAMP,
  112. );
  113. $data['article_show'] = intval(input('post.article_show'));
  114. $article_validate = ds_validate('article');
  115. if (!$article_validate->scene('add')->check($data)) {
  116. $this->error($article_validate->getError());
  117. }
  118. $article_id = model('article')->addArticle($data);
  119. if ($article_id) {
  120. //更新图片信息ID
  121. $upload_model = model('upload');
  122. $file_id_array = input('post.file_id/a');
  123. if (is_array($file_id_array)) {
  124. foreach ($file_id_array as $k => $v) {
  125. $update_array = array();
  126. $update_array['item_id'] = $article_id;
  127. $upload_model->editUpload($update_array,array(array('upload_id','=',intval($v))));
  128. unset($update_array);
  129. }
  130. }
  131. //上传文章封面
  132. if (!empty($_FILES['_pic']['name'])) {
  133. $res=ds_upload_pic(ATTACH_ARTICLE,'_pic');
  134. if($res['code']){
  135. $article_pic=$res['data']['file_name'];
  136. model('article')->editArticle(array('article_pic' => $article_pic), $article_id);
  137. }else{
  138. $this->error($res['msg'], (string) url('Article/edit', ['article_id' => $article_id]));
  139. }
  140. }
  141. $this->success(lang('ds_common_save_succ'), 'Article/index');
  142. } else {
  143. $this->error(lang('ds_common_save_fail'));
  144. }
  145. }
  146. }
  147. public function edit() {
  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. $article_id = input('param.article_id');
  207. if (empty($article_id)) {
  208. ds_json_encode(10001, lang('param_error'));
  209. }
  210. $condition = array();
  211. $condition[] = array('article_id','=',$article_id);
  212. $article = model('article')->getOneArticle($condition);
  213. if(!$article){
  214. ds_json_encode(10001, lang('ds_no_record'));
  215. }
  216. //删除图片
  217. if($article['article_pic']){
  218. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_ARTICLE . DIRECTORY_SEPARATOR . $article['article_pic']);
  219. }
  220. $article_pic_list=model('upload')->getUploadList(array('upload_type'=>'1','item_id'=>$article_id));
  221. foreach($article_pic_list as $article_pic){
  222. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_ARTICLE . DIRECTORY_SEPARATOR . $article_pic['file_name']);
  223. }
  224. $result = model('article')->delArticle($article_id);
  225. if ($result) {
  226. ds_json_encode(10000, lang('ds_common_op_succ'));
  227. } else {
  228. ds_json_encode(10001, lang('error'));
  229. }
  230. }
  231. /**
  232. * 文章图片上传
  233. */
  234. public function article_pic_upload() {
  235. $file_name = '';
  236. $file_object = request()->file('fileupload');
  237. if ($file_object) {
  238. $res=ds_upload_pic(ATTACH_ARTICLE,'fileupload');
  239. if($res['code']){
  240. $file_name=$res['data']['file_name'];
  241. }else{
  242. echo $res['msg'];
  243. exit;
  244. }
  245. } else {
  246. echo 'error';
  247. exit;
  248. }
  249. /**
  250. * 模型实例化
  251. */
  252. $upload_model = model('upload');
  253. /**
  254. * 图片数据入库
  255. */
  256. $insert_array = array();
  257. $insert_array['file_name'] = $file_name;
  258. $insert_array['upload_type'] = '1';
  259. $insert_array['file_size'] = $_FILES['fileupload']['size'];
  260. $insert_array['item_id'] = intval(input('param.item_id'));
  261. $insert_array['upload_time'] = TIMESTAMP;
  262. $result = $upload_model->addUpload($insert_array);
  263. if ($result) {
  264. $data = array();
  265. $data['file_id'] = $result;
  266. $data['file_name'] = $file_name;
  267. $data['file_path'] = ds_get_pic(ATTACH_ARTICLE , $file_name);
  268. /**
  269. * 整理为json格式
  270. */
  271. $output = json_encode($data);
  272. echo $output;
  273. }
  274. }
  275. /**
  276. * ajax操作
  277. */
  278. public function ajax() {
  279. switch (input('param.branch')) {
  280. /**
  281. * 删除文章图片
  282. */
  283. case 'del_file_upload':
  284. if (intval(input('param.file_id')) > 0) {
  285. $upload_model = model('upload');
  286. /**
  287. * 删除图片
  288. */
  289. $file_array = $upload_model->getOneUpload(intval(input('param.file_id')));
  290. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_ARTICLE . DIRECTORY_SEPARATOR . $file_array['file_name']);
  291. /**
  292. * 删除信息
  293. */
  294. $condition = array();
  295. $condition[] = array('upload_id','=',intval(input('param.file_id')));
  296. $upload_model->delUpload($condition);
  297. echo 'true';
  298. exit;
  299. } else {
  300. echo 'false';
  301. exit;
  302. }
  303. break;
  304. }
  305. }
  306. /**
  307. * 获取卖家栏目列表,针对控制器下的栏目
  308. */
  309. protected function getAdminItemList() {
  310. $menu_array = array(
  311. array(
  312. 'name' => 'index',
  313. 'text' => lang('ds_manage'),
  314. 'url' => (string)url('Article/index')
  315. ),
  316. );
  317. if (request()->action() == 'add' || request()->action() == 'index') {
  318. $menu_array[] = array(
  319. 'name' => 'add',
  320. 'text' => lang('ds_new'),
  321. 'url' => (string)url('Article/add')
  322. );
  323. }
  324. if (request()->action() == 'edit') {
  325. $menu_array[] = array(
  326. 'name' => 'edit',
  327. 'text' => lang('ds_edit'),
  328. 'url' => 'javascript:void(0)'
  329. );
  330. }
  331. return $menu_array;
  332. }
  333. }