BlogController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <?php
  2. namespace App\Web\Controllers;
  3. use App\Http\Controllers\BaseController;
  4. use App\Web\Services\BlogService;
  5. use Illuminate\Http\Request;
  6. class BlogController extends BaseController
  7. {
  8. protected $service = null;
  9. /**
  10. * 构造函数
  11. * BlogController constructor.
  12. * @param BlogService $service
  13. */
  14. public function __construct(BlogService $service)
  15. {
  16. $this->service = $service;
  17. }
  18. /**
  19. * 获取文章类型列表
  20. * */
  21. public function getBlogTypeList(Request $request)
  22. {
  23. $params = $request->only(['page_size', 'page', 'status', 'keyword', 'plate_id']);
  24. $ret = $this->service->getBlogTypeList($params);
  25. return $this->jsonResponse(
  26. 'ok',
  27. $ret
  28. );
  29. }
  30. /**
  31. * 文章类型保存
  32. * */
  33. public function saveBlogType(Request $request)
  34. {
  35. $this->validate($request, [
  36. 'type_name' => 'required',
  37. ], [
  38. 'type_name.required' => '类型名称不能为空',
  39. ]);
  40. $params = $request->only(['id', 'type_name', 'status', 'sort', 'seo_data', 'seo_id', 'expand_content', 'plate_id']);
  41. $ret = $this->service->saveBlogType($params);
  42. return $this->jsonResponse(
  43. 'ok',
  44. $ret
  45. );
  46. }
  47. /**
  48. * 变更文章类型状态
  49. * */
  50. public function changeBlogTypeStatus(Request $request)
  51. {
  52. $this->validate($request, [
  53. 'id' => 'required',
  54. ], [
  55. 'id.required' => 'id不能为空',
  56. ]);
  57. $params = $request->only(['id', 'status']);
  58. $ret = $this->service->saveBlogType($params);
  59. return $this->jsonResponse(
  60. 'ok',
  61. $ret
  62. );
  63. }
  64. /**
  65. * 保存文章类型关系
  66. * */
  67. public function typeRelationSave(Request $request)
  68. {
  69. $this->validate($request, [
  70. 'blog_id' => 'required',
  71. ], [
  72. 'blog_id.required' => '文章id不能为空',
  73. ]);
  74. $params = $request->only(['blog_id', 'type_ids']);
  75. $ret = $this->service->typeRelationSave($params);
  76. return $this->jsonResponse(
  77. 'ok',
  78. $ret
  79. );
  80. }
  81. /**
  82. * 根据文章类型 保存文章关系
  83. * */
  84. public function addRelationByTypeId(Request $request)
  85. {
  86. $this->validate($request, [
  87. 'blog_ids' => 'required',
  88. 'type_id' => 'required',
  89. ], [
  90. 'blog_ids.required' => '文章id不能为空',
  91. 'type_id.required' => '文章类型id不能为空',
  92. ]);
  93. $typeId = $request->input('type_id', 0);
  94. $blogIds = $request->input('blog_ids', []);
  95. $ret = $this->service->addRelationByTypeId($typeId, $blogIds);
  96. return $this->jsonResponse(
  97. 'ok',
  98. $ret
  99. );
  100. }
  101. /**
  102. * 根据类型id及文章id 删除关联关系
  103. * */
  104. public function delTypeRelation(Request $request)
  105. {
  106. $this->validate($request, [
  107. 'blog_id' => 'required',
  108. 'type_id' => 'required',
  109. ], [
  110. 'blog_id.required' => '文章id不能为空',
  111. 'type_id.required' => '类型id不能未空',
  112. ]);
  113. $blogId = $request->input('blog_id', '');
  114. $typeId = $request->input('type_id', '');
  115. $ret = $this->service->delTypeRelation($typeId, $blogId);
  116. return $this->jsonResponse(
  117. 'ok',
  118. $ret
  119. );
  120. }
  121. /**
  122. * 获取文章标签列表
  123. * */
  124. public function getBlogTagList(Request $request)
  125. {
  126. $params = $request->only(['page_size', 'page', 'status', 'keyword']);
  127. $ret = $this->service->getBlogTagList($params);
  128. return $this->jsonResponse(
  129. 'ok',
  130. $ret
  131. );
  132. }
  133. /**
  134. * 文章标签保存
  135. * */
  136. public function saveBlogTag(Request $request)
  137. {
  138. $this->validate($request, [
  139. 'tag_name' => 'required',
  140. ], [
  141. 'tag_name.required' => '类型名称不能为空',
  142. ]);
  143. $params = $request->only(['id', 'tag_name', 'is_hot', 'status', 'sort', 'seo_id', 'seo_data']);
  144. $ret = $this->service->saveBlogTag($params);
  145. return $this->jsonResponse(
  146. 'ok',
  147. $ret
  148. );
  149. }
  150. /**
  151. * 变更文章类型状态
  152. * */
  153. public function changeBlogTagStatus(Request $request)
  154. {
  155. $this->validate($request, [
  156. 'id' => 'required',
  157. ], [
  158. 'id.required' => 'id不能为空',
  159. ]);
  160. $params = $request->only(['id', 'status']);
  161. $ret = $this->service->saveBlogTag($params);
  162. return $this->jsonResponse(
  163. 'ok',
  164. $ret
  165. );
  166. }
  167. /**
  168. * 保存文章标签关系
  169. * */
  170. public function tagRelationSave(Request $request)
  171. {
  172. $this->validate($request, [
  173. 'blog_id' => 'required',
  174. ], [
  175. 'blog_id.required' => '文章id不能为空',
  176. ]);
  177. $params = $request->only(['blog_id', 'tag_ids']);
  178. $ret = $this->service->tagRelationSave($params);
  179. return $this->jsonResponse(
  180. 'ok',
  181. $ret
  182. );
  183. }
  184. /**
  185. * 根据文章标签 保存文章关系
  186. * */
  187. public function addRelationByTagId(Request $request)
  188. {
  189. $this->validate($request, [
  190. 'blog_ids' => 'required',
  191. 'tag_id' => 'required',
  192. ], [
  193. 'blog_ids.required' => '文章id不能为空',
  194. 'tag_id.required' => '文章标签id不能为空',
  195. ]);
  196. $tagId = $request->input('tag_id', 0);
  197. $blogIds = $request->input('blog_ids', []);
  198. $ret = $this->service->addRelationByTagId($tagId, $blogIds);
  199. return $this->jsonResponse(
  200. 'ok',
  201. $ret
  202. );
  203. }
  204. /**
  205. * 根据标签id及文章id 删除关联关系
  206. * */
  207. public function delTagRelation(Request $request)
  208. {
  209. $this->validate($request, [
  210. 'blog_id' => 'required',
  211. 'tag_id' => 'required',
  212. ], [
  213. 'blog_id.required' => '文章id不能为空',
  214. 'tag_id.required' => '标签id不能未空',
  215. ]);
  216. $blogId = $request->input('blog_id', '');
  217. $tagId = $request->input('tag_id', '');
  218. $ret = $this->service->delTagRelation($tagId, $blogId);
  219. return $this->jsonResponse(
  220. 'ok',
  221. $ret
  222. );
  223. }
  224. /**
  225. * 获取文章板块列表
  226. * */
  227. public function getBlogPlateList(Request $request)
  228. {
  229. $params = $request->only(['page_size', 'page', 'status', 'keyword']);
  230. $ret = $this->service->getBlogPlateList($params);
  231. return $this->jsonResponse(
  232. 'ok',
  233. $ret
  234. );
  235. }
  236. /**
  237. * 文章板块保存
  238. * */
  239. public function saveBlogPlate(Request $request)
  240. {
  241. $this->validate($request, [
  242. 'plate_name' => 'required',
  243. ], [
  244. 'plate_name.required' => '类型名称不能为空',
  245. ]);
  246. $params = $request->only(['id', 'plate_name', 'status', 'sort', 'seo_id', 'seo_data']);
  247. $ret = $this->service->saveBlogPlate($params);
  248. return $this->jsonResponse(
  249. 'ok',
  250. $ret
  251. );
  252. }
  253. /**
  254. * 变更文章板块状态
  255. * */
  256. public function changeBlogPlateStatus(Request $request)
  257. {
  258. $this->validate($request, [
  259. 'id' => 'required',
  260. ], [
  261. 'id.required' => 'id不能为空',
  262. ]);
  263. $params = $request->only(['id', 'status']);
  264. $ret = $this->service->saveBlogPlate($params);
  265. return $this->jsonResponse(
  266. 'ok',
  267. $ret
  268. );
  269. }
  270. /**
  271. * 获取文章列表
  272. * */
  273. public function getBlogList(Request $request)
  274. {
  275. $params = $request->only(['page_size', 'page', 'status', 'keyword', 'plate_id', 'sort', 'is_admin']);
  276. $ret = $this->service->getBlogList($params);
  277. return $this->jsonResponse(
  278. 'ok',
  279. $ret
  280. );
  281. }
  282. /**
  283. * 获取文章详情
  284. * */
  285. public function getBlogInfo(Request $request)
  286. {
  287. $this->validate($request, [
  288. 'id' => 'required',
  289. ], [
  290. 'id.required' => '文章id不能为空',
  291. ]);
  292. $id = $request->input('id', 0);
  293. $ret = $this->service->getBlogInfo(['id' => $id]);
  294. return $this->jsonResponse(
  295. 'ok',
  296. $ret
  297. );
  298. }
  299. /**
  300. * 获取文章分类未关联的文章
  301. * */
  302. public function getUnrelatedBlogByTypeIds(Request $request)
  303. {
  304. $this->validate($request, [
  305. 'type_id' => 'required',
  306. ], [
  307. 'type_id.required' => 'id不能为空',
  308. ]);
  309. $params = $request->only(['page_size', 'page', 'status', 'keyword', 'type_id']);
  310. $ret = $this->service->getUnrelatedBlogByTypeIds($params);
  311. return $this->jsonResponse(
  312. 'ok',
  313. $ret
  314. );
  315. }
  316. /**
  317. * 获取文章标签未关联的文章
  318. * */
  319. public function getUnrelatedBlogByTagIds(Request $request)
  320. {
  321. $this->validate($request, [
  322. 'tag_id' => 'required',
  323. ], [
  324. 'tag_id.required' => 'id不能为空',
  325. ]);
  326. $params = $request->only(['page_size', 'page', 'status', 'keyword', 'tag_id']);
  327. $ret = $this->service->getUnrelatedBlogByTagIds($params);
  328. return $this->jsonResponse(
  329. 'ok',
  330. $ret
  331. );
  332. }
  333. /**
  334. * 根据类型id 获取文章列表
  335. * */
  336. public function getBlogListByType(Request $request)
  337. {
  338. $params = $request->only(['page_size', 'page', 'status', 'keyword', 'type_id']);
  339. $ret = $this->service->getBlogByTypeIds($params);
  340. return $this->jsonResponse(
  341. 'ok',
  342. $ret
  343. );
  344. }
  345. /**
  346. * 根据标签id 获取文章列表
  347. * */
  348. public function getBlogListByTag(Request $request)
  349. {
  350. $params = $request->only(['page_size', 'page', 'status', 'keyword', 'tag_id']);
  351. $ret = $this->service->getBlogByTagIds($params);
  352. return $this->jsonResponse(
  353. 'ok',
  354. $ret
  355. );
  356. }
  357. /**
  358. * 变更文章板块状态
  359. * */
  360. public function changeBlogStatus(Request $request)
  361. {
  362. $this->validate($request, [
  363. 'id' => 'required',
  364. ], [
  365. 'id.required' => 'id不能为空',
  366. ]);
  367. $params = $request->only(['id', 'status']);
  368. $ret = $this->service->saveBlog($params);
  369. return $this->jsonResponse(
  370. 'ok',
  371. $ret
  372. );
  373. }
  374. /**
  375. * 保存文章数据
  376. * */
  377. public function saveBlog(Request $request)
  378. {
  379. $this->validate($request, [
  380. 'plate_id' => 'required',
  381. 'title' => 'required',
  382. 'content' => 'required|string',
  383. 'type_ids' => 'array',
  384. 'tag_ids' => 'array',
  385. ]);
  386. $params = $request->only(['id', 'plate_id', 'pub_date', 'title', 'author', 'content', 'outline_bar', 'description',
  387. 'image_url', 'image_alt', 'main_image', 'main_image_alt', 'virtual_view',
  388. 'download_url', 'seo_id', 'sort', 'seo_data', 'status', 'tag_ids', 'type_ids', 'is_top', 'expand_content']);
  389. $ret = $this->service->saveBlog($params);
  390. return $this->jsonResponse(
  391. 'ok',
  392. $ret
  393. );
  394. }
  395. /**
  396. * 文章推荐设置
  397. * */
  398. public function recommendSet(Request $request)
  399. {
  400. $this->validate($request, [
  401. 'id' => 'required',
  402. ]);
  403. $params = $request->only(['id', 'is_recommend']);
  404. $ret = $this->service->recommendSet($params);
  405. return $this->jsonResponse(
  406. 'ok',
  407. $ret
  408. );
  409. }
  410. /**
  411. * 更新文章的浏览量
  412. * */
  413. public function upBlogPv(Request $request)
  414. {
  415. $this->validate($request, [
  416. 'id' => 'required',
  417. ], [
  418. 'id.required' => 'id不能为空',
  419. ]);
  420. $blogId = $request->input('id', 0);
  421. $incValue = $request->input('inc_val', 1);
  422. $ret = $this->service->upBlogPv($blogId, $incValue);
  423. return $this->jsonResponse(
  424. 'ok',
  425. $ret
  426. );
  427. }
  428. /**
  429. * 文章预览
  430. * */
  431. public function previewBlog(Request $request)
  432. {
  433. $this->validate($request, [
  434. 'id' => 'required',
  435. ], [
  436. 'id.required' => 'id不能为空',
  437. ]);
  438. $blogId = $request->input('id', 0);
  439. $ret = $this->service->getBlogRenderData(['id' => $blogId]);
  440. return $this->jsonResponse(
  441. 'ok',
  442. $ret
  443. );
  444. }
  445. /**
  446. * 根据类型id获取文章
  447. * */
  448. public function getPublishBlogListByTypeId(Request $request)
  449. {
  450. $params = $request->only(['page_size', 'page', 'type_id', 'plate_id']);
  451. $ret = $this->service->getPublishBlogListByTypeId($params);
  452. return $this->jsonResponse(
  453. 'ok',
  454. $ret
  455. );
  456. }
  457. /**
  458. * 根据标签id获取文章
  459. * */
  460. public function getPublishBlogListByTagId(Request $request)
  461. {
  462. $params = $request->only(['page_size', 'page', 'tag_id', 'plate_id']);
  463. $ret = $this->service->getPublishBlogListByTagId($params);
  464. return $this->jsonResponse(
  465. 'ok',
  466. $ret
  467. );
  468. }
  469. /**
  470. * 搜索获取已发布的文章
  471. * */
  472. public function getSearchBlogList(Request $request)
  473. {
  474. $params = $request->only(['page_size', 'keyword']);
  475. $ret = $this->service->getPublishBlogList($params);
  476. return $this->jsonResponse(
  477. 'ok',
  478. $ret
  479. );
  480. }
  481. /**
  482. * 新闻排序上下移
  483. * @url /api/blog/change-sort
  484. */
  485. public function changeBlogSort(Request $request)
  486. {
  487. $this->validate($request, [
  488. 'id' => 'required',
  489. 'sort' => 'required',
  490. ]);
  491. $ret = $this->service->changeBlogSort($request->input('id'), $request->input('sort'));
  492. return $this->jsonResponse('ok', $ret);
  493. }
  494. // 批量修改分类
  495. public function typePluralEdit(Request $request)
  496. {
  497. $this->validate($request, [
  498. 'ids' => 'required',
  499. ]);
  500. $ret = $this->service->typePluralEdit($request->all());
  501. return $this->jsonResponse('ok', $ret);
  502. }
  503. // 批量修改标签
  504. public function tagPluralEdit(Request $request)
  505. {
  506. $this->validate($request, [
  507. 'ids' => 'required',
  508. ]);
  509. $ret = $this->service->tagPluralEdit($request->all());
  510. return $this->jsonResponse('ok', $ret);
  511. }
  512. // 批量删除
  513. public function batchDeleteNews(Request $request)
  514. {
  515. $this->validate($request, [
  516. 'ids' => 'required',
  517. ]);
  518. $ret = $this->service->batchDelete($request->all(), 2);
  519. return $this->jsonResponse('ok', $ret);
  520. }
  521. // 批量修改状态
  522. public function batchStatusNews(Request $request)
  523. {
  524. $this->validate($request, [
  525. 'ids' => 'required',
  526. ]);
  527. $ret = $this->service->batchStatus($request->all());
  528. return $this->jsonResponse('ok', $ret);
  529. }
  530. }