SystemController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Module\Base;
  5. use App\Module\Users;
  6. use Request;
  7. /**
  8. * @apiDefine system
  9. *
  10. * 系统
  11. */
  12. class SystemController extends Controller
  13. {
  14. public function __invoke($method, $action = '')
  15. {
  16. $app = $method ? $method : 'main';
  17. if ($action) {
  18. $app .= "__" . $action;
  19. }
  20. return (method_exists($this, $app)) ? $this->$app() : Base::ajaxError("404 not found (" . str_replace("__", "/", $app) . ").");
  21. }
  22. /**
  23. * 获取设置、保存设置
  24. *
  25. * @apiParam {String} type
  26. * - get: 获取(默认)
  27. * - save: 保存设置(参数:logo、github、reg、callav、autoArchived、archivedDay)
  28. */
  29. public function setting()
  30. {
  31. $type = trim(Request::input('type'));
  32. if ($type == 'save') {
  33. if (env("SYSTEM_SETTING") == 'disabled') {
  34. return Base::retError('当前环境禁止修改!');
  35. }
  36. $user = Users::authE();
  37. if (Base::isError($user)) {
  38. return $user;
  39. } else {
  40. $user = $user['data'];
  41. }
  42. if (Base::isError(Users::identity('admin'))) {
  43. return Base::retError('权限不足!', [], -1);
  44. }
  45. $all = Request::input();
  46. foreach ($all AS $key => $value) {
  47. if (!in_array($key, ['logo', 'github', 'reg', 'callav', 'autoArchived', 'archivedDay'])) {
  48. unset($all[$key]);
  49. }
  50. }
  51. $all['logo'] = is_array($all['logo']) ? $all['logo'][0]['path'] : $all['logo'];
  52. $all['archivedDay'] = intval($all['archivedDay']);
  53. if ($all['autoArchived'] == 'open') {
  54. if ($all['archivedDay'] <= 0) {
  55. return Base::retError(['自动归档时间不可小于%天!', 1]);
  56. } elseif ($all['archivedDay'] > 100) {
  57. return Base::retError(['自动归档时间不可大于%天!', 100]);
  58. }
  59. }
  60. $setting = Base::setting('system', Base::newTrim($all));
  61. } else {
  62. $setting = Base::setting('system');
  63. }
  64. $setting['logo'] = Base::fillUrl($setting['logo']);
  65. $setting['enterprise'] = env('ENTERPRISE_SHOW') ? 'show': '';
  66. return Base::retSuccess('success', $setting ? $setting : json_decode('{}'));
  67. }
  68. /**
  69. * 获取终端详细信息
  70. */
  71. public function get__info()
  72. {
  73. if (Request::input("key") !== env('APP_KEY')) {
  74. return [];
  75. }
  76. return Base::retSuccess('success', [
  77. 'ip' => Base::getIp(),
  78. 'ip-info' => Base::getIpInfo(Base::getIp()),
  79. 'ip-iscn' => Base::isCnIp(Base::getIp()),
  80. 'header' => Request::header(),
  81. 'token' => Base::getToken(),
  82. 'url' => url('') . Base::getUrl(),
  83. ]);
  84. }
  85. /**
  86. * 获取IP地址
  87. */
  88. public function get__ip() {
  89. return Base::getIp();
  90. }
  91. /**
  92. * 是否中国IP地址
  93. */
  94. public function get__cnip() {
  95. return Base::isCnIp(Request::input('ip'));
  96. }
  97. /**
  98. * 获取IP地址详细信息
  99. */
  100. public function get__ipinfo() {
  101. return Base::getIpInfo(Request::input("ip"));
  102. }
  103. /**
  104. * 获取websocket地址
  105. */
  106. public function get__wsurl() {
  107. $wsurl = env('LARAVELS_PROXY_URL');
  108. if (!$wsurl) {
  109. $wsurl = url('');
  110. $wsurl = str_replace('https://', 'wss://', $wsurl);
  111. $wsurl = str_replace('http://', 'ws://', $wsurl);
  112. $wsurl.= '/ws';
  113. }
  114. return Base::retSuccess('success', [
  115. 'wsurl' => $wsurl,
  116. ]);
  117. }
  118. /**
  119. * 上传图片
  120. */
  121. public function imgupload()
  122. {
  123. if (Users::token2userid() === 0) {
  124. return Base::retError('身份失效,等重新登录!');
  125. }
  126. $scale = [intval(Request::input('width')), intval(Request::input('height'))];
  127. if (!$scale[0] && !$scale[1]) {
  128. $scale = [2160, 4160, -1];
  129. }
  130. $path = "uploads/picture/" . Users::token2userid() . "/" . date("Ym") . "/";
  131. $image64 = trim(Base::getPostValue('image64'));
  132. $fileName = trim(Base::getPostValue('filename'));
  133. if ($image64) {
  134. $data = Base::image64save([
  135. "image64" => $image64,
  136. "path" => $path,
  137. "fileName" => $fileName,
  138. "scale" => $scale
  139. ]);
  140. } else {
  141. $data = Base::upload([
  142. "file" => Request::file('image'),
  143. "type" => 'image',
  144. "path" => $path,
  145. "fileName" => $fileName,
  146. "scale" => $scale
  147. ]);
  148. }
  149. if (Base::isError($data)) {
  150. return Base::retError($data['msg']);
  151. } else {
  152. return Base::retSuccess('success', $data['data']);
  153. }
  154. }
  155. /**
  156. * 浏览图片空间
  157. */
  158. public function imgview()
  159. {
  160. if (Users::token2userid() === 0) {
  161. return Base::retError('身份失效,等重新登录!');
  162. }
  163. $publicPath = "uploads/picture/" . Users::token2userid() . "/";
  164. $dirPath = public_path($publicPath);
  165. $dirs = $files = [];
  166. //
  167. $path = Request::input('path');
  168. if ($path && is_string($path)) {
  169. $path = str_replace(array('||', '|'), '/', $path);
  170. $path = trim($path, '/');
  171. $path = str_replace('..', '', $path);
  172. $path = Base::leftDelete($path, $publicPath);
  173. if ($path) {
  174. $path = $path . '/';
  175. $dirPath .= $path;
  176. //
  177. $dirs[] = [
  178. 'type' => 'dir',
  179. 'title' => '...',
  180. 'path' => substr(substr($path, 0, -1), 0, strripos(substr($path, 0, -1), '/')),
  181. 'url' => '',
  182. 'thumb' => Base::fillUrl('images/other/dir.png'),
  183. 'inode' => 0,
  184. ];
  185. }
  186. } else {
  187. $path = '';
  188. }
  189. $list = glob($dirPath . '*', GLOB_BRACE);
  190. foreach ($list as $v) {
  191. $filename = basename($v);
  192. $pathTemp = $publicPath . $path . $filename;
  193. if (is_dir($v)) {
  194. $dirs[] = [
  195. 'type' => 'dir',
  196. 'title' => $filename,
  197. 'path' => $pathTemp,
  198. 'url' => Base::fillUrl($pathTemp),
  199. 'thumb' => Base::fillUrl('images/other/dir.png'),
  200. 'inode' => fileatime($v),
  201. ];
  202. } elseif (substr($filename, -10) != "_thumb.jpg") {
  203. $array = [
  204. 'type' => 'file',
  205. 'title' => $filename,
  206. 'path' => $pathTemp,
  207. 'url' => Base::fillUrl($pathTemp),
  208. 'thumb' => $pathTemp,
  209. 'inode' => fileatime($v),
  210. ];
  211. //
  212. $extension = pathinfo($dirPath . $filename, PATHINFO_EXTENSION);
  213. if (in_array($extension, array('gif', 'jpg', 'jpeg', 'png', 'bmp'))) {
  214. if (file_exists($dirPath . $filename . '_thumb.jpg')) {
  215. $array['thumb'] .= '_thumb.jpg';
  216. }
  217. $array['thumb'] = Base::fillUrl($array['thumb']);
  218. $files[] = $array;
  219. }
  220. }
  221. }
  222. if ($dirs) {
  223. $inOrder = [];
  224. foreach ($dirs as $key => $item) {
  225. $inOrder[$key] = $item['title'];
  226. }
  227. array_multisort($inOrder, SORT_DESC, $dirs);
  228. }
  229. if ($files) {
  230. $inOrder = [];
  231. foreach ($files as $key => $item) {
  232. $inOrder[$key] = $item['inode'];
  233. }
  234. array_multisort($inOrder, SORT_DESC, $files);
  235. }
  236. //
  237. return Base::retSuccess('success', ['dirs' => $dirs, 'files' => $files]);
  238. }
  239. /**
  240. * 清理opcache数据
  241. * @return int
  242. */
  243. public function opcache()
  244. {
  245. opcache_reset();
  246. return Base::time();
  247. }
  248. }