| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- use Illuminate\Database\Migrations\Migration;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Schema;
- /**
- * 代理域名池:总后台维护可用域名,查看哪些在使用、绑定到哪个代理。
- * - agent_domain_pool:按主机名 host 唯一;type 为 pc / h5 / 空(通用);status 1 启用、0 停用(停用后不能再绑定);
- * - 导入已有的代理绑定域名(agent_domains);
- * - 菜单 agentDomainPool(挂在「代理充值记录」同一父菜单下)和新增 / 修改 / 删除按钮。
- */
- return new class extends Migration {
- private const BUTTONS = [
- 'admin/agent/domain-pool/create' => '代理域名池-新增',
- 'admin/agent/domain-pool/update' => '代理域名池-修改',
- 'admin/agent/domain-pool/delete' => '代理域名池-删除',
- ];
- public function up(): void
- {
- if (!Schema::hasTable('agent_domain_pool')) {
- Schema::create('agent_domain_pool', function (Blueprint $t) {
- $t->id();
- $t->string('domain', 255)->comment('域名或完整地址,原样保存');
- $t->string('host', 255)->unique()->comment('主机名(小写),用于唯一和匹配绑定');
- $t->string('type', 16)->default('')->comment('pc / h5 / 空=通用');
- $t->unsignedTinyInteger('status')->default(1)->comment('1 启用 0 停用');
- $t->string('remark', 255)->default('');
- $t->unsignedBigInteger('created_by')->nullable();
- $t->timestamps();
- $t->index(['status', 'type']);
- });
- }
- if (Schema::hasTable('agent_domains')) {
- $now = date('Y-m-d H:i:s');
- foreach (DB::table('agent_domains')->orderBy('id')->get(['domain', 'host', 'type']) as $row) {
- if ($row->host === '' || DB::table('agent_domain_pool')->where('host', $row->host)->exists()) continue;
- DB::table('agent_domain_pool')->insert(['domain' => $row->domain, 'host' => $row->host, 'type' => (string) $row->type,
- 'status' => 1, 'remark' => '', 'created_at' => $now, 'updated_at' => $now]);
- }
- }
- $this->menus();
- }
- private function menus(): void
- {
- if (!Schema::hasTable('menus')) return;
- $now = date('Y-m-d H:i:s');
- $parentId = (int) DB::table('menus')->where('uri', 'agentRecharges')->where('type', 1)->value('parent_id');
- $menuId = DB::table('menus')->where('uri', 'agentDomainPool')->where('type', 1)->value('id');
- if (!$menuId) {
- $menuId = DB::table('menus')->insertGetId([
- 'parent_id' => $parentId, 'title' => '代理域名管理', 'icon' => null, 'uri' => 'agentDomainPool',
- 'permission_name' => 'agent.agentDomainPool', 'sort' => 25, 'status' => 1, 'type' => 1,
- 'created_at' => $now, 'updated_at' => $now,
- ]);
- }
- $sort = 0;
- foreach (self::BUTTONS as $uri => $title) {
- if (!DB::table('menus')->where('uri', $uri)->exists()) {
- DB::table('menus')->insert(['parent_id' => $menuId, 'title' => $title, 'icon' => null, 'uri' => $uri,
- 'permission_name' => str_replace('/', '.', $uri), 'sort' => $sort, 'status' => 1, 'type' => 2,
- 'created_at' => $now, 'updated_at' => $now]);
- }
- $sort++;
- }
- }
- public function down(): void
- {
- if (Schema::hasTable('menus')) {
- $ids = DB::table('menus')->whereIn('uri', array_merge(['agentDomainPool'], array_keys(self::BUTTONS)))->pluck('id');
- if (Schema::hasTable('role_menu')) DB::table('role_menu')->whereIn('menu_id', $ids)->delete();
- DB::table('menus')->whereIn('id', $ids)->delete();
- }
- Schema::dropIfExists('agent_domain_pool');
- }
- };
|