Admin.php 7.5 KB

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