LinkStore.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 != '全部') {
  21. $list = $list->whereRaw("find_in_set('$area',area)");
  22. }
  23. $list = $list->order("hot", '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('hot', 'desc')->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. $data = $this->request->post("form");
  51. $info = LinkStoreModel::where("id", $data['id'])->update($data);
  52. return $this->success('修改成功', $info);
  53. }
  54. public function add(): \think\response\Json
  55. {
  56. $admin = $this->getAdmin();
  57. $data = $this->request->post('form');
  58. if ($data) {
  59. if (isset($data['id']) && $data['id']) { //更新
  60. return $this->update();
  61. } else {
  62. $data['create_time'] = date("Y-m-d H:i:s");
  63. $info = (new \app\model\LinkStoreModel)->insert($data);
  64. return $this->success('添加成功', $info);
  65. }
  66. }
  67. return $this->error('缺少数据');
  68. }
  69. public function getIcon(): \think\response\Json
  70. {
  71. $url = $this->request->post('url', false);
  72. if ($url) {
  73. if (mb_substr($url, 0, 4) == 'tab:') {
  74. } else {
  75. if (mb_substr($url, 0, 4) != 'http') {
  76. $url = 'https://' . $url;
  77. }
  78. $url = parse_url($url);
  79. $url = $url['host'];
  80. }
  81. $data = LinkStoreModel::whereRaw("FIND_IN_SET('$url',domain)")->find();
  82. if ($data) {
  83. return $this->success('ok', $data);
  84. }
  85. }
  86. return $this->error('no', '未查询到相关信息');
  87. }
  88. function install_num(): \think\response\Json
  89. {
  90. $id = $this->request->post('id', false);
  91. //给标签+=1
  92. $res = Db::table("linkstore")->where('id', $id)->inc('install_num')->update();
  93. if ($res) {
  94. return $this->success('ok');
  95. }
  96. return $this->error('fail');
  97. }
  98. function createFolder(): \think\response\Json
  99. {
  100. $type = $this->request->post('type', false);
  101. $this->getAdmin();
  102. if ($type === 'edit') {
  103. $form = $this->request->post('info');
  104. $id = $this->request->post('info.id', false);
  105. if ($id && $id > 0) {
  106. $model = LinkFolderModel::find($id);
  107. $model->update($form);
  108. } else {
  109. $model = new LinkFolderModel();
  110. $model->insert($form);
  111. }
  112. } else if ($type === 'del') {
  113. $id = $this->request->post('id');
  114. $result = LinkFolderModel::where("id", $id)->find();
  115. if ($result) {
  116. $result->delete();
  117. Db::query(
  118. "UPDATE linkstore
  119. SET area = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', area, ','), ',$id,', ','))
  120. WHERE FIND_IN_SET('$id', area) > 0;"
  121. );
  122. }
  123. }
  124. return $this->success('处理完毕!');
  125. }
  126. public function del(): \think\response\Json
  127. {
  128. $this->getAdmin();
  129. $ids = $this->request->post('ids', []);
  130. LinkStoreModel::where("id", 'in', $ids)->delete();
  131. return $this->success('删除成功');
  132. }
  133. }