| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- /**
- * 系统设置.
- *
- * @author ningque
- * @date 2020-01-24 16:42:58
- */
- declare (strict_types=1);
- namespace Modules\Api\Controllers;
- use App\Controller\CoreController;
- use App\Model\VersionModel;
- use App\Model\ConfigModel;
- use App\Repositories\ConfigRepoistory;
- use App\Repositories\Service\QueueService;
- use Carbon\Carbon;
- use Hyperf\HttpServer\Request;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- use Modules\Api\Services\SiteService;
- use Psr\SimpleCache\CacheInterface;
- class SiteController extends CoreController
- {
- use \Hyperf\Di\Aop\ProxyTrait;
- use \Hyperf\Di\Aop\PropertyHandlerTrait;
- function __construct()
- {
- if (method_exists(parent::class, '__construct')) {
- parent::__construct(...func_get_args());
- }
- $this->__handlePropertyHandler(__CLASS__);
- }
- public function ping()
- {
- return 'pong';
- }
- public function result()
- {
- $params = $this->request->all();
- if ($params['file_path']) {
- $info = finfo_open(FILEINFO_MIME_TYPE);
- $type = finfo_file($info, $params['file_path']);
- if (!in_array($type, ['video/mp4', 'image/png', 'image/jpg', 'image/jpeg', 'image/gif']) || !$params['dir']) {
- return response('文件类型错误' . $type . '或目录不存在', 400);
- }
- finfo_close($info);
- $data['file_name'] = $params['file_name'];
- $data['file_size'] = $params['file_size'];
- if ($type != 'video/mp4') {
- $data['file_path'] = make(SiteService::class)->encryptImage($params['file_path'], $params['dir']);
- } else {
- $data['file_path'] = make(SiteService::class)->moveVideo($params['file_path'], $params['dir']);
- }
- $res = ['result' => $data, 'code' => 0, 'message' => '上传成功'];
- return response()->json($res);
- } else {
- return response()->json('fail', 400);
- }
- }
- public function auth()
- {
- $params = $this->request->all();
- if ($params['platform'] == 'I') {
- $secret = 'pb5pe0MXChXsic0N';
- }
- if ($params['platform'] == 'A') {
- $secret = '4rKFwocMESIp4Anf';
- }
- if ($params['platform'] == 'P') {
- $secret = 'Z6Kbz8hpT9VtGmcu';
- }
- if (!$params['platform'] || !$params['timestamp'] || !$params['sign'] || !isset($secret)) {
- logger('sign:参数缺失', $params);
- return response('参数缺失', 403);
- }
- if (abs(time() - $params['timestamp']) > 3600) {
- logger('sign:时间验签失败' . time(), $params);
- return response('时间验签失败', 403);
- }
- $sign = strtoupper(md5($params['platform'] . $params['timestamp'] . $secret));
- if ($sign != $params['sign']) {
- logger('sign:时间验签失败' . $sign, $params);
- return response('签名验签失败', 403);
- }
- return json_success('ok');
- }
- public function move()
- {
- $params = $this->request->all();
- logger('开始移动文件:', $params);
- if (!isset($params['filepath']) || !isset($params['pathname'])) {
- logger('move', ['filepath/pathname不能为空']);
- return false;
- }
- if (!in_array($params['pathname'], ['vu', 'va', 'vd'])) {
- logger('move', ['mp4下载二级目录地址错误']);
- return false;
- }
- $path = '/var/www/resource/wp/mp/' . $params['pathname'] . '/' . date('mdH', time());
- $newname = $path . '/' . md5(microtime()) . mt_rand(1000, 9999) . '.mp4';
- make(SiteService::class)->mkDirs($path);
- if (rename($params['filepath'], $newname)) {
- logger('rename', [$newname . '->文件移动成功!']);
- return true;
- } else {
- logger('rename', ['文件移动失败']);
- return false;
- }
- }
- public function shift()
- {
- $params = $this->request->all();
- if (!$params['path'] || !$params['table'] || !$params['id'] || !$params['update']) {
- return json_fail('error');
- }
- $result = make(SiteService::class)->videoShift($params['path'], $params['table'], $params['id'], $params['update']);
- return json_success($result);
- }
- public function pushCrashLog(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- // Demo 这是在控制器内校验器的写法.
- $rules = ['ua' => 'required', 'user_id' => 'nullable', 'message' => 'required', 'version' => 'required', 'device_no' => 'required', 'device_type' => 'required|in:A,I,H'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $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']]);
- return json_success($result);
- }
- public function getVersion(Request $request, ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $request->all();
- // Demo 这是在控制器内校验器的写法.
- $rules = ['device_type' => 'required|in:A,I,H', 'version_code' => 'nullable|string'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $hVersionCode = data_get($inputData, 'version_code', '');
- $cacheKey = 'version:' . $inputData['device_type'];
- if (!empty($hVersionCode)) {
- $arrTmp = explode('.', $hVersionCode);
- if (count($arrTmp) != 3) {
- return json_fail('版本错误');
- }
- if (intval($arrTmp[0]) > 99 || intval($arrTmp[1]) > 99 || intval($arrTmp[2]) > 99) {
- return json_fail('版本溢出错误');
- }
- $cacheKey .= ':v' . $hVersionCode;
- }
- $result = make(CacheInterface::class)->remember($cacheKey, 60 * 60, function () use($inputData, $hVersionCode) {
- $conditions = ['status' => 1, 'device_type' => $inputData['device_type'], 'version_type' => 'free'];
- if (!empty($hVersionCode)) {
- $conditions['version_code'] = $hVersionCode;
- }
- $item = VersionModel::query()->where($conditions)->orderByDesc('version_code')->first();
- if (!empty($item)) {
- $item = $item->toArray();
- $item['app_url'] = make(ConfigRepoistory::class)->get('share_url', 'sys');
- $conditions = ['status' => 1, 'device_type' => $inputData['device_type'], 'version_type' => 'merge', 'version_code' => $item['version_code']];
- $item2 = VersionModel::query()->where($conditions)->orderByDesc('version_code')->first();
- $item['merge_path'] = '';
- // 分割下载url
- if (!empty($item2)) {
- $item['merge_path'] = $item2['package_path'];
- }
- return $item;
- } else {
- return null;
- }
- });
- $res = ['code' => 200, 'message' => '', 'data' => $result, 'handshake' => ''];
- return response()->json($res);
- }
- /**
- * 提供给H5-获取最新版本下载地址
- */
- public function getVersionH5(Request $request, ValidatorFactoryInterface $validatorFactory)
- {
- $android_info = VersionModel::query()->where(['status' => 1, 'device_type' => 'A', 'version_type' => 'free'])->orderByDesc('version_code')->first();
- $ios_free_info = VersionModel::query()->where(['status' => 1, 'device_type' => 'I', 'version_type' => 'free'])->orderByDesc('version_code')->first();
- $ios_shop_info = VersionModel::query()->where(['status' => 1, 'device_type' => 'I', 'version_type' => 'shop'])->orderByDesc('version_code')->first();
- $ios_testflight_info = VersionModel::query()->where(['status' => 1, 'device_type' => 'I', 'version_type' => 'testflight'])->orderByDesc('version_code')->first();
- $ios_enterprise_info = VersionModel::query()->where(['status' => 1, 'device_type' => 'I', 'version_type' => 'enterprise'])->orderByDesc('version_code')->first();
- $ios_super_info = VersionModel::query()->where(['status' => 1, 'device_type' => 'I', 'version_type' => 'super'])->orderByDesc('version_code')->first();
- $ios_ipa_info = VersionModel::query()->where(['status' => 1, 'device_type' => 'I', 'version_type' => 'ipa'])->orderByDesc('version_code')->first();
- $ios_ipa_gbox = VersionModel::query()->where(['status' => 1, 'device_type' => 'I', 'version_type' => 'gbox'])->orderByDesc('version_code')->first();
- $androidMergeInfo = VersionModel::query()->where(['status' => 1, 'device_type' => 'A', 'version_type' => 'merge'])->orderByDesc('version_code')->first();
- $potato_info = ConfigModel::query()->where('skey', 'potoato_url')->first();
- $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 : ''];
- $res = ['code' => 200, 'message' => '', 'data' => $data, 'handshake' => ''];
- return response()->json($res);
- }
- public function test123()
- {
- $filePath = base_path('storage/cache/m3u8_url/ff19/3e65/ff193e657aeaeb971d072becae6061df.url');
- if (file_exists($filePath)) {
- $mtime = filemtime($filePath);
- $phpDate = date('Y-m-d H:i:s', $mtime);
- $diffSeconds = Carbon::now()->diffInSeconds(Carbon::parse($mtime));
- $expiredSeconds = 2 * 60 * 60;
- // 原始文件保存2小时.
- $diffSeconds2 = abs($diffSeconds);
- $isExpired = $diffSeconds > $expiredSeconds;
- var_dump($mtime, $phpDate, $diffSeconds, $expiredSeconds, $diffSeconds2, $isExpired);
- }
- return json_success(['a1' => 'ok']);
- }
- }
|