$app->id, 'app_name' => $app->name, 'recharge_user_id' => $rechargeResult['user_id'], 'withdraw_user_id' => $withdrawResult['user_id'], ]); return [ 'success' => true, 'message' => '开发者账户创建成功', 'data' => $results ]; } catch (\Exception $e) { DB::rollBack(); Log::error('开发者账户创建失败', [ 'app_id' => $app->id, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return [ 'success' => false, 'message' => '开发者账户创建失败: ' . $e->getMessage(), 'data' => null ]; } } /** * 创建充值专用账户 * * @param OpenApiApp $app 应用对象 * @return array */ private static function createRechargeAccount(OpenApiApp $app): array { $userId = self::getRechargeUserId($app->id); // 创建用户记录(如果不存在) $user = self::createUserIfNotExists($userId, "充值账户-{$app->name}", 'recharge'); // 创建钻石账户 $fundService = new FundService($userId, FUND_TYPE::FUND2->value); $account = $fundService->getAccount(); if (!$account) { // 如果账户不存在,初始化账户 $fundService->admin(FUND_TYPE::FUND2, 0, 1, '初始化开发者充值账户'); } return [ 'user_id' => $userId, 'username' => $user->username, 'fund_type' => FUND_TYPE::FUND2->value, 'currency_type' => FUND_CURRENCY_TYPE::ZUANSHI->value, 'purpose' => 'recharge' ]; } /** * 创建提取专用账户 * * @param OpenApiApp $app 应用对象 * @return array */ private static function createWithdrawAccount(OpenApiApp $app): array { $userId = self::getWithdrawUserId($app->id); // 创建用户记录(如果不存在) $user = self::createUserIfNotExists($userId, "提取账户-{$app->name}", 'withdraw'); // 创建钻石账户 $fundService = new FundService($userId, FUND_TYPE::FUND2->value); $account = $fundService->getAccount(); if (!$account) { // 如果账户不存在,初始化账户 $fundService->admin(FUND_TYPE::FUND2, 0, 1, '初始化开发者提取账户'); } return [ 'user_id' => $userId, 'username' => $user->username, 'fund_type' => FUND_TYPE::FUND2->value, 'currency_type' => FUND_CURRENCY_TYPE::ZUANSHI->value, 'purpose' => 'withdraw' ]; } /** * 创建用户记录(如果不存在) * * @param int $userId 用户ID * @param string $username 用户名 * @param string $type 账户类型 * @return User */ private static function createUserIfNotExists(int $userId, string $username, string $type): User { $user = User::find($userId); if (!$user) { $user = new User(); $user->id = $userId; $user->username = $username; $user->email = "{$type}_{$userId}@openapi.system"; $user->password = bcrypt('system_account_' . time()); $user->status = 1; // 激活状态 $user->created_at = now(); $user->updated_at = now(); $user->save(); } return $user; } /** * 获取开发者账户信息 * * @param OpenApiApp $app 应用对象 * @return array */ public static function getDeveloperAccountInfo(OpenApiApp $app): array { $rechargeUserId = self::getRechargeUserId($app->id); $withdrawUserId = self::getWithdrawUserId($app->id); // 获取充值账户信息 $rechargeFundService = new FundService($rechargeUserId, FUND_TYPE::FUND2->value); $rechargeAccount = $rechargeFundService->getAccount(); // 获取提取账户信息 $withdrawFundService = new FundService($withdrawUserId, FUND_TYPE::FUND2->value); $withdrawAccount = $withdrawFundService->getAccount(); return [ 'app_id' => $app->id, 'app_name' => $app->name, 'recharge_account' => [ 'user_id' => $rechargeUserId, 'balance' => $rechargeAccount ? $rechargeAccount->balance : 0, 'fund_type' => FUND_TYPE::FUND2->value, 'currency_type' => FUND_CURRENCY_TYPE::ZUANSHI->value, ], 'withdraw_account' => [ 'user_id' => $withdrawUserId, 'balance' => $withdrawAccount ? $withdrawAccount->balance : 0, 'fund_type' => FUND_TYPE::FUND2->value, 'currency_type' => FUND_CURRENCY_TYPE::ZUANSHI->value, ] ]; } /** * 检查开发者账户是否存在 * * @param OpenApiApp $app 应用对象 * @return array */ public static function checkDeveloperAccounts(OpenApiApp $app): array { $rechargeUserId = self::getRechargeUserId($app->id); $withdrawUserId = self::getWithdrawUserId($app->id); $rechargeUser = User::find($rechargeUserId); $withdrawUser = User::find($withdrawUserId); $rechargeFundService = new FundService($rechargeUserId, FUND_TYPE::FUND2->value); $withdrawFundService = new FundService($withdrawUserId, FUND_TYPE::FUND2->value); return [ 'recharge_account_exists' => $rechargeUser !== null && $rechargeFundService->getAccount() !== null, 'withdraw_account_exists' => $withdrawUser !== null && $withdrawFundService->getAccount() !== null, 'recharge_user_id' => $rechargeUserId, 'withdraw_user_id' => $withdrawUserId, ]; } }