Link.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. HistoryModel::create(['user_id' => $user['user_id'], 'link' => $is['link']]); //历史记录备份,用于用户误操作恢复用途
  21. $is->link = $link;
  22. $is->save();
  23. } else {
  24. LinkModel::create(["user_id" => $user['user_id'], "link" => $link]);
  25. }
  26. Cache::delete("Link.{$user['user_id']}");
  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->systemSetting("defaultTab", 'static/defaultTab.json', true);
  48. if ($config) {
  49. $fp = public_path() . $config;
  50. if (!file_exists($fp)) {
  51. $fp = public_path() . "static/defaultTab.json";
  52. }
  53. if (file_exists($fp)) {
  54. $file = file_get_contents($fp);
  55. $json = json_decode($file, true);
  56. return $this->success('ok', $json['link'] ?? []);
  57. }
  58. }
  59. return $this->success('ok', []);
  60. }
  61. function refreshWebAppCache(): \think\response\Json
  62. {
  63. $this->getAdmin();
  64. Cache::tag('linkCache')->clear();
  65. return $this->success('刷新完毕');
  66. }
  67. public function history(): \think\response\Json
  68. {
  69. $user = $this->getUser(true);
  70. $history = HistoryModel::where("user_id", $user['user_id'])->whereNotNull("create_time")->field('id,user_id,create_time')->limit(100)->order("id", "desc")->select();
  71. return $this->success('ok', $history);
  72. }
  73. public function delBack(): \think\response\Json
  74. {
  75. $user = $this->getUser(true);
  76. $id = $this->request->post('id');
  77. if ($id) {
  78. $res = HistoryModel::where('id', $id)->where('user_id', $user['user_id'])->delete();
  79. if ($res) {
  80. return $this->success('ok');
  81. }
  82. }
  83. return $this->error('备份节点不存在');
  84. }
  85. public function rollBack(): \think\response\Json
  86. {
  87. $user = $this->getUser(true);
  88. $id = $this->request->post("id");
  89. if ($id) {
  90. $res = HistoryModel::where('id', $id)->where("user_id", $user['user_id'])->find();
  91. if ($res) {
  92. $link = $res['link'];
  93. Cache::delete("Link.{$user['user_id']}");
  94. LinkModel::update(["user_id" => $user['user_id'], "link" => $link]);
  95. return $this->success('ok');
  96. }
  97. }
  98. return $this->error("备份节点不存在");
  99. }
  100. public function reset(): \think\response\Json
  101. {
  102. $user = $this->getUser();
  103. if ($user) {
  104. $data = LinkModel::find($user['user_id']);
  105. if ($data) {
  106. Cache::delete("Link.{$user['user_id']}");
  107. $data->delete();
  108. }
  109. $data = TabbarModel::find($user['user_id']);
  110. if ($data) {
  111. $data->delete();
  112. }
  113. $data = ConfigModel::find($user['user_id']);
  114. if ($data) {
  115. $data->delete();
  116. }
  117. $data = UserSearchEngineModel::find($user['user_id']);
  118. if ($data) {
  119. $data->delete();
  120. }
  121. }
  122. return $this->success('ok');
  123. }
  124. }