Admin.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\command\repair;
  5. use app\model\ConfigModel;
  6. use app\model\FileModel;
  7. use app\model\HistoryModel;
  8. use app\model\LinkModel;
  9. use app\model\LinkStoreModel;
  10. use app\model\NoteModel;
  11. use app\model\SettingModel;
  12. use app\model\TabbarModel;
  13. use app\model\TokenModel;
  14. use app\model\UserModel;
  15. use app\model\UserSearchEngineModel;
  16. use DateInterval;
  17. use DatePeriod;
  18. use DateTime;
  19. use mysqli;
  20. use think\facade\Cache;
  21. use think\facade\Db;
  22. class Admin extends BaseController
  23. {
  24. public function UserList(): \think\response\Json
  25. {
  26. $this->getAdmin();
  27. $limit = $this->request->all('limit', 50);
  28. $search = $this->request->post('search');
  29. $group = $this->request->post('search.group_id', -1);
  30. $sql = [];
  31. if (isset($search['mail']) && mb_strlen($search['mail']) > 0) {
  32. $sql[] = ['mail', 'like', "%$search[mail]%"];
  33. }
  34. if (isset($search['nickname']) && mb_strlen($search['nickname']) > 0) {
  35. $sql[] = ["nickname", "like", "%$search[nickname]%"];
  36. }
  37. if (isset($search['status']) && $search['status'] >= 0) {
  38. $sql[] = ['status', '=', $search['status']];
  39. }
  40. if (is_numeric($group) && $group >= 0) {
  41. $sql['group_id'] = $search['group_id'];
  42. }
  43. $user = UserModel::where($sql)->withoutField('password')->order($this->request->post('sort.prop', 'id'), $this->request->post('sort.order', 'desc'))->paginate($limit);
  44. return $this->success('ok', $user);
  45. }
  46. function userUpdate(): \think\response\Json
  47. {
  48. $this->getAdmin();
  49. is_demo_mode(true);
  50. $id = $this->request->post('id');
  51. $user = UserModel::where('id', $id)->find();
  52. $data = $this->request->post();
  53. if (!$user) {
  54. $user = new UserModel();
  55. }
  56. //如果字段中的password有内容则md5加密后保存
  57. if (isset($data['password']) && mb_strlen($data['password']) > 0) {
  58. $data['password'] = md5($data['password']);
  59. } else {
  60. unset($data['password']);
  61. }
  62. $user->save($data);
  63. return $this->success('保存成功');
  64. }
  65. //用户删除函数
  66. function userDelete(): \think\response\Json
  67. {
  68. $this->getAdmin();
  69. is_demo_mode(true);
  70. $id = $this->request->post('id');
  71. $user = UserModel::where('id', $id)->find();
  72. if ($user) {//删除当前用户下的所有数据。
  73. LinkModel::where("user_id", $user['id'])->delete();//删除标签
  74. TabbarModel::where("user_id", $user['id'])->delete();//删除快捷图标
  75. HistoryModel::where('user_id', $user['id'])->delete();//删除历史图标
  76. ConfigModel::where('user_id', $user['id'])->delete();//删除配置信息
  77. NoteModel::where('user_id', $user['id'])->delete();//删除笔记
  78. UserSearchEngineModel::where('user_id', $user['id'])->delete();//删除自定义搜索引擎
  79. TokenModel::where('user_id', $user['id'])->delete();//删除所有Token
  80. $user->delete();//删除用户
  81. }
  82. return $this->success("删除完毕");
  83. }
  84. function export(): \think\response\Json
  85. {
  86. $this->getAdmin();
  87. is_demo_mode(true);
  88. $link = $this->request->post('link', []);
  89. if ($link) {
  90. $saveName = public_path() . 'static/exportsTabLink.json';
  91. $status = file_put_contents($saveName, json_encode($link, true, JSON_UNESCAPED_UNICODE));
  92. if ($status) {
  93. $setting = new SettingModel();
  94. if ($setting->find('defaultTab')) {
  95. $setting->update(['value' => 'static/exportsTabLink.json'], ['keys' => 'defaultTab']);
  96. } else {
  97. $setting->save(['keys' => 'defaultTab', 'value' => 'static/exportsTabLink.json']);
  98. }
  99. Cache::delete('webConfig');
  100. return $this->success('保存成功');
  101. }
  102. }
  103. return $this->error('保存失败');
  104. }
  105. private function countFilesInDirectory($directory): int
  106. {
  107. $fileCount = 0;
  108. // 获取目录中的文件和子目录
  109. $files = scandir($directory);
  110. foreach ($files as $file) {
  111. // 排除"."和".."
  112. if ($file != '.' && $file != '..') {
  113. $filePath = $directory . '/' . $file;
  114. // 如果是目录,则递归调用函数
  115. if (is_dir($filePath)) {
  116. $fileCount += $this->countFilesInDirectory($filePath);
  117. } else {
  118. // 如果是文件,则增加文件数量
  119. $fileCount++;
  120. }
  121. }
  122. }
  123. return $fileCount;
  124. }
  125. function xyCheck(): \think\response\Json
  126. {
  127. $this->getAdmin();
  128. if (is_file(root_path() . 'xy.pem')) {
  129. if (file_get_contents(root_path() . 'xy.pem') === file_get_contents(config_path() . 'LICENSE.html')) {
  130. return $this->success("ok");
  131. }
  132. }
  133. return $this->error("未找到证书文件", ['license' => file_get_contents(config_path() . 'LICENSE.html')]);
  134. }
  135. function xy(): \think\response\Json
  136. {
  137. $this->getAdmin();
  138. file_put_contents(root_path() . "xy.pem", file_get_contents(config_path() . 'LICENSE.html'));
  139. return $this->success("ok");
  140. }
  141. function getServicesStatus(): \think\response\Json
  142. {
  143. $this->getAdmin();
  144. $userNum = UserModel::count('id');
  145. $linkNum = LinkStoreModel::count('id');
  146. $redisNum = 0;
  147. $fileNum = FileModel::field('id')->count("id");
  148. $userWeekActive = 0;
  149. if (Cache::get('userWeekActive')) {
  150. $userWeekActive = Cache::get('userWeekActive');
  151. } else {
  152. $start = date('Y-m-d', strtotime('-7 days'));
  153. $userWeekActive = UserModel::where('active', ">", $start)->field("id,active")->count('id');
  154. Cache::set('userWeekActive', $userWeekActive, 60);
  155. }
  156. return $this->success('ok', ['userNum' => $userNum, 'linkNum' => $linkNum, 'redisNum' => $redisNum, 'fileNum' => $fileNum, "userWeekActive" => $userWeekActive]);
  157. }
  158. function getUserLine(): \think\response\Json
  159. {
  160. $this->getAdmin();
  161. $today = new DateTime();
  162. $firstDayOfMonth = new DateTime($today->format('Y-m-01'));
  163. $lastDayOfMonth = new DateTime($today->format('Y-m-t'));
  164. $formatDate = function ($date) {
  165. return $date->format('Y-m-d');
  166. };
  167. $defaultDate = [
  168. $formatDate($firstDayOfMonth),
  169. $formatDate($lastDayOfMonth)
  170. ];
  171. $dateSelect = $this->request->post('dateSelect', $defaultDate);
  172. $result = UserModel::whereBetweenTime('create_time', $dateSelect[0], $dateSelect[1]); // 当前月
  173. $result = $result->field('DATE_FORMAT(create_time, "%Y-%m-%d") as time, count(id) as total');
  174. $result = $result->group('time')->select();
  175. return $this->success('ok', $this->render($result, $dateSelect[0], $dateSelect[1]));
  176. }
  177. function getHotTab(): \think\response\Json
  178. {
  179. $this->getAdmin();
  180. $list = LinkStoreModel::order('install_num', 'desc')->limit(30)->cache('hotTab', 60)->select()->toArray();
  181. return $this->success('ok', $list);
  182. }
  183. private function render($arr, $start, $end): array
  184. {
  185. $info = [];
  186. foreach ($arr as $key => $value) {
  187. $info[$value['time']] = $value['total'];
  188. }
  189. $time = [];
  190. $total = [];
  191. $start_date = new DateTime($start);
  192. $end_date = new DateTime($end);
  193. $interval = new DateInterval('P1D');
  194. $dateRange = new DatePeriod($start_date, $interval, $end_date);
  195. $ts = null;
  196. foreach ($dateRange as $date) {
  197. $ts = $date->format('Y-m-d');
  198. $time[] = $ts;
  199. if (isset($info[$ts])) {
  200. $total[] = $info[$ts];
  201. } else {
  202. $total[] = 0;
  203. }
  204. }
  205. // 判断是否需要添加最后一天的数据
  206. if ($end_date->format('Y-m-d') != $ts) {
  207. $time[] = $end_date->format('Y-m-d');
  208. $total[] = isset($info[$end_date->format('Y-m-d')]) ? $info[$end_date->format('Y-m-d')] : 0;
  209. }
  210. return ['time' => $time, 'total' => $total, 'sum' => array_sum($total)];
  211. }
  212. function userLoginRecord(): \think\response\Json
  213. {
  214. $this->getAdmin();
  215. $user_id = $this->request->post('user_id');
  216. if ($user_id && !is_demo_mode()) {
  217. $list = TokenModel::where("user_id", $user_id)->field('user_id,FROM_UNIXTIME(create_time) as create_time,user_agent,ip')->order('create_time', 'desc')->limit(100)->select()->toArray();
  218. return $this->success('', $list);
  219. }
  220. return $this->success('', []);
  221. }
  222. function repair(): \think\response\Json
  223. {
  224. $this->getAdmin();
  225. is_demo_mode(true);
  226. repair::repair();
  227. return $this->success("修复完毕");
  228. }
  229. }