IndexController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Module\Base;
  4. use App\Module\Users;
  5. use Redirect;
  6. use Request;
  7. /**
  8. * 页面
  9. * Class IndexController
  10. * @package App\Http\Controllers
  11. */
  12. class IndexController extends Controller
  13. {
  14. private $version = '100000';
  15. public function __invoke($method, $action = '', $child = '')
  16. {
  17. $app = $method ? $method : 'main';
  18. if ($action) {
  19. $app .= "__" . $action;
  20. }
  21. return (method_exists($this, $app)) ? $this->$app($child) : Base::ajaxError("404 not found (" . str_replace("__", "/", $app) . ").");
  22. }
  23. /**
  24. * 获取终端详细信息
  25. * @return array
  26. */
  27. public function get()
  28. {
  29. if (Request::input("key") !== env('APP_KEY')) {
  30. return [];
  31. }
  32. return Base::retSuccess('success', [
  33. 'ip' => Base::getIp(),
  34. 'ip-info' => Base::getIpInfo(Base::getIp()),
  35. 'ip-iscn' => Base::isCnIp(Base::getIp()),
  36. 'header' => Request::header(),
  37. 'token' => Base::getToken(),
  38. 'url' => url('') . Base::getUrl(),
  39. ]);
  40. }
  41. /**
  42. * 获取IP地址
  43. * @return array|mixed
  44. */
  45. public function get__ip() {
  46. return Base::getIp();
  47. }
  48. /**
  49. * 是否中国IP地址
  50. * @return array|mixed
  51. */
  52. public function get__cnip() {
  53. return Base::isCnIp(Request::input('ip'));
  54. }
  55. /**
  56. * 获取IP地址详细信息
  57. * @return array|mixed
  58. */
  59. public function get__ipinfo() {
  60. return Base::getIpInfo(Request::input("ip"));
  61. }
  62. /**
  63. * 获取websocket地址
  64. * @return array|mixed
  65. */
  66. public function get__wsurl() {
  67. $wsurl = env('LARAVELS_PROXY_URL');
  68. if (!$wsurl) {
  69. $wsurl = url('');
  70. $wsurl = str_replace('https://', 'wss://', $wsurl);
  71. $wsurl = str_replace('http://', 'ws://', $wsurl);
  72. $wsurl.= '/ws';
  73. }
  74. return Base::retSuccess('success', [
  75. 'wsurl' => $wsurl,
  76. ]);
  77. }
  78. /**
  79. * 首页
  80. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  81. */
  82. public function main()
  83. {
  84. return view('main', ['version' => $this->version]);
  85. }
  86. /**
  87. * 接口文档
  88. * @return \Illuminate\Http\RedirectResponse
  89. */
  90. public function api()
  91. {
  92. return Redirect::to(Base::fillUrl('docs'), 301);
  93. }
  94. /**
  95. * 上传图片
  96. * @return array
  97. */
  98. public function api__imgupload()
  99. {
  100. if (Users::token2userid() === 0) {
  101. return Base::retError('身份失效,等重新登录!');
  102. }
  103. $scale = [intval(Request::input('width')), intval(Request::input('height'))];
  104. if (!$scale[0] && !$scale[1]) {
  105. $scale = [2160, 4160, -1];
  106. }
  107. $path = "uploads/picture/" . Users::token2userid() . "/" . date("Ym") . "/";
  108. if (Request::input('from') == 'chat') {
  109. $path = "uploads/chat/" . Users::token2userid() . "/" . date("Ym") . "/";
  110. }
  111. $data = Base::upload([
  112. "file" => Request::file('image'),
  113. "type" => 'image',
  114. "path" => $path,
  115. "scale" => $scale
  116. ]);
  117. if (Base::isError($data)) {
  118. return Base::retError($data['msg']);
  119. } else {
  120. return Base::retSuccess('success', $data['data']);
  121. }
  122. }
  123. /**
  124. * 浏览图片空间
  125. * @return array
  126. */
  127. public function api__imgview()
  128. {
  129. if (Users::token2userid() === 0) {
  130. return Base::retError('身份失效,等重新登录!');
  131. }
  132. $publicPath = "uploads/picture/" . Users::token2userid() . "/";
  133. $dirPath = public_path($publicPath);
  134. $dirs = $files = [];
  135. //
  136. $path = Request::input('path');
  137. if ($path && is_string($path)) {
  138. $path = str_replace(array('||', '|'), '/', $path);
  139. $path = trim($path, '/');
  140. $path = str_replace('..', '', $path);
  141. $path = Base::leftDelete($path, $publicPath);
  142. if ($path) {
  143. $path = $path . '/';
  144. $dirPath .= $path;
  145. //
  146. $dirs[] = [
  147. 'type' => 'dir',
  148. 'title' => '...',
  149. 'path' => substr(substr($path, 0, -1), 0, strripos(substr($path, 0, -1), '/')),
  150. 'url' => '',
  151. 'thumb' => Base::fillUrl('images/other/dir.png'),
  152. 'inode' => 0,
  153. ];
  154. }
  155. } else {
  156. $path = '';
  157. }
  158. $list = glob($dirPath . '*', GLOB_BRACE);
  159. foreach ($list as $v) {
  160. $filename = basename($v);
  161. $pathTemp = $publicPath . $path . $filename;
  162. if (is_dir($v)) {
  163. $dirs[] = [
  164. 'type' => 'dir',
  165. 'title' => $filename,
  166. 'path' => $pathTemp,
  167. 'url' => Base::fillUrl($pathTemp),
  168. 'thumb' => Base::fillUrl('images/other/dir.png'),
  169. 'inode' => fileatime($v),
  170. ];
  171. } elseif (substr($filename, -10) != "_thumb.jpg") {
  172. $array = [
  173. 'type' => 'file',
  174. 'title' => $filename,
  175. 'path' => $pathTemp,
  176. 'url' => Base::fillUrl($pathTemp),
  177. 'thumb' => $pathTemp,
  178. 'inode' => fileatime($v),
  179. ];
  180. //
  181. $extension = pathinfo($dirPath . $filename, PATHINFO_EXTENSION);
  182. if (in_array($extension, array('gif', 'jpg', 'jpeg', 'png', 'bmp'))) {
  183. if (file_exists($dirPath . $filename . '_thumb.jpg')) {
  184. $array['thumb'] .= '_thumb.jpg';
  185. }
  186. $array['thumb'] = Base::fillUrl($array['thumb']);
  187. $files[] = $array;
  188. }
  189. }
  190. }
  191. if ($dirs) {
  192. $inOrder = [];
  193. foreach ($dirs as $key => $item) {
  194. $inOrder[$key] = $item['title'];
  195. }
  196. array_multisort($inOrder, SORT_DESC, $dirs);
  197. }
  198. if ($files) {
  199. $inOrder = [];
  200. foreach ($files as $key => $item) {
  201. $inOrder[$key] = $item['inode'];
  202. }
  203. array_multisort($inOrder, SORT_DESC, $files);
  204. }
  205. //
  206. return Base::retSuccess('success', ['dirs' => $dirs, 'files' => $files]);
  207. }
  208. /**
  209. * 获取国际化列表
  210. */
  211. public function language__lists()
  212. {
  213. $lists = Base::readDir(resource_path('assets/js'));
  214. $array = [];
  215. foreach ($lists AS $file) {
  216. $content = file_get_contents($file);
  217. preg_match_all('/\$L\(([\'"])(.*?)\\1/', $content, $matchs);
  218. foreach ($matchs[2] AS $key=>$text) {
  219. if (Base::strExists($text, "',")) {
  220. $text = Base::getMiddle($text, null, "',");
  221. }
  222. if (!isset($array[$text])) {
  223. $array[$text] = null;
  224. }
  225. }
  226. }
  227. return json_encode($array, JSON_UNESCAPED_UNICODE);
  228. }
  229. /**
  230. * 清理opcache数据
  231. * @return int
  232. */
  233. public function opcache__reset()
  234. {
  235. opcache_reset();
  236. return Base::time();
  237. }
  238. }