Link.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. class Link extends BaseController
  9. {
  10. public function update(): \think\response\Json
  11. {
  12. $user = $this->getUser(true);
  13. if ($user) {
  14. $link = $this->request->post("link", []);
  15. if ($link) {
  16. $is = LinkModel::where("user_id", $user['user_id'])->find();
  17. if ($is) {
  18. $is->link = $link;
  19. $is->save();
  20. } else {
  21. LinkModel::create(["user_id" => $user['user_id'], "link" => $link]);
  22. }
  23. HistoryModel::create(["user_id" => $user['user_id'], "link" => $link]); //历史记录备份,用于用户误操作恢复用途
  24. return $this->success('ok');
  25. }
  26. }
  27. return $this->error('保存失败');
  28. }
  29. public function get(): \think\response\Json
  30. {
  31. $user = $this->getUser();
  32. if ($user) {
  33. $data = LinkModel::find($user['user_id']);
  34. if ($data) {
  35. return $this->success('ok', $data['link']);
  36. }
  37. }
  38. $config = $this->Setting("defaultTab", 'static/defaultTab.json', true);
  39. if ($config) {
  40. $fp = public_path() . $config;
  41. if (file_exists($fp)) {
  42. $file = file_get_contents($fp);
  43. $json = json_decode($file, true);
  44. return $this->success('ok', $json['link'] ?? []);
  45. }
  46. }
  47. return $this->success('ok', []);
  48. }
  49. public function reset(): \think\response\Json
  50. {
  51. $user = $this->getUser();
  52. if ($user) {
  53. $data = LinkModel::find($user['user_id']);
  54. if ($data) {
  55. $data->delete();
  56. }
  57. $data = TabbarModel::find($user['user_id']);
  58. if ($data) {
  59. $data->delete();
  60. }
  61. $data = ConfigModel::find($user['user_id']);
  62. if ($data) {
  63. $data->delete();
  64. }
  65. }
  66. return $this->success('ok');
  67. }
  68. }