belongsTo(Keyword::class, 'keyword_id'); } //根据聊天内容匹配关键词 public static function getKeywordByContent($content){ //缓存关键词列表 $keywordList = cache('keywordList'); if (!$keywordList) { $keywordList = self::where('status', 1)->field(['id','language_code', 'name'])->order('weight','desc')->select(); cache('keywordList', $keywordList); } $list = []; $all_match = [];//完全匹配的关键词 $match = []; //模糊匹配的关键词 foreach ($keywordList as $keyword) { //判断content是否包含关键词 if (strpos($content, $keyword->name) !== false) { if ($keyword->name == $content) { $all_match[] = $keyword->id; } else { $match[] = $keyword->id; } $list[] = $keyword->id; } } if ($all_match) { return self::whereIn('id', $all_match)->increment('all_match_num', 1); } if ($match) { return self::whereIn('id', $match)->increment('match_num', 1); } return $list; } }