Article.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. *
  4. * User: inuo
  5. * Date: 2020-04-23
  6. * Time: 16:16
  7. */
  8. namespace app\controller\admin\article;
  9. use app\validate\admin\NoticeValidate;
  10. use crmeb\basic\BaseController;
  11. use app\common\repositories\article\ArticleCategoryRepository;
  12. use app\common\repositories\article\ArticleContentRepository;
  13. use app\common\repositories\article\ArticleRepository;
  14. use FormBuilder\Exception\FormBuilderException;
  15. use think\App;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use app\validate\admin\ArticleValidate;
  20. use think\facade\Db;
  21. class Article extends BaseController
  22. {
  23. /**
  24. * @var ArticleRepository
  25. */
  26. protected $repository;
  27. /**
  28. * Article constructor.
  29. * @param App $app
  30. * @param ArticleRepository $repository
  31. */
  32. public function __construct(App $app,ArticleRepository $repository)
  33. {
  34. parent::__construct($app);
  35. $this->repository = $repository;
  36. }
  37. /**
  38. * @return mixed
  39. * @author Qinii
  40. */
  41. public function getList()
  42. {
  43. [$page, $limit] = $this->getPage();
  44. $where = $this->request->params(['cid','title']);
  45. return app('json')->success($this->repository->search($this->request->merId(),$where, $page, $limit));
  46. }
  47. /**
  48. * 添加
  49. * @param ArticleValidate $validate
  50. * @param ArticleCategoryRepository $repository
  51. * @return mixed
  52. * @author Qinii
  53. */
  54. public function create(ArticleValidate $validate,ArticleCategoryRepository $repository)
  55. {
  56. $data = $this->checkParams($validate);
  57. $data['admin_id'] = $this->request->adminId();
  58. $data['mer_id'] = $this->request->merId()?? 0 ;
  59. if (!$repository->merExists($data['mer_id'],$data['cid']))
  60. return app('json')->fail('分类不存在');
  61. $this->repository->create($data);
  62. return app('json')->success('添加成功');
  63. }
  64. public function detail($id)
  65. {
  66. if (!$this->repository->merExists($this->request->merId(),$id))
  67. return app('json')->fail('数据不存在');
  68. return app('json')->success($this->repository->get($id,$this->request->merId()));
  69. }
  70. /**
  71. * 更新
  72. * @param $id
  73. * @param ArticleValidate $validate
  74. * @param ArticleCategoryRepository $articleCategoryRepository
  75. * @return mixed
  76. * @author Qinii
  77. */
  78. public function update($id,ArticleValidate $validate,ArticleCategoryRepository $articleCategoryRepository)
  79. {
  80. $data = $this->checkParams($validate);
  81. if (!$this->repository->merExists($this->request->merId(),$id))
  82. return app('json')->fail('数据不存在');
  83. if (!$articleCategoryRepository->merExists($this->request->merId(),$data['cid']))
  84. return app('json')->fail('分类不存在');
  85. $this->repository->update($id,$data);
  86. return app('json')->success('编辑成功');
  87. }
  88. /**
  89. * 删除
  90. * @param $id
  91. * @return mixed
  92. * @throws DataNotFoundException
  93. * @throws DbException
  94. * @throws ModelNotFoundException
  95. * @author Qinii
  96. */
  97. public function delete($id)
  98. {
  99. if (!$this->repository->merExists($this->request->merId(),$id))
  100. return app('json')->fail('数据不存在');
  101. $this->repository->delete($id,$this->request->merId());
  102. return app('json')->success('删除成功');
  103. }
  104. /**
  105. * @param ArticleValidate $validate
  106. * @return array
  107. * @author Qinii
  108. */
  109. public function checkParams(ArticleValidate $validate)
  110. {
  111. $data = $this->request->params([['cid', 0], 'title', 'content', 'author', 'image_input','status','sort','synopsis','is_hot','is_banner','url']);
  112. $validate->check($data);
  113. return $data;
  114. }
  115. /**
  116. * @return mixed
  117. * @author cabbage
  118. */
  119. public function noticeList()
  120. {
  121. [$page, $limit] = $this->getPage();
  122. $data = $this->request->params(['title','status']);
  123. if(isset($data['title']) && $data['title'] != ''){
  124. $where['title'] = ["like","%{$data['title']}%"];
  125. }
  126. if(isset($data['status']) && $data['status'] != ''){
  127. $where['status'] = $data['status'];
  128. }
  129. $where['is_del'] = 0 ;
  130. $where['mer_id'] = $this->request->merId();
  131. // var_dump($where);
  132. return app('json')->success($this->repository->notice($where, $page, $limit));
  133. }
  134. /*添加公告*/
  135. public function noticeCreate(NoticeValidate $validate,ArticleCategoryRepository $repository)
  136. {
  137. $data = $this->noticeParams($validate);
  138. $data['admin_id'] = $this->request->adminId();
  139. $data['create_time'] = date("Y-m-d H:i:s");
  140. $data['mer_id'] = $this->request->merId();
  141. $this->repository->noticeCreate($data);
  142. return app('json')->success('添加成功');
  143. }
  144. /**
  145. * @param NoticeValidate $validate
  146. * @return array
  147. */
  148. public function noticeParams(NoticeValidate $validate)
  149. {
  150. $data = $this->request->params(['title','content','status']);
  151. $validate->check($data);
  152. return $data;
  153. }
  154. /*删除公告*/
  155. public function noticeDelete($id)
  156. {
  157. if (!$this->repository->noticeExist($id))
  158. return app('json')->fail('数据不存在');
  159. $this->repository->noticeDelete($id);
  160. return app('json')->success('删除成功');
  161. }
  162. /*修改状态*/
  163. public function switchStatus($id)
  164. {
  165. $is_best = $this->request->param('status', 1) == 1 ? 1 : 2;
  166. if (!$this->repository->noticeExist($id))
  167. return app('json')->fail('数据不存在');
  168. $this->repository->noticeUpdate($id, ['status'=>$is_best]);
  169. return app('json')->success('修改成功');
  170. }
  171. /*详情*/
  172. public function noticeDetail($id){
  173. if (!$this->repository->noticeExist($id))
  174. return app('json')->fail('数据不存在');
  175. return app('json')->success($this->repository->noticeExist($id));
  176. }
  177. /*编辑*/
  178. public function noticeUpdate($id,NoticeValidate $validate){
  179. $data = $this->noticeParams($validate);
  180. if (!$this->repository->noticeExist($id))
  181. return app('json')->fail('数据不存在');
  182. $this->repository->noticeUpdate($id,$data);
  183. return app('json')->success('编辑成功');
  184. }
  185. //评论详情
  186. public function article_comment($id)
  187. {
  188. [$page, $limit] = $this->getPage();
  189. $data=Db::name('information')->where('pid',$id)->where('is_del',0)->page($page,$limit)->field("id,content,uid,add_time")->select();
  190. if(!$data->isEmpty()){
  191. $data=$data->toArray();
  192. }
  193. foreach ($data as $k=>&$v){
  194. $v['user']=Db::name('user')->where('uid',$v['uid'])->field('nickname,avatar,uid')->find();
  195. //自己是否赞了自己
  196. $v["fabulous_status"]=Db::name('information_fabulous')->where(["pid"=>$v["id"],"uid"=>$v['uid'],'type'=>0])->count();
  197. //评论时间
  198. $v["add_time"]=$this->diffBetweenTwoDay($v["add_time"]);
  199. //有多少人赞了改评论
  200. $v["fabulous"]=Db::name('information_fabulous')->where(["pid"=>$v["id"],'type'=>0])->count();
  201. }
  202. return app('json')->success($data);
  203. }
  204. //删除评论
  205. public function article_comment_del($id)
  206. {
  207. $res=Db::name('information')->where('id',$id)->update(['is_del'=>1]);
  208. if($res!==false){
  209. return app('json')->success('删除成功');
  210. }else{
  211. return app('json')->fail('删除失败');
  212. }
  213. }
  214. /**
  215. * 格式化时间
  216. */
  217. public function diffBetweenTwoDay($pastDay){
  218. $timeC = time() - $pastDay;
  219. $dateC = round((strtotime(date('Y-m-d')) - strtotime(date('Y-m-d',$pastDay)))/60/60/24);
  220. if($timeC<=3*60){
  221. $dayC = '刚刚';
  222. }elseif($timeC>3*60&&$timeC<=5*60){
  223. $dayC = '3分钟前';
  224. }elseif($timeC>5*60&&$timeC<=10*60){
  225. $dayC = '5分钟前';
  226. }elseif($timeC>10*60&&$timeC<=30*60){
  227. $dayC = '10分钟前';
  228. }elseif($timeC>30*60&&$timeC<=60*60){
  229. $dayC = '30分钟前';
  230. }elseif($timeC>60*60&&$timeC<=120*60){
  231. $dayC = '1小时前';
  232. }elseif($timeC>120*60&&$dateC == 0){
  233. $dayC = '今天';
  234. }elseif($dateC == 1){
  235. $dayC = '昨天';
  236. }else{
  237. $dayC = date('Y-m-d',$pastDay);
  238. }
  239. return $dayC;
  240. }
  241. }