Config.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\model\ConfigModel;
  5. use app\model\SearchEngineModel;
  6. use app\model\UserSearchEngineModel;
  7. class Config extends BaseController
  8. {
  9. public function update(): \think\response\Json
  10. {
  11. $user = $this->getUser(true);
  12. if ($user) {
  13. $config = $this->request->post("config", []);
  14. if ($config) {
  15. $is = ConfigModel::where("user_id", $user['user_id'])->find();
  16. if ($is) {
  17. $is->config = $config;
  18. $is->save();
  19. } else {
  20. ConfigModel::create(["user_id" => $user['user_id'], "config" => $config]);
  21. }
  22. return $this->success('ok');
  23. }
  24. }
  25. return $this->error('保存失败');
  26. }
  27. public function get(): \think\response\Json
  28. {
  29. $user = $this->getUser();
  30. if ($user) {
  31. $data = ConfigModel::find($user['user_id']);
  32. if ($data) {
  33. return $this->success("ok", $data['config']);
  34. }
  35. }
  36. $config = $this->Setting('defaultTab', 'static/defaultTab.json', true);
  37. if ($config) {
  38. $fp = public_path() . $config;
  39. if (file_exists($fp)) {
  40. $file = file_get_contents($fp);
  41. $json = json_decode($file, true);
  42. if (isset($json['config'])) {
  43. return $this->success('ok', $json['config']);
  44. }
  45. }
  46. }
  47. return $this->error('no Config');
  48. }
  49. }