RuleForm.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace addons\Wechat\merchant\forms;
  3. use yii\helpers\Json;
  4. use common\helpers\ArrayHelper;
  5. use addons\Wechat\common\models\Rule;
  6. use addons\Wechat\common\enums\RuleModuleEnum;
  7. /**
  8. * Class RuleForm
  9. * @package merchant\modules\wechat\models
  10. * @author jianyan74 <751393839@qq.com>
  11. */
  12. class RuleForm extends Rule
  13. {
  14. public $keyword;
  15. public $text;
  16. public $image;
  17. public $news;
  18. public $video;
  19. public $voice;
  20. public $api_url;
  21. public $default;
  22. public $cache_time;
  23. public $description;
  24. /**
  25. * @return array
  26. */
  27. public function rules()
  28. {
  29. $rule = parent::rules();
  30. $rule[] = [['keyword'], 'required', 'message' => '关键字不能为空'];
  31. $rule[] = [['cache_time'], 'integer', 'min' => 0];
  32. $rule[] = [['api_url', 'description', 'api_url'], 'string', 'max' => 255];
  33. $rule[] = [['default'], 'string', 'max' => 50];
  34. $rule[] = [['text', 'image', 'news', 'video', 'voice'], 'string'];
  35. $rule[] = [['name'], 'verifyRequired'];
  36. return $rule;
  37. }
  38. /**
  39. * @return array
  40. */
  41. public function attributeLabels()
  42. {
  43. $labels = [
  44. 'api_url' => '接口地址',
  45. 'description' => '备注说明',
  46. 'default' => '默认回复文字',
  47. 'cache_time' => '缓存时间',
  48. 'text' => '内容',
  49. 'image' => '图片',
  50. 'video' => '视频',
  51. 'voice' => '音频',
  52. 'news' => '图文',
  53. ];
  54. return ArrayHelper::merge(parent::attributeLabels(), $labels);
  55. }
  56. public function verifyRequired($attribute)
  57. {
  58. if ($this->module == RuleModuleEnum::TEXT && !$this->text) {
  59. $this->addError($attribute, '请在下方填写内容');
  60. }
  61. if ($this->module == RuleModuleEnum::IMAGE && !$this->image) {
  62. $this->addError($attribute, '请在下方选择图片');
  63. }
  64. if ($this->module == RuleModuleEnum::VIDEO && !$this->video) {
  65. $this->addError($attribute, '请在下方选择视频');
  66. }
  67. if ($this->module == RuleModuleEnum::VOICE && !$this->voice) {
  68. $this->addError($attribute, '请在下方选择语音');
  69. }
  70. if ($this->module == RuleModuleEnum::NEWS && !$this->news) {
  71. $this->addError($attribute, '请在下方选择图文');
  72. }
  73. if ($this->module == RuleModuleEnum::USER_API && !$this->api_url) {
  74. $this->addError($attribute, '请在下方选择接口地址');
  75. }
  76. }
  77. public function afterFind()
  78. {
  79. if ($this->module == RuleModuleEnum::TEXT) {
  80. $this->text = $this->data;
  81. }
  82. if ($this->module == RuleModuleEnum::IMAGE) {
  83. $this->image = $this->data;
  84. }
  85. if ($this->module == RuleModuleEnum::VIDEO) {
  86. $this->video = $this->data;
  87. }
  88. if ($this->module == RuleModuleEnum::VOICE) {
  89. $this->voice = $this->data;
  90. }
  91. if ($this->module == RuleModuleEnum::NEWS) {
  92. $this->news = $this->data;
  93. }
  94. if ($this->module == RuleModuleEnum::USER_API) {
  95. $data = Json::decode($this->data);
  96. $this->api_url = $data['api_url'];
  97. $this->default = $data['default'];
  98. $this->cache_time = $data['cache_time'];
  99. $this->description = $data['description'];
  100. }
  101. parent::afterFind();
  102. }
  103. public function beforeSave($insert)
  104. {
  105. if ($this->module == RuleModuleEnum::TEXT) {
  106. $this->data = $this->text;
  107. }
  108. if ($this->module == RuleModuleEnum::IMAGE) {
  109. $this->data = $this->image;
  110. }
  111. if ($this->module == RuleModuleEnum::VIDEO) {
  112. $this->data = $this->video;
  113. }
  114. if ($this->module == RuleModuleEnum::VOICE) {
  115. $this->data = $this->voice;
  116. }
  117. if ($this->module == RuleModuleEnum::NEWS) {
  118. $this->data = $this->news;
  119. }
  120. if ($this->module == RuleModuleEnum::USER_API) {
  121. $data = [
  122. 'api_url' => $this->api_url,
  123. 'default' => $this->default,
  124. 'cache_time' => $this->cache_time,
  125. 'description' => $this->description,
  126. ];
  127. $this->data = Json::encode($data);
  128. }
  129. return parent::beforeSave($insert);
  130. }
  131. }
粤ICP备19079148号