Admin.php 7.1 KB

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