SearchEngine.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\model\SearchEngineModel;
  5. use app\model\UserSearchEngineModel;
  6. use think\facade\Cache;
  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. Cache::delete("searchEngine");
  38. return $this->success("保存成功!");
  39. }
  40. return $this->error('缺少数据');
  41. }
  42. function del(): \think\response\Json
  43. {
  44. is_demo_mode(true);
  45. $this->getAdmin();
  46. $ids = $this->request->post('ids', []);
  47. SearchEngineModel::where('id', 'in', $ids)->delete();
  48. Cache::delete("searchEngine");
  49. return $this->success('删除成功');
  50. }
  51. function searchEngine(): \think\response\Json
  52. {
  53. $user = $this->getUser();
  54. if ($user) {
  55. $data = UserSearchEngineModel::find($user['user_id']);
  56. if ($data) {
  57. return $this->success('ok', $data['list']);
  58. }
  59. }
  60. $list = Cache::get("searchEngine", false);
  61. if (!$list) {
  62. $list = SearchEngineModel::where('status', 1)->order('sort', 'desc')->limit(10)->select()->toArray();
  63. Cache::set("searchEngine", $list, 60 * 60 * 24);
  64. }
  65. return $this->success('ok', $list);
  66. }
  67. function saveSearchEngine(): \think\response\Json
  68. {
  69. $user = $this->getUser(true);
  70. if ($user) {
  71. $config = $this->request->post('searchEngine', []);
  72. if ($config) {
  73. $is = UserSearchEngineModel::where('user_id', $user['user_id'])->find();
  74. if ($is) {
  75. $is->list = $config;
  76. $is->force()->save();
  77. } else {
  78. UserSearchEngineModel::create(['user_id' => $user['user_id'], 'list' => $config]);
  79. }
  80. return $this->success('ok');
  81. }
  82. }
  83. return $this->error('保存失败');
  84. }
  85. function sort(): \think\response\Json
  86. {
  87. $sort = (array)$this->request->post();
  88. foreach ($sort as $key => $value) {
  89. SearchEngineModel::where("id", $value['id'])->update(['sort' => $value['sort']]);
  90. }
  91. Cache::delete('searchEngine');
  92. return $this->success("ok");
  93. }
  94. }