ArticlesController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: pl125 <xskjs888@163.com>
  8. // +----------------------------------------------------------------------
  9. namespace api\portal\controller;
  10. use api\portal\service\PortalPostService;
  11. use api\user\model\UserFavoriteModel;
  12. use api\user\model\UserLikeModel;
  13. use cmf\controller\RestBaseController;
  14. use api\portal\model\PortalPostModel;
  15. use think\Db;
  16. class ArticlesController extends RestBaseController
  17. {
  18. /**
  19. * 文章列表
  20. * @throws \think\exception\DbException
  21. */
  22. public function index()
  23. {
  24. $page = $this->request->post('page', 1);
  25. $size = $this->request->post('size', 10);
  26. $keyword = $this->request->post('keyword', '');
  27. //搜索条件
  28. $where = [];
  29. if (!empty($keyword)) {
  30. $where[] = ['post_title', 'like', "%{$keyword}%"];
  31. }
  32. $list = PortalPostModel::where($where)->order('is_top desc,create_time desc')->page($page, $size)->select();
  33. $this->success('成功', $list);
  34. }
  35. /**
  36. * 获取指定的文章
  37. * @param $id
  38. * @throws \think\Exception
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. * @throws \think\exception\DbException
  42. */
  43. public function read($id)
  44. {
  45. if (intval($id) === 0) {
  46. $this->error('无效的文章id!');
  47. } else {
  48. $postModel = new PortalPostModel();
  49. $data = $postModel->where('id', $id)->find();
  50. if (empty($data)) {
  51. $this->error('文章不存在!');
  52. } else {
  53. $postModel->where('id', $id)->setInc('post_hits');
  54. // $url = cmf_url('portal/Article/index', ['id' => $id, 'cid' => $data['categories'][0]['id']], true, true);
  55. // $data['url'] = $url;
  56. $this->success('请求成功!', $data);
  57. }
  58. }
  59. }
  60. /**
  61. * 我的文章列表
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. * @throws \think\exception\DbException
  65. */
  66. public function my()
  67. {
  68. $params = $this->request->get();
  69. $params['user_id'] = $this->getUserId();
  70. $postService = new PortalPostService();
  71. $data = $postService->postArticles($params);
  72. if (empty($this->apiVersion) || $this->apiVersion == '1.0.0') {
  73. $response = [$data];
  74. } else {
  75. $response = ['list' => $data];
  76. }
  77. $this->success('请求成功!', $response);
  78. }
  79. /**
  80. * 添加文章
  81. * @throws \think\Exception
  82. */
  83. public function save()
  84. {
  85. $data = $this->request->post();
  86. $data['user_id'] = $this->getUserId();
  87. $result = $this->validate($data, 'Articles.article');
  88. if ($result !== true) {
  89. $this->error($result);
  90. }
  91. if (empty($data['published_time'])) {
  92. $data['published_time'] = time();
  93. }
  94. $postModel = new PortalPostModel();
  95. $postModel->addArticle($data);
  96. $this->success('添加成功!');
  97. }
  98. /**
  99. * 更新文章
  100. * @param $id
  101. * @throws \think\Exception
  102. */
  103. public function update($id)
  104. {
  105. $data = $this->request->put();
  106. $result = $this->validate($data, 'Articles.article');
  107. if ($result !== true) {
  108. $this->error($result);
  109. }
  110. $postModel = new PortalPostModel();
  111. $res = $postModel->articleFind(['id' => $id, 'user_id' => $this->getUserId()]);
  112. if (empty($res)) {
  113. $this->error('文章不存在或者已经删除!');
  114. }
  115. $result = $postModel->editArticle($data, $id, $this->getUserId());
  116. if ($result === false) {
  117. $this->error('编辑失败!');
  118. } else {
  119. $this->success('编辑成功!');
  120. }
  121. }
  122. /**
  123. * 删除文章
  124. * @param $id
  125. * @throws \think\db\exception\DataNotFoundException
  126. * @throws \think\db\exception\ModelNotFoundException
  127. * @throws \think\exception\DbException
  128. */
  129. public function delete($id)
  130. {
  131. if (empty($id)) {
  132. $this->error('无效的文章id');
  133. }
  134. $postModel = new PortalPostModel();
  135. $result = $postModel->deleteArticle($id, $this->getUserId());
  136. if ($result == -1) {
  137. $this->error('文章已删除');
  138. }
  139. if ($result) {
  140. $this->success('删除成功!');
  141. } else {
  142. $this->error('删除失败!');
  143. }
  144. }
  145. /**
  146. * 批量删除文章
  147. * @throws \think\db\exception\DataNotFoundException
  148. * @throws \think\db\exception\ModelNotFoundException
  149. * @throws \think\exception\DbException
  150. */
  151. public function deletes()
  152. {
  153. $ids = $this->request->post('ids/a');
  154. if (empty($ids)) {
  155. $this->error('文章id不能为空');
  156. }
  157. $postModel = new PortalPostModel();
  158. $result = $postModel->deleteArticle($ids, $this->getUserId());
  159. if ($result == -1) {
  160. $this->error('文章已删除');
  161. }
  162. if ($result) {
  163. $this->success('删除成功!');
  164. } else {
  165. $this->error('删除失败!');
  166. }
  167. }
  168. /**
  169. * 搜索查询
  170. * @throws \think\db\exception\DataNotFoundException
  171. * @throws \think\db\exception\ModelNotFoundException
  172. * @throws \think\exception\DbException
  173. */
  174. public function search()
  175. {
  176. $params = $this->request->get();
  177. if (!empty($params['keyword'])) {
  178. $postService = new PortalPostService();
  179. $data = $postService->postArticles($params);
  180. if (empty($this->apiVersion) || $this->apiVersion == '1.0.0') {
  181. $response = $data;
  182. } else {
  183. $response = ['list' => $data,];
  184. }
  185. $this->success('请求成功!', $response);
  186. } else {
  187. $this->error('搜索关键词不能为空!');
  188. }
  189. }
  190. /**
  191. * 文章点赞
  192. * @throws \think\db\exception\DataNotFoundException
  193. * @throws \think\db\exception\ModelNotFoundException
  194. * @throws \think\exception\DbException
  195. */
  196. public function doLike()
  197. {
  198. $userId = $this->getUserId();
  199. $articleId = $this->request->param('id', 0, 'intval');
  200. $userLikeModel = new UserLikeModel();
  201. $findLikeCount = $userLikeModel->where([
  202. 'user_id' => $userId,
  203. 'object_id' => $articleId,
  204. ])->where('table_name', 'portal_post')->count();
  205. if (empty($findLikeCount)) {
  206. $postModel = new PortalPostModel();
  207. $article = $postModel->where('id', $articleId)->field('id,post_title,post_excerpt,more')->find();
  208. if (empty($article)) {
  209. $this->error('文章不存在!');
  210. }
  211. Db::startTrans();
  212. try {
  213. $postModel->where(['id' => $articleId])->setInc('post_like');
  214. $thumbnail = empty($article['more']['thumbnail']) ? '' : $article['more']['thumbnail'];
  215. $userLikeModel->insert([
  216. 'user_id' => $userId,
  217. 'object_id' => $articleId,
  218. 'table_name' => 'portal_post',
  219. 'title' => $article['post_title'],
  220. 'thumbnail' => $thumbnail,
  221. 'description' => $article['post_excerpt'],
  222. 'url' => json_encode(['action' => 'portal/Article/index', 'param' => ['id' => $articleId, 'cid' => $article['categories'][0]['id']]]),
  223. 'create_time' => time(),
  224. ]);
  225. Db::commit();
  226. } catch (\Exception $e) {
  227. Db::rollback();
  228. $this->error('点赞失败!');
  229. }
  230. $likeCount = $postModel->where('id', $articleId)->value('post_like');
  231. $this->success("赞好啦!", ['post_like' => $likeCount]);
  232. } else {
  233. $this->error("您已赞过啦!");
  234. }
  235. }
  236. /**
  237. * 取消文章点赞
  238. */
  239. public function cancelLike()
  240. {
  241. $userId = $this->getUserId();
  242. $articleId = $this->request->param('id', 0, 'intval');
  243. $userLikeModel = new UserLikeModel();
  244. $findLikeCount = $userLikeModel->where([
  245. 'user_id' => $userId,
  246. 'object_id' => $articleId,
  247. ])->where('table_name', 'portal_post')->count();
  248. if (!empty($findLikeCount)) {
  249. $postModel = new PortalPostModel();
  250. Db::startTrans();
  251. try {
  252. $postModel->where(['id' => $articleId])->setDec('post_like');
  253. $userLikeModel->where([
  254. 'user_id' => $userId,
  255. 'object_id' => $articleId,
  256. ])->where('table_name', 'portal_post')->delete();
  257. Db::commit();
  258. } catch (\Exception $e) {
  259. Db::rollback();
  260. $this->error('取消点赞失败!');
  261. }
  262. $likeCount = $postModel->where('id', $articleId)->value('post_like');
  263. $this->success("取消点赞成功!", ['post_like' => $likeCount]);
  264. } else {
  265. $this->error("您还没赞过!");
  266. }
  267. }
  268. /**
  269. * 文章收藏
  270. * @throws \think\db\exception\DataNotFoundException
  271. * @throws \think\db\exception\ModelNotFoundException
  272. * @throws \think\exception\DbException
  273. */
  274. public function doFavorite()
  275. {
  276. $userId = $this->getUserId();
  277. $articleId = $this->request->param('id', 0, 'intval');
  278. $userFavoriteModel = new UserFavoriteModel();
  279. $findFavoriteCount = $userFavoriteModel->where([
  280. 'user_id' => $userId,
  281. 'object_id' => $articleId,
  282. ])->where('table_name', 'portal_post')->count();
  283. if (empty($findFavoriteCount)) {
  284. $postModel = new PortalPostModel();
  285. $article = $postModel->where(['id' => $articleId])->field('id,post_title,post_excerpt,more')->find();
  286. if (empty($article)) {
  287. $this->error('文章不存在!');
  288. }
  289. Db::startTrans();
  290. try {
  291. $postModel->where(['id' => $articleId])->setInc('post_favorites');
  292. $thumbnail = empty($article['more']['thumbnail']) ? '' : $article['more']['thumbnail'];
  293. $userFavoriteModel->insert([
  294. 'user_id' => $userId,
  295. 'object_id' => $articleId,
  296. 'table_name' => 'portal_post',
  297. 'thumbnail' => $thumbnail,
  298. 'title' => $article['post_title'],
  299. 'description' => $article['post_excerpt'],
  300. 'url' => json_encode(['action' => 'portal/Article/index', 'param' => ['id' => $articleId, 'cid' => $article['categories'][0]['id']]]),
  301. 'create_time' => time(),
  302. ]);
  303. Db::commit();
  304. } catch (\Exception $e) {
  305. Db::rollback();
  306. $this->error('收藏失败!');
  307. }
  308. $favoriteCount = $postModel->where('id', $articleId)->value('post_favorites');
  309. $this->success("收藏好啦!", ['post_favorites' => $favoriteCount]);
  310. } else {
  311. $this->error("您已收藏过啦!");
  312. }
  313. }
  314. /**
  315. * 取消文章收藏
  316. */
  317. public function cancelFavorite()
  318. {
  319. $userId = $this->getUserId();
  320. $articleId = $this->request->param('id', 0, 'intval');
  321. $userFavoriteModel = new UserFavoriteModel();
  322. $findFavoriteCount = $userFavoriteModel->where([
  323. 'user_id' => $userId,
  324. 'object_id' => $articleId,
  325. ])->where('table_name', 'portal_post')->count();
  326. if (!empty($findFavoriteCount)) {
  327. $postModel = new PortalPostModel();
  328. Db::startTrans();
  329. try {
  330. $postModel->where(['id' => $articleId])->setDec('post_favorites');
  331. $userFavoriteModel->where([
  332. 'user_id' => $userId,
  333. 'object_id' => $articleId,
  334. ])->where('table_name', 'portal_post')->delete();
  335. Db::commit();
  336. } catch (\Exception $e) {
  337. Db::rollback();
  338. $this->error('取消失败!');
  339. }
  340. $favoriteCount = $postModel->where('id', $articleId)->value('post_favorites');
  341. $this->success("取消成功!", ['post_favorites' => $favoriteCount]);
  342. } else {
  343. $this->error("您还没收藏过!");
  344. }
  345. }
  346. /**
  347. * 相关文章列表
  348. * @throws \think\db\exception\DataNotFoundException
  349. * @throws \think\db\exception\ModelNotFoundException
  350. * @throws \think\exception\DbException
  351. */
  352. public function relatedArticles()
  353. {
  354. $articleId = $this->request->param('id', 0, 'intval');
  355. $categoryId = Db::name('portal_category_post')->where('post_id', $articleId)->value('category_id');
  356. $postModel = new PortalPostModel();
  357. $articles = $postModel
  358. ->alias('post')
  359. ->join('__PORTAL_CATEGORY_POST__ category_post', 'post.id=category_post.post_id')
  360. ->where(['post.delete_time' => 0, 'post.post_status' => 1, 'category_post.category_id' => $categoryId])
  361. ->orderRaw('rand()')
  362. ->limit(5)
  363. ->select();
  364. if ($articles->isEmpty()) {
  365. $this->error('没有相关文章!');
  366. }
  367. $this->success('success', ['list' => $articles]);
  368. }
  369. }