SendForm.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace addons\Wechat\merchant\forms;
  3. use Yii;
  4. use common\helpers\ArrayHelper;
  5. use addons\Wechat\common\models\MassRecord;
  6. use common\enums\StatusEnum;
  7. use addons\Wechat\common\enums\RuleModuleEnum;
  8. /**
  9. * Class SendForm
  10. * @package merchant\modules\wechat\models
  11. * @author jianyan74 <751393839@qq.com>
  12. */
  13. class SendForm extends MassRecord
  14. {
  15. public $text;
  16. public $image;
  17. public $news;
  18. public $video;
  19. public $voice;
  20. /**
  21. * 群发消息
  22. *
  23. * @var array
  24. */
  25. protected $sendMethod = [
  26. 'text' => 'sendText',
  27. 'news' => 'sendNews',
  28. 'voice' => 'sendVoice',
  29. 'image' => 'sendImage',
  30. 'video' => 'sendVideo',
  31. 'card' => 'sendCard',
  32. ];
  33. /**
  34. * @return array
  35. */
  36. public function rules()
  37. {
  38. $rules = parent::rules();
  39. $rules[] = ['send_type', 'integer'];
  40. $rules[] = [['text', 'image', 'news', 'video', 'voice'], 'string'];
  41. $rules[] = [['tag_id'], 'verifyRequired'];
  42. return $rules;
  43. }
  44. /**
  45. * @return array
  46. */
  47. public function attributeLabels()
  48. {
  49. return ArrayHelper::merge(parent::attributeLabels(), [
  50. 'send_type' => '发送类型'
  51. ]);
  52. }
  53. public function verifyRequired($attribute)
  54. {
  55. if ($this->module == RuleModuleEnum::TEXT && !$this->text) {
  56. $this->addError($attribute, '请填写内容');
  57. }
  58. if ($this->module == RuleModuleEnum::IMAGE && !$this->image) {
  59. $this->addError($attribute, '请选择图片');
  60. }
  61. if ($this->module == RuleModuleEnum::VIDEO && !$this->video) {
  62. $this->addError($attribute, '请选择视频');
  63. }
  64. if ($this->module == RuleModuleEnum::VOICE && !$this->voice) {
  65. $this->addError($attribute, '请选择语音');
  66. }
  67. if ($this->module == RuleModuleEnum::NEWS && !$this->news) {
  68. $this->addError($attribute, '请选择图文');
  69. }
  70. }
  71. public function afterFind()
  72. {
  73. if ($this->module == RuleModuleEnum::TEXT) {
  74. $this->text = $this->data;
  75. }
  76. if ($this->module == RuleModuleEnum::IMAGE) {
  77. $this->image = $this->data;
  78. }
  79. if ($this->module == RuleModuleEnum::VIDEO) {
  80. $this->video = $this->data;
  81. }
  82. if ($this->module == RuleModuleEnum::VOICE) {
  83. $this->voice = $this->data;
  84. }
  85. if ($this->module == RuleModuleEnum::NEWS) {
  86. $this->news = $this->data;
  87. }
  88. parent::afterFind();
  89. }
  90. /**
  91. * @param bool $insert
  92. * @return bool
  93. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  94. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  95. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  96. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  97. * @throws \Psr\SimpleCache\InvalidArgumentException
  98. * @throws \yii\web\UnprocessableEntityHttpException
  99. */
  100. public function beforeSave($insert)
  101. {
  102. if ($this->module == RuleModuleEnum::TEXT) {
  103. $this->data = $this->text;
  104. }
  105. if ($this->module == RuleModuleEnum::IMAGE) {
  106. $this->data = $this->image;
  107. }
  108. if ($this->module == RuleModuleEnum::VIDEO) {
  109. $this->data = $this->video;
  110. }
  111. if ($this->module == RuleModuleEnum::VOICE) {
  112. $this->data = $this->voice;
  113. }
  114. if ($this->module == RuleModuleEnum::NEWS) {
  115. $this->data = $this->news;
  116. }
  117. $this->tag_name = '全部粉丝';
  118. if ($this->tag_id > 0) {
  119. $tag = Yii::$app->wechatService->fansTags->findById($this->tag_id);
  120. $this->tag_name = $tag['name'];
  121. $this->fans_num = $tag['count'];
  122. }
  123. return parent::beforeSave($insert);
  124. }
  125. /**
  126. * @param bool $insert
  127. * @param array $changedAttributes
  128. * @throws \Psr\SimpleCache\InvalidArgumentException
  129. */
  130. public function afterSave($insert, $changedAttributes)
  131. {
  132. // 群发消息
  133. if ($this->send_type == StatusEnum::ENABLED && $this->send_status != StatusEnum::ENABLED) {
  134. Yii::$app->wechatService->message->send($this);
  135. }
  136. parent::afterSave($insert, $changedAttributes);
  137. }
  138. }
粤ICP备19079148号