1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace app\controller;
- use app\BaseController;
- use app\model\ConfigModel;
- use app\model\HistoryModel;
- use app\model\LinkModel;
- use app\model\TabbarModel;
- use app\model\UserSearchEngineModel;
- use think\facade\Cache;
- class Link extends BaseController
- {
- public function update(): \think\response\Json
- {
- $user = $this->getUser(true);
- if ($user) {
- $link = $this->request->post("link", []);
- if ($link) {
- $is = LinkModel::where("user_id", $user['user_id'])->find();
- if ($is) {
- $is->link = $link;
- $is->save();
- } else {
- LinkModel::create(["user_id" => $user['user_id'], "link" => $link]);
- }
- Cache::delete("Link.{$user['user_id']}");
- HistoryModel::create(["user_id" => $user['user_id'], "link" => $link]); //历史记录备份,用于用户误操作恢复用途
- return $this->success('ok');
- }
- }
- return $this->error('保存失败');
- }
- public function get(): \think\response\Json
- {
- $user = $this->getUser();
- if ($user) {
- $c = Cache::get("Link.{$user['user_id']}");
- if ($c) {
- return $this->success('ok', $c);
- }
- $data = LinkModel::where('user_id', $user['user_id'])->find();
- if ($data) {
- $c = $data['link'];
- Cache::tag("linkCache")->set("Link.{$user['user_id']}", $c, 60 * 60);
- return $this->success('ok', $c);
- }
- }
- $config = $this->Setting("defaultTab", 'static/defaultTab.json', true);
- if ($config) {
- $fp = public_path() . $config;
- if (file_exists($fp)) {
- $file = file_get_contents($fp);
- $json = json_decode($file, true);
- return $this->success('ok', $json['link'] ?? []);
- }
- }
- return $this->success('ok', []);
- }
- function refreshWebAppCache(): \think\response\Json
- {
- $this->getAdmin();
- Cache::tag('linkCache')->clear();
- return $this->success('刷新完毕');
- }
- public function reset(): \think\response\Json
- {
- $user = $this->getUser();
- if ($user) {
- $data = LinkModel::find($user['user_id']);
- if ($data) {
- Cache::delete("Link.{$user['user_id']}");
- $data->delete();
- }
- $data = TabbarModel::find($user['user_id']);
- if ($data) {
- $data->delete();
- }
- $data = ConfigModel::find($user['user_id']);
- if ($data) {
- $data->delete();
- }
- $data = UserSearchEngineModel::find($user['user_id']);
- if ($data) {
- $data->delete();
- }
- }
- return $this->success('ok');
- }
- }
|