request->get('card_no', ''); $since = $this->request->get('since', 0); if (empty($cardNo)) { return json(['code' => -1, 'data' => null, 'msg' => '缺少号码牌']); } $query = MessageModel::where('card_no', $cardNo) ->order('created_at', 'asc'); if ($since > 0) { $query->where('id', '>', intval($since)); } $messages = $query->select()->toArray(); $list = array_map(function ($m) { return [ 'id' => $m['id'], 'cardNo' => $m['card_no'], 'senderType' => $m['sender_type'], 'content' => $m['content'], 'time' => date('Y-m-d H:i:s', strtotime($m['created_at'])), 'staffId' => $m['staff_id'] ?? null, ]; }, $messages); return json(['code' => 0, 'data' => $list, 'msg' => 'ok']); } public function send() { $cardNo = $this->request->post('cardNo', ''); $senderType = $this->request->post('senderType', 'customer'); $content = $this->request->post('content', ''); $staffId = $this->request->post('staffId', null); if (empty($cardNo) || empty($content)) { return json(['code' => -1, 'data' => null, 'msg' => '参数不完整']); } if (!in_array($senderType, ['customer', 'staff', 'system'])) { return json(['code' => -1, 'data' => null, 'msg' => '发送者类型无效']); } if (mb_strlen($content) > 500) { return json(['code' => -1, 'data' => null, 'msg' => '消息过长,最多500字']); } // 存储原始文本,Vue 模板 {{ }} 自动转义防 XSS $msg = MessageModel::create([ 'card_no' => $cardNo, 'sender_type' => $senderType, 'staff_id' => $staffId, 'content' => $content, ]); return json(['code' => 0, 'data' => ['id' => $msg->id], 'msg' => 'ok']); } }