action = $action; $this->title = $action == 1 ? '上传小程序' : '上传给所有代理商'; } protected function html() { $class = 'btn btn-sm btn-primary'; $this->appendHtmlAttribute('class', $class); $this->defaultHtmlAttribute('href', 'javascript:;'); return "formatHtmlAttributes()}>{$this->title}"; } public function handle(Request $request) { return $request->action == 1 ? $this->uploadOne() : $this->uploadAll(); } //注:uploadOne在代理商列表页使用 private function uploadOne() { $template = MiniProgramTemplate::orderBy('template_id', 'desc')->first(); $agent = Agent::find($this->getKey()); if (empty($agent->appid)) { return $this->response()->error('该代理商未注册过小程序,请先注册'); } try { new UploadMiniProgram($agent, $template); return $this->response()->success("上传成功,并已提交审核")->refresh(); } catch (GuzzleException | \Exception $e) { return $this->response()->error($e->getMessage()); } } //注:uploadAll在小程序模板列表页使用 private function uploadAll() { //上传单个时,$this->getKey()是代理商ID,上传多个时,是小程序模板ID $template_id = $this->getKey(); $ids = Agent::whereNotNull('appid') ->where([ ['status', '=', UserStatus::NORMAL], ['appid', '<>', ''], ])->pluck('id'); foreach ($ids as $id) { BatchUploadMiniProgram::dispatch($template_id, $id); } return $this->response()->success('已经为所有已注册过小程序,且状态正常的代理商上传小程序。上传需要一定时间,请耐心等待'); } public function confirm() { if ($this->action == 1) { $last_template_id = MiniProgramTemplate::max('template_id'); return ["确定要上传模板ID为{$last_template_id}的小程序吗?", '']; } else { return ['上传确认', '确定要将此小程序模板上传给所有已审核的代理商吗?']; } } public function parameters() { return ['action' => $this->action]; } }