load(); } $capsule = new Capsule; $capsule->addConnection([ 'driver' => 'mysql', 'host' => $_ENV['DB_HOST'] ?? '127.0.0.1', 'port' => $_ENV['DB_PORT'] ?? '3306', 'database' => $_ENV['DB_DATABASE'] ?? '', 'username' => $_ENV['DB_USERNAME'] ?? '', 'password' => $_ENV['DB_PASSWORD'] ?? '', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', ]); $capsule->setAsGlobal(); $capsule->bootEloquent(); // 1. 创建 users 表 if (!Capsule::schema()->hasTable('users')) { Capsule::schema()->create('users', function ($table) { $table->id(); $table->string('openid', 64)->nullable()->unique(); $table->string('mobile', 20)->nullable()->unique(); $table->string('nickname', 50)->nullable(); $table->string('avatar', 255)->nullable(); $table->tinyInteger('status')->default(1)->comment('1:正常 0:禁用'); $table->timestamps(); }); echo "Table 'users' created successfully.\n"; } // 2. 创建 orders 表 if (!Capsule::schema()->hasTable('orders')) { Capsule::schema()->create('orders', function ($table) { $table->id(); $table->string('order_no', 32)->unique(); $table->unsignedBigInteger('user_id')->index(); $table->string('category', 32); $table->string('service_type', 64); $table->string('brand', 64); $table->string('model', 128)->nullable(); $table->string('remark', 500)->nullable(); $table->tinyInteger('is_fast')->default(0); $table->decimal('total_price', 10, 2)->default(0.00); $table->string('status', 20)->default('wait_pay')->index(); $table->string('express_company', 32)->nullable(); $table->string('express_no', 64)->nullable(); $table->timestamp('pay_time')->nullable(); $table->timestamps(); }); echo "Table 'orders' created successfully.\n"; } // 3. 创建 order_logs 表 if (!Capsule::schema()->hasTable('order_logs')) { Capsule::schema()->create('order_logs', function ($table) { $table->id(); $table->unsignedBigInteger('order_id'); $table->string('action_type', 32); $table->string('title', 64); $table->string('description', 500)->nullable(); $table->string('operator_type', 20)->default('system'); $table->bigInteger('operator_id')->default(0); $table->timestamps(); $table->index(['order_id', 'created_at']); }); echo "Table 'order_logs' created successfully.\n"; } // 4. 创建 reports 表 if (!Capsule::schema()->hasTable('reports')) { Capsule::schema()->create('reports', function ($table) { $table->id(); $table->string('report_no', 32)->unique(); $table->unsignedBigInteger('order_id')->unique(); $table->string('conclusion', 20); $table->string('level', 20)->nullable(); $table->json('flaws_json')->nullable(); $table->json('images_json'); $table->bigInteger('inspector_id'); $table->string('verify_code', 32)->unique(); $table->timestamps(); }); echo "Table 'reports' created successfully.\n"; } if (!Capsule::schema()->hasTable('admin_users')) { Capsule::schema()->create('admin_users', function ($table) { $table->id(); $table->string('username', 50)->unique(); $table->string('password_hash', 255); $table->string('nickname', 50)->nullable(); $table->tinyInteger('is_super')->default(0); $table->tinyInteger('status')->default(1); $table->timestamps(); }); echo "Table 'admin_users' created successfully.\n"; } if (!Capsule::schema()->hasTable('roles')) { Capsule::schema()->create('roles', function ($table) { $table->id(); $table->string('name', 50); $table->string('code', 50)->unique(); $table->timestamps(); }); echo "Table 'roles' created successfully.\n"; } if (!Capsule::schema()->hasTable('permissions')) { Capsule::schema()->create('permissions', function ($table) { $table->id(); $table->string('name', 50); $table->string('code', 80)->unique(); $table->timestamps(); }); echo "Table 'permissions' created successfully.\n"; } if (!Capsule::schema()->hasTable('admin_roles')) { Capsule::schema()->create('admin_roles', function ($table) { $table->id(); $table->unsignedBigInteger('admin_id'); $table->unsignedBigInteger('role_id'); $table->timestamps(); $table->unique(['admin_id', 'role_id']); $table->index(['admin_id']); $table->index(['role_id']); }); echo "Table 'admin_roles' created successfully.\n"; } if (!Capsule::schema()->hasTable('role_permissions')) { Capsule::schema()->create('role_permissions', function ($table) { $table->id(); $table->unsignedBigInteger('role_id'); $table->unsignedBigInteger('permission_id'); $table->timestamps(); $table->unique(['role_id', 'permission_id']); $table->index(['role_id']); $table->index(['permission_id']); }); echo "Table 'role_permissions' created successfully.\n"; } if (!Capsule::schema()->hasTable('user_tokens')) { Capsule::schema()->create('user_tokens', function ($table) { $table->id(); $table->unsignedBigInteger('user_id'); $table->string('token_hash', 64)->unique(); $table->timestamp('expired_at')->nullable(); $table->timestamps(); $table->index(['user_id']); $table->index(['expired_at']); }); echo "Table 'user_tokens' created successfully.\n"; } if (!Capsule::schema()->hasTable('admin_tokens')) { Capsule::schema()->create('admin_tokens', function ($table) { $table->id(); $table->unsignedBigInteger('admin_id'); $table->string('token_hash', 64)->unique(); $table->timestamp('expired_at')->nullable(); $table->timestamps(); $table->index(['admin_id']); $table->index(['expired_at']); }); echo "Table 'admin_tokens' created successfully.\n"; } echo "All migrations completed.\n";