2026_11_09_120000_create_agent_domain_pool.php 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Schema;
  6. /**
  7. * 代理域名池:总后台维护可用域名,查看哪些在使用、绑定到哪个代理。
  8. * - agent_domain_pool:按主机名 host 唯一;type 为 pc / h5 / 空(通用);status 1 启用、0 停用(停用后不能再绑定);
  9. * - 导入已有的代理绑定域名(agent_domains);
  10. * - 菜单 agentDomainPool(挂在「代理充值记录」同一父菜单下)和新增 / 修改 / 删除按钮。
  11. */
  12. return new class extends Migration {
  13. private const BUTTONS = [
  14. 'admin/agent/domain-pool/create' => '代理域名池-新增',
  15. 'admin/agent/domain-pool/update' => '代理域名池-修改',
  16. 'admin/agent/domain-pool/delete' => '代理域名池-删除',
  17. ];
  18. public function up(): void
  19. {
  20. if (!Schema::hasTable('agent_domain_pool')) {
  21. Schema::create('agent_domain_pool', function (Blueprint $t) {
  22. $t->id();
  23. $t->string('domain', 255)->comment('域名或完整地址,原样保存');
  24. $t->string('host', 255)->unique()->comment('主机名(小写),用于唯一和匹配绑定');
  25. $t->string('type', 16)->default('')->comment('pc / h5 / 空=通用');
  26. $t->unsignedTinyInteger('status')->default(1)->comment('1 启用 0 停用');
  27. $t->string('remark', 255)->default('');
  28. $t->unsignedBigInteger('created_by')->nullable();
  29. $t->timestamps();
  30. $t->index(['status', 'type']);
  31. });
  32. }
  33. if (Schema::hasTable('agent_domains')) {
  34. $now = date('Y-m-d H:i:s');
  35. foreach (DB::table('agent_domains')->orderBy('id')->get(['domain', 'host', 'type']) as $row) {
  36. if ($row->host === '' || DB::table('agent_domain_pool')->where('host', $row->host)->exists()) continue;
  37. DB::table('agent_domain_pool')->insert(['domain' => $row->domain, 'host' => $row->host, 'type' => (string) $row->type,
  38. 'status' => 1, 'remark' => '', 'created_at' => $now, 'updated_at' => $now]);
  39. }
  40. }
  41. $this->menus();
  42. }
  43. private function menus(): void
  44. {
  45. if (!Schema::hasTable('menus')) return;
  46. $now = date('Y-m-d H:i:s');
  47. $parentId = (int) DB::table('menus')->where('uri', 'agentRecharges')->where('type', 1)->value('parent_id');
  48. $menuId = DB::table('menus')->where('uri', 'agentDomainPool')->where('type', 1)->value('id');
  49. if (!$menuId) {
  50. $menuId = DB::table('menus')->insertGetId([
  51. 'parent_id' => $parentId, 'title' => '代理域名管理', 'icon' => null, 'uri' => 'agentDomainPool',
  52. 'permission_name' => 'agent.agentDomainPool', 'sort' => 25, 'status' => 1, 'type' => 1,
  53. 'created_at' => $now, 'updated_at' => $now,
  54. ]);
  55. }
  56. $sort = 0;
  57. foreach (self::BUTTONS as $uri => $title) {
  58. if (!DB::table('menus')->where('uri', $uri)->exists()) {
  59. DB::table('menus')->insert(['parent_id' => $menuId, 'title' => $title, 'icon' => null, 'uri' => $uri,
  60. 'permission_name' => str_replace('/', '.', $uri), 'sort' => $sort, 'status' => 1, 'type' => 2,
  61. 'created_at' => $now, 'updated_at' => $now]);
  62. }
  63. $sort++;
  64. }
  65. }
  66. public function down(): void
  67. {
  68. if (Schema::hasTable('menus')) {
  69. $ids = DB::table('menus')->whereIn('uri', array_merge(['agentDomainPool'], array_keys(self::BUTTONS)))->pluck('id');
  70. if (Schema::hasTable('role_menu')) DB::table('role_menu')->whereIn('menu_id', $ids)->delete();
  71. DB::table('menus')->whereIn('id', $ids)->delete();
  72. }
  73. Schema::dropIfExists('agent_domain_pool');
  74. }
  75. };