config = $config; $this->client = new Client(); $this->app_id = $app_id; if (!request()->isAjax()) { try { $gtStr = file_get_contents("php://input"); $this->msgArr = XML::parse($gtStr); if (isset($this->msgArr['Encrypt'])) { $Encryptor = new Encryptor($config['app_id'], $config['token'], $config['aes_key']); $data = $Encryptor->decrypt($this->msgArr['Encrypt'], input('msg_signature'), input('nonce'), input('timestamp')); $this->message = XML::parse($data); } } catch (\Exception $e) { $this->err = $e->getMessage(); } } } public function ticket() { if ($this->message && $this->message['InfoType'] == 'component_verify_ticket') { try { $query = [ 'component_appid' => $this->config['app_id'], 'component_appsecret' => $this->config['secret'], 'component_verify_ticket' => $this->message['ComponentVerifyTicket'], ]; $resp = $this->client->post($this->base_uri . 'api/component/token/get', [ 'headers' => [ 'Accept' => 'application/json', ], 'body' => json_encode($query) ]); if ($text = json_decode($resp->getBody()->getContents(), true)) { if (isset($text['component_access_token'])) { Cache::set('qq_component_access_token', $text['component_access_token'], 7200); $respre = $this->client->post($this->base_uri . 'api/component/preauthcode/get?component_access_token=' . $text['component_access_token'], [ 'headers' => [ 'Accept' => 'application/json', ], 'body' => json_encode([ 'component_appid' => $this->config['app_id'] ]) ]); if ($prtxt = json_decode($respre->getBody()->getContents(), true)) { Cache::set('qq_pre_auth_code', $prtxt['pre_auth_code'], 600); } } } return 'success'; } catch (\Exception $e) { return $e->getMessage(); } } } public function event() { } /*** * 获取授权地址 * @param string $callbackUrl * @param array $optional * @return string */ public function getPreAuthorizationUrl(string $callbackUrl, $optional = []): string { // 兼容旧版 API 设计 if (\is_string($optional)) { $optional = [ 'pre_auth_code' => $optional, ]; } else { $optional['pre_auth_code'] = Cache::get('qq_pre_auth_code'); } $queries = \array_merge($optional, [ 'component_appid' => $this->config['app_id'], 'redirect_uri' => $callbackUrl, ]); return 'https://sp.q.qq.com/#/app_auth?' . http_build_query($queries); } /*** * 获取授权信息 * @param string $code * @return string */ public function handleAuthorize($code = '') { try { $query = [ 'component_appid' => $this->config['app_id'], 'authorization_code' => $code ]; $resp = $this->client->post($this->base_uri . 'api/component/authinfo/get?component_access_token=' . Cache::get('qq_component_access_token'), [ 'headers' => [ 'Accept' => 'application/json', ], 'body' => json_encode($query) ]); if ($text = json_decode($resp->getBody()->getContents(), true)) { if (isset($text['authorization_info'])) { $data = $text['authorization_info']; $data['expires_time'] = time(); Cache::set($text['authorization_info']['authorizer_appid'], $data); } return $text; } } catch (\Exception $e) { return $e->getMessage(); } } /** * 获取基本新 * @param $authorizer_appid * @return array */ public function getAuthorizer($authorizer_appid) { $appinfo = $this->getAccessToken($authorizer_appid); if ($appinfo) { $authorizer_access_token = $appinfo['authorizer_access_token']; $info = $this->client->get($this->base_uri . 'api/component/app/getaccountbasicinfo?access_token=' . $authorizer_access_token, [ 'headers' => [ 'Accept' => 'application/json', ] ]); $text = json_decode($info->getBody()->getContents(), true); return array_merge($appinfo, $text); } else { abort(0, '未授权'); } } /*** * 获取授权TOKEN * @param $authorizer_appid * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getAccessToken() { $appinfo = Cache::get($this->app_id); if (empty($appinfo)) { $config_model = new UniAccount(); $config_result = $config_model->getUniConfig(['appid' => $this->app_id, 'app_type' => 'qqapp'], 'QQAPP_CONFIG')['data']; $site_id = $config_result['site_id']; if ($config_result['values']) { $values = $config_result['values']; $appinfo['authorizer_access_token'] = $values['access_token']; $appinfo['authorizer_refresh_token'] = $values['auth_refresh_token']; $appinfo['expires_time'] = strtotime($config_result['update_time']); } } if ($appinfo && (time() - $appinfo['expires_time'] > 7200)) { $query = [ 'component_appid' => $this->config['app_id'], 'authorizer_appid' => $this->app_id, 'authorizer_refresh_token' => $appinfo['authorizer_refresh_token'] ]; $resp = $this->client->post($this->base_uri . 'api/component/authorizer_token/get?component_access_token=' . Cache::get('qq_component_access_token'), [ 'headers' => [ 'Accept' => 'application/json', ], 'body' => json_encode($query) ]); if ($text = json_decode($resp->getBody()->getContents(), true)) { if (isset($text['authorizer_access_token'])) { $appinfo['expires_time'] = time(); $appinfo['authorizer_access_token'] = $text['authorizer_access_token']; $appinfo['authorizer_refresh_token'] = $text['authorizer_refresh_token']; Cache::set($this->app_id, $appinfo); if ($site_id) { $config_model = new UniAccount(); $condition = ['appid' => $this->app_id, 'site_id' => $site_id]; $data['access_token'] = $text['authorizer_access_token']; $data['auth_refresh_token'] = $text['authorizer_refresh_token']; $data = array_merge($values, $data); $config_model->setUniConfig($condition, 'QQAPP_CONFIG', 'qqapp', $data, 0); } } } } return $appinfo; } /*** * 获取设置域名 * @param $authorizer_appid * @param $params * @return mixed */ public function domain($authorizer_appid, $params) { $appinfo = $this->getAccessToken($authorizer_appid); if ($appinfo) { $authorizer_access_token = $appinfo['authorizer_access_token']; $info = $this->client->post($this->base_uri . 'api/component/domain/modify_domain?access_token=' . $authorizer_access_token, [ 'headers' => [ 'Accept' => 'application/json', ], 'body' => json_encode($params) ]); $text = json_decode($info->getBody()->getContents(), true); return $text; } } /** * 获取模板 * @return mixed */ public function getTemplate() { $info = $this->client->get($this->base_uri . 'api/component/template/gettemplatelist?component_access_token=' . Cache::get('qq_component_access_token'), [ 'headers' => [ 'Accept' => 'application/json', ], ]); $text = json_decode($info->getBody()->getContents(), true); return $text; } /** * 提交代码 */ public function codeCommit($authorizer_appid, $params) { $appinfo = $this->getAccessToken($authorizer_appid); if ($appinfo) { $authorizer_access_token = $appinfo['authorizer_access_token']; $info = $this->client->post($this->base_uri . 'api/component/code/commit?access_token=' . $authorizer_access_token, [ 'headers' => [ 'Accept' => 'application/json', ], 'body' => json_encode($params) ]); $text = json_decode($info->getBody()->getContents(), true); return $text; } } /**获取体验二维码 * @param $authorizer_appid * @return string */ public function getQrCode($authorizer_appid) { $appinfo = $this->getAccessToken($authorizer_appid); if ($appinfo) { $authorizer_access_token = $appinfo['authorizer_access_token']; $info = $this->client->get($this->base_uri . 'api/component/code/get_qrcode?access_token=' . $authorizer_access_token, [ 'headers' => [ 'Accept' => 'application/json', ], ]); $text = $info->getBody()->getContents(); return $text; } } /** * 获取提交页面 * @param string $authorizer_appid * @return mixed */ public function getPage($authorizer_appid = '') { if ($authorizer_appid) { $this->app_id = $authorizer_appid; } $appinfo = $this->getAccessToken(); $info = $this->client->get($this->base_uri . 'api/component/code/get_pages?access_token=' . $appinfo['authorizer_access_token'], [ 'headers' => [ 'Accept' => 'application/json', ], ]); $text = json_decode($info->getBody()->getContents(), true); return $text; } /*** * 提交审核 * @param $params * @param string $authorizer_appid * @return mixed */ public function submitAudit($params, $authorizer_appid = '') { if ($authorizer_appid) { $this->app_id = $authorizer_appid; } $appinfo = $this->getAccessToken(); $info = $this->client->get($this->base_uri . 'api/component/code/submit_audit?access_token=' . $appinfo['authorizer_access_token'], [ 'headers' => [ 'Accept' => 'application/json' ], 'body' => json_encode($params) ]); $text = json_decode($info->getBody()->getContents(), true); return $text; } /** * 快速审核 * @return mixed */ public function speedupAudit() { } /*** * 回滚上一版本 * @return mixed */ public function rollbackRelease() { $appinfo = $this->getAccessToken(); $info = $this->client->get($this->base_uri . 'api/component/code/undocodeaudit?access_token=' . $appinfo['authorizer_access_token'], [ 'headers' => [ 'Accept' => 'application/json' ], ]); $text = json_decode($info->getBody()->getContents(), true); return $text; } /** * 发布小程序 * @return mixed */ public function release() { $appinfo = $this->getAccessToken(); $info = $this->client->get($this->base_uri . 'api/component/code/release?access_token=' . $appinfo['authorizer_access_token'], [ 'headers' => [ 'Accept' => 'application/json' ], ]); $text = json_decode($info->getBody()->getContents(), true); return $text; } /*** * 撤销审核 * @return mixed */ public function withdrawAudit() { $appinfo = $this->getAccessToken(); $info = $this->client->get($this->base_uri . 'wxa/undocodeaudit?access_token=' . $appinfo['authorizer_access_token'], [ 'headers' => [ 'Accept' => 'application/json' ], ]); $text = json_decode($info->getBody()->getContents(), true); return $text; } }