Index.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace app\controller\admin;
  3. use app\BaseController;
  4. use app\model\LinkStoreModel;
  5. use app\model\SettingModel;
  6. use app\model\UserModel;
  7. use DateInterval;
  8. use DatePeriod;
  9. use DateTime;
  10. use think\facade\Cache;
  11. use think\facade\Db;
  12. //use Upgrade;
  13. class Index extends BaseController
  14. {
  15. public string $authService = "http://auth.mtab.cc";
  16. public string $authCode = '';
  17. function setSubscription(): \think\response\Json
  18. {
  19. $code = $this->request->post("code", "");
  20. if (trim($code)) {
  21. Db::table('setting')->replace()->insert(['keys' => 'authCode', 'value' => $code]);
  22. SettingModel::refreshSetting();
  23. }
  24. return $this->success("ok");
  25. }
  26. private function initAuth()
  27. {
  28. $authCode = $this->Setting('authCode', '', true);
  29. if (strlen($authCode) == 0) {
  30. $authCode = env('authCode', '');
  31. }
  32. $this->authCode = $authCode;
  33. $this->authService = $this->Setting('authServer', 'http://auth.mtab.cc', true);
  34. }
  35. function updateApp(): \think\response\Json
  36. {
  37. $this->getAdmin();
  38. $this->initAuth();
  39. $result = \Axios::http()->post($this->authService . '/getUpGrade', [
  40. 'timeout' => 10,
  41. 'form_params' => [
  42. 'authorization_code' => $this->authCode,
  43. 'version_code' => app_version_code,
  44. ]
  45. ]);
  46. if ($result->getStatusCode() == 200) {
  47. $json = json_decode($result->getBody()->getContents(), true);
  48. if ($json['code'] === 1) {
  49. $upgradePhp = runtime_path() . 'update.php';
  50. $f = "";
  51. $upGrade = null;
  52. if (!empty($json['info']['update_php'])) {
  53. try {//用远程脚本更新
  54. $f = file_get_contents($json['info']['update_php']);
  55. file_put_contents(runtime_path() . 'update.php', $f);
  56. require_once $upgradePhp;
  57. $upGrade = new \Upgrade();
  58. } catch (\Exception $e) {
  59. }
  60. }
  61. if ($upGrade === null) {
  62. $upGrade = new \Upgrade2();
  63. }
  64. if (!empty($json['info']['update_zip'])) {
  65. $upGrade->update_download_url = $json['info']['update_zip'];
  66. }
  67. if (!empty($json['info']['update_sql'])) {
  68. $upGrade->update_sql_url = $json['info']['update_sql'];
  69. }
  70. $status = $upGrade->run();//启动任务
  71. try {
  72. unlink($upgradePhp);
  73. } catch (\Exception $e) {
  74. }
  75. if ($status === true) {
  76. return $this->success('更新完毕');
  77. } else {
  78. return $this->error($status);
  79. }
  80. } else {
  81. return $this->error($json['msg']);
  82. }
  83. }
  84. return $this->error("没有更新的版本");
  85. }
  86. function authorization(): \think\response\Json
  87. {
  88. $this->getAdmin();
  89. $this->initAuth();
  90. $result = \Axios::http()->post($this->authService . '/checkAuth', [
  91. 'timeout' => 5,
  92. 'form_params' => [
  93. 'authorization_code' => $this->authCode,
  94. 'version_code' => app_version_code,
  95. ]
  96. ]);
  97. $info = [];
  98. $info['version'] = app_version;
  99. $info['version_code'] = app_version_code;
  100. $info['php_version'] = phpversion();
  101. if ($result->getStatusCode() == 200) {
  102. $jsonStr = $result->getBody()->getContents();
  103. $json = json_decode($jsonStr, true);
  104. $info['remote'] = $json;
  105. return $this->success($info);
  106. } else {
  107. return $this->error('授权服务器连接失败', $info);
  108. }
  109. }
  110. function countFilesInDirectory($directory): int
  111. {
  112. $fileCount = 0;
  113. // 获取目录中的文件和子目录
  114. $files = scandir($directory);
  115. foreach ($files as $file) {
  116. // 排除"."和".."
  117. if ($file != '.' && $file != '..') {
  118. $filePath = $directory . '/' . $file;
  119. // 如果是目录,则递归调用函数
  120. if (is_dir($filePath)) {
  121. $fileCount += $this->countFilesInDirectory($filePath);
  122. } else {
  123. // 如果是文件,则增加文件数量
  124. $fileCount++;
  125. }
  126. }
  127. }
  128. return $fileCount;
  129. }
  130. function getServicesStatus(): \think\response\Json
  131. {
  132. $this->getAdmin();
  133. $userNum = UserModel::count("id");
  134. $linkNum = LinkStoreModel::count("id");
  135. $redisNum = 0;
  136. $fileNum = Cache::get("fileNum");
  137. if(!$fileNum){
  138. if (is_dir(public_path() . 'images')) {
  139. $fileNum = $this->countFilesInDirectory(public_path() . 'images');
  140. Cache::set('fileNum', $fileNum, 300);
  141. }
  142. }
  143. return $this->success("ok", ["userNum" => $userNum, "linkNum" => $linkNum, "redisNum" => $redisNum, "fileNum" => $fileNum]);
  144. }
  145. function getUserLine(): \think\response\Json
  146. {
  147. $this->getAdmin();
  148. $result = UserModel::whereMonth('create_time');
  149. $result = $result->field('DATE_FORMAT(create_time, "%Y-%m-%d") as time, count(id) as total');
  150. $result = $result->group('time')->select();
  151. return $this->success('ok', $this->render($result));
  152. }
  153. function getHotTab(): \think\response\Json
  154. {
  155. $this->getAdmin();
  156. $list = LinkStoreModel::order('install_num', 'desc')->limit(20)->cache('hotTab', 60)->select()->toArray();
  157. return $this->success('ok', $list);
  158. }
  159. function render($arr): array
  160. {
  161. $info = [];
  162. foreach ($arr as $key => $value) {
  163. $info[$value['time']] = $value['total'];
  164. }
  165. $time = [];
  166. $total = [];
  167. //当月的第一天
  168. $start = date('Y-m-01', strtotime(date('Y-m-d')));
  169. //当月的最后一天
  170. $end = date('Y-m-d', strtotime(date('Y-m-01') . ' +1 month -1 day'));
  171. $start_date = new DateTime($start);
  172. $end_date = new DateTime($end);
  173. $interval = new DateInterval('P1D');
  174. $dateRange = new DatePeriod($start_date, $interval, $end_date);
  175. $ts = null;
  176. foreach ($dateRange as $date) {
  177. $ts = $date->format('Y-m-d');
  178. $time[] = $ts;
  179. if (isset($info[$ts])) {
  180. $total[] = $info[$ts];
  181. } else {
  182. $total[] = 0;
  183. }
  184. }
  185. // 判断是否需要添加最后一天的数据
  186. if ($end_date->format('Y-m-d') != $ts) {
  187. $time[] = $end_date->format('Y-m-d');
  188. $total[] = isset($info[$end_date->format('Y-m-d')]) ? $info[$end_date->format('Y-m-d')] : 0;
  189. }
  190. return ['time' => $time, 'total' => $total, 'sum' => array_sum($total)];
  191. }
  192. }