Link.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\model\ConfigModel;
  5. use app\model\HistoryModel;
  6. use app\model\LinkModel;
  7. use app\model\TabbarModel;
  8. use app\model\UserSearchEngineModel;
  9. use think\facade\Cache;
  10. class Link extends BaseController
  11. {
  12. public function update(): \think\response\Json
  13. {
  14. $user = $this->getUser(true);
  15. if ($user) {
  16. $link = $this->request->post("link", []);
  17. if ($link) {
  18. $is = LinkModel::where("user_id", $user['user_id'])->find();
  19. if ($is) {
  20. $is->link = $link;
  21. $is->save();
  22. } else {
  23. LinkModel::create(["user_id" => $user['user_id'], "link" => $link]);
  24. }
  25. Cache::delete("Link.{$user['user_id']}");
  26. HistoryModel::create(["user_id" => $user['user_id'], "link" => $link]); //历史记录备份,用于用户误操作恢复用途
  27. return $this->success('ok');
  28. }
  29. }
  30. return $this->error('保存失败');
  31. }
  32. public function get(): \think\response\Json
  33. {
  34. $user = $this->getUser();
  35. if ($user) {
  36. $c = Cache::get("Link.{$user['user_id']}");
  37. if ($c) {
  38. return $this->success('ok', $c);
  39. }
  40. $data = LinkModel::where('user_id', $user['user_id'])->find();
  41. if ($data) {
  42. $c = $data['link'];
  43. Cache::tag("linkCache")->set("Link.{$user['user_id']}", $c, 60 * 60);
  44. return $this->success('ok', $c);
  45. }
  46. }
  47. $config = $this->Setting("defaultTab", 'static/defaultTab.json', true);
  48. if ($config) {
  49. $fp = public_path() . $config;
  50. if (file_exists($fp)) {
  51. $file = file_get_contents($fp);
  52. $json = json_decode($file, true);
  53. return $this->success('ok', $json['link'] ?? []);
  54. }
  55. }
  56. return $this->success('ok', []);
  57. }
  58. function refreshWebAppCache(): \think\response\Json
  59. {
  60. $this->getAdmin();
  61. Cache::tag('linkCache')->clear();
  62. return $this->success('刷新完毕');
  63. }
  64. public function reset(): \think\response\Json
  65. {
  66. $user = $this->getUser();
  67. if ($user) {
  68. $data = LinkModel::find($user['user_id']);
  69. if ($data) {
  70. Cache::delete("Link.{$user['user_id']}");
  71. $data->delete();
  72. }
  73. $data = TabbarModel::find($user['user_id']);
  74. if ($data) {
  75. $data->delete();
  76. }
  77. $data = ConfigModel::find($user['user_id']);
  78. if ($data) {
  79. $data->delete();
  80. }
  81. $data = UserSearchEngineModel::find($user['user_id']);
  82. if ($data) {
  83. $data->delete();
  84. }
  85. }
  86. return $this->success('ok');
  87. }
  88. }