使用OpenAi写一个测试后台
为实现完整的PHP系统后端以及后台管理功能,我们可以使用Laravel框架来构建。这是一个常见的PHP框架,支持MVC架构、路由管理、数据库迁移等功能,可以简化开发流程。
下面是详细的系统开发步骤,包括数据库迁移、模型、控制器和视图的代码示例。
一. 环境搭建
- 安装Laravel 首先,确保你已经安装了Composer和PHP,然后在命令行中运行以下命令来创建一个新的Laravel项目:
composer create-project --prefer-dist laravel/laravel BuyerManagementSystem
- 配置数据库 在.env文件中配置数据库连接信息,例如:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=buyer_management DB_USERNAME=root DB_PASSWORD=your_password
二. 数据库迁移和模型
- 创建迁移文件 我们需要为每个数据表创建迁移文件。可以使用Laravel的artisan命令生成迁移:
php artisan make:migration create_buyers_table --create=buyers php artisan make:migration create_team_members_table --create=team_members
- 编辑迁移文件在database/migrations目录下编辑刚刚创建的迁移文件,为buyers表和team_members表添加字段。2023_xx_xx_create_buyers_table.php:
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateBuyersTable extends Migration { public function up() { Schema::create('buyers', function (Blueprint $table) { $table->id(); $table->string('name'); $table->enum('gender', ['男', '女']); $table->text('avatar')->nullable(); $table->boolean('is_amazon_member'); $table->string('channel')->nullable(); $table->string('social_id')->unique(); $table->string('refund_account')->nullable(); $table->text('amazon_profile_link'); $table->string('profile_name')->nullable(); $table->text('profile_avatar')->nullable(); $table->string('review_product_name')->nullable(); $table->string('site')->nullable(); $table->date('store_order_date')->nullable(); $table->string('review_order_number')->nullable(); $table->string('order_status')->nullable(); $table->decimal('refund_amount', 10, 2)->nullable(); $table->text('refund_screenshot')->nullable(); $table->string('refund_status')->nullable(); $table->date('refund_date')->nullable(); $table->boolean('is_review_posted')->default(false); $table->date('review_post_date')->nullable(); $table->text('review_link')->nullable(); $table->text('review_screenshot')->nullable(); $table->string('review_type')->nullable(); $table->dateTime('review_time_in_china')->nullable(); $table->string('review_status')->nullable(); $table->decimal('commission_amount', 10, 2)->nullable(); $table->date('commission_payment_date')->nullable(); $table->string('buyer_account_level')->nullable(); $table->string('referrer_name')->nullable(); $table->string('referrer_contact')->nullable(); $table->foreignId('team_member_id')->constrained('team_members'); $table->timestamps(); }); }public function down() { Schema::dropIfExists('buyers'); } }
2023_xx_xx_create_team_members_table.php:
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateTeamMembersTable extends Migration { public function up() { Schema::create('team_members', function (Blueprint $table) { $table->id(); $table->string('username')->unique(); $table->string('password'); $table->enum('role', ['admin', 'team_member']); $table->timestamps(); }); }public function down() { Schema::dropIfExists('team_members'); } }
- 运行迁移 完成迁移文件的编辑后,运行迁移以创建数据库表:
php artisan migrate
三. 创建模型
Laravel模型用于与数据库表交互,每个模型对应一张数据表。可以使用以下命令生成模型:
php artisan make:model Buyer
php artisan make:model TeamMember
- Buyer模型app/Models/Buyer.php:
namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Buyer extends Model { use HasFactory; protected $fillable = [ 'name', 'gender', 'avatar', 'is_amazon_member', 'channel', 'social_id', 'refund_account', 'amazon_profile_link', 'profile_name', 'profile_avatar', 'review_product_name', 'site', 'store_order_date', 'review_order_number', 'order_status', 'refund_amount', 'refund_screenshot', 'refund_status', 'refund_date', 'is_review_posted', 'review_post_date', 'review_link', 'review_screenshot', 'review_type', 'review_time_in_china', 'review_status', 'commission_amount', 'commission_payment_date', 'buyer_account_level', 'referrer_name', 'referrer_contact', 'team_member_id' ]; public function teamMember() { return $this->belongsTo(TeamMember::class); } }
- TeamMember模型app/Models/TeamMember.php:
namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class TeamMember extends Authenticatable { use HasFactory, Notifiable; protected $fillable = [ 'username', 'password', 'role', ]; protected $hidden = [ 'password', 'remember_token', ]; public function buyers() { return $this->hasMany(Buyer::class); } }
四. 创建控制器
- Buyer控制器生成Buyer控制器:
php artisan make:controller BuyerController
app/Http/Controllers/BuyerController.php:
namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Buyer; class BuyerController extends Controller { public function index() { $buyers = auth()->user()->role === 'admin' ? Buyer::all() : Buyer::where('team_member_id', auth()->id())->get(); return view('buyers.index', compact('buyers')); } public function create() { return view('buyers.create'); } public function store(Request $request) { $request->validate([ 'social_id' => 'required|unique:buyers', 'amazon_profile_link' => 'required|unique:buyers', ]); Buyer::create($request->all() + ['team_member_id' => auth()->id()]); return redirect('/buyers')->with('success', '买家信息已保存'); } public function edit($id) { $buyer = Buyer::findOrFail($id); return view('buyers.edit', compact('buyer')); } public function update(Request $request, $id) { $buyer = Buyer::findOrFail($id); $buyer->update($request->all()); return redirect('/buyers')->with('success', '买家信息已更新'); } public function destroy($id) { $buyer = Buyer::findOrFail($id); $buyer->delete(); return redirect('/buyers')->with('success', '买家信息已删除'); } }
- TeamMember控制器生成TeamMember控制器:
php artisan make:controller TeamMemberController
app/Http/Controllers/TeamMemberController.php:
namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\TeamMember; class TeamMemberController extends Controller { public function index() { $teamMembers = TeamMember::all(); return view('team_members.index', compact('teamMembers')); } public function create() { return view('team_members.create'); } public function store(Request $request) { $request->validate([ 'username' => 'required|unique:team_members', 'password' => 'required|min:8', ]); TeamMember::create([ 'username' => $request->username, 'password' => bcrypt($request->password), 'role' => $request->role, ]); return redirect('/team-members')->with('success', '团队成员已添加'); } public function edit($id) { $teamMember = TeamMember::findOrFail($id); return view('team_members.edit', compact('teamMember')); } public function update(Request $request, $id) { $teamMember = TeamMember::findOrFail($id); $teamMember->update([ 'username' => $request->username, 'password' => $request->password ? bcrypt($request->password) : $teamMember->password, 'role' => $request->role, ]); return redirect('/team-members')->with('success', '团队成员信息已更新'); } public function destroy($id) { $teamMember = TeamMember::findOrFail($id); $teamMember->delete(); return redirect('/team-members')->with('success', '团队成员已删除'); } }
五. 路由配置
在routes/web.php中配置路由:
use App\Http\Controllers\BuyerController;
use App\Http\Controllers\TeamMemberController;Route::middleware('auth')->group(function () {
Route::resource('buyers', BuyerController::class);
Route::resource('team-members', TeamMemberController::class)->middleware('admin');
});Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
Route::post('/login', [AuthController::class, 'login']);
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
六. 中间件
- auth中间件
Laravel默认提供auth中间件用于认证,可以确保只有登录的用户才能访问受保护的路由。
- admin中间件
创建一个admin中间件,确保只有管理员角色的用户才能访问某些路由。
php artisan make:middleware AdminMiddleware
app/Http/Middleware/AdminMiddleware.php:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class AdminMiddleware
{
public function handle(Request $request, Closure $next)
{
if (auth()->user() && auth()->user()->role === 'admin') {
return $next($request);
}
return redirect('/')->withErrors('无权限访问该页面');
}
}
然后在app/Http/Kernel.php中注册中间件:
protected $routeMiddleware = [
// ...
'admin' => \App\Http\Middleware\AdminMiddleware::class,
];
七. 前端视图
使用Blade模板引擎创建视图文件:
- 买家管理视图resources/views/buyers/index.blade.php:
<!-- 省略HTML头部代码 --> <body> <h1>买家列表</h1> <a href="{{ route('buyers.create') }}">添加新买家</a><table> <thead> <tr> <th>名称</th> <th>性别</th> <th>社媒ID</th> <th>Amazon Profile</th> <th>操作</th> </tr> </thead> <tbody> @foreach ($buyers as $buyer) <tr> <td>{{ $buyer->name }}</td> <td>{{ $buyer->gender }}</td> <td>{{ $buyer->social_id }}</td> <td><a href="{{ $buyer->amazon_profile_link }}" target="_blank">Profile Link</a></td> <td> <a href="{{ route('buyers.edit', $buyer->id) }}">编辑</a> <form action="{{ route('buyers.destroy', $buyer->id) }}" method="POST" style="display:inline-block;"> @csrf @method('DELETE') <button type="submit">删除</button> </form> </td> </tr> @endforeach </tbody> </table> </body> <!-- 省略HTML尾部代码 -->
- 团队成员管理视图resources/views/team_members/index.blade.php:
<!-- 省略HTML头部代码 --> <body> <h1>团队成员列表</h1> <a href="{{ route('team-members.create') }}">添加新成员</a><table> <thead> <tr> <th>用户名</th> <th>角色</th> <th>操作</th> </tr> </thead> <tbody> @foreach ($teamMembers as $member) <tr> <td>{{ $member->username }}</td> <td>{{ $member->role }}</td> <td> <a href="{{ route('team-members.edit', $member->id) }}">编辑</a> <form action="{{ route('team-members.destroy', $member->id) }}" method="POST" style="display:inline-block;"> @csrf @method('DELETE') <button type="submit">删除</button> </form> </td> </tr> @endforeach </tbody> </table> </body> <!-- 省略HTML尾部代码 -->
八. 认证系统
为了让用户可以登录和退出,可以使用Laravel自带的认证系统或自行实现。这里简单实现一个手动认证系统。
- AuthController生成AuthController:
php artisan make:controller AuthController
app/Http/Controllers/AuthController.php:
namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; class AuthController extends Controller { public function showLoginForm() { return view('auth.login'); } public function login(Request $request) { $credentials = $request->only('username', 'password'); if (Auth::attempt($credentials)) { return redirect()->intended('/'); } return back()->withErrors(['msg' => '登录失败,用户名或密码错误']); } public function logout() { Auth::logout(); return redirect('/login'); } }
- 登录视图resources/views/auth/login.blade.php:
<!-- 省略HTML头部代码 --> <body> <h1>登录</h1> <form method="POST" action="{{ route('login') }}"> @csrf <div> <label>用户名:</label> <input type="text" name="username" required> </div> <div> <label>密码:</label> <input type="password" name="password" required> </div> <button type="submit">登录</button> </form> </body> <!-- 省略HTML尾部代码 -->
九. 运行和测试
- 运行开发服务器 使用以下命令启动Laravel的内置开发服务器:
php artisan serve
- 测试功能
- 访问/login页面进行登录。
- 登录后可以管理买家信息和团队成员。
- 检查权限控制和查重功能是否正常工作。
十. 部署
在本地开发测试完成后,可以将代码部署到生产环境中。可以选择使用LAMP(Linux, Apache, MySQL, PHP)栈或其他Web服务器环境来部署。
温馨提示:
本文最后更新于:2024-08-16 ,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。