GenerateFundCurrencyConfigJson.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Module\Fund\Commands;
  3. use App\Module\Fund\Models\FundConfigModel;
  4. use App\Module\Fund\Models\FundCurrencyModel;
  5. use App\Module\Game\DCache\FundCurrencyJsonConfig;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\DB;
  8. /**
  9. * 生成货币配置表JSON数据命令
  10. *
  11. * 该命令用于从数据库中的货币配置表生成JSON数据文件,供客户端使用。
  12. * 生成的JSON文件包含货币的基本信息,如ID、名称、标识、图标等。
  13. * 该命令通常在货币配置数据更新后运行,以确保客户端获取最新的配置数据。
  14. */
  15. class GenerateFundCurrencyConfigJson extends Command
  16. {
  17. /**
  18. * 命令名称
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'fund:generate-currency-json';
  23. /**
  24. * 命令描述
  25. *
  26. * @var string
  27. */
  28. protected $description = '生成货币配置JSON文件';
  29. /**
  30. * 生成货币配置JSON数据
  31. *
  32. * @return array|bool 生成的数据或失败标志
  33. */
  34. public static function generateJson()
  35. {
  36. try {
  37. // 获取所有货币配置
  38. $currencies = FundCurrencyModel::orderBy('id')->get();
  39. // 获取所有账户种类配置
  40. $fundConfigs = FundConfigModel::orderBy('id')->get();
  41. // 准备JSON数据
  42. $jsonData = [
  43. 'generated_ts' => time(),
  44. 'currencies' => [],
  45. 'fund' => []
  46. ];
  47. // 处理币种数据
  48. foreach ($currencies as $currency) {
  49. $currencyData = [
  50. 'id' => $currency->id,
  51. 'identification' => $currency->identification,
  52. 'name' => $currency->name,
  53. 'icon' => $currency->icon,
  54. ];
  55. // 如果有额外数据,解析并添加
  56. if (!empty($currency->data1)) {
  57. $extraData = json_decode($currency->data1, true);
  58. if (is_array($extraData)) {
  59. $currencyData['extra_data'] = $extraData;
  60. }
  61. }
  62. $jsonData['currencies'][] = $currencyData;
  63. }
  64. // 处理账户种类数据
  65. foreach ($fundConfigs as $fundConfig) {
  66. // 直接使用数据库中存储的currency_id字段
  67. $fundConfigData = [
  68. 'id' => $fundConfig->id,
  69. 'name' => $fundConfig->name,
  70. 'currency_id' => $fundConfig->currency_id, // 关联的币种ID
  71. ];
  72. $jsonData['fund'][] = $fundConfigData;
  73. }
  74. return $jsonData;
  75. } catch (\Exception $e) {
  76. if (php_sapi_name() === 'cli') {
  77. echo "Error: Generate fund_currency.json failed: {$e->getMessage()}\n";
  78. }
  79. return false;
  80. }
  81. }
  82. /**
  83. * 执行命令
  84. *
  85. * @return int
  86. */
  87. public function handle()
  88. {
  89. $this->info('开始生成货币配置JSON文件...');
  90. try {
  91. // 直接调用静态方法生成JSON,而不通过缓存类
  92. $result = FundCurrencyJsonConfig::getData([], true);
  93. if ($result !== false) {
  94. $this->info('货币配置JSON文件生成成功');
  95. $this->info('共生成 ' . count($result['currencies']) . ' 条币种配置数据');
  96. $this->info('共生成 ' . count($result['fund']) . ' 条账户种类配置数据');
  97. return Command::SUCCESS;
  98. } else {
  99. $this->error('生成货币配置JSON文件失败');
  100. return Command::FAILURE;
  101. }
  102. } catch (\Exception $e) {
  103. $this->error('生成货币配置JSON文件失败: ' . $e->getMessage());
  104. return Command::FAILURE;
  105. }
  106. }
  107. }