Link.php 4.8 KB

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