PicController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 小夏 < 449134904@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use app\portal\model\PortalPostModel;
  13. use cmf\controller\AdminBaseController;
  14. use think\Db;
  15. use think\db\Query;
  16. /**
  17. * Class PicController
  18. */
  19. class PicController extends AdminBaseController
  20. {
  21. /**
  22. * 列表
  23. */
  24. public function index(){
  25. /**搜索条件**/
  26. $params = input();
  27. $params['type'] = isset($params['type'])? $params['type'] : 0;
  28. $params['keyword'] = isset($params['keyword'])? $params['keyword'] : '';
  29. $datas = PortalPostModel::alias('p')
  30. ->leftJoin('sg_user u','u.id=p.user_id')
  31. ->where(['p.post_status'=>1,'p.post_type'=>3])
  32. ->where(function (Query $query) use ($params) {
  33. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  34. if ($keyword) {
  35. $query->where('p.post_title', 'like', "%$keyword%");
  36. }
  37. $type = isset($params['type'])? $params['type'] : 0;
  38. if($type){
  39. $query->where('p.type', $type);
  40. }
  41. })
  42. ->field('p.id,p.post_title,p.post_type,p.type,p.user_id,p.update_time,p.create_time,p.published_time,p.post_excerpt,p.thumbnail,p.albums,p.post_status,u.user_nickname')
  43. ->order("p.is_top DESC,p.published_time DESC")
  44. ->paginate(10,false,['query'=>request()->param()])
  45. ->each(function($item,$key){
  46. $albums = isset($item['albums']) && $item['albums']? explode(',', $item['albums']) : [];
  47. foreach ($albums as &$v){
  48. $v = $v? cmf_get_image_preview_url($v) : '';
  49. }
  50. unset($v);
  51. $item['albums_list'] = $albums;
  52. return $item;
  53. });
  54. // 获取分页显示
  55. $page = $datas->render();
  56. $this->assign("params", $params);
  57. $this->assign("page", $page);
  58. $this->assign("datas", $datas);
  59. return $this->fetch();
  60. }
  61. /**
  62. * 删除
  63. */
  64. public function delete()
  65. {
  66. $param = $this->request->param();
  67. if (isset($param['ids']) && isset($param["yes"])) {
  68. $ids = $this->request->param('ids/a');
  69. PortalPostModel::where('id', 'in', $ids)->update(['post_status' => 2]);
  70. PortalPostModel::where('post_status', 2)
  71. ->where('update_time','<', time() - 3 * 24 * 3600)
  72. ->delete();
  73. $this->success("删除成功!", '');
  74. }
  75. }
  76. //增加
  77. public function add()
  78. {
  79. return $this->fetch('add');
  80. }
  81. /**
  82. * 添加提交
  83. */
  84. public function addPost(){
  85. if($this->request->isPost()){
  86. $data = $this->request->param();
  87. $result = $this->validate($data['post'], 'PortalPost');
  88. if ($result !== true) {
  89. $this->error($result);
  90. }
  91. $post = isset($data['post'])? $data['post'] : [];
  92. if (empty($post['post_title'])) {
  93. $this->error('标题不为空!');
  94. }
  95. $albums = isset($data['photo_urls']) && $data['photo_urls']? join(',', $data['photo_urls']) : '';
  96. if(count($data['photo_urls']) > 6){
  97. $this->error('相册最多6张图!');
  98. }
  99. if($albums){
  100. $post['albums'] = $albums;
  101. }
  102. $post['published_time'] = isset($post['published_time']) && $post['published_time']? strtotime($post['published_time']) : time();
  103. $post['create_time'] = time();
  104. $post['user_id'] = cmf_get_current_admin_id();
  105. $post['post_type'] = 3;
  106. PortalPostModel::insertGetId($post);
  107. $this->success("添加成功!",url("Pic/index"));
  108. }
  109. }
  110. /**
  111. * 编辑
  112. */
  113. public function edit(){
  114. $id = $this->request->param('id', 0, 'intval');
  115. $model = new PortalPostModel();
  116. $info = $model->get($id);
  117. $info['albums'] = isset($info['albums']) && $info['albums']? explode(',', $info['albums']) : [];
  118. $this->assign('info', $info);
  119. return $this->fetch();
  120. }
  121. /**
  122. * 编辑提交
  123. */
  124. public function editPost()
  125. {
  126. $data = $this->request->param();
  127. $result = $this->validate($data['post'], 'PortalPost');
  128. if ($result !== true) {
  129. $this->error($result);
  130. }
  131. $post = isset($data['post'])? $data['post'] : [];
  132. if (empty($post['post_title'])) {
  133. $this->error('标题不为空!');
  134. }
  135. $albums = isset($data['photo_urls']) && $data['photo_urls']? join(',', $data['photo_urls']) : '';
  136. if(count($data['photo_urls']) > 6){
  137. $this->error('相册最多6张图!');
  138. }
  139. if($albums){
  140. $post['albums'] = $albums;
  141. }
  142. $post['published_time'] = isset($post['published_time']) && $post['published_time']? strtotime($post['published_time']) : time();
  143. $post['post_type'] = 3;
  144. PortalPostModel::where(['id'=>$post['id']])->update($post);
  145. $this->success("保存成功!", url("Pic/index"));
  146. }
  147. }