Modules_Api_Controllers_SiteController.proxy.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. use \Hyperf\Di\Aop\ProxyTrait;
  23. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  24. function __construct()
  25. {
  26. if (method_exists(parent::class, '__construct')) {
  27. parent::__construct(...func_get_args());
  28. }
  29. $this->__handlePropertyHandler(__CLASS__);
  30. }
  31. public function ping()
  32. {
  33. return 'pong';
  34. }
  35. public function result()
  36. {
  37. $params = $this->request->all();
  38. if ($params['file_path']) {
  39. $info = finfo_open(FILEINFO_MIME_TYPE);
  40. $type = finfo_file($info, $params['file_path']);
  41. if (!in_array($type, ['video/mp4', 'image/png', 'image/jpg', 'image/jpeg', 'image/gif']) || !$params['dir']) {
  42. return response('文件类型错误' . $type . '或目录不存在', 400);
  43. }
  44. finfo_close($info);
  45. $data['file_name'] = $params['file_name'];
  46. $data['file_size'] = $params['file_size'];
  47. if ($type != 'video/mp4') {
  48. $data['file_path'] = make(SiteService::class)->encryptImage($params['file_path'], $params['dir']);
  49. } else {
  50. $data['file_path'] = make(SiteService::class)->moveVideo($params['file_path'], $params['dir']);
  51. }
  52. $res = ['result' => $data, 'code' => 0, 'message' => '上传成功'];
  53. return response()->json($res);
  54. } else {
  55. return response()->json('fail', 400);
  56. }
  57. }
  58. public function auth()
  59. {
  60. $params = $this->request->all();
  61. if ($params['platform'] == 'I') {
  62. $secret = 'pb5pe0MXChXsic0N';
  63. }
  64. if ($params['platform'] == 'A') {
  65. $secret = '4rKFwocMESIp4Anf';
  66. }
  67. if ($params['platform'] == 'P') {
  68. $secret = 'Z6Kbz8hpT9VtGmcu';
  69. }
  70. if (!$params['platform'] || !$params['timestamp'] || !$params['sign'] || !isset($secret)) {
  71. logger('sign:参数缺失', $params);
  72. return response('参数缺失', 403);
  73. }
  74. if (abs(time() - $params['timestamp']) > 3600) {
  75. logger('sign:时间验签失败' . time(), $params);
  76. return response('时间验签失败', 403);
  77. }
  78. $sign = strtoupper(md5($params['platform'] . $params['timestamp'] . $secret));
  79. if ($sign != $params['sign']) {
  80. logger('sign:时间验签失败' . $sign, $params);
  81. return response('签名验签失败', 403);
  82. }
  83. return json_success('ok');
  84. }
  85. public function move()
  86. {
  87. $params = $this->request->all();
  88. logger('开始移动文件:', $params);
  89. if (!isset($params['filepath']) || !isset($params['pathname'])) {
  90. logger('move', ['filepath/pathname不能为空']);
  91. return false;
  92. }
  93. if (!in_array($params['pathname'], ['vu', 'va', 'vd'])) {
  94. logger('move', ['mp4下载二级目录地址错误']);
  95. return false;
  96. }
  97. $path = '/var/www/resource/wp/mp/' . $params['pathname'] . '/' . date('mdH', time());
  98. $newname = $path . '/' . md5(microtime()) . mt_rand(1000, 9999) . '.mp4';
  99. make(SiteService::class)->mkDirs($path);
  100. if (rename($params['filepath'], $newname)) {
  101. logger('rename', [$newname . '->文件移动成功!']);
  102. return true;
  103. } else {
  104. logger('rename', ['文件移动失败']);
  105. return false;
  106. }
  107. }
  108. public function shift()
  109. {
  110. $params = $this->request->all();
  111. if (!$params['path'] || !$params['table'] || !$params['id'] || !$params['update']) {
  112. return json_fail('error');
  113. }
  114. $result = make(SiteService::class)->videoShift($params['path'], $params['table'], $params['id'], $params['update']);
  115. return json_success($result);
  116. }
  117. public function pushCrashLog(ValidatorFactoryInterface $validatorFactory)
  118. {
  119. $inputData = $this->getJsonData();
  120. // Demo 这是在控制器内校验器的写法.
  121. $rules = ['ua' => 'required', 'user_id' => 'nullable', 'message' => 'required', 'version' => 'required', 'device_no' => 'required', 'device_type' => 'required|in:A,I,H'];
  122. $validator = $validatorFactory->make($inputData, $rules);
  123. if ($validator->fails()) {
  124. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  125. return json_fail($validator->errors()->first());
  126. }
  127. $result = make(QueueService::class)->crashLogPush(['ua' => $inputData['ua'], 'user_id' => $inputData['user_id'] ?? '', 'message' => $inputData['message'], 'version' => $inputData['version'], 'device_no' => $inputData['device_no'], 'device_type' => $inputData['device_type']]);
  128. return json_success($result);
  129. }
  130. public function getVersion(Request $request, ValidatorFactoryInterface $validatorFactory)
  131. {
  132. $inputData = $request->all();
  133. // Demo 这是在控制器内校验器的写法.
  134. $rules = ['device_type' => 'required|in:A,I,H', 'version_code' => 'nullable|string'];
  135. $validator = $validatorFactory->make($inputData, $rules);
  136. if ($validator->fails()) {
  137. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  138. return json_fail($validator->errors()->first());
  139. }
  140. $hVersionCode = data_get($inputData, 'version_code', '');
  141. $cacheKey = 'version:' . $inputData['device_type'];
  142. if (!empty($hVersionCode)) {
  143. $arrTmp = explode('.', $hVersionCode);
  144. if (count($arrTmp) != 3) {
  145. return json_fail('版本错误');
  146. }
  147. if (intval($arrTmp[0]) > 99 || intval($arrTmp[1]) > 99 || intval($arrTmp[2]) > 99) {
  148. return json_fail('版本溢出错误');
  149. }
  150. $cacheKey .= ':v' . $hVersionCode;
  151. }
  152. $result = make(CacheInterface::class)->remember($cacheKey, 60 * 60, function () use($inputData, $hVersionCode) {
  153. $conditions = ['status' => 1, 'device_type' => $inputData['device_type'], 'version_type' => 'free'];
  154. if (!empty($hVersionCode)) {
  155. $conditions['version_code'] = $hVersionCode;
  156. }
  157. $item = VersionModel::query()->where($conditions)->orderByDesc('version_code')->first();
  158. if (!empty($item)) {
  159. $item = $item->toArray();
  160. $item['app_url'] = make(ConfigRepoistory::class)->get('share_url', 'sys');
  161. $conditions = ['status' => 1, 'device_type' => $inputData['device_type'], 'version_type' => 'merge', 'version_code' => $item['version_code']];
  162. $item2 = VersionModel::query()->where($conditions)->orderByDesc('version_code')->first();
  163. $item['merge_path'] = '';
  164. // 分割下载url
  165. if (!empty($item2)) {
  166. $item['merge_path'] = $item2['package_path'];
  167. }
  168. return $item;
  169. } else {
  170. return null;
  171. }
  172. });
  173. $res = ['code' => 200, 'message' => '', 'data' => $result, 'handshake' => ''];
  174. return response()->json($res);
  175. }
  176. /**
  177. * 提供给H5-获取最新版本下载地址
  178. */
  179. public function getVersionH5(Request $request, ValidatorFactoryInterface $validatorFactory)
  180. {
  181. $android_info = VersionModel::query()->where(['status' => 1, 'device_type' => 'A', 'version_type' => 'free'])->orderByDesc('version_code')->first();
  182. $ios_free_info = VersionModel::query()->where(['status' => 1, 'device_type' => 'I', 'version_type' => 'free'])->orderByDesc('version_code')->first();
  183. $ios_shop_info = VersionModel::query()->where(['status' => 1, 'device_type' => 'I', 'version_type' => 'shop'])->orderByDesc('version_code')->first();
  184. $ios_testflight_info = VersionModel::query()->where(['status' => 1, 'device_type' => 'I', 'version_type' => 'testflight'])->orderByDesc('version_code')->first();
  185. $ios_enterprise_info = VersionModel::query()->where(['status' => 1, 'device_type' => 'I', 'version_type' => 'enterprise'])->orderByDesc('version_code')->first();
  186. $ios_super_info = VersionModel::query()->where(['status' => 1, 'device_type' => 'I', 'version_type' => 'super'])->orderByDesc('version_code')->first();
  187. $ios_ipa_info = VersionModel::query()->where(['status' => 1, 'device_type' => 'I', 'version_type' => 'ipa'])->orderByDesc('version_code')->first();
  188. $ios_ipa_gbox = VersionModel::query()->where(['status' => 1, 'device_type' => 'I', 'version_type' => 'gbox'])->orderByDesc('version_code')->first();
  189. $androidMergeInfo = VersionModel::query()->where(['status' => 1, 'device_type' => 'A', 'version_type' => 'merge'])->orderByDesc('version_code')->first();
  190. $potato_info = ConfigModel::query()->where('skey', 'potoato_url')->first();
  191. $data = ['url_android' => $android_info ? $android_info->package_path : '', 'url_android_merge' => $androidMergeInfo ? $androidMergeInfo->package_path : '', 'url_ios_free' => $ios_free_info ? $ios_free_info->package_path : '', 'url_ios_shop' => $ios_shop_info ? $ios_shop_info->package_path : '', 'url_ios_testflight' => $ios_testflight_info ? $ios_testflight_info->package_path : '', 'url_ios_super' => $ios_super_info ? $ios_super_info->package_path : '', 'url_ios_enterprise' => $ios_enterprise_info ? $ios_enterprise_info->package_path : '', 'url_ios_ipa' => $ios_ipa_info ? $ios_ipa_info->package_path : '', 'url_ios_gbox' => $ios_ipa_gbox ? $ios_ipa_gbox->package_path : '', 'url_potato' => $potato_info ? $potato_info->svalue : ''];
  192. $res = ['code' => 200, 'message' => '', 'data' => $data, 'handshake' => ''];
  193. return response()->json($res);
  194. }
  195. public function test123()
  196. {
  197. $filePath = base_path('storage/cache/m3u8_url/ff19/3e65/ff193e657aeaeb971d072becae6061df.url');
  198. if (file_exists($filePath)) {
  199. $mtime = filemtime($filePath);
  200. $phpDate = date('Y-m-d H:i:s', $mtime);
  201. $diffSeconds = Carbon::now()->diffInSeconds(Carbon::parse($mtime));
  202. $expiredSeconds = 2 * 60 * 60;
  203. // 原始文件保存2小时.
  204. $diffSeconds2 = abs($diffSeconds);
  205. $isExpired = $diffSeconds > $expiredSeconds;
  206. var_dump($mtime, $phpDate, $diffSeconds, $expiredSeconds, $diffSeconds2, $isExpired);
  207. }
  208. return json_success(['a1' => 'ok']);
  209. }
  210. }