LinkStore.php 5.5 KB

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