diff --git a/app/Admin/Extensions/Grid/MiniProgramAuditStatus.php b/app/Admin/Extensions/Grid/MiniProgramAuditStatus.php new file mode 100644 index 0000000..2de218e --- /dev/null +++ b/app/Admin/Extensions/Grid/MiniProgramAuditStatus.php @@ -0,0 +1,97 @@ +action = $action; //$action:1=通过;2=拒绝 + $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(Request $request) + { + $agent = Agent::find($this->getKey()); + if (empty($agent->appid)) { + return $this->response()->error('该代理商未注册过小程序,请先注册'); + } + + $template_id = MiniProgramTemplate::max('template_id'); + $log = MiniProgramUploadLog::where(['agent_id' => $agent->id, 'template_id' => $template_id])->orderBy('id', 'desc')->first(); + if (!$log) { + 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 = Factory::openPlatform($config); + $refreshToken = $openPlatform->getAuthorizer($agent->appid)['authorization_info']['authorizer_refresh_token'] ?? null; + if (!$refreshToken) { + return $this->response()->error('获取refresh_token失败'); + } + /** @var Client $code */ + $code = $openPlatform->miniProgram($agent->appid, $refreshToken)['code'] ?? null; + 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') { + $statusArr = ['审核成功', '审核被拒绝', '审核中', '已撤回', '审核延后']; + if (isset($statusArr[$res['status']])) { + return $this->response()->success($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()); + } + } + + public function parameters() + { + return ['action' => $this->action]; + } +}