SystemController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. if (Request::input('from') == 'chat') {
  131. $path = "uploads/chat/" . Users::token2userid() . "/" . date("Ym") . "/";
  132. }
  133. $data = Base::upload([
  134. "file" => Request::file('image'),
  135. "type" => 'image',
  136. "path" => $path,
  137. "scale" => $scale
  138. ]);
  139. if (Base::isError($data)) {
  140. return Base::retError($data['msg']);
  141. } else {
  142. return Base::retSuccess('success', $data['data']);
  143. }
  144. }
  145. /**
  146. * 浏览图片空间
  147. */
  148. public function imgview()
  149. {
  150. if (Users::token2userid() === 0) {
  151. return Base::retError('身份失效,等重新登录!');
  152. }
  153. $publicPath = "uploads/picture/" . Users::token2userid() . "/";
  154. $dirPath = public_path($publicPath);
  155. $dirs = $files = [];
  156. //
  157. $path = Request::input('path');
  158. if ($path && is_string($path)) {
  159. $path = str_replace(array('||', '|'), '/', $path);
  160. $path = trim($path, '/');
  161. $path = str_replace('..', '', $path);
  162. $path = Base::leftDelete($path, $publicPath);
  163. if ($path) {
  164. $path = $path . '/';
  165. $dirPath .= $path;
  166. //
  167. $dirs[] = [
  168. 'type' => 'dir',
  169. 'title' => '...',
  170. 'path' => substr(substr($path, 0, -1), 0, strripos(substr($path, 0, -1), '/')),
  171. 'url' => '',
  172. 'thumb' => Base::fillUrl('images/other/dir.png'),
  173. 'inode' => 0,
  174. ];
  175. }
  176. } else {
  177. $path = '';
  178. }
  179. $list = glob($dirPath . '*', GLOB_BRACE);
  180. foreach ($list as $v) {
  181. $filename = basename($v);
  182. $pathTemp = $publicPath . $path . $filename;
  183. if (is_dir($v)) {
  184. $dirs[] = [
  185. 'type' => 'dir',
  186. 'title' => $filename,
  187. 'path' => $pathTemp,
  188. 'url' => Base::fillUrl($pathTemp),
  189. 'thumb' => Base::fillUrl('images/other/dir.png'),
  190. 'inode' => fileatime($v),
  191. ];
  192. } elseif (substr($filename, -10) != "_thumb.jpg") {
  193. $array = [
  194. 'type' => 'file',
  195. 'title' => $filename,
  196. 'path' => $pathTemp,
  197. 'url' => Base::fillUrl($pathTemp),
  198. 'thumb' => $pathTemp,
  199. 'inode' => fileatime($v),
  200. ];
  201. //
  202. $extension = pathinfo($dirPath . $filename, PATHINFO_EXTENSION);
  203. if (in_array($extension, array('gif', 'jpg', 'jpeg', 'png', 'bmp'))) {
  204. if (file_exists($dirPath . $filename . '_thumb.jpg')) {
  205. $array['thumb'] .= '_thumb.jpg';
  206. }
  207. $array['thumb'] = Base::fillUrl($array['thumb']);
  208. $files[] = $array;
  209. }
  210. }
  211. }
  212. if ($dirs) {
  213. $inOrder = [];
  214. foreach ($dirs as $key => $item) {
  215. $inOrder[$key] = $item['title'];
  216. }
  217. array_multisort($inOrder, SORT_DESC, $dirs);
  218. }
  219. if ($files) {
  220. $inOrder = [];
  221. foreach ($files as $key => $item) {
  222. $inOrder[$key] = $item['inode'];
  223. }
  224. array_multisort($inOrder, SORT_DESC, $files);
  225. }
  226. //
  227. return Base::retSuccess('success', ['dirs' => $dirs, 'files' => $files]);
  228. }
  229. /**
  230. * 清理opcache数据
  231. * @return int
  232. */
  233. public function opcache()
  234. {
  235. opcache_reset();
  236. return Base::time();
  237. }
  238. }