Link.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\model\HistoryModel;
  5. use app\model\LinkModel;
  6. use app\model\SettingModel;
  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 = joinPath(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. }
  62. return $this->success('ok');
  63. }
  64. }