appendHtmlAttribute('class', 'btn btn-sm btn-primary'); $this->defaultHtmlAttribute('href', 'javascript:;'); return "formatHtmlAttributes()}>{$this->title}"; } public function handle(Request $request) { $template = MiniProgramTemplateList::orderBy('template_id', 'desc')->first(); if (MiniProgramUploadLog::query()->where(['agent_id' => $this->getKey(), 'template_id' => $template->template_id])->exists()) { return $this->response()->error('该代理商已经上传过小程序,无需重复上传'); } $agent = Agent::find($this->getKey()); if (empty($agent->appid)) { return $this->response()->error('该代理商未注册过小程序,请先注册'); } try { $setting = AdminSetting::val(['service_appid', 'service_appsecret', 'service_token', 'service_aeskey']); $config = [ 'app_id' => $setting['service_appid'], 'secret' => $setting['service_appsecret'], 'token' => $setting['service_token'], 'aes_key' => $setting['service_aeskey'], ]; $openPlatform = \EasyWeChat\Factory::openPlatform($config); $refreshToken = $openPlatform->getAuthorizer($agent->appid)['authorization_info']['authorizer_refresh_token'] ?? null; if (!$refreshToken) { return $this->response()->error('获取refresh_token失败'); } $code = $openPlatform->miniProgram($agent->appid, $refreshToken)['code']; $templateId = $template->template_id; $extJson = json_encode(['extAppid' => $agent->appid]); $version = $template->user_version; $description = $agent->company_name; $commit = $code->commit($templateId, $extJson, $version, $description); // $qrcode = $code->getQrCode(); if (isset($commit['errcode'], $commit['errmsg']) && $commit['errcode'] == 0 && $commit['errmsg'] == 'ok') { MiniProgramUploadLog::insert([ 'agent_id' => $agent->id, 'appid' => $agent->appid, 'template_id' => $templateId, ]); return $this->response()->success("上传成功")->refresh(); } else { throw new \Exception(join(',', $commit)); } } catch (\Exception $e) { return $this->response()->error($e->getMessage()); } } public function confirm() { return ['确定要上传小程序吗?', '']; } }