$url, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 30, CURLOPT_HTTPHEADER => ["Authorization: $authToken"], CURLOPT_SSL_VERIFYPEER => false, ]); $resp = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch); if ($error) { return ['error' => "请求失败: $error"]; } if ($httpCode !== 200) { return ['error' => "HTTP $httpCode"]; } $data = json_decode($resp, true); if (!$data || !isset($data['code']) || $data['code'] !== 0) { return ['error' => $data['message'] ?? '接口返回格式异常']; } return $data; } function syncCouponStatus(PDO $pdo, array $product, int $startTime, int $endTime): array { $page = 1; $size = 100; $matched = 0; $updated = 0; $matchedCodes = []; $unmatchedCodes = []; $total = 0; while (true) { $result = callCouponApi($product['api_url'], $product['code'], $startTime, $endTime, $page, $size); if (isset($result['error'])) { return ['success' => false, 'message' => $result['error']]; } $items = $result['data']['items'] ?? []; $total = (int)$result['data']['total']; foreach ($items as $item) { if ((string)$item['status'] !== '2') continue; $code = $item['coupon_code']; $usedAt = $item['use_time'] ?? null; $userId = $item['user_id'] ?? null; $stmt = $pdo->prepare("SELECT id, status FROM claim_records WHERE code = ? AND product_id = ? LIMIT 1"); $stmt->execute([$code, $product['id']]); $cr = $stmt->fetch(); if (!$cr) { $unmatchedCodes[] = $code; continue; } $matched++; $matchedCodes[] = $code; if ((int)$cr['status'] !== 2) { $stmt = $pdo->prepare("UPDATE claim_records SET status = 2, used_at = ?, used_user_id = ?, remark = COALESCE(remark, 'API同步'), updated_at = NOW() WHERE id = ? AND status != 2"); $stmt->execute([$usedAt, $userId, $cr['id']]); $updated += $stmt->rowCount(); } } if ($page * $size >= $total) break; $page++; } return [ 'success' => true, 'total' => $total, 'matched' => $matched, 'updated' => $updated, 'matched_codes' => $matchedCodes, 'unmatched_codes' => $unmatchedCodes, ]; }