LinkStore.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\model\LinkStoreModel;
  5. use think\facade\Db;
  6. class LinkStore extends BaseController
  7. {
  8. public function list(): \think\response\Json
  9. {
  10. $limit = $this->request->post('limit', 15);
  11. $name = $this->request->post('name', false);
  12. $area = $this->request->post('area', false);
  13. $sql = [];
  14. if ($name) {
  15. $sql[] = ['name', 'like', "%" . $name . "%"];
  16. }
  17. $list = LinkStoreModel::where($sql);
  18. //area需要使用find_in_set来匹配
  19. if ($area) {
  20. $list = $list->whereRaw("find_in_set('$area',area)");
  21. }
  22. $list = $list->order("hot", 'desc')->paginate($limit);
  23. return $this->success('ok', $list);
  24. }
  25. private function update(): \think\response\Json
  26. {
  27. $data = $this->request->post("form");
  28. $info = LinkStoreModel::where("id", $data['id'])->update($data);
  29. return $this->success('修改成功', $info);
  30. }
  31. public function add(): \think\response\Json
  32. {
  33. $admin = $this->getAdmin();
  34. $data = $this->request->post('form');
  35. if ($data) {
  36. if (isset($data['id']) && $data['id']) { //更新
  37. return $this->update();
  38. } else {
  39. $data['create_time'] = date("Y-m-d H:i:s");
  40. $info = (new \app\model\LinkStoreModel)->insert($data);
  41. return $this->success('添加成功', $info);
  42. }
  43. }
  44. return $this->error('缺少数据');
  45. }
  46. public function getIcon(): \think\response\Json
  47. {
  48. $url = $this->request->post('url', false);
  49. if ($url) {
  50. if (mb_substr($url, 0, 4) == 'tab:') {
  51. } else {
  52. if (mb_substr($url, 0, 4) != 'http') {
  53. $url = 'https://' . $url;
  54. }
  55. $url = parse_url($url);
  56. $url = $url['host'];
  57. }
  58. $data = LinkStoreModel::whereRaw("FIND_IN_SET('$url',domain)")->find();
  59. if ($data) {
  60. return $this->success('ok', $data);
  61. }
  62. }
  63. return $this->error('no', '未查询到相关信息');
  64. }
  65. function install_num(): \think\response\Json
  66. {
  67. $id = $this->request->post('id', false);
  68. //给标签+=1
  69. $res = Db::table("linkstore")->where('id', $id)->inc('install_num')->update();
  70. if ($res) {
  71. return $this->success('ok');
  72. }
  73. return $this->error('fail');
  74. }
  75. public function del(): \think\response\Json
  76. {
  77. $this->getAdmin();
  78. $ids = $this->request->post('ids', []);
  79. LinkStoreModel::where("id", 'in', $ids)->delete();
  80. return $this->success('删除成功');
  81. }
  82. }