SystemController.php 8.1 KB

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