|
|
@@ -3328,11 +3328,11 @@ class UserRepository extends BaseRepository
|
|
3328
|
3328
|
* 使用证书方式,通过 authCode 换取支付宝 AccessToken 和用户信息
|
|
3329
|
3329
|
*
|
|
3330
|
3330
|
* @param string $authorization_code 从 APP 获取的授权码
|
|
3331
|
|
- * @param bool $info 是否在获取 Token 后立即获取用户信息 (true: 获取, false: 只获取 Token)
|
|
3332
|
|
- * @return array|string 成功时返回包含 token 或用户信息的数组,失败时返回错误信息数组或字符串
|
|
|
3331
|
+ * @param bool $info 是否在获取 Token 后立即获取用户信息 (true: 获取, false: 只获取 Token)
|
|
|
3332
|
+ * @return array 成功时返回包含 token 或用户信息的数组,失败时返回错误信息数组或字符串
|
|
3333
|
3333
|
* @throws \Exception
|
|
3334
|
3334
|
*/
|
|
3335
|
|
- public function getTokenByCodeWithCert($authorization_code, $info = false)
|
|
|
3335
|
+ public function getTokenByCodeWithCert(string $authorization_code, bool $info = false): array
|
|
3336
|
3336
|
{
|
|
3337
|
3337
|
try {
|
|
3338
|
3338
|
// --- 1. 配置证书信息 ---
|
|
|
@@ -3392,17 +3392,17 @@ class UserRepository extends BaseRepository
|
|
3392
|
3392
|
if (isset($resultObj->$responseNode) && !isset($resultObj->error_response)) {
|
|
3393
|
3393
|
// 成功获取 Token
|
|
3394
|
3394
|
$response = $resultObj->$responseNode; // 获取响应数据对象
|
|
3395
|
|
- $return_data = [
|
|
3396
|
|
- 'code' => 200,
|
|
3397
|
|
- 'msg' => 'Token fetched successfully',
|
|
3398
|
|
- 'user_id' => $response->user_id, // 从 $response 获取
|
|
3399
|
|
- 'access_token' => $response->access_token, // 从 $response 获取
|
|
3400
|
|
- 'refresh_token' => $response->refresh_token,
|
|
3401
|
|
- 'expires_in' => $response->expires_in,
|
|
3402
|
|
- 're_expires_in' => $response->re_expires_in,
|
|
3403
|
|
- // 确保 alipay_user_id 也捕获,如果需要的话 (看你的业务逻辑)
|
|
3404
|
|
- // 'alipay_user_id' => $response->alipay_user_id,
|
|
3405
|
|
- ];
|
|
|
3395
|
+ // $return_data = [
|
|
|
3396
|
+ // 'code' => 200,
|
|
|
3397
|
+ // 'msg' => 'Token fetched successfully',
|
|
|
3398
|
+ // 'user_id' => $response->user_id, // 从 $response 获取
|
|
|
3399
|
+ // 'access_token' => $response->access_token, // 从 $response 获取
|
|
|
3400
|
+ // 'refresh_token' => $response->refresh_token,
|
|
|
3401
|
+ // 'expires_in' => $response->expires_in,
|
|
|
3402
|
+ // 're_expires_in' => $response->re_expires_in,
|
|
|
3403
|
+ // // 确保 alipay_user_id 也捕获,如果需要的话 (看你的业务逻辑)
|
|
|
3404
|
+ // // 'alipay_user_id' => $response->alipay_user_id,
|
|
|
3405
|
+ // ];
|
|
3406
|
3406
|
$access_token = $response->access_token;
|
|
3407
|
3407
|
|
|
3408
|
3408
|
// --- 6. (可选) 获取用户信息 ---
|
|
|
@@ -3418,7 +3418,8 @@ class UserRepository extends BaseRepository
|
|
3418
|
3418
|
if (isset($userInfoResultObj->$userInfoResponseNode) && isset($userInfoResultObj->$userInfoResponseNode->code) && $userInfoResultObj->$userInfoResponseNode->code == '10000') {
|
|
3419
|
3419
|
// 成功获取用户信息
|
|
3420
|
3420
|
$userInfoResponse = $userInfoResultObj->$userInfoResponseNode;
|
|
3421
|
|
- $return_data['user_info'] = [
|
|
|
3421
|
+ Db::name('log')->insert(['data' => '支付宝用户信息获取成功: ' . json_encode($userInfoResponse, JSON_UNESCAPED_UNICODE)]);
|
|
|
3422
|
+ return [
|
|
3422
|
3423
|
'user_id' => $userInfoResponse->user_id,
|
|
3423
|
3424
|
'nick_name' => $userInfoResponse->nick_name ?? null,
|
|
3424
|
3425
|
'avatar' => $userInfoResponse->avatar ?? null,
|
|
|
@@ -3427,8 +3428,6 @@ class UserRepository extends BaseRepository
|
|
3427
|
3428
|
// 'province' => $userInfoResponse->province ?? null,
|
|
3428
|
3429
|
// 'city' => $userInfoResponse->city ?? null,
|
|
3429
|
3430
|
];
|
|
3430
|
|
- Db::name('log')->insert(['data' => '支付宝用户信息获取成功: ' . json_encode($userInfoResponse, JSON_UNESCAPED_UNICODE)]);
|
|
3431
|
|
- return $return_data;
|
|
3432
|
3431
|
} else {
|
|
3433
|
3432
|
// 获取用户信息失败
|
|
3434
|
3433
|
$errorMsg = 'Failed to get user info';
|
|
|
@@ -3444,13 +3443,12 @@ class UserRepository extends BaseRepository
|
|
3444
|
3443
|
$subMsg = $userInfoResultObj->error_response->sub_msg;
|
|
3445
|
3444
|
}
|
|
3446
|
3445
|
Db::name('log')->insert(['data' => '支付宝用户信息获取失败(Token已成功): ' . $errorMsg . ' | Raw UserInfo: ' . $userInfoResult]);
|
|
3447
|
|
- $return_data['user_info_error'] = $subMsg ?: $errorMsg;
|
|
3448
|
|
- return $return_data; // 仍然返回成功的 Token 和用户信息错误标记
|
|
|
3446
|
+ return []; // 仍然返回成功的 Token 和用户信息错误标记
|
|
3449
|
3447
|
}
|
|
3450
|
3448
|
} else {
|
|
3451
|
3449
|
// 不需要获取用户信息,直接返回 Token 数据
|
|
3452
|
3450
|
Db::name('log')->insert(['data' => '支付宝Token获取成功(无用户信息请求): ' . json_encode($response, JSON_UNESCAPED_UNICODE)]);
|
|
3453
|
|
- return $return_data;
|
|
|
3451
|
+ return [];
|
|
3454
|
3452
|
}
|
|
3455
|
3453
|
|
|
3456
|
3454
|
} else {
|
|
|
@@ -3468,12 +3466,12 @@ class UserRepository extends BaseRepository
|
|
3468
|
3466
|
$subMsg = $resultObj->$responseNode->sub_msg ?? '';
|
|
3469
|
3467
|
}
|
|
3470
|
3468
|
Db::name('log')->insert(['data' => '支付宝Token获取失败: ' . $errorMsg . ' | Raw Token: ' . $result]);
|
|
3471
|
|
- return ['code' => 500, 'msg' => $subMsg ?: $errorMsg];
|
|
|
3469
|
+ return [];
|
|
3472
|
3470
|
}
|
|
3473
|
3471
|
|
|
3474
|
3472
|
} catch (\Exception $e) { // 捕获 SDK 或其他异常
|
|
3475
|
3473
|
Db::name("log")->insert(['data' => 'Alipay Token/Info Error: '.$e->getFile() . ':' . $e->getLine() . '【' . $e->getMessage() . '】']); //日志记录
|
|
3476
|
|
- return ['code' => 500, 'msg' => 'System Error: ' . $e->getMessage()];
|
|
|
3474
|
+ return [];
|
|
3477
|
3475
|
}
|
|
3478
|
3476
|
}
|
|
3479
|
3477
|
|