GenerateFundCurrencyConfigJson.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. 'display_attributes' => $currency->display_attributes,
  52. 'identification' => $currency->identification,
  53. 'name' => $currency->name,
  54. 'icon' => $currency->icon,
  55. ];
  56. // 如果有额外数据,解析并添加
  57. if (!empty($currency->data1)) {
  58. $extraData = json_decode($currency->data1, true);
  59. if (is_array($extraData)) {
  60. $currencyData['extra_data'] = $extraData;
  61. }
  62. }
  63. $jsonData['currencies'][] = $currencyData;
  64. }
  65. // 处理账户种类数据
  66. foreach ($fundConfigs as $fundConfig) {
  67. // 直接使用数据库中存储的currency_id字段
  68. $fundConfigData = [
  69. 'id' => $fundConfig->id,
  70. 'display_attributes' => $fundConfig->display_attributes,
  71. 'name' => $fundConfig->name,
  72. 'currency_id' => $fundConfig->currency_id, // 关联的币种ID
  73. ];
  74. $jsonData['fund'][] = $fundConfigData;
  75. }
  76. return $jsonData;
  77. } catch (\Exception $e) {
  78. if (php_sapi_name() === 'cli') {
  79. echo "Error: Generate fund_currency.json failed: {$e->getMessage()}\n";
  80. }
  81. return false;
  82. }
  83. }
  84. /**
  85. * 执行命令
  86. *
  87. * @return int
  88. */
  89. public function handle()
  90. {
  91. $this->info('开始生成货币配置JSON文件...');
  92. try {
  93. $result = FundCurrencyJsonConfig::getData([], true);
  94. if ($result !== false) {
  95. $this->info('货币配置JSON文件生成成功');
  96. $this->info('共生成 ' . count($result['currencies']) . ' 条币种配置数据');
  97. $this->info('共生成 ' . count($result['fund']) . ' 条账户种类配置数据');
  98. return Command::SUCCESS;
  99. } else {
  100. $this->error('生成货币配置JSON文件失败');
  101. return Command::FAILURE;
  102. }
  103. } catch (\Exception $e) {
  104. $this->error('生成货币配置JSON文件失败: ' . $e->getMessage());
  105. return Command::FAILURE;
  106. }
  107. }
  108. }