ArticlesController.php 14 KB

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