RuleKeyword.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace common\models\wechat;
  3. use common\enums\StatusEnum;
  4. use EasyWeChat\Kernel\Messages\Image;
  5. use EasyWeChat\Kernel\Messages\News;
  6. use EasyWeChat\Kernel\Messages\NewsItem;
  7. use EasyWeChat\Kernel\Messages\Text;
  8. use EasyWeChat\Kernel\Messages\Video;
  9. use EasyWeChat\Kernel\Messages\Voice;
  10. use Yii;
  11. /**
  12. * This is the model class for table "{{%rule_keyword}}".
  13. *
  14. *
  15. * @property int $id
  16. * @property int $mall_id
  17. * @property int $rule_id
  18. * @property string $keyword
  19. * @property int $status 状态【1启用 0禁用 -1删除】
  20. * @property int $type 1包含 2 精准匹配
  21. * @property int $created_at
  22. * @property int $updated_at
  23. */
  24. class RuleKeyword extends \common\models\base\BaseActiveRecord
  25. {
  26. const TYPE_INCLUDE = 1;//包含关键词
  27. const TYPE_MATCH = 2;//精确匹配
  28. const RULE_TYPE_ARTICLE = 'article';
  29. const RULE_TYPE_TEXT = 'text';
  30. const RULE_TYPE_IMAGE = 'image';
  31. const RULE_TYPE_VIDEO = 'video';
  32. const RULE_TYPE_VOICE = 'voice';
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public static function tableName()
  37. {
  38. return '{{%wechat_rule_keyword}}';
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function rules()
  44. {
  45. return [
  46. [['mall_id', 'rule_id', 'keyword'], 'required'],
  47. [['mall_id', 'rule_id', 'status', 'type', 'created_at', 'updated_at'], 'integer'],
  48. [['keyword'], 'string', 'max' => 45],
  49. ];
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function attributeLabels()
  55. {
  56. return [
  57. 'id' => 'ID',
  58. 'mall_id' => 'Mall ID',
  59. 'rule_id' => 'Rule ID',
  60. 'keyword' => 'Keyword',
  61. 'status' => '状态【1启用 0禁用 -1删除】',
  62. 'type' => '1包含 2 精准匹配',
  63. 'created_at' => 'Created At',
  64. 'updated_at' => 'Updated At',
  65. ];
  66. }
  67. /**
  68. * 保存数据
  69. * @Author lun
  70. * @DateTime 2021-09-17 14:16:24
  71. * @copyright: Copyright (c) 2020 广东七件事集团
  72. * @param array $params
  73. * @return object|boolean
  74. */
  75. public static function setData($mall_id, $ruleId, array $params){
  76. try{
  77. // 删除原有数据
  78. self::updateAll(['status' => StatusEnum::DELETE], ['rule_id' => $ruleId]);
  79. $include = [self::TYPE_INCLUDE . '&' => explode(',', $params['include_keywords'])];
  80. $match = [self::TYPE_MATCH . '&' => explode(',', $params['match_keywords'])];
  81. $keyword_list = array_merge_recursive($include, $match);
  82. $time = time();
  83. foreach ($keyword_list as $key => $item) {
  84. foreach ($item as $value) {
  85. if ($value) {
  86. $data[] = [
  87. 'mall_id' => $mall_id,
  88. 'rule_id' => $ruleId,
  89. 'keyword' => $value,
  90. 'type' => trim($key, '&'),
  91. 'status' => StatusEnum::ENABLED,
  92. 'created_at' => $time,
  93. 'updated_at' => $time,
  94. ];
  95. }
  96. }
  97. }
  98. if (!empty($data)) {
  99. $key = ['mall_id', 'rule_id', 'keyword', 'type', 'status', 'created_at', 'updated_at'];
  100. $res = Yii::$app->db->createCommand()->batchInsert(self::tableName(), $key, $data)->execute();
  101. if (!$res) throw new \Exception('关键字保存失败');
  102. }
  103. return true;
  104. }catch(\Exception $e){
  105. self::$static_error = $e->getMessage();
  106. return false;
  107. }
  108. }
  109. /**
  110. * 文字回复
  111. * @Author: lun
  112. * @DateTime: 2021/12/8 0008 17:29
  113. * @Copyright: copyright (c) 2021 广东七件事集团
  114. * @param $content
  115. * @param int $mall_id
  116. * @return bool|Image|News|Text|Video|Voice
  117. */
  118. public static function match($content,$mall_id=0)
  119. {
  120. $keyword = self::find()->where([
  121. 'or',
  122. ['and', '{{type}} = :typeMatch', '{{keyword}} = :keyword'], // 直接匹配关键字
  123. ['and', '{{type}} = :typeInclude', 'INSTR(:keyword, {{keyword}}) > 0'], // 包含关键字
  124. ])->addParams([
  125. ':keyword' => $content,
  126. ':typeMatch' => RuleKeyword::TYPE_MATCH,
  127. ':typeInclude' => RuleKeyword::TYPE_INCLUDE,
  128. ])
  129. ->andWhere(['status' => StatusEnum::ENABLED])
  130. ->andWhere(['=','mall_id',$mall_id])
  131. ->orderBy('id desc')
  132. ->one();
  133. if (!$keyword) {
  134. return false;
  135. }
  136. /* @var $model ReplyRule */
  137. $model = ReplyRule::find()->where(['id' => $keyword->rule_id])->one();
  138. switch ($model->reply_type) {
  139. // 文字回复
  140. case self::RULE_TYPE_TEXT :
  141. return new Text($model->content);
  142. break;
  143. // 图文回复
  144. case self::RULE_TYPE_ARTICLE:
  145. $article_list = MaterialArticle::find()->where(['article_id' => $model->content, 'status' => StatusEnum::ENABLED, 'mall_id' => $mall_id])->all();
  146. $newsList = [];
  147. /**
  148. * @var MaterialArticle $article
  149. */
  150. foreach ($article_list as $article) {
  151. $newsList[] = new NewsItem([
  152. 'title' => $article->title,
  153. 'description' => $article->article_desc,
  154. 'url' => $article->url,
  155. 'image' => $article->thumb_url,
  156. ]);
  157. }
  158. return new News($newsList);
  159. break;
  160. // 图片回复
  161. case self::RULE_TYPE_IMAGE :
  162. return new Image($model->content);
  163. break;
  164. // 视频回复
  165. case self::RULE_TYPE_VIDEO :
  166. $material = Material::findOne(['status' => StatusEnum::ENABLED, 'media_id' => $model->content]);
  167. if ($material) {
  168. return new Video($model->content, [
  169. 'title' => $material->name,
  170. 'description' => $material->material_desc,
  171. ]);
  172. }
  173. return new Text('未找到您想要内容!');
  174. break;
  175. // 语音回复
  176. case self::RULE_TYPE_VOICE :
  177. $material = Material::findOne(['status' => StatusEnum::ENABLED, 'media_id' => $model->content]);
  178. if ($material) {
  179. return new Voice($model->content);
  180. }
  181. return new Text('未找到您想要内容!');
  182. break;
  183. default :
  184. return false;
  185. break;
  186. }
  187. }
  188. public static function sendMessage($model){
  189. switch ($model->reply_type) {
  190. // 文字回复
  191. case self::RULE_TYPE_TEXT :
  192. return new Text($model->content);
  193. break;
  194. // 图文回复
  195. case self::RULE_TYPE_ARTICLE:
  196. $article_list = MaterialArticle::find()->where(['media_id' => $model->content, 'status' => StatusEnum::ENABLED, 'mall_id' => \Yii::$app->mallId])->all();
  197. $newsList = [];
  198. /**
  199. * @var MaterialArticle $article
  200. */
  201. foreach ($article_list as $article) {
  202. $newsList[] = new NewsItem([
  203. 'title' => $article->title,
  204. 'description' => $article->article_desc,
  205. 'url' => $article->url,
  206. 'image' => $article->thumb_url,
  207. ]);
  208. }
  209. return new News($newsList);
  210. break;
  211. // 图片回复
  212. case self::RULE_TYPE_IMAGE :
  213. return new Image($model->content);
  214. break;
  215. // 视频回复
  216. case self::RULE_TYPE_VIDEO :
  217. $material = Material::findOne(['status' => StatusEnum::ENABLED, 'media_id' => $model->content]);
  218. if ($material) {
  219. return new Video($model->content, [
  220. 'title' => $material->name,
  221. 'description' => $material->material_desc,
  222. ]);
  223. }
  224. return new Text('未找到您想要内容!');
  225. break;
  226. // 语音回复
  227. case self::RULE_TYPE_VOICE :
  228. $material = Material::findOne(['status' => StatusEnum::ENABLED, 'media_id' => $model->content]);
  229. if ($material) {
  230. return new Voice($model->content);
  231. }
  232. return new Text('未找到您想要内容!');
  233. break;
  234. default :
  235. return false;
  236. break;
  237. }
  238. }
  239. }