Link.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. $error = "";
  16. try {
  17. if ($user) {
  18. $link = $this->request->post("link", []);
  19. if (is_array($link)) {
  20. $is = LinkModel::where("user_id", $user['user_id'])->find();
  21. if ($is) {
  22. HistoryModel::create(['user_id' => $user['user_id'], 'link' => $is['link'], 'create_time' => date("Y-m-d H:i:s")]); //历史记录备份,用于用户误操作恢复用途
  23. $ids = HistoryModel::where("user_id", $user['user_id'])->order("id", 'desc')->limit(50)->select()->toArray();
  24. $ids = array_column($ids, "id");
  25. HistoryModel::where("user_id", $user['user_id'])->whereNotIn("id", $ids)->delete();
  26. $is->link = $link;
  27. $is->save();
  28. } else {
  29. LinkModel::create(["user_id" => $user['user_id'], "link" => $link]);
  30. }
  31. Cache::delete("Link.{$user['user_id']}");
  32. return $this->success('ok');
  33. }
  34. }
  35. } catch (\Throwable $th) {
  36. $error = $th->getMessage();
  37. }
  38. return $this->error('保存失败'.$error);
  39. }
  40. public function get(): \think\response\Json
  41. {
  42. $user = $this->getUser();
  43. if ($user) {
  44. $c = Cache::get("Link.{$user['user_id']}");
  45. if ($c) {
  46. return $this->success('ok', $c);
  47. }
  48. $data = LinkModel::where('user_id', $user['user_id'])->find();
  49. if ($data) {
  50. $c = $data['link'];
  51. Cache::tag("linkCache")->set("Link.{$user['user_id']}", $c, 60 * 60);
  52. return $this->success('ok', $c);
  53. }
  54. }
  55. $config = $this->systemSetting("defaultTab", 'static/defaultTab.json', true);
  56. if ($config) {
  57. $fp = public_path() . $config;
  58. if (!file_exists($fp)) {
  59. $fp = public_path() . "static/defaultTab.json";
  60. }
  61. if (file_exists($fp)) {
  62. $file = file_get_contents($fp);
  63. $json = json_decode($file, true);
  64. return $this->success('ok', $json['link'] ?? []);
  65. }
  66. }
  67. return $this->success('ok', []);
  68. }
  69. function refreshWebAppCache(): \think\response\Json
  70. {
  71. $this->getAdmin();
  72. Cache::tag('linkCache')->clear();
  73. return $this->success('刷新完毕');
  74. }
  75. public function history(): \think\response\Json
  76. {
  77. $user = $this->getUser(true);
  78. $history = HistoryModel::where("user_id", $user['user_id'])->whereNotNull("create_time")->field('id,user_id,create_time')->limit(100)->order("id", "desc")->select();
  79. return $this->success('ok', $history);
  80. }
  81. public function delBack(): \think\response\Json
  82. {
  83. $user = $this->getUser(true);
  84. $id = $this->request->post('id');
  85. if ($id) {
  86. $res = HistoryModel::where('id', $id)->where('user_id', $user['user_id'])->delete();
  87. if ($res) {
  88. return $this->success('ok');
  89. }
  90. }
  91. return $this->error('备份节点不存在');
  92. }
  93. public function rollBack(): \think\response\Json
  94. {
  95. $user = $this->getUser(true);
  96. $id = $this->request->post("id");
  97. if ($id) {
  98. $res = HistoryModel::where('id', $id)->where("user_id", $user['user_id'])->find();
  99. if ($res) {
  100. $link = $res['link'];
  101. Cache::delete("Link.{$user['user_id']}");
  102. LinkModel::update(["user_id" => $user['user_id'], "link" => $link]);
  103. return $this->success('ok');
  104. }
  105. }
  106. return $this->error("备份节点不存在");
  107. }
  108. public function reset(): \think\response\Json
  109. {
  110. $user = $this->getUser();
  111. if ($user) {
  112. $data = LinkModel::find($user['user_id']);
  113. if ($data) {
  114. Cache::delete("Link.{$user['user_id']}");
  115. $data->delete();
  116. }
  117. $data = TabbarModel::find($user['user_id']);
  118. if ($data) {
  119. $data->delete();
  120. }
  121. $data = ConfigModel::find($user['user_id']);
  122. if ($data) {
  123. $data->delete();
  124. }
  125. $data = UserSearchEngineModel::find($user['user_id']);
  126. if ($data) {
  127. $data->delete();
  128. }
  129. }
  130. return $this->success('ok');
  131. }
  132. }