apiurl = $base !== '' ? rtrim($base, '/') . '/connect.php' : ''; $this->appid = trim((string) ($config['appid'] ?? '')); $this->appkey = trim((string) ($config['appkey'] ?? '')); $this->callback = (string) ($config['callback'] ?? ''); } public function login(string $type, string $state): array { $type = sanitize_key($type); if (!in_array($type, ['qq', 'weixin', 'weibo'], true) || $state === '') { return ['code' => 1, 'msg' => __('聚合登录参数无效', 'jio')]; } return $this->request([ 'act' => 'login', 'appid' => $this->appid, 'appkey' => $this->appkey, 'type' => $this->to_api_type($type), 'redirect_uri' => $this->callback, 'state' => $state, ], true); } public function callback(string $code): array { return $this->request([ 'act' => 'callback', 'appid' => $this->appid, 'appkey' => $this->appkey, 'code' => $code, ]); } public function to_api_type(string $type): string { switch (sanitize_key($type)) { case 'weixin': return 'wx'; case 'weibo': return 'sina'; default: return sanitize_key($type); } } public function from_api_type(string $type): string { switch (sanitize_key($type)) { case 'wx': return 'weixin'; case 'sina': return 'weibo'; default: return sanitize_key($type); } } private function request(array $params, bool $expect_url = false): array { if ($this->apiurl === '' || $this->appid === '' || $this->appkey === '') { return ['code' => 1, 'msg' => __('聚合登录配置无效', 'jio')]; } $url = $this->apiurl . '?' . http_build_query($params); $response = wp_safe_remote_get($url, [ 'timeout' => 15, 'redirects' => 1, 'limit_response_size' => 256 * 1024, 'headers' => ['User-Agent' => 'Jio-CLogin/2.0'], ]); if (is_wp_error($response)) { return ['code' => 1, 'msg' => $response->get_error_message()]; } $status = (int) wp_remote_retrieve_response_code($response); if ($status < 200 || $status >= 300) { return ['code' => 1, 'msg' => __('聚合登录接口请求失败', 'jio')]; } $data = json_decode((string) wp_remote_retrieve_body($response), true); if (!is_array($data)) { return ['code' => 1, 'msg' => __('聚合登录接口返回异常', 'jio')]; } if ($expect_url && empty($data['code']) && OAuthUrlPolicy::public_https((string) ($data['url'] ?? '')) === '') { return ['code' => 1, 'msg' => __('聚合登录返回地址无效', 'jio')]; } return $data; } }