SearchEngine.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\model\LinkStoreModel;
  5. use app\model\SearchEngineModel;
  6. use app\model\UserSearchEngineModel;
  7. class SearchEngine extends BaseController
  8. {
  9. function index(): \think\response\Json
  10. {
  11. $list = SearchEngineModel::where("status", 1)->order('sort', 'desc')->select();
  12. return $this->success("ok", $list);
  13. }
  14. public function list(): \think\response\Json
  15. {
  16. $this->getAdmin();
  17. $name = $this->request->post('search.name', false);
  18. $sql = [];
  19. if ($name) {
  20. $sql[] = ['name|tips', 'like', '%' . $name . '%'];
  21. }
  22. $list = SearchEngineModel::where($sql);
  23. $list = $list->order('sort', 'desc')->select();
  24. return $this->success('ok', $list);
  25. }
  26. function add(): \think\response\Json
  27. {
  28. is_demo_mode(true);
  29. $this->getAdmin();
  30. $data = $this->request->post('form');
  31. if ($data) {
  32. $model = new SearchEngineModel();
  33. if (isset($data['id']) && $data['id']) { //更新
  34. $model = $model->find($data['id']);
  35. }
  36. $model->save($data);
  37. return $this->success("保存成功!");
  38. }
  39. return $this->error('缺少数据');
  40. }
  41. function del(): \think\response\Json
  42. {
  43. is_demo_mode(true);
  44. $this->getAdmin();
  45. $ids = $this->request->post('ids', []);
  46. SearchEngineModel::where('id', 'in', $ids)->delete();
  47. return $this->success('删除成功');
  48. }
  49. function searchEngine(): \think\response\Json
  50. {
  51. $user = $this->getUser();
  52. if ($user) {
  53. $data = UserSearchEngineModel::find($user['user_id']);
  54. if ($data) {
  55. return $this->success('ok', $data['list']);
  56. }
  57. }
  58. $list = SearchEngineModel::where('status', 1)->order('sort', 'desc')->limit(10)->select()->toArray();
  59. return $this->success('ok', $list);
  60. }
  61. function saveSearchEngine(): \think\response\Json
  62. {
  63. $user = $this->getUser(true);
  64. if ($user) {
  65. $config = $this->request->post('searchEngine', []);
  66. if ($config) {
  67. $is = UserSearchEngineModel::where('user_id', $user['user_id'])->find();
  68. if ($is) {
  69. $is->list = $config;
  70. $is->force()->save();
  71. } else {
  72. UserSearchEngineModel::create(['user_id' => $user['user_id'], 'list' => $config]);
  73. }
  74. return $this->success('ok');
  75. }
  76. }
  77. return $this->error('保存失败');
  78. }
  79. }