target($request, $token); $expires = $this->expiresAt($request); $files = $this->store->db()->table('courier_bill_files')->where('group_id', $t['group']->id)->where('bill_month', $t['month']) ->orderByDesc('id')->limit(20)->get(); $pending = array_map(static fn (array $file): object => (object) [ 'original_name' => $file['name'], 'row_count' => $file['count'], 'amount' => $file['amount'], 'status' => $file['status'], 'message' => $file['message'], 'created_at' => $file['at'], ], $this->imports->recent($t['companyId'], (string) $t['group']->name, (int) $t['group']->id, $t['month'])); $files = $files->concat($pending)->sortByDesc('created_at')->take(20)->values(); return response()->view('business.pages.courier-payables.courier-bills.upload-share', [ 'group' => $t['group'], 'month' => $t['month'], 'monthLabel' => self::monthLabel($t['month']), 'files' => $files, 'expiresAt' => $expires, 'confirmed' => $t['confirmed'], 'uploadUrl' => $this->links->signedRoute('signed.courier-bill-upload.upload', $token, $t['companyId'], $expires), 'templateUrl' => $this->links->signedRoute('signed.courier-bill-upload.template', $token, $t['companyId'], $expires), ])->withHeaders(['Cache-Control' => 'private, no-store, max-age=0', 'X-Robots-Tag' => 'noindex, nofollow, noarchive', 'Referrer-Policy' => 'no-referrer']); } public function template(Request $request, string $token): StreamedResponse { $t = $this->target($request, $token); return $this->template->download($t['group']->name . '_' . $t['month'] . '_账单上传模板.xlsx'); } public function upload(Request $request, string $token): RedirectResponse { $t = $this->target($request, $token); $back = $this->links->signedRoute('signed.courier-bill-upload.show', $token, $t['companyId'], $this->expiresAt($request)); if ($t['confirmed']) { throw ValidationException::withMessages(['upload' => '这个月的账单财务已确认结算,不能再上传;有问题请在群里联系财务。']); } $request->validate(['import' => ['required', 'file', 'max:51200']], [], ['import' => '账单文件']); $file = $request->file('import'); $extension = strtolower($file->getClientOriginalExtension()); if (! in_array($extension, ['xlsx', 'xls', 'csv'], true)) { throw ValidationException::withMessages(['upload' => '只支持 xlsx、xls、csv,请用上传模板。']); } $this->imports->submit($file, [ 'name' => (string) $t['group']->name, 'kind' => 'courier', 'month' => $t['month'], 'target_id' => (int) $t['group']->id, 'company_id' => $t['companyId'], 'source' => 'courier_upload_link', 'uploader' => mb_substr($t['group']->name . '(快递上传链接)', 0, 64), ]); return redirect()->to($back)->with('success', '文件已收到,正在等待校验和读入。刷新本页可查看结果,读入成功后会向已配置的钉钉群发送回执。'); } /** @return array{companyId:int,group:object,month:string,confirmed:bool} */ private function target(Request $request, string $token): array { $companyId = (int) ($request->route('company_id') ?: company_id() ?: 1); $payload = $this->links->resolveToken($token, $companyId); abort_if($payload === null, 404, '链接无效或已过期。'); $group = collect($this->store->groups())->firstWhere('id', $payload['group_id']); abort_if($group === null, 404, '链接无效或已过期。'); $confirmed = $this->store->db()->table('courier_bill_statements')->where('group_id', $group->id) ->where('bill_month', $payload['month'])->where('status', 'confirmed')->exists(); return ['companyId' => $companyId, 'group' => $group, 'month' => $payload['month'], 'confirmed' => $confirmed]; } private function expiresAt(Request $request): CarbonImmutable { $expires = (int) $request->query('expires', 0); return $expires > 0 ? CarbonImmutable::createFromTimestamp($expires) : CarbonImmutable::now()->addDays(CourierUploadLink::TTL_DAYS); } private static function monthLabel(string $month): string { [$y, $m] = explode('-', $month); return (int) $y . '年' . (int) $m . '月'; } }