Config.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. use stdClass;
  8. class Config extends BaseController
  9. {
  10. public function update(): \think\response\Json
  11. {
  12. $user = $this->getUser(true);
  13. if ($user) {
  14. $config = $this->request->post("config", []);
  15. if ($config) {
  16. $is = ConfigModel::where("user_id", $user['user_id'])->find();
  17. if ($is) {
  18. $is->config = $config;
  19. $is->save();
  20. } else {
  21. ConfigModel::create(["user_id" => $user['user_id'], "config" => $config]);
  22. }
  23. return $this->success('ok');
  24. }
  25. }
  26. return $this->error('保存失败');
  27. }
  28. public function get(): \think\response\Json
  29. {
  30. $user = $this->getUser();
  31. if ($user) {
  32. $data = ConfigModel::find($user['user_id']);
  33. if ($data) {
  34. return $this->success("ok", $data['config']);
  35. }
  36. }
  37. $config = $this->systemSetting('defaultTab', 'static/defaultTab.json', true);
  38. if ($config) {
  39. $fp = public_path() . $config;
  40. if (!file_exists($fp)) {
  41. $fp = public_path() . 'static/defaultTab.json';
  42. }
  43. if (file_exists($fp)) {
  44. $file = file_get_contents($fp);
  45. $json = json_decode($file, true);
  46. if (isset($json['config'])) {
  47. return $this->success('noLogin', $json['config']);
  48. }
  49. }
  50. }
  51. return $this->success('no Config', new stdClass());
  52. }
  53. }