Laravel后端实现30分钟预约结束后展示评分弹窗方案咨询
Laravel后端实现方案及Google Meet API集成建议
一、基于Laravel的时间判断与前端交互
你已经用Carbon::addMinutes计算触发时间,接下来可以通过两种方式和前端联动:
1. 前端定时轮询后端接口
后端先编写接口,接收预约ID并校验当前时间是否满足弹窗触发条件:
// routes/api.php Route::get('/appointments/{id}/show-rating', function (App\Models\Appointment $appointment) { if ($appointment->duration !== 30) { return response()->json(['show' => false]); } $triggerTime = Carbon::parse($appointment->start_time)->addMinutes(30); return response()->json(['show' => now()->gte($triggerTime)]); });
前端用setInterval每隔1分钟请求一次接口,返回show: true时渲染评分弹窗:
setInterval(() => { fetch(`/api/appointments/{{ $appointment->id }}/show-rating`) .then(res => res.json()) .then(data => { if (data.show) { // 执行展示评分弹窗的逻辑 clearInterval(this); // 满足条件后停止轮询 } }); }, 60000);
用户刷新页面后,重新加载的前端会再次发起轮询,不会错过触发时机。
2. 实时推送(Laravel Echo + WebSocket)
如果需要更精准的实时触发,可采用WebSocket推送方案:
- 先安装Laravel Echo及Pusher(或其他WebSocket服务)
- 在预约创建时,设置延迟任务,到触发时间向前端推送事件:
// Appointment模型的boot方法 public static function boot() { parent::boot(); static::created(function ($appointment) { if ($appointment->duration === 30) { $delay = Carbon::parse($appointment->start_time)->addMinutes(30)->diffInSeconds(now()); if ($delay > 0) { dispatch(new SendRatingTrigger($appointment))->delay($delay); } } }); } // 任务类SendRatingTrigger public function handle() { broadcast(new RatingTriggered($this->appointment))->toOthers(); }
前端监听对应事件,收到后直接展示弹窗:
Echo.channel('appointments') .listen('RatingTriggered', (e) => { if (e.appointment.id === {{ $appointment->id }}) { // 展示评分弹窗 } });
二、评分提交与后台仪表盘展示
- 前端弹窗收集评分后,提交至Laravel后端接口:
// routes/api.php Route::post('/appointments/{id}/ratings', function (App\Models\Appointment $appointment, Request $request) { $appointment->rating()->create([ 'score' => $request->score, 'comment' => $request->comment ?? '', ]); return response()->json(['success' => true]); });
- 后台仪表盘直接从数据库读取评分数据渲染,控制器示例:
// DashboardController.php public function index() { $ratings = App\Models\Rating::with('appointment')->latest()->get(); return view('dashboard', compact('ratings')); }
三、Google Meet API的集成可行性
Google Meet本身没有直接的“通话结束触发评分”API,但可通过Google Calendar API间接实现:
- 为Laravel应用授权Google Calendar API权限,获取用户日历事件访问权限
- 监听对应预约的Calendar事件状态变化(如从“进行中”变为“已结束”)
- 检测到事件结束时,触发评分弹窗逻辑(可结合前面的WebSocket/轮询方案)
如果是通过你的应用创建Google Meet会议,可记录会议的conferenceId,后续通过Calendar API查询实际结束时间,替代单纯基于预约开始时间的计算,更贴合真实通话时长。
内容的提问来源于stack exchange,提问作者amine dahmani
相关产品推荐
相关产品推荐

