IndexController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. * 首页
  64. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  65. */
  66. public function main()
  67. {
  68. return view('main', ['version' => $this->version]);
  69. }
  70. /**
  71. * 接口文档
  72. * @return \Illuminate\Http\RedirectResponse
  73. */
  74. public function api()
  75. {
  76. return Redirect::to(Base::fillUrl('docs'), 301);
  77. }
  78. /**
  79. * 上传图片
  80. * @return array
  81. */
  82. public function api__imgupload()
  83. {
  84. if (Users::token2userid() === 0) {
  85. return Base::retError('身份失效,等重新登录!');
  86. }
  87. $scale = [intval(Request::input('width')), intval(Request::input('height'))];
  88. if (!$scale[0] && !$scale[1]) {
  89. $scale = [2160, 4160, -1];
  90. }
  91. $path = "uploads/picture/" . Users::token2userid() . "/" . date("Ym") . "/";
  92. if (Request::input('from') == 'chat') {
  93. $path = "uploads/chat/" . Users::token2userid() . "/" . date("Ym") . "/";
  94. }
  95. $data = Base::upload([
  96. "file" => Request::file('image'),
  97. "type" => 'image',
  98. "path" => $path,
  99. "scale" => $scale
  100. ]);
  101. if (Base::isError($data)) {
  102. return Base::retError($data['msg']);
  103. } else {
  104. return Base::retSuccess('success', $data['data']);
  105. }
  106. }
  107. /**
  108. * 浏览图片空间
  109. * @return array
  110. */
  111. public function api__imgview()
  112. {
  113. if (Users::token2userid() === 0) {
  114. return Base::retError('身份失效,等重新登录!');
  115. }
  116. $publicPath = "uploads/picture/" . Users::token2userid() . "/";
  117. $dirPath = public_path($publicPath);
  118. $dirs = $files = [];
  119. //
  120. $path = Request::input('path');
  121. if ($path && is_string($path)) {
  122. $path = str_replace(array('||', '|'), '/', $path);
  123. $path = trim($path, '/');
  124. $path = str_replace('..', '', $path);
  125. $path = Base::leftDelete($path, $publicPath);
  126. if ($path) {
  127. $path = $path . '/';
  128. $dirPath .= $path;
  129. //
  130. $dirs[] = [
  131. 'type' => 'dir',
  132. 'title' => '...',
  133. 'path' => substr(substr($path, 0, -1), 0, strripos(substr($path, 0, -1), '/')),
  134. 'url' => '',
  135. 'thumb' => Base::fillUrl('images/other/dir.png'),
  136. 'inode' => 0,
  137. ];
  138. }
  139. } else {
  140. $path = '';
  141. }
  142. $list = glob($dirPath . '*', GLOB_BRACE);
  143. foreach ($list as $v) {
  144. $filename = basename($v);
  145. $pathTemp = $publicPath . $path . $filename;
  146. if (is_dir($v)) {
  147. $dirs[] = [
  148. 'type' => 'dir',
  149. 'title' => $filename,
  150. 'path' => $pathTemp,
  151. 'url' => Base::fillUrl($pathTemp),
  152. 'thumb' => Base::fillUrl('images/other/dir.png'),
  153. 'inode' => fileatime($v),
  154. ];
  155. } elseif (substr($filename, -10) != "_thumb.jpg") {
  156. $array = [
  157. 'type' => 'file',
  158. 'title' => $filename,
  159. 'path' => $pathTemp,
  160. 'url' => Base::fillUrl($pathTemp),
  161. 'thumb' => $pathTemp,
  162. 'inode' => fileatime($v),
  163. ];
  164. //
  165. $extension = pathinfo($dirPath . $filename, PATHINFO_EXTENSION);
  166. if (in_array($extension, array('gif', 'jpg', 'jpeg', 'png', 'bmp'))) {
  167. if (file_exists($dirPath . $filename . '_thumb.jpg')) {
  168. $array['thumb'] .= '_thumb.jpg';
  169. }
  170. $array['thumb'] = Base::fillUrl($array['thumb']);
  171. $files[] = $array;
  172. }
  173. }
  174. }
  175. if ($dirs) {
  176. $inOrder = [];
  177. foreach ($dirs as $key => $item) {
  178. $inOrder[$key] = $item['title'];
  179. }
  180. array_multisort($inOrder, SORT_DESC, $dirs);
  181. }
  182. if ($files) {
  183. $inOrder = [];
  184. foreach ($files as $key => $item) {
  185. $inOrder[$key] = $item['inode'];
  186. }
  187. array_multisort($inOrder, SORT_DESC, $files);
  188. }
  189. //
  190. return Base::retSuccess('success', ['dirs' => $dirs, 'files' => $files]);
  191. }
  192. /**
  193. * 获取国际化列表
  194. */
  195. public function language__lists()
  196. {
  197. $lists = Base::readDir(resource_path('assets/js'));
  198. $array = [];
  199. foreach ($lists AS $file) {
  200. $content = file_get_contents($file);
  201. preg_match_all('/\$L\(([\'"])(.*?)\\1/', $content, $matchs);
  202. foreach ($matchs[2] AS $key=>$text) {
  203. if (Base::strExists($text, "',")) {
  204. $text = Base::getMiddle($text, null, "',");
  205. }
  206. if (!isset($array[$text])) {
  207. $array[$text] = null;
  208. }
  209. }
  210. }
  211. return json_encode($array, JSON_UNESCAPED_UNICODE);
  212. }
  213. /**
  214. * 清理opcache数据
  215. * @return int
  216. */
  217. public function opcache__reset()
  218. {
  219. opcache_reset();
  220. return Base::time();
  221. }
  222. }