Index.php 5.9 KB

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