| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- namespace common\models\wechat;
- use common\enums\StatusEnum;
- use EasyWeChat\Kernel\Messages\Image;
- use EasyWeChat\Kernel\Messages\News;
- use EasyWeChat\Kernel\Messages\NewsItem;
- use EasyWeChat\Kernel\Messages\Text;
- use EasyWeChat\Kernel\Messages\Video;
- use EasyWeChat\Kernel\Messages\Voice;
- use Yii;
- /**
- * This is the model class for table "{{%rule_keyword}}".
- *
- *
- * @property int $id
- * @property int $mall_id
- * @property int $rule_id
- * @property string $keyword
- * @property int $status 状态【1启用 0禁用 -1删除】
- * @property int $type 1包含 2 精准匹配
- * @property int $created_at
- * @property int $updated_at
- */
- class RuleKeyword extends \common\models\base\BaseActiveRecord
- {
- const TYPE_INCLUDE = 1;//包含关键词
- const TYPE_MATCH = 2;//精确匹配
- const RULE_TYPE_ARTICLE = 'article';
- const RULE_TYPE_TEXT = 'text';
- const RULE_TYPE_IMAGE = 'image';
- const RULE_TYPE_VIDEO = 'video';
- const RULE_TYPE_VOICE = 'voice';
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%wechat_rule_keyword}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'rule_id', 'keyword'], 'required'],
- [['mall_id', 'rule_id', 'status', 'type', 'created_at', 'updated_at'], 'integer'],
- [['keyword'], 'string', 'max' => 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;
- }
- }
- }
|