/** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Request $request) { $device = Device::current(); if (Input::hasFile('file')) { $inputFile = Input::file('file'); $package = Package::createFromInputFile($inputFile, $device->user_id); } else { $package = Package::findOrFailFromArg(Input::get('package'), $device->user_id); } $ids = explode(',', Input::get('devices')); if (count($ids) == 1 && !is_numeric($ids[0])) { $token = Token::whereValue($ids[0])->valid()->first(); if ($token) { $devices = Device::whereId($token->owner)->get(); } else { return Response::error(trans('errors.expired_device_qrcode') . $ids[0], 400); } } if (empty($devices)) { $devices = Device::whereIn('id', $ids)->where(function ($query) use($device) { $query->whereUserId($device->user_id)->orWhere(function ($query) use($device) { $authed_device_ids = DUAuth::whereUserId($device->user_id)->lists('device_id'); $query->whereIn('id', $authed_device_ids); }); })->get(); } try { $push = Push::send($devices, $package, $device->user_id); return Response::json($push); } catch (\Exception $e) { return Response::exception($e); } }
/** * Execute the job. * * @param JPushClient $client */ public function handle(JPushClient $client) { $error = ''; try { if ($this->download($this->url)) { $package = Package::createFromFile($this->tmpFile, basename($this->url), $this->user->id); $result = Push::send($this->devices, $package, $this->user->id); try { if ($this->devices->count() > 1) { $msg = "已在向{$this->devices->count()}台设备发出推送..."; } else { $msg = "已向{$this->devices->first()->alias}发出推送..."; } Log::info($msg); } catch (\Exception $e) { Log::error('[PushApk Job]推送失败' . $e->getMessage()); return; } } else { $error = trans('errors.download_failed'); Log::error('[PushApk Job]下载文件失败: ' . $this->url); } } catch (Exception $e) { $error = $e->getMessage(); Log::error('[PushApk]' . $e->getMessage() . ' ' . $this->url); } $installIds = $this->devices->pluck('install_id')->toArray(); try { $result = $client->push()->setPlatform(M\all)->setAudience(M\registration_id($installIds))->setNotification(M\notification('出现错误,请检查URL是否正确'))->send(); } catch (Exception $ignored) { } }
/** * 提交Push */ public function postPush() { $data = Input::all(); $devices_ids = Input::get('devices'); $devices = Device::whereUserId(Auth::id())->whereIn('devices.id', $devices_ids)->get(); if (!Input::has('package') && Input::has('url')) { $this->dispatch(new App\Jobs\PushApk(Auth::user(), $data['url'], $devices)); return redirect('/install')->withToast('服务器正在努力处理中,请稍等...'); } $package = Package::findOrFailFromArg($data['package'], Auth::id()); $result = Push::send($devices, $package, Auth::id()); try { if ($devices->count() > 1) { $msg = "已在向{$devices->count()}台设备发出推送..."; } else { $msg = "已向{$devices->first()->alias}发出推送..."; } return redirect('/install')->withToast($msg); } catch (\Exception $e) { return Redirect::back()->withToast($result); } }