45], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => 'Mall ID', 'rule_id' => 'Rule ID', 'keyword' => 'Keyword', 'status' => '状态【1启用 0禁用 -1删除】', 'type' => '1包含 2 精准匹配', 'created_at' => 'Created At', 'updated_at' => 'Updated At', ]; } /** * 保存数据 * @Author lun * @DateTime 2021-09-17 14:16:24 * @copyright: Copyright (c) 2020 广东七件事集团 * @param array $params * @return object|boolean */ public static function setData($mall_id, $ruleId, array $params){ try{ // 删除原有数据 self::updateAll(['status' => StatusEnum::DELETE], ['rule_id' => $ruleId]); $include = [self::TYPE_INCLUDE . '&' => explode(',', $params['include_keywords'])]; $match = [self::TYPE_MATCH . '&' => explode(',', $params['match_keywords'])]; $keyword_list = array_merge_recursive($include, $match); $time = time(); foreach ($keyword_list as $key => $item) { foreach ($item as $value) { if ($value) { $data[] = [ 'mall_id' => $mall_id, 'rule_id' => $ruleId, 'keyword' => $value, 'type' => trim($key, '&'), 'status' => StatusEnum::ENABLED, 'created_at' => $time, 'updated_at' => $time, ]; } } } if (!empty($data)) { $key = ['mall_id', 'rule_id', 'keyword', 'type', 'status', 'created_at', 'updated_at']; $res = Yii::$app->db->createCommand()->batchInsert(self::tableName(), $key, $data)->execute(); if (!$res) throw new \Exception('关键字保存失败'); } return true; }catch(\Exception $e){ self::$static_error = $e->getMessage(); return false; } } /** * 文字回复 * @Author: lun * @DateTime: 2021/12/8 0008 17:29 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $content * @param int $mall_id * @return bool|Image|News|Text|Video|Voice */ public static function match($content,$mall_id=0) { $keyword = self::find()->where([ 'or', ['and', '{{type}} = :typeMatch', '{{keyword}} = :keyword'], // 直接匹配关键字 ['and', '{{type}} = :typeInclude', 'INSTR(:keyword, {{keyword}}) > 0'], // 包含关键字 ])->addParams([ ':keyword' => $content, ':typeMatch' => RuleKeyword::TYPE_MATCH, ':typeInclude' => RuleKeyword::TYPE_INCLUDE, ]) ->andWhere(['status' => StatusEnum::ENABLED]) ->andWhere(['=','mall_id',$mall_id]) ->orderBy('id desc') ->one(); if (!$keyword) { return false; } /* @var $model ReplyRule */ $model = ReplyRule::find()->where(['id' => $keyword->rule_id])->one(); switch ($model->reply_type) { // 文字回复 case self::RULE_TYPE_TEXT : return new Text($model->content); break; // 图文回复 case self::RULE_TYPE_ARTICLE: $article_list = MaterialArticle::find()->where(['article_id' => $model->content, 'status' => StatusEnum::ENABLED, 'mall_id' => $mall_id])->all(); $newsList = []; /** * @var MaterialArticle $article */ foreach ($article_list as $article) { $newsList[] = new NewsItem([ 'title' => $article->title, 'description' => $article->article_desc, 'url' => $article->url, 'image' => $article->thumb_url, ]); } return new News($newsList); break; // 图片回复 case self::RULE_TYPE_IMAGE : return new Image($model->content); break; // 视频回复 case self::RULE_TYPE_VIDEO : $material = Material::findOne(['status' => StatusEnum::ENABLED, 'media_id' => $model->content]); if ($material) { return new Video($model->content, [ 'title' => $material->name, 'description' => $material->material_desc, ]); } return new Text('未找到您想要内容!'); break; // 语音回复 case self::RULE_TYPE_VOICE : $material = Material::findOne(['status' => StatusEnum::ENABLED, 'media_id' => $model->content]); if ($material) { return new Voice($model->content); } return new Text('未找到您想要内容!'); break; default : return false; break; } } public static function sendMessage($model){ switch ($model->reply_type) { // 文字回复 case self::RULE_TYPE_TEXT : return new Text($model->content); break; // 图文回复 case self::RULE_TYPE_ARTICLE: $article_list = MaterialArticle::find()->where(['media_id' => $model->content, 'status' => StatusEnum::ENABLED, 'mall_id' => \Yii::$app->mallId])->all(); $newsList = []; /** * @var MaterialArticle $article */ foreach ($article_list as $article) { $newsList[] = new NewsItem([ 'title' => $article->title, 'description' => $article->article_desc, 'url' => $article->url, 'image' => $article->thumb_url, ]); } return new News($newsList); break; // 图片回复 case self::RULE_TYPE_IMAGE : return new Image($model->content); break; // 视频回复 case self::RULE_TYPE_VIDEO : $material = Material::findOne(['status' => StatusEnum::ENABLED, 'media_id' => $model->content]); if ($material) { return new Video($model->content, [ 'title' => $material->name, 'description' => $material->material_desc, ]); } return new Text('未找到您想要内容!'); break; // 语音回复 case self::RULE_TYPE_VOICE : $material = Material::findOne(['status' => StatusEnum::ENABLED, 'media_id' => $model->content]); if ($material) { return new Voice($model->content); } return new Text('未找到您想要内容!'); break; default : return false; break; } } }