IndexController.php 6.9 KB

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