LinkStore.php 9.4 KB

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