SiteController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /**
  3. * 系统设置.
  4. *
  5. * @author ningque
  6. * @date 2020-01-24 16:42:58
  7. */
  8. declare(strict_types=1);
  9. namespace Modules\Api\Controllers;
  10. use App\Controller\CoreController;
  11. use App\Model\VersionModel;
  12. use App\Model\ConfigModel;
  13. use App\Repositories\ConfigRepoistory;
  14. use App\Repositories\Service\QueueService;
  15. use Carbon\Carbon;
  16. use Hyperf\HttpServer\Request;
  17. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  18. use Modules\Api\Services\SiteService;
  19. use Psr\SimpleCache\CacheInterface;
  20. class SiteController extends CoreController
  21. {
  22. public function ping()
  23. {
  24. return 'pong';
  25. }
  26. public function result()
  27. {
  28. $params = $this->request->all();
  29. if ($params['file_path']) {
  30. $info = finfo_open(FILEINFO_MIME_TYPE);
  31. $type = finfo_file($info, $params['file_path']);
  32. if (!in_array($type, ['video/mp4','image/png','image/jpg','image/jpeg','image/gif']) || !$params['dir']) {
  33. return response('文件类型错误'.$type.'或目录不存在', 400);
  34. }
  35. finfo_close($info);
  36. $data['file_name'] = $params['file_name'];
  37. $data['file_size'] = $params['file_size'];
  38. if ($type != 'video/mp4') {
  39. $data['file_path'] = make(SiteService::class)
  40. ->encryptImage($params['file_path'], $params['dir']);
  41. } else {
  42. $data['file_path'] = make(SiteService::class)
  43. ->moveVideo($params['file_path'], $params['dir']);
  44. }
  45. $res = [
  46. 'result' => $data,
  47. 'code' => 0,
  48. 'message' => '上传成功'
  49. ];
  50. return response()->json($res);
  51. } else {
  52. return response()->json('fail', 400);
  53. }
  54. }
  55. public function auth()
  56. {
  57. $params = $this->request->all();
  58. if ($params['platform'] == 'I') {
  59. $secret = 'pb5pe0MXChXsic0N';
  60. }
  61. if ($params['platform'] == 'A') {
  62. $secret = '4rKFwocMESIp4Anf';
  63. }
  64. if ($params['platform'] == 'P') {
  65. $secret = 'Z6Kbz8hpT9VtGmcu';
  66. }
  67. if (!$params['platform'] || !$params['timestamp'] || !$params['sign'] || !isset($secret)) {
  68. logger('sign:参数缺失', $params);
  69. return response('参数缺失', 403);
  70. }
  71. if (abs(time() - $params['timestamp']) > 3600) {
  72. logger('sign:时间验签失败' . time(), $params);
  73. return response('时间验签失败', 403);
  74. }
  75. $sign = strtoupper(md5($params['platform'] . $params['timestamp'] . $secret));
  76. if ($sign != $params['sign']) {
  77. logger('sign:时间验签失败' . $sign, $params);
  78. return response('签名验签失败', 403);
  79. }
  80. return json_success('ok');
  81. }
  82. public function move()
  83. {
  84. $params = $this->request->all();
  85. logger('开始移动文件:', $params);
  86. if (!isset($params['filepath']) || !isset($params['pathname'])) {
  87. logger('move', ['filepath/pathname不能为空']);
  88. return false;
  89. }
  90. if (!in_array($params['pathname'], ['vu','va','vd'])) {
  91. logger('move', ['mp4下载二级目录地址错误']);
  92. return false;
  93. }
  94. $path = '/var/www/resource/wp/mp/' . $params['pathname'] . '/' . date('mdH', time());
  95. $newname = $path . '/' . md5(microtime()) . mt_rand(1000, 9999) . '.mp4';
  96. make(SiteService::class)->mkDirs($path);
  97. if (rename($params['filepath'], $newname)) {
  98. logger('rename', [$newname . '->文件移动成功!']);
  99. return true;
  100. } else {
  101. logger('rename', ['文件移动失败']);
  102. return false;
  103. }
  104. }
  105. public function shift()
  106. {
  107. $params = $this->request->all();
  108. if (!$params['path'] || !$params['table'] || !$params['id'] || !$params['update']) {
  109. return json_fail('error');
  110. }
  111. $result = make(SiteService::class)
  112. ->videoShift($params['path'], $params['table'], $params['id'], $params['update']);
  113. return json_success($result);
  114. }
  115. public function pushCrashLog(
  116. ValidatorFactoryInterface $validatorFactory
  117. ) {
  118. $inputData = $this->getJsonData();
  119. // Demo 这是在控制器内校验器的写法.
  120. $rules = [
  121. 'ua' => 'required',
  122. 'user_id' => 'nullable',
  123. 'message' => 'required',
  124. 'version' => 'required',
  125. 'device_no' => 'required',
  126. 'device_type' => 'required|in:A,I,H',
  127. ];
  128. $validator = $validatorFactory->make($inputData, $rules);
  129. if ($validator->fails()) {
  130. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  131. return json_fail($validator->errors()->first());
  132. }
  133. $result = make(QueueService::class)->crashLogPush(
  134. [
  135. 'ua' => $inputData['ua'],
  136. 'user_id' => $inputData['user_id'] ?? '',
  137. 'message' => $inputData['message'],
  138. 'version' => $inputData['version'],
  139. 'device_no' => $inputData['device_no'],
  140. 'device_type' => $inputData['device_type']
  141. ]
  142. );
  143. return json_success($result);
  144. }
  145. public function getVersion(
  146. Request $request,
  147. ValidatorFactoryInterface $validatorFactory
  148. ) {
  149. $inputData = $request->all();
  150. // Demo 这是在控制器内校验器的写法.
  151. $rules = [
  152. 'device_type' => 'required|in:A,I,H',
  153. 'version_code' => 'nullable|string',
  154. ];
  155. $validator = $validatorFactory->make($inputData, $rules);
  156. if ($validator->fails()) {
  157. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  158. return json_fail($validator->errors()->first());
  159. }
  160. $hVersionCode = data_get($inputData, 'version_code', '');
  161. $cacheKey = 'version:' . $inputData['device_type'];
  162. if (!empty($hVersionCode)) {
  163. $arrTmp = explode('.', $hVersionCode);
  164. if (count($arrTmp) != 3) {
  165. return json_fail('版本错误');
  166. }
  167. if (intval($arrTmp[0]) > 99 || intval($arrTmp[1]) > 99 || intval($arrTmp[2]) > 99) {
  168. return json_fail('版本溢出错误');
  169. }
  170. $cacheKey .= ':v'. $hVersionCode;
  171. }
  172. $result = make(CacheInterface::class)->remember(
  173. $cacheKey,
  174. 60 * 60,
  175. function () use ($inputData, $hVersionCode) {
  176. $conditions = [
  177. 'status' => 1,
  178. 'device_type' => $inputData['device_type'],
  179. 'version_type' => 'free'
  180. ];
  181. if (!empty($hVersionCode)) {
  182. $conditions['version_code'] = $hVersionCode;
  183. }
  184. $item = VersionModel::query()->where($conditions)->orderByDesc('version_code')->first();
  185. if (!empty($item)) {
  186. $item = $item->toArray();
  187. $item['app_url'] = make(ConfigRepoistory::class)
  188. ->get('share_url', 'sys');
  189. $conditions = [
  190. 'status' => 1,
  191. 'device_type' => $inputData['device_type'],
  192. 'version_type' => 'merge',
  193. 'version_code' => $item['version_code'],
  194. ];
  195. $item2 = VersionModel::query()->where($conditions)->orderByDesc('version_code')->first();
  196. $item['merge_path'] = ''; // 分割下载url
  197. if (!empty($item2)) {
  198. $item['merge_path'] = $item2['package_path'];
  199. }
  200. return $item;
  201. } else {
  202. return null;
  203. }
  204. }
  205. );
  206. $res = [
  207. 'code' => 200,
  208. 'message' => '',
  209. 'data' => $result,
  210. 'handshake' => '',
  211. ];
  212. return response()->json($res);
  213. }
  214. /**
  215. * 提供给H5-获取最新版本下载地址
  216. */
  217. public function getVersionH5(Request $request, ValidatorFactoryInterface $validatorFactory)
  218. {
  219. $android_info = VersionModel::query()
  220. ->where(
  221. [
  222. 'status' => 1,
  223. 'device_type' => 'A',
  224. 'version_type' => 'free',
  225. ]
  226. )
  227. ->orderByDesc('version_code')
  228. ->first();
  229. $ios_free_info = VersionModel::query()
  230. ->where(
  231. [
  232. 'status' => 1,
  233. 'device_type' => 'I',
  234. 'version_type' => 'free',
  235. ]
  236. )
  237. ->orderByDesc('version_code')
  238. ->first();
  239. $ios_shop_info = VersionModel::query()
  240. ->where(
  241. [
  242. 'status' => 1,
  243. 'device_type' => 'I',
  244. 'version_type' => 'shop',
  245. ]
  246. )
  247. ->orderByDesc('version_code')
  248. ->first();
  249. $ios_testflight_info = VersionModel::query()
  250. ->where(
  251. [
  252. 'status' => 1,
  253. 'device_type' => 'I',
  254. 'version_type' => 'testflight',
  255. ]
  256. )
  257. ->orderByDesc('version_code')
  258. ->first();
  259. $ios_enterprise_info = VersionModel::query()
  260. ->where(
  261. [
  262. 'status' => 1,
  263. 'device_type' => 'I',
  264. 'version_type' => 'enterprise',
  265. ]
  266. )
  267. ->orderByDesc('version_code')
  268. ->first();
  269. $ios_super_info = VersionModel::query()
  270. ->where(
  271. [
  272. 'status' => 1,
  273. 'device_type' => 'I',
  274. 'version_type' => 'super',
  275. ]
  276. )
  277. ->orderByDesc('version_code')
  278. ->first();
  279. $ios_ipa_info = VersionModel::query()
  280. ->where(
  281. [
  282. 'status' => 1,
  283. 'device_type' => 'I',
  284. 'version_type' => 'ipa',
  285. ]
  286. )
  287. ->orderByDesc('version_code')
  288. ->first();
  289. $ios_ipa_gbox = VersionModel::query()
  290. ->where(
  291. [
  292. 'status' => 1,
  293. 'device_type' => 'I',
  294. 'version_type' => 'gbox',
  295. ]
  296. )
  297. ->orderByDesc('version_code')
  298. ->first();
  299. $androidMergeInfo = VersionModel::query()
  300. ->where(
  301. [
  302. 'status' => 1,
  303. 'device_type' => 'A',
  304. 'version_type' => 'merge',
  305. ]
  306. )
  307. ->orderByDesc('version_code')
  308. ->first();
  309. $potato_info = ConfigModel::query()
  310. ->where('skey', 'potoato_url')
  311. ->first();
  312. $data = [
  313. 'url_android' => $android_info ? $android_info->package_path : '',
  314. 'url_android_merge' => $androidMergeInfo ? $androidMergeInfo->package_path : '',
  315. 'url_ios_free' => $ios_free_info ? $ios_free_info->package_path : '',
  316. 'url_ios_shop' => $ios_shop_info ? $ios_shop_info->package_path : '',
  317. 'url_ios_testflight' => $ios_testflight_info ? $ios_testflight_info->package_path : '',
  318. 'url_ios_super' => $ios_super_info ? $ios_super_info->package_path : '',
  319. 'url_ios_enterprise' => $ios_enterprise_info ? $ios_enterprise_info->package_path : '',
  320. 'url_ios_ipa' => $ios_ipa_info ? $ios_ipa_info->package_path : '',
  321. 'url_ios_gbox' => $ios_ipa_gbox ? $ios_ipa_gbox->package_path : '',
  322. 'url_potato' => $potato_info ? $potato_info->svalue : '',
  323. ];
  324. $res = [
  325. 'code' => 200,
  326. 'message' => '',
  327. 'data' => $data,
  328. 'handshake' => '',
  329. ];
  330. return response()->json($res);
  331. }
  332. public function test123()
  333. {
  334. $filePath = base_path('storage/cache/m3u8_url/ff19/3e65/ff193e657aeaeb971d072becae6061df.url');
  335. if (file_exists($filePath)) {
  336. $mtime = filemtime($filePath);
  337. $phpDate = date('Y-m-d H:i:s', $mtime);
  338. $diffSeconds = Carbon::now()->diffInSeconds(Carbon::parse($mtime));
  339. $expiredSeconds = 2 * 60 * 60; // 原始文件保存2小时.
  340. $diffSeconds2 = abs($diffSeconds);
  341. $isExpired = $diffSeconds > $expiredSeconds;
  342. var_dump($mtime, $phpDate, $diffSeconds, $expiredSeconds, $diffSeconds2, $isExpired);
  343. }
  344. return json_success(['a1' => 'ok']);
  345. }
  346. }