| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace App\Module\Game\Commands;
- use App\Module\Game\Services\ConsumeService;
- use Illuminate\Console\Command;
- /**
- * 测试消耗组功能的命令
- */
- class TestConsumeCommand extends Command
- {
- /**
- * 命令名称
- *
- * @var string
- */
- protected $signature = 'game:test-consume {user_id} {consume_group} {--check : 只检查不执行消耗}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试消耗组功能';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $userId = (int) $this->argument('user_id');
- $consumeGroup = $this->argument('consume_group');
- $checkOnly = $this->option('check');
- $this->info("测试消耗组功能");
- $this->info("用户ID: {$userId}");
- $this->info("消耗组: {$consumeGroup}");
- $this->info("模式: " . ($checkOnly ? '只检查' : '检查并执行'));
- if ($checkOnly) {
- // 只检查不执行
- $result = ConsumeService::checkConsume($userId, $consumeGroup);
- } else {
- // 检查并执行
- $result = ConsumeService::executeConsume($userId, $consumeGroup, 'test_command');
- }
- // 处理 Res 对象或数组结果
- $success = is_object($result) ? $result->success : $result['success'];
- $message = is_object($result) ? $result->message : $result['message'];
- $data = is_object($result) ? $result->data : $result;
- if ($success) {
- $this->info("结果: 成功");
- $this->info("消息: {$message}");
- } else {
- $this->error("结果: 失败");
- $this->error("消息: {$message}");
- }
- // 输出详细信息
- $this->line('');
- $this->line('详细信息:');
- if (is_object($result)) {
- // Res 对象
- if (!empty($result->data)) {
- foreach ($result->data as $key => $value) {
- if (is_array($value)) {
- $this->line("{$key}: " . json_encode($value, JSON_UNESCAPED_UNICODE));
- } else {
- $this->line("{$key}: {$value}");
- }
- }
- }
- } else {
- // 数组结果
- unset($data['success']);
- unset($data['message']);
- foreach ($data as $key => $value) {
- if (is_array($value)) {
- $this->line("{$key}: " . json_encode($value, JSON_UNESCAPED_UNICODE));
- } else {
- $this->line("{$key}: {$value}");
- }
- }
- }
- return 0;
- }
- }
|