| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- namespace App\Module\GameItems\Commands;
- use Illuminate\Console\Command;
- use App\Module\GameItems\Models\Item;
- use Illuminate\Support\Facades\File;
- class GenerateItemsJsonCommand extends Command
- {
- /**
- * 命令名称和签名
- *
- * @var string
- */
- protected $signature = 'gameitems:generate-json';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = 'Generate items.json from Item table';
- /**
- * 执行命令
- */
- public function handle()
- {
- // 查询Item表中的数据
- $items = Item::query()
- ->select([
- 'id',
- 'name',
- 'description',
- 'sell_price',
- 'display_attributes'
- ])
- ->get()
- ->map(function ($item) {
- return [
- 'id' => $item->id,
- 'name' => $item->name,
- 'description' => $item->description,
- 'sell_price' => $item->sell_price,
- 'display_attributes' => $item->display_attributes
- ];
- })
- ->toArray();
- // 确保public/json目录存在
- $directory = public_path('json');
- if (!File::exists($directory)) {
- File::makeDirectory($directory, 0755, true);
- }
- // 准备完整数据,包含生成时间
- $data = [
- 'generated_at' => now()->toDateTimeString(),
- 'items' => $items
- ];
- // 写入JSON文件
- $json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
- File::put($directory . '/items.json', $json);
- $this->info('Successfully generated items.json with timestamp');
- }
- }
|