handleRegister($params); case 'deposit': return $this->handleDeposit($params); case 'withdraw': return $this->handleWithdraw($params); case 'check': return $this->handleCheck($params); default: throw new \Exception("不支持的操作类型: {$action}"); } } /** * 处理用户注册 * * @param array $params 请求参数 * @return array */ protected function handleRegister(array $params): array { // 获取URS配置 $config = $this->getConfig(); $apiUrl = $config['api_url'] ?? ''; $appId = $config['app_id'] ?? ''; $appSecret = $config['app_secret'] ?? ''; // 构建注册请求数据 $requestData = [ 'app_id' => $appId, 'user_id' => $params['user_id'], 'username' => $params['username'], 'timestamp' => time(), ]; // 生成签名 $requestData['sign'] = $this->generateSign($requestData, $appSecret); // 发送HTTP请求到URS $response = $this->sendHttpRequest($apiUrl . '/register', $requestData); return [ 'success' => $response['code'] === 0, 'message' => $response['message'] ?? '', 'data' => $response['data'] ?? [], ]; } /** * 处理充值操作 * * @param array $params 请求参数 * @return array */ protected function handleDeposit(array $params): array { $config = $this->getConfig(); $apiUrl = $config['api_url'] ?? ''; $appId = $config['app_id'] ?? ''; $appSecret = $config['app_secret'] ?? ''; $requestData = [ 'app_id' => $appId, 'user_id' => $params['user_id'], 'amount' => $params['amount'], 'order_id' => $params['order_id'], 'timestamp' => time(), ]; $requestData['sign'] = $this->generateSign($requestData, $appSecret); $response = $this->sendHttpRequest($apiUrl . '/deposit', $requestData); return [ 'success' => $response['code'] === 0, 'message' => $response['message'] ?? '', 'data' => $response['data'] ?? [], ]; } /** * 处理提取操作 * * @param array $params 请求参数 * @return array */ protected function handleWithdraw(array $params): array { $config = $this->getConfig(); $apiUrl = $config['api_url'] ?? ''; $appId = $config['app_id'] ?? ''; $appSecret = $config['app_secret'] ?? ''; $requestData = [ 'app_id' => $appId, 'user_id' => $params['user_id'], 'amount' => $params['amount'], 'order_id' => $params['order_id'], 'timestamp' => time(), ]; $requestData['sign'] = $this->generateSign($requestData, $appSecret); $response = $this->sendHttpRequest($apiUrl . '/withdraw', $requestData); return [ 'success' => $response['code'] === 0, 'message' => $response['message'] ?? '', 'data' => $response['data'] ?? [], ]; } /** * 处理余额检查 * * @param array $params 请求参数 * @return array */ protected function handleCheck(array $params): array { $config = $this->getConfig(); $apiUrl = $config['api_url'] ?? ''; $appId = $config['app_id'] ?? ''; $appSecret = $config['app_secret'] ?? ''; $requestData = [ 'app_id' => $appId, 'user_id' => $params['user_id'], 'timestamp' => time(), ]; $requestData['sign'] = $this->generateSign($requestData, $appSecret); $response = $this->sendHttpRequest($apiUrl . '/check', $requestData); return [ 'success' => $response['code'] === 0, 'message' => $response['message'] ?? '', 'data' => $response['data'] ?? [], ]; } /** * 生成签名 * * @param array $data 数据 * @param string $secret 密钥 * @return string */ protected function generateSign(array $data, string $secret): string { // 排序参数 ksort($data); // 构建签名字符串 $signString = ''; foreach ($data as $key => $value) { if ($key !== 'sign') { $signString .= $key . '=' . $value . '&'; } } $signString .= 'key=' . $secret; return md5($signString); } /** * 发送HTTP请求 * * @param string $url 请求URL * @param array $data 请求数据 * @return array * @throws \Exception */ protected function sendHttpRequest(string $url, array $data): array { $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $url, CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($data), CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 30, CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'User-Agent: KKU-ThirdParty-URS/1.0', ], ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch); if ($error) { throw new \Exception("HTTP请求失败: {$error}"); } if ($httpCode !== 200) { throw new \Exception("HTTP请求失败,状态码: {$httpCode}"); } $result = json_decode($response, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new \Exception("响应数据格式错误: " . json_last_error_msg()); } return $result; } }