LinkStore.php 4.8 KB

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