action = $action; $this->title = $action == 1 ? '审核状态' : '审核撤销'; } protected function html() { $class = $this->action == 1 ? 'btn btn-sm btn-success' : 'btn btn-sm btn-danger'; $this->appendHtmlAttribute('class', $class); $this->defaultHtmlAttribute('href', 'javascript:;'); return "formatHtmlAttributes()}>{$this->title}"; } public function handle() { return $this->action == 1 ? $this->auditStatus() : $this->undoAudit(); } private function auditStatus() { $agent = Agent::find($this->getKey()); if (empty($agent->appid)) { return $this->response()->error('该代理商未注册过小程序,请先注册'); } $log = MiniProgramUploadLog::where(['agent_id' => $agent->id])->orderBy('id', 'desc')->first(); if (!$log) { return $this->response()->error('该代理商未上传及发布过最新版本的小程序'); } else if (!$log->audit_id) { return $this->response()->error('未找到审核记录audit_id'); } try { $openPlatform = new OpenPlatform(); $refreshToken = $openPlatform->refreshToken($agent->appid); if (!$refreshToken) { return $this->response()->error('获取refresh_token失败'); } /** @var Client $code */ $code = $openPlatform->code($agent->appid, $refreshToken); if (!$code) { return $this->response()->error('获取code失败'); } $res = $code->getAuditStatus($log->audit_id); if (isset($res['errcode'], $res['errmsg'], $res['status']) && $res['errcode'] == 0 && $res['errmsg'] == 'ok') { //保存审核状态 $log->is_success = $res['status']; $log->save(); $statusArr = [0 => '审核成功', 1 => '审核被拒绝', 2 => '审核中', 3 => '已撤回', 4 => '审核延后']; if ($res['status'] === 0) { //如果审核成功则发布 $res_release = $code->release(); //发布结果处理 $releaseArr = [-1 => '系统繁忙', 85019 => '没有审核版本', 85020 => '审核状态未满足发布']; if (isset($res_release['errcode'], $res_release['errmsg']) && $res_release['errcode'] == 0 && $res_release['errmsg'] == 'ok') { return $this->response()->success("模板ID {$log->template_id} 的小程序已审核成功且发布")->refresh(); } else if (isset($releaseArr[$res_release['errcode']])) { return $this->response()->warning('审核成功,发布结果为:' . $releaseArr[$res_release['errcode']])->refresh(); } else { return $this->response()->warning('审核成功,但发布出错:' . join(',', $res_release))->refresh(); } } else if (isset($statusArr[$res['status']])) { return $this->response()->warning($statusArr[$res['status']])->refresh(); } else { return $this->response()->error($res['reason'] ?? join(',', $res)); } } else { return $this->response()->error($res['errmsg'] ?? join(',', $res)); } } catch (InvalidConfigException | GuzzleException | \Exception $e) { return $this->response()->error($e->getMessage()); } } private function undoAudit() { $agent = Agent::find($this->getKey()); if (empty($agent->appid)) { return $this->response()->error('该代理商未注册过小程序,请先注册'); } $log = MiniProgramUploadLog::where(['agent_id' => $agent->id])->orderBy('id', 'desc')->first(); if (!$log) { return $this->response()->error('该代理商未上传及发布过最新版本的小程序'); } else if (!$log->audit_id) { return $this->response()->error('未找到审核记录audit_id'); } try { $openPlatform = new OpenPlatform(); $refreshToken = $openPlatform->refreshToken($agent->appid); if (!$refreshToken) { return $this->response()->error('获取refresh_token失败'); } /** @var Client $code */ $code = $openPlatform->code($agent->appid, $refreshToken); if (!$code) { return $this->response()->error('获取code失败'); } $res = $code->withdrawAudit(); if (isset($res['errcode'], $res['errmsg']) && $res['errcode'] == 0 && $res['errmsg'] == 'ok') { //保存审核状态为已撤回 $log->is_success = 3; $log->save(); return $this->response()->success("撤回最后审核单成功")->refresh(); } else if (isset($res['errcode']) && $res['errcode'] != 0) { $error = [ -1 => '腾讯内部系统错误', 87013 => '撤回次数达到上限(每天5次,每个月 10 次)', ]; return $this->response()->error(isset($error[$res['errcode']]) ? $error[$res['errcode']] : ($res['errmsg'] ?? join(',', $res))); } else { return $this->response()->error($res['errmsg'] ?? join(',', $res)); } } catch (InvalidConfigException | GuzzleException | \Exception $e) { return $this->response()->error($e->getMessage()); } } public function confirm() { if ($this->action == 2) { return ['撤回次数每天最多5次,每个月最多10次,是否继续?', '']; } } public function parameters() { return ['action' => $this->action]; } }