ArticleController.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\Article;
  5. use app\model\ArticleCategory;
  6. class ArticleController extends Base
  7. {
  8. function index()
  9. {
  10. $weid = weid();
  11. $keyword = input('post.keyword', '', 'serach_in');
  12. $query = Article::where(['weid' => $weid]);
  13. if (!empty($keyword)) {
  14. $query->where('title', 'like', '%' . $keyword . '%');
  15. }
  16. $res = $query->order('sort asc,id asc')
  17. ->paginate(getpage())
  18. ->toArray();
  19. $data['data'] = $res;
  20. return $this->json($data);
  21. }
  22. function listUpdate()
  23. {
  24. $data = only('id,status,sort');
  25. if (!$data['id']) throw new ValidateException('参数错误');
  26. Article::update($data);
  27. return $this->json(['msg' => '操作成功']);
  28. }
  29. public function update()
  30. {
  31. $id = $this->request->post('id');
  32. $data = input('post.');
  33. unset($data['create_time']);
  34. if (empty($id)) {
  35. $data['weid'] = weid();
  36. try {
  37. $res = Article::create($data);
  38. if ($res->id && empty($data['sort'])) {
  39. Article::update(['sort' => $res->id, 'id' => $res->id]);
  40. }
  41. } catch (\Exception $e) {
  42. throw new ValidateException($e->getMessage());
  43. }
  44. return $this->json(['msg' => '添加成功', 'data' => $res->id]);
  45. } else {
  46. try {
  47. Article::update($data);
  48. } catch (\Exception $e) {
  49. throw new ValidateException($e->getMessage());
  50. }
  51. return $this->json(['msg' => '修改成功']);
  52. }
  53. }
  54. function getInfo()
  55. {
  56. $id = $this->request->post('id', '', 'serach_in');
  57. if (!$id) throw new ValidateException('参数错误');
  58. $data = Article::field('*')->find($id)->toArray();
  59. $data['content'] = \app\model\DomainReplace::setreplace($data['content']);
  60. return $this->json(['data' => $data]);
  61. }
  62. function delete()
  63. {
  64. return $this->del(new Article());
  65. }
  66. function getField()
  67. {
  68. $data['cidarray'] = ArticleCategory::getpagearray();
  69. return $this->json(['data' => $data]);
  70. }
  71. }