LinkStore.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\model\ConfigModel;
  5. use app\model\LinkFolderModel;
  6. use app\model\LinkStoreModel;
  7. use think\facade\Db;
  8. class LinkStore extends BaseController
  9. {
  10. public function list(): \think\response\Json
  11. {
  12. $limit = $this->request->post('limit', 15);
  13. $name = $this->request->post('name', false);
  14. $area = $this->request->post('area', false);
  15. $sql = [];
  16. if ($name) {
  17. $sql[] = ['name|tips', 'like', "%" . $name . "%"];
  18. }
  19. $list = LinkStoreModel::where($sql);
  20. //area需要使用find_in_set来匹配
  21. if ($area && $area != 0) {
  22. $list = $list->whereRaw("find_in_set('$area',area)");
  23. }
  24. $list = $list->order("create_time", 'desc')->paginate($limit);
  25. return $this->success('ok', $list);
  26. }
  27. public function ListManager(): \think\response\Json
  28. {
  29. $admin = $this->getAdmin();
  30. $limit = $this->request->post('limit', 15);
  31. $name = $this->request->post('search.name', false);
  32. $area = $this->request->post('search.area', false);
  33. $sql = [];
  34. if ($name) {
  35. $sql[] = ['name|tips', 'like', '%' . $name . '%'];
  36. }
  37. $list = LinkStoreModel::where($sql);
  38. //area需要使用find_in_set来匹配
  39. if ($area && $area != '全部') {
  40. $list = $list->whereRaw("find_in_set('$area',area)");
  41. }
  42. $list = $list->order($this->request->post('sort.prop', 'id'), $this->request->post('sort.order', 'asc'))->paginate($limit);
  43. return json(["msg" => "ok", 'data' => $list, 'auth' => $this->auth]);
  44. }
  45. function getFolder(): \think\response\Json
  46. {
  47. return $this->success("ok", LinkFolderModel::order("sort", "desc")->select());
  48. }
  49. private function update(): \think\response\Json
  50. {
  51. is_demo_mode(true);
  52. $admin = $this->getAdmin();
  53. $data = $this->request->post("form");
  54. $info = LinkStoreModel::where("id", $data['id'])->update($data);
  55. return $this->success('修改成功', $info);
  56. }
  57. public function add(): \think\response\Json
  58. {
  59. $admin = $this->getAdmin();
  60. is_demo_mode(true);
  61. $data = $this->request->post('form');
  62. if ($data) {
  63. if (isset($data['id']) && $data['id']) { //更新
  64. return $this->update();
  65. } else {
  66. $data['create_time'] = date("Y-m-d H:i:s");
  67. $info = (new \app\model\LinkStoreModel)->insert($data);
  68. return $this->success('添加成功', $info);
  69. }
  70. }
  71. return $this->error('缺少数据');
  72. }
  73. public function getIcon(): \think\response\Json
  74. {
  75. $url = $this->request->post('url', false);
  76. if ($url) {
  77. if (mb_substr($url, 0, 4) == 'tab:') {
  78. } else {
  79. if (mb_substr($url, 0, 4) != 'http') {
  80. $url = 'https://' . $url;
  81. }
  82. $url = parse_url($url);
  83. $url = $url['host'];
  84. }
  85. $data = LinkStoreModel::whereRaw("FIND_IN_SET('$url',domain)")->find();
  86. if ($data) {
  87. return $this->success('ok', $data);
  88. }
  89. }
  90. return $this->error('no', '未查询到相关信息');
  91. }
  92. function install_num(): \think\response\Json
  93. {
  94. $id = $this->request->post('id', false);
  95. //给标签+=1
  96. $res = Db::table("linkstore")->where('id', $id)->inc('install_num')->update();
  97. if ($res) {
  98. return $this->success('ok');
  99. }
  100. return $this->error('fail');
  101. }
  102. function createFolder(): \think\response\Json
  103. {
  104. is_demo_mode(true);
  105. $type = $this->request->post('type', false);
  106. $this->getAdmin();
  107. if ($type === 'edit') {
  108. $form = $this->request->post('info');
  109. $id = $this->request->post('info.id', false);
  110. if ($id && $id > 0) {
  111. $model = LinkFolderModel::find($id);
  112. $model->update($form);
  113. } else {
  114. $model = new LinkFolderModel();
  115. $model->insert($form);
  116. }
  117. } else if ($type === 'del') {
  118. $id = $this->request->post('id');
  119. $result = LinkFolderModel::where("id", $id)->find();
  120. if ($result) {
  121. $result->delete();
  122. Db::query(
  123. "UPDATE linkstore
  124. SET area = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', area, ','), ',$id,', ','))
  125. WHERE FIND_IN_SET('$id', area) > 0;"
  126. );
  127. }
  128. }
  129. return $this->success('处理完毕!');
  130. }
  131. function moveFolder(): \think\response\Json
  132. {
  133. is_demo_mode(true);
  134. $this->getAdmin();
  135. $ids = $this->request->post('link', []);
  136. $area = $this->request->post('area', '');
  137. LinkStoreModel::where('id', 'in', $ids)->update(['area' => $area]);
  138. return $this->success('处理完毕!');
  139. }
  140. function sortFolder()
  141. {
  142. $sort = (array)$this->request->post();
  143. foreach ($sort as $key => $value) {
  144. LinkFolderModel::where("id", $value['id'])->update(['sort' => $value['sort']]);
  145. }
  146. return $this->success("ok");
  147. }
  148. public function del(): \think\response\Json
  149. {
  150. is_demo_mode(true);
  151. $this->getAdmin();
  152. $ids = $this->request->post('ids', []);
  153. LinkStoreModel::where("id", 'in', $ids)->delete();
  154. return $this->success('删除成功');
  155. }
  156. }