SearchEngine.php 3.1 KB

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