IndexController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 = '100001';
  15. public function __invoke($method, $action = '', $child = '', $name = '')
  16. {
  17. $app = $method ? $method : 'main';
  18. if ($app == 'uploads') {
  19. $child = $action . '/' . $child . '/' . $name;
  20. $action = '';
  21. }
  22. if ($action) {
  23. $app .= "__" . $action;
  24. }
  25. @error_reporting(E_ALL & ~E_NOTICE);
  26. if (Request::input('__Access-Control-Allow-Origin')) {
  27. header('Access-Control-Allow-Origin:*');
  28. header('Access-Control-Allow-Methods:GET,POST,PUT,DELETE,OPTIONS');
  29. header('Access-Control-Allow-Headers:Content-Type, platform, platform-channel, token, release, Access-Control-Allow-Origin');
  30. }
  31. return (method_exists($this, $app)) ? $this->$app($child) : Base::ajaxError("404 not found (" . str_replace("__", "/", $app) . ").");
  32. }
  33. /**
  34. * 获取IP地址
  35. * @return array|mixed
  36. */
  37. public function get__ip() {
  38. return Base::getIp();
  39. }
  40. /**
  41. * 是否中国IP地址
  42. * @return array|mixed
  43. */
  44. public function get__cnip() {
  45. return Base::isCnIp(Request::input('ip'));
  46. }
  47. /**
  48. * 获取IP地址详细信息
  49. * @return array|mixed
  50. */
  51. public function get__ipinfo() {
  52. return Base::getIpInfo(Request::input("ip"));
  53. }
  54. /**
  55. * 首页
  56. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  57. */
  58. public function main()
  59. {
  60. return view('main', ['version' => $this->version]);
  61. }
  62. /**
  63. * 接口文档
  64. * @return \Illuminate\Http\RedirectResponse
  65. */
  66. public function api()
  67. {
  68. return Redirect::to(Base::fillUrl('docs'), 301);
  69. }
  70. /**
  71. * 上传图片
  72. * @return array
  73. */
  74. public function api__imgupload()
  75. {
  76. if (Users::token2userid() === 0) {
  77. return Base::retError('身份失效,等重新登录!');
  78. }
  79. $scale = [intval(Request::input('width')), intval(Request::input('height'))];
  80. if (!$scale[0] && !$scale[1]) {
  81. $scale = [2160, 4160, -1];
  82. }
  83. $data = Base::upload([
  84. "file" => Request::file('image'),
  85. "type" => 'image',
  86. "path" => "uploads/picture/" . Users::token2userid() . "/" . date("Ym") . "/",
  87. "scale" => $scale
  88. ]);
  89. if (Base::isError($data)) {
  90. return Base::retError($data['msg']);
  91. } else {
  92. return Base::retSuccess('success', $data['data']);
  93. }
  94. }
  95. /**
  96. * 浏览图片空间
  97. * @return array
  98. */
  99. public function api__imgview()
  100. {
  101. if (Users::token2userid() === 0) {
  102. return Base::retError('身份失效,等重新登录!');
  103. }
  104. $publicPath = "uploads/picture/" . Users::token2userid() . "/";
  105. $dirPath = public_path($publicPath);
  106. $dirs = $files = [];
  107. //
  108. $path = Request::input('path');
  109. if ($path && is_string($path)) {
  110. $path = str_replace(array('||', '|'), '/', $path);
  111. $path = trim($path, '/');
  112. $path = str_replace('..', '', $path);
  113. $path = Base::leftDelete($path, $publicPath);
  114. if ($path) {
  115. $path = $path . '/';
  116. $dirPath .= $path;
  117. //
  118. $dirs[] = [
  119. 'type' => 'dir',
  120. 'title' => '...',
  121. 'path' => substr(substr($path, 0, -1), 0, strripos(substr($path, 0, -1), '/')),
  122. 'url' => '',
  123. 'thumb' => Base::fillUrl('images/dir.png'),
  124. 'inode' => 0,
  125. ];
  126. }
  127. } else {
  128. $path = '';
  129. }
  130. $list = glob($dirPath . '*', GLOB_BRACE);
  131. foreach ($list as $v) {
  132. $filename = basename($v);
  133. $pathTemp = $publicPath . $path . $filename;
  134. if (is_dir($v)) {
  135. $dirs[] = [
  136. 'type' => 'dir',
  137. 'title' => $filename,
  138. 'path' => $pathTemp,
  139. 'url' => Base::fillUrl($pathTemp),
  140. 'thumb' => Base::fillUrl('images/dir.png'),
  141. 'inode' => fileatime($v),
  142. ];
  143. } elseif (substr($filename, -10) != "_thumb.jpg") {
  144. $array = [
  145. 'type' => 'file',
  146. 'title' => $filename,
  147. 'path' => $pathTemp,
  148. 'url' => Base::fillUrl($pathTemp),
  149. 'thumb' => $pathTemp,
  150. 'inode' => fileatime($v),
  151. ];
  152. //
  153. $extension = pathinfo($dirPath . $filename, PATHINFO_EXTENSION);
  154. if (in_array($extension, array('gif', 'jpg', 'jpeg', 'png', 'bmp'))) {
  155. if (file_exists($dirPath . $filename . '_thumb.jpg')) {
  156. $array['thumb'] .= '_thumb.jpg';
  157. }
  158. $array['thumb'] = Base::fillUrl($array['thumb']);
  159. $files[] = $array;
  160. }
  161. }
  162. }
  163. if ($dirs) {
  164. $inOrder = [];
  165. foreach ($dirs as $key => $item) {
  166. $inOrder[$key] = $item['title'];
  167. }
  168. array_multisort($inOrder, SORT_DESC, $dirs);
  169. }
  170. if ($files) {
  171. $inOrder = [];
  172. foreach ($files as $key => $item) {
  173. $inOrder[$key] = $item['inode'];
  174. }
  175. array_multisort($inOrder, SORT_DESC, $files);
  176. }
  177. //
  178. return Base::retSuccess('success', ['dirs' => $dirs, 'files' => $files]);
  179. }
  180. /**
  181. * 清理opcache数据
  182. * @return int
  183. */
  184. public function opcache__reset()
  185. {
  186. opcache_reset();
  187. return Base::time();
  188. }
  189. }