SystemController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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['footerText'] = env('WEBSITE_FOOTER', '');
  65. $setting['logo'] = Base::fillUrl($setting['logo']);
  66. $setting['enterprise'] = env('ENTERPRISE_SHOW') ? 'show': '';
  67. return Base::retSuccess('success', $setting ? $setting : json_decode('{}'));
  68. }
  69. /**
  70. * 获取终端详细信息
  71. */
  72. public function get__info()
  73. {
  74. if (Request::input("key") !== env('APP_KEY')) {
  75. return [];
  76. }
  77. return Base::retSuccess('success', [
  78. 'ip' => Base::getIp(),
  79. 'ip-info' => Base::getIpInfo(Base::getIp()),
  80. 'ip-iscn' => Base::isCnIp(Base::getIp()),
  81. 'header' => Request::header(),
  82. 'token' => Base::getToken(),
  83. 'url' => url('') . Base::getUrl(),
  84. ]);
  85. }
  86. /**
  87. * 获取IP地址
  88. */
  89. public function get__ip() {
  90. return Base::getIp();
  91. }
  92. /**
  93. * 是否中国IP地址
  94. */
  95. public function get__cnip() {
  96. return Base::isCnIp(Request::input('ip'));
  97. }
  98. /**
  99. * 获取IP地址详细信息
  100. */
  101. public function get__ipinfo() {
  102. return Base::getIpInfo(Request::input("ip"));
  103. }
  104. /**
  105. * 获取websocket地址
  106. */
  107. public function get__wsurl() {
  108. $wsurl = env('LARAVELS_PROXY_URL');
  109. if (!$wsurl) {
  110. $wsurl = url('');
  111. $wsurl = str_replace('https://', 'wss://', $wsurl);
  112. $wsurl = str_replace('http://', 'ws://', $wsurl);
  113. $wsurl.= '/ws';
  114. }
  115. return Base::retSuccess('success', [
  116. 'wsurl' => $wsurl,
  117. ]);
  118. }
  119. /**
  120. * 上传图片
  121. */
  122. public function imgupload()
  123. {
  124. if (Users::token2userid() === 0) {
  125. return Base::retError('身份失效,等重新登录!');
  126. }
  127. $scale = [intval(Request::input('width')), intval(Request::input('height'))];
  128. if (!$scale[0] && !$scale[1]) {
  129. $scale = [2160, 4160, -1];
  130. }
  131. $path = "uploads/picture/" . Users::token2userid() . "/" . date("Ym") . "/";
  132. $image64 = trim(Base::getPostValue('image64'));
  133. $fileName = trim(Base::getPostValue('filename'));
  134. if ($image64) {
  135. $data = Base::image64save([
  136. "image64" => $image64,
  137. "path" => $path,
  138. "fileName" => $fileName,
  139. "scale" => $scale
  140. ]);
  141. } else {
  142. $data = Base::upload([
  143. "file" => Request::file('image'),
  144. "type" => 'image',
  145. "path" => $path,
  146. "fileName" => $fileName,
  147. "scale" => $scale
  148. ]);
  149. }
  150. if (Base::isError($data)) {
  151. return Base::retError($data['msg']);
  152. } else {
  153. return Base::retSuccess('success', $data['data']);
  154. }
  155. }
  156. /**
  157. * 浏览图片空间
  158. */
  159. public function imgview()
  160. {
  161. if (Users::token2userid() === 0) {
  162. return Base::retError('身份失效,等重新登录!');
  163. }
  164. $publicPath = "uploads/picture/" . Users::token2userid() . "/";
  165. $dirPath = public_path($publicPath);
  166. $dirs = $files = [];
  167. //
  168. $path = Request::input('path');
  169. if ($path && is_string($path)) {
  170. $path = str_replace(array('||', '|'), '/', $path);
  171. $path = trim($path, '/');
  172. $path = str_replace('..', '', $path);
  173. $path = Base::leftDelete($path, $publicPath);
  174. if ($path) {
  175. $path = $path . '/';
  176. $dirPath .= $path;
  177. //
  178. $dirs[] = [
  179. 'type' => 'dir',
  180. 'title' => '...',
  181. 'path' => substr(substr($path, 0, -1), 0, strripos(substr($path, 0, -1), '/')),
  182. 'url' => '',
  183. 'thumb' => Base::fillUrl('images/other/dir.png'),
  184. 'inode' => 0,
  185. ];
  186. }
  187. } else {
  188. $path = '';
  189. }
  190. $list = glob($dirPath . '*', GLOB_BRACE);
  191. foreach ($list as $v) {
  192. $filename = basename($v);
  193. $pathTemp = $publicPath . $path . $filename;
  194. if (is_dir($v)) {
  195. $dirs[] = [
  196. 'type' => 'dir',
  197. 'title' => $filename,
  198. 'path' => $pathTemp,
  199. 'url' => Base::fillUrl($pathTemp),
  200. 'thumb' => Base::fillUrl('images/other/dir.png'),
  201. 'inode' => fileatime($v),
  202. ];
  203. } elseif (substr($filename, -10) != "_thumb.jpg") {
  204. $array = [
  205. 'type' => 'file',
  206. 'title' => $filename,
  207. 'path' => $pathTemp,
  208. 'url' => Base::fillUrl($pathTemp),
  209. 'thumb' => $pathTemp,
  210. 'inode' => fileatime($v),
  211. ];
  212. //
  213. $extension = pathinfo($dirPath . $filename, PATHINFO_EXTENSION);
  214. if (in_array($extension, array('gif', 'jpg', 'jpeg', 'png', 'bmp'))) {
  215. if (file_exists($dirPath . $filename . '_thumb.jpg')) {
  216. $array['thumb'] .= '_thumb.jpg';
  217. }
  218. $array['thumb'] = Base::fillUrl($array['thumb']);
  219. $files[] = $array;
  220. }
  221. }
  222. }
  223. if ($dirs) {
  224. $inOrder = [];
  225. foreach ($dirs as $key => $item) {
  226. $inOrder[$key] = $item['title'];
  227. }
  228. array_multisort($inOrder, SORT_DESC, $dirs);
  229. }
  230. if ($files) {
  231. $inOrder = [];
  232. foreach ($files as $key => $item) {
  233. $inOrder[$key] = $item['inode'];
  234. }
  235. array_multisort($inOrder, SORT_DESC, $files);
  236. }
  237. //
  238. return Base::retSuccess('success', ['dirs' => $dirs, 'files' => $files]);
  239. }
  240. /**
  241. * 上传文件
  242. */
  243. public function fileupload()
  244. {
  245. if (Users::token2userid() === 0) {
  246. return Base::retError('身份失效,等重新登录!');
  247. }
  248. $path = "uploads/files/" . Users::token2userid() . "/" . date("Ym") . "/";
  249. $image64 = trim(Base::getPostValue('image64'));
  250. $fileName = trim(Base::getPostValue('filename'));
  251. if ($image64) {
  252. $data = Base::image64save([
  253. "image64" => $image64,
  254. "path" => $path,
  255. "fileName" => $fileName,
  256. ]);
  257. } else {
  258. $data = Base::upload([
  259. "file" => Request::file('files'),
  260. "type" => 'file',
  261. "path" => $path,
  262. "fileName" => $fileName,
  263. ]);
  264. }
  265. //
  266. return $data;
  267. }
  268. /**
  269. * 清理opcache数据
  270. * @return int
  271. */
  272. public function opcache()
  273. {
  274. opcache_reset();
  275. return Base::time();
  276. }
  277. }