Admin.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 xyCheck(): \think\response\Json
  120. {
  121. $this->getAdmin();
  122. if(is_file(root_path() . 'xy.pem')){
  123. if(file_get_contents(root_path().'xy.pem')===file_get_contents(config_path().'LICENSE.html')){
  124. return $this->success("ok");
  125. }
  126. }
  127. return $this->error("未找到证书文件",['license'=>file_get_contents(config_path().'LICENSE.html')]);
  128. }
  129. function xy(): \think\response\Json
  130. {
  131. $this->getAdmin();
  132. file_put_contents(root_path()."xy.pem", file_get_contents(config_path().'LICENSE.html'));
  133. return $this->success("ok");
  134. }
  135. function getServicesStatus(): \think\response\Json
  136. {
  137. $this->getAdmin();
  138. $userNum = UserModel::count('id');
  139. $linkNum = LinkStoreModel::count('id');
  140. $redisNum = 0;
  141. $fileNum = FileModel::field('id')->count("id");
  142. $userWeekActive = 0;
  143. if (Cache::get('userWeekActive')) {
  144. $userWeekActive = Cache::get('userWeekActive');
  145. } else {
  146. $start = date('Y-m-d', strtotime('-7 days'));
  147. $userWeekActive = UserModel::where('active',">", $start)->field("id,active")->count('id');
  148. Cache::set('userWeekActive', $userWeekActive, 60);
  149. }
  150. return $this->success('ok', ['userNum' => $userNum, 'linkNum' => $linkNum, 'redisNum' => $redisNum, 'fileNum' => $fileNum,"userWeekActive"=>$userWeekActive]);
  151. }
  152. function getUserLine(): \think\response\Json
  153. {
  154. $this->getAdmin();
  155. $result = UserModel::whereMonth('create_time');
  156. $result = $result->field('DATE_FORMAT(create_time, "%Y-%m-%d") as time, count(id) as total');
  157. $result = $result->group('time')->select();
  158. return $this->success('ok', $this->render($result));
  159. }
  160. function getHotTab(): \think\response\Json
  161. {
  162. $this->getAdmin();
  163. $list = LinkStoreModel::order('install_num', 'desc')->limit(30)->cache('hotTab', 60)->select()->toArray();
  164. return $this->success('ok', $list);
  165. }
  166. private function render($arr): array
  167. {
  168. $info = [];
  169. foreach ($arr as $key => $value) {
  170. $info[$value['time']] = $value['total'];
  171. }
  172. $time = [];
  173. $total = [];
  174. //当月的第一天
  175. $start = date('Y-m-01', strtotime(date('Y-m-d')));
  176. //当月的最后一天
  177. $end = date('Y-m-d', strtotime(date('Y-m-01') . ' +1 month -1 day'));
  178. $start_date = new DateTime($start);
  179. $end_date = new DateTime($end);
  180. $interval = new DateInterval('P1D');
  181. $dateRange = new DatePeriod($start_date, $interval, $end_date);
  182. $ts = null;
  183. foreach ($dateRange as $date) {
  184. $ts = $date->format('Y-m-d');
  185. $time[] = $ts;
  186. if (isset($info[$ts])) {
  187. $total[] = $info[$ts];
  188. } else {
  189. $total[] = 0;
  190. }
  191. }
  192. // 判断是否需要添加最后一天的数据
  193. if ($end_date->format('Y-m-d') != $ts) {
  194. $time[] = $end_date->format('Y-m-d');
  195. $total[] = isset($info[$end_date->format('Y-m-d')]) ? $info[$end_date->format('Y-m-d')] : 0;
  196. }
  197. return ['time' => $time, 'total' => $total, 'sum' => array_sum($total)];
  198. }
  199. function userLoginRecord(): \think\response\Json
  200. {
  201. $this->getAdmin();
  202. $user_id = $this->request->post('user_id');
  203. if ($user_id && !is_demo_mode()) {
  204. $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();
  205. return $this->success('', $list);
  206. }
  207. return $this->success('', []);
  208. }
  209. function repair(): \think\response\Json
  210. {
  211. $this->getAdmin();
  212. is_demo_mode(true);
  213. repair::repair();
  214. return $this->success("修复完毕");
  215. }
  216. }