IndexController.php 6.3 KB

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