|
|
@@ -186,34 +186,45 @@ class Auth
|
|
|
/**
|
|
|
* 解析API响应
|
|
|
*
|
|
|
+ * 货拉拉 API 返回格式:
|
|
|
+ * {
|
|
|
+ * "ret": 0,
|
|
|
+ * "msg": "success",
|
|
|
+ * "data": {
|
|
|
+ * "auth_mobile": "17168665598",
|
|
|
+ * "access_token": "a3207b6b5ad04249ad1dbf6a9824dbea",
|
|
|
+ * "refresh_token": "4ccbbab0e9e443159c518d1d10741ad",
|
|
|
+ * "auth_end_time": "2020-06-07 15:35:08"
|
|
|
+ * }
|
|
|
+ * }
|
|
|
+ *
|
|
|
* @param array $response HTTP响应
|
|
|
* @return array 标准化的响应格式
|
|
|
*/
|
|
|
protected function parseResponse($response)
|
|
|
{
|
|
|
- // 如果是HTTP错误
|
|
|
- if (isset($response['code']) && $response['code'] != 200) {
|
|
|
+ // 检查 ret 字段(0 表示成功)
|
|
|
+ if (!isset($response['ret'])) {
|
|
|
return [
|
|
|
'success' => false,
|
|
|
- 'error' => $response['error'] ?? $response['msg'] ?? '请求失败',
|
|
|
- 'status_code' => $response['code'] ?? 0,
|
|
|
+ 'error' => $response['msg'] ?? '响应格式错误,缺少 ret 字段',
|
|
|
];
|
|
|
}
|
|
|
|
|
|
- // 检查API状态码(货拉拉通常返回 status 或直接在顶层)
|
|
|
- if (isset($response['status']) && $response['status'] != 200) {
|
|
|
+ // 如果 ret 不等于 0,表示请求失败
|
|
|
+ if ($response['ret'] != 0) {
|
|
|
return [
|
|
|
'success' => false,
|
|
|
'error' => $response['msg'] ?? 'API返回异常',
|
|
|
- 'status' => $response['status'],
|
|
|
+ 'ret' => $response['ret'],
|
|
|
];
|
|
|
}
|
|
|
|
|
|
- // 货拉拉可能直接返回数据或包含在 data 字段
|
|
|
- $data = $response['data'] ?? $response;
|
|
|
+ // 提取 data 字段中的数据
|
|
|
+ $data = $response['data'] ?? [];
|
|
|
|
|
|
- // 检查是否包含token信息
|
|
|
- if (empty($data['access_token']) && empty($response['access_token'])) {
|
|
|
+ // 检查是否包含必需的token信息
|
|
|
+ if (empty($data['access_token'])) {
|
|
|
return [
|
|
|
'success' => false,
|
|
|
'error' => $response['msg'] ?? '未获取到 access_token',
|
|
|
@@ -223,9 +234,10 @@ class Auth
|
|
|
// 成功响应
|
|
|
return [
|
|
|
'success' => true,
|
|
|
- 'access_token' => $data['access_token'] ?? $response['access_token'] ?? '',
|
|
|
- 'refresh_token' => $data['refresh_token'] ?? $response['refresh_token'] ?? '',
|
|
|
- 'expires_in' => $data['expires_in'] ?? $response['expires_in'] ?? 0,
|
|
|
+ 'access_token' => $data['access_token'] ?? '',
|
|
|
+ 'refresh_token' => $data['refresh_token'] ?? '',
|
|
|
+ 'auth_mobile' => $data['auth_mobile'] ?? '',
|
|
|
+ 'auth_end_time' => $data['auth_end_time'] ?? '',
|
|
|
];
|
|
|
}
|
|
|
|