NewsController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace SixShop\News\Controller\Admin;
  3. use think\Request;
  4. use think\facade\Validate;
  5. use SixShop\News\Service\NewsServiceAdapter;
  6. class NewsController
  7. {
  8. protected $service;
  9. public function __construct()
  10. {
  11. $this->service = new NewsServiceAdapter();
  12. }
  13. /**
  14. * 文章列表
  15. */
  16. public function index(Request $request)
  17. {
  18. $where = [];
  19. if ($request->get('category_id')) {
  20. $where['category_id'] = $request->get('category_id');
  21. }
  22. if ($request->get('status') !== null) {
  23. $where['status'] = $request->get('status');
  24. }
  25. $limit = (int)$request->get('limit', 20);
  26. $data = $this->service->getList($where, ['is_top' => 'desc','id' => 'desc'], $limit);
  27. // 从数据库获取分类数据
  28. $categoryMap = [];
  29. try {
  30. // 使用ThinkPHP的Db类查询数据库
  31. $categories = \think\facade\Db::table('cy_news_category')
  32. ->where('status', 1)
  33. ->whereNull('delete_time')
  34. ->field('id,name')
  35. ->select()
  36. ->toArray();
  37. // 构建分类映射
  38. foreach ($categories as $category) {
  39. $categoryMap[$category['id']] = $category['name'];
  40. }
  41. } catch (\Exception $e) {
  42. // 如果查询失败,使用默认映射
  43. error_log('获取分类数据失败: ' . $e->getMessage());
  44. $categoryMap = [
  45. 1 => '默认分类',
  46. 2 => '新闻分类',
  47. 3 => '公告分类'
  48. ];
  49. }
  50. // 字段映射:数据库cover_image -> 前端cover,添加分类名称
  51. if (is_array($data) && isset($data['data'])) {
  52. foreach ($data['data'] as &$item) {
  53. if (isset($item['cover_image'])) {
  54. $item['cover'] = $item['cover_image'];
  55. }
  56. if (isset($item['category_id']) && isset($categoryMap[$item['category_id']])) {
  57. $item['category_name'] = $categoryMap[$item['category_id']];
  58. }
  59. }
  60. } elseif (is_array($data) && isset($data['list'])) {
  61. foreach ($data['list'] as &$item) {
  62. if (isset($item['cover_image'])) {
  63. $item['cover'] = $item['cover_image'];
  64. }
  65. if (isset($item['category_id']) && isset($categoryMap[$item['category_id']])) {
  66. $item['category_name'] = $categoryMap[$item['category_id']];
  67. }
  68. }
  69. } elseif (is_array($data)) {
  70. foreach ($data as &$item) {
  71. if (isset($item['cover_image'])) {
  72. $item['cover'] = $item['cover_image'];
  73. }
  74. if (isset($item['category_id']) && isset($categoryMap[$item['category_id']])) {
  75. $item['category_name'] = $categoryMap[$item['category_id']];
  76. }
  77. }
  78. }
  79. return json(['code' => 200, 'msg' => 'success', 'data' => $data]);
  80. }
  81. /**
  82. * 文章详情
  83. */
  84. public function read($id)
  85. {
  86. $info = $this->service->getById($id);
  87. if (!$info) {
  88. return json(['code' => 404, 'msg' => '文章不存在']);
  89. }
  90. // 字段映射:数据库cover_image -> 前端cover
  91. if (isset($info['cover_image'])) {
  92. $info['cover'] = $info['cover_image'];
  93. }
  94. return json(['code' => 200, 'msg' => 'success', 'data' => $info]);
  95. }
  96. /**
  97. * 新增文章
  98. */
  99. public function create(Request $request)
  100. {
  101. $data = $request->only(['category_id','title','cover','summary','content','author','is_top','status']);
  102. $validate = Validate::rule([
  103. 'category_id' => 'require|number',
  104. 'title' => 'require|max:128',
  105. 'cover' => 'max:255',
  106. 'summary' => 'max:255',
  107. 'content' => 'require',
  108. 'author' => 'max:64',
  109. 'is_top' => 'in:0,1',
  110. 'status' => 'in:0,1,2',
  111. ]);
  112. if (!$validate->check($data)) {
  113. return json(['code' => 422, 'msg' => $validate->getError()]);
  114. }
  115. // 字段映射:前端cover -> 数据库cover_image
  116. if (isset($data['cover'])) {
  117. $data['cover_image'] = $data['cover'];
  118. unset($data['cover']);
  119. }
  120. // 不传递时间字段,让 Go 服务自动设置
  121. // Go 服务会在创建时自动设置 create_time 和 update_time
  122. $res = $this->service->create($data);
  123. return json(['code' => 200, 'msg' => '创建成功', 'data' => $res]);
  124. }
  125. /**
  126. * 编辑文章
  127. */
  128. public function update($id, Request $request)
  129. {
  130. $data = $request->only(['category_id','title','cover','summary','content','author','is_top','status']);
  131. $validate = Validate::rule([
  132. 'category_id' => 'number',
  133. 'title' => 'max:128',
  134. 'cover' => 'max:255',
  135. 'summary' => 'max:255',
  136. 'author' => 'max:64',
  137. 'is_top' => 'in:0,1',
  138. 'status' => 'in:0,1,2',
  139. ]);
  140. if (!$validate->check($data)) {
  141. return json(['code' => 422, 'msg' => $validate->getError()]);
  142. }
  143. // 字段映射:前端cover -> 数据库cover_image
  144. if (isset($data['cover'])) {
  145. $data['cover_image'] = $data['cover'];
  146. unset($data['cover']);
  147. }
  148. // 不传递时间字段,让 Go 服务自动设置
  149. // Go 服务会在更新时自动设置 update_time
  150. $res = $this->service->update($id, $data);
  151. if (!$res) {
  152. return json(['code' => 404, 'msg' => '文章不存在']);
  153. }
  154. return json(['code' => 200, 'msg' => '更新成功', 'data' => $res]);
  155. }
  156. /**
  157. * 删除文章
  158. */
  159. public function delete($id)
  160. {
  161. $res = $this->service->delete($id);
  162. if (!$res) {
  163. return json(['code' => 404, 'msg' => '文章不存在']);
  164. }
  165. return json(['code' => 200, 'msg' => '删除成功']);
  166. }
  167. }