LinkStore.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\model\ConfigModel;
  5. use app\model\FileModel;
  6. use app\model\LinkFolderModel;
  7. use app\model\LinkStoreModel;
  8. use think\facade\Db;
  9. class LinkStore extends BaseController
  10. {
  11. public function list(): \think\response\Json
  12. {
  13. $user = $this->getUser();
  14. $limit = $this->request->post('limit', 12);
  15. $name = $this->request->post('name', false);
  16. $area = $this->request->post('area', false);
  17. $sql = [];
  18. if ($name) {
  19. $sql[] = ['name|tips|url', 'like', '%' . $name . '%'];
  20. }
  21. $list = LinkStoreModel::where($sql)->where('status', 1)->withoutField('user_id');
  22. // 使用 find_in_set 匹配 area
  23. if ($area && $area != 0) {
  24. $list = $list->whereRaw('find_in_set(?,area)', [$area]);
  25. }
  26. // 将两个 whereOrRaw 条件组合在一起
  27. $list = $list->where(function ($query) use ($user) {
  28. $query->whereRaw('find_in_set(0,group_ids)');
  29. if ($user) {
  30. $query->whereOrRaw('find_in_set(?,group_ids)', [$user['group_id']]);
  31. }
  32. });
  33. $list = $list->order(['hot' => 'desc', 'create_time' => 'desc'])->paginate($limit);
  34. return $this->success('ok', $list);
  35. }
  36. public function ListManager(): \think\response\Json
  37. {
  38. $admin = $this->getAdmin();
  39. $limit = $this->request->post('limit', 15);
  40. $name = $this->request->post('search.name', false);
  41. $area = $this->request->post('search.area', false);
  42. $group_id = $this->request->post('search.group_id',false);
  43. $sql = [];
  44. if ($name) {
  45. $sql[] = ['name|tips', 'like', '%' . $name . '%'];
  46. }
  47. $list = LinkStoreModel::with(['userInfo'])->where($sql);
  48. //area需要使用find_in_set来匹配
  49. if ($area && $area != '全部') {
  50. $list = $list->whereRaw("find_in_set(?,area)", [$area]);
  51. }
  52. if($group_id){
  53. $list = $list->whereRaw('find_in_set(?,group_ids)', [$group_id]);
  54. }
  55. $list = $list->order($this->request->post('sort.prop', 'id'), $this->request->post('sort.order', 'asc'))->paginate($limit);
  56. return json(["msg" => "ok", 'data' => $list, 'auth' => $this->auth]);
  57. }
  58. function getFolder(): \think\response\Json
  59. {
  60. $user = $this->getUser();
  61. $list = new LinkFolderModel();
  62. $list = $list->whereOrRaw("find_in_set(0,group_ids)");
  63. if ($user&&(int)$user['group_id'] != 0) {
  64. $list = $list->whereOrRaw('find_in_set(?,group_ids)', [$user['group_id']]);
  65. }
  66. return $this->success("ok", $list->order('sort', 'desc')->select());
  67. }
  68. function getFolderAdmin(): \think\response\Json
  69. {
  70. $user = $this->getAdmin();
  71. $list = new LinkFolderModel();
  72. return $this->success('ok', $list->order('sort', 'desc')->select());
  73. }
  74. private function update(): \think\response\Json
  75. {
  76. is_demo_mode(true);
  77. $admin = $this->getAdmin();
  78. $data = $this->request->post("form");
  79. try {
  80. unset($data['userInfo']);
  81. } catch (\Exception $exception) {
  82. }
  83. $info = LinkStoreModel::where("id", $data['id'])->withoutField(['userInfo'])->find();
  84. $info->update($data);
  85. return $this->success('修改成功', $info);
  86. }
  87. function addPublic(): \think\response\Json
  88. {
  89. $user = $this->getAdmin();
  90. $info = $this->request->post();
  91. $info['create_time'] = date("Y-m-d H:i:s");
  92. $info['domain'] = $this->getDomain($info['url']);
  93. $info['src'] = $this->downloadLogo($info['src']);
  94. FileModel::addFile($info['src'], $user['id']);
  95. if (isset($info['id'])) {
  96. unset($info['id']);
  97. }
  98. (new \app\model\LinkStoreModel)->allowField(["name", "src", "url", "domain", "create_time", "tips", "app"])->insert($info);
  99. return $this->success('添加成功', $info);
  100. }
  101. private function downloadLogo($src): string
  102. {
  103. $f = file_get_contents($src);
  104. $pathinfo = pathinfo($src);
  105. try {
  106. mkdir(public_path() . 'images/' . date("Y/m/d"), 0755, true);
  107. } catch (\Throwable $th) {
  108. //throw $th;
  109. }
  110. $filePath = '/images/' . date("Y/m/d") . '/' . md5($src) . '.' . $pathinfo['extension'];
  111. file_put_contents(joinPath(public_path(), $filePath), $f);
  112. return $filePath;
  113. }
  114. function push(): \think\response\Json
  115. {
  116. $user = $this->getUser(true);
  117. $data = $this->request->post();
  118. $info = [];
  119. if ($data) {
  120. if (isset($data['name'])) {
  121. $info['name'] = $data['name'];
  122. }
  123. if (isset($data['src'])) {
  124. $info['src'] = $data['src'];
  125. }
  126. if (isset($data['url']) && mb_strlen($data['url']) > 2) {
  127. $info['url'] = $data['url'];
  128. } else {
  129. return $this->error('推送失败');
  130. }
  131. if (isset($data['bgColor'])) {
  132. $info['bgColor'] = $data['bgColor'];
  133. }
  134. if (isset($data['app'])) {
  135. $info['app'] = $data['app'];
  136. }
  137. if (isset($data['tips'])) {
  138. $info['tips'] = $data['tips'];
  139. }
  140. $info['domain'] = $this->getDomain($info['url']);
  141. $info['user_id'] = $user['user_id'];
  142. $info['status'] = 0;
  143. $info['create_time'] = date('Y-m-d H:i:s');
  144. if (!LinkStoreModel::where("url", $info['url'])->find()) {
  145. LinkStoreModel::create($info);
  146. return $this->success('推送完毕');
  147. }
  148. }
  149. return $this->error('推送失败');
  150. }
  151. private function getDomain($url)
  152. {
  153. $domain = $url;
  154. $p = parse_url($domain);
  155. if (isset($p['host'])) {
  156. return $p['host'];
  157. }
  158. if (isset($p['path'])) {
  159. return $p['path'];
  160. }
  161. return '';
  162. }
  163. public function add(): \think\response\Json
  164. {
  165. $admin = $this->getAdmin();
  166. is_demo_mode(true);
  167. $data = $this->request->post('form', []);
  168. if ($data) {
  169. try {
  170. unset($data['userInfo']);
  171. } catch (\Exception $exception) {
  172. }
  173. if (isset($data['id']) && $data['id']) { //更新
  174. return $this->update();
  175. } else {
  176. $data['create_time'] = date("Y-m-d H:i:s");
  177. $info = (new \app\model\LinkStoreModel)->create($data);
  178. return $this->success('添加成功', $info);
  179. }
  180. }
  181. return $this->error('缺少数据');
  182. }
  183. public function getIcon(): \think\response\Json
  184. {
  185. $url = $this->request->post('url', false);
  186. if ($url) {
  187. if (mb_substr($url, 0, 4) == 'tab:') {
  188. } else {
  189. if (mb_substr($url, 0, 4) != 'http') {
  190. $url = 'https://' . $url;
  191. }
  192. $url = parse_url($url);
  193. $url = $url['host'];
  194. }
  195. $data = LinkStoreModel::whereRaw("FIND_IN_SET(?,domain)", [$url])->find();
  196. if ($data) {
  197. return $this->success('ok', $data);
  198. }
  199. }
  200. return $this->error('no', '未查询到相关信息');
  201. }
  202. function install_num(): \think\response\Json
  203. {
  204. $id = $this->request->post('id', false);
  205. //给标签+=1
  206. $res = Db::table("linkstore")->where('id', $id)->inc('install_num')->update();
  207. if ($res) {
  208. return $this->success('ok');
  209. }
  210. return $this->error('fail');
  211. }
  212. function createFolder(): \think\response\Json
  213. {
  214. is_demo_mode(true);
  215. $type = $this->request->post('type', false);
  216. $this->getAdmin();
  217. if ($type === 'edit') {
  218. $form = $this->request->post('info');
  219. $id = $this->request->post('info.id', false);
  220. if ($id && $id > 0) {
  221. $model = LinkFolderModel::find($id);
  222. $model->update($form);
  223. } else {
  224. $model = new LinkFolderModel();
  225. $model->create($form);
  226. }
  227. } else if ($type === 'del') {
  228. $id = $this->request->post('id');
  229. $result = LinkFolderModel::where("id", $id)->find();
  230. if ($result) {
  231. $result->delete();
  232. Db::query(
  233. "UPDATE linkstore
  234. SET area = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', area, ','), ',$id,', ','))
  235. WHERE FIND_IN_SET(?, area) > 0;"
  236. , [$id]);
  237. }
  238. }
  239. return $this->success('处理完毕!');
  240. }
  241. function moveGroup(): \think\response\Json
  242. {
  243. is_demo_mode(true);
  244. $this->getAdmin();
  245. $ids = $this->request->post('link', []);
  246. $group_ids = $this->request->post('group_ids', '');
  247. LinkStoreModel::where('id', 'in', $ids)->update(['group_ids' => $group_ids]);
  248. return $this->success('处理完毕!');
  249. }
  250. function moveFolder(): \think\response\Json
  251. {
  252. is_demo_mode(true);
  253. $this->getAdmin();
  254. $ids = $this->request->post('link', []);
  255. $area = $this->request->post('area', '');
  256. LinkStoreModel::where('id', 'in', $ids)->update(['area' => $area]);
  257. return $this->success('处理完毕!');
  258. }
  259. function sortFolder(): \think\response\Json
  260. {
  261. $sort = (array)$this->request->post();
  262. foreach ($sort as $key => $value) {
  263. LinkFolderModel::where("id", $value['id'])->update(['sort' => $value['sort']]);
  264. }
  265. return $this->success("ok");
  266. }
  267. public function del(): \think\response\Json
  268. {
  269. is_demo_mode(true);
  270. $this->getAdmin();
  271. $ids = $this->request->post('ids', []);
  272. LinkStoreModel::where("id", 'in', $ids)->delete();
  273. return $this->success('删除成功');
  274. }
  275. function domains(): \think\response\Json
  276. {
  277. $domains = $this->request->post('domains', []);
  278. $tmp = [];
  279. foreach (LinkStoreModel::where('status', 1)->cursor() as $value) {
  280. $d = $this->getDomain($value['url']);
  281. if (in_array($d, $domains)) {
  282. $tmp[$d] = ["domain" => $d, "name" => $value['name'], "src" => $value['src'], "bgColor" => $value['bgColor'], 'tips' => $value['tips']];
  283. } else if ($value['domain']) {
  284. $r = explode(",", $value['domain']);
  285. foreach ($r as $v) {
  286. if (in_array($v, $domains)) {
  287. $tmp[$v] = ['domain' => $v, 'name' => $value['name'], 'src' => $value['src'], 'bgColor' => $value['bgColor'], 'tips' => $value['tips']];
  288. break;
  289. }
  290. }
  291. }
  292. }
  293. return $this->success('ok', $tmp);
  294. }
  295. }