| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace addons\Wechat\merchant\forms;
- use yii\helpers\Json;
- use common\helpers\ArrayHelper;
- use addons\Wechat\common\models\Rule;
- use addons\Wechat\common\enums\RuleModuleEnum;
- /**
- * Class RuleForm
- * @package merchant\modules\wechat\models
- * @author jianyan74 <751393839@qq.com>
- */
- class RuleForm extends Rule
- {
- public $keyword;
- public $text;
- public $image;
- public $news;
- public $video;
- public $voice;
- public $api_url;
- public $default;
- public $cache_time;
- public $description;
- /**
- * @return array
- */
- public function rules()
- {
- $rule = parent::rules();
- $rule[] = [['keyword'], 'required', 'message' => '关键字不能为空'];
- $rule[] = [['cache_time'], 'integer', 'min' => 0];
- $rule[] = [['api_url', 'description', 'api_url'], 'string', 'max' => 255];
- $rule[] = [['default'], 'string', 'max' => 50];
- $rule[] = [['text', 'image', 'news', 'video', 'voice'], 'string'];
- $rule[] = [['name'], 'verifyRequired'];
- return $rule;
- }
- /**
- * @return array
- */
- public function attributeLabels()
- {
- $labels = [
- 'api_url' => '接口地址',
- 'description' => '备注说明',
- 'default' => '默认回复文字',
- 'cache_time' => '缓存时间',
- 'text' => '内容',
- 'image' => '图片',
- 'video' => '视频',
- 'voice' => '音频',
- 'news' => '图文',
- ];
- return ArrayHelper::merge(parent::attributeLabels(), $labels);
- }
- public function verifyRequired($attribute)
- {
- if ($this->module == RuleModuleEnum::TEXT && !$this->text) {
- $this->addError($attribute, '请在下方填写内容');
- }
- if ($this->module == RuleModuleEnum::IMAGE && !$this->image) {
- $this->addError($attribute, '请在下方选择图片');
- }
- if ($this->module == RuleModuleEnum::VIDEO && !$this->video) {
- $this->addError($attribute, '请在下方选择视频');
- }
- if ($this->module == RuleModuleEnum::VOICE && !$this->voice) {
- $this->addError($attribute, '请在下方选择语音');
- }
- if ($this->module == RuleModuleEnum::NEWS && !$this->news) {
- $this->addError($attribute, '请在下方选择图文');
- }
- if ($this->module == RuleModuleEnum::USER_API && !$this->api_url) {
- $this->addError($attribute, '请在下方选择接口地址');
- }
- }
- public function afterFind()
- {
- if ($this->module == RuleModuleEnum::TEXT) {
- $this->text = $this->data;
- }
- if ($this->module == RuleModuleEnum::IMAGE) {
- $this->image = $this->data;
- }
- if ($this->module == RuleModuleEnum::VIDEO) {
- $this->video = $this->data;
- }
- if ($this->module == RuleModuleEnum::VOICE) {
- $this->voice = $this->data;
- }
- if ($this->module == RuleModuleEnum::NEWS) {
- $this->news = $this->data;
- }
- if ($this->module == RuleModuleEnum::USER_API) {
- $data = Json::decode($this->data);
- $this->api_url = $data['api_url'];
- $this->default = $data['default'];
- $this->cache_time = $data['cache_time'];
- $this->description = $data['description'];
- }
- parent::afterFind();
- }
- public function beforeSave($insert)
- {
- if ($this->module == RuleModuleEnum::TEXT) {
- $this->data = $this->text;
- }
- if ($this->module == RuleModuleEnum::IMAGE) {
- $this->data = $this->image;
- }
- if ($this->module == RuleModuleEnum::VIDEO) {
- $this->data = $this->video;
- }
- if ($this->module == RuleModuleEnum::VOICE) {
- $this->data = $this->voice;
- }
- if ($this->module == RuleModuleEnum::NEWS) {
- $this->data = $this->news;
- }
- if ($this->module == RuleModuleEnum::USER_API) {
- $data = [
- 'api_url' => $this->api_url,
- 'default' => $this->default,
- 'cache_time' => $this->cache_time,
- 'description' => $this->description,
- ];
- $this->data = Json::encode($data);
- }
- return parent::beforeSave($insert);
- }
- }
|