CouponSelect.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace addons\TinyShop\common\widgets\coupon;
  3. use Yii;
  4. use yii\helpers\Html;
  5. use common\helpers\ArrayHelper;
  6. use common\helpers\StringHelper;
  7. use unclead\multipleinput\MultipleInputColumn;
  8. /**
  9. * Class CouponSelect
  10. * @package addons\TinyShop\common\widgets\coupon
  11. * @author jianyan74 <751393839@qq.com>
  12. */
  13. class CouponSelect extends \yii\widgets\InputWidget
  14. {
  15. /**
  16. * 唯一ID
  17. *
  18. * @var string
  19. */
  20. public $box_id = 'couponSelect';
  21. /**
  22. * 判断可选的上限
  23. *
  24. * 0 为不限制
  25. *
  26. * @var int
  27. */
  28. public $max = 0;
  29. /**
  30. * 判断可选的最低
  31. *
  32. * 0 为不限制
  33. */
  34. public $min = 0;
  35. /**
  36. * @var array
  37. */
  38. public $columns = [];
  39. /**
  40. * 默认字段值
  41. *
  42. * @var array
  43. */
  44. protected $column = [
  45. 'label' => '数量',
  46. 'name' => 'number',
  47. 'value' => '',
  48. 'type' => MultipleInputColumn::TYPE_TEXT_INPUT,
  49. 'options' => [
  50. 'style' => ''
  51. ],
  52. // 字段规则验证
  53. 'rule' => [
  54. 'comparisonFieldMin' => '', // 比较字段(不能低于) 多个字段支持 , 分割
  55. 'comparisonFieldMax' => '', // 比较字段(不能超出) 多个字段支持 , 分割
  56. 'min' => 0, // 最小值
  57. 'max' => '', // 最大值
  58. ],
  59. ];
  60. /**
  61. * 多选开启
  62. *
  63. * @var bool
  64. */
  65. public $multiple = true;
  66. /**
  67. * @return string
  68. * @throws \Exception
  69. */
  70. public function run()
  71. {
  72. $value = $this->hasModel() ? Html::getAttributeValue($this->model, $this->attribute) : $this->value;
  73. $name = $this->hasModel() ? Html::getInputName($this->model, $this->attribute) : $this->name;
  74. empty($value) && $value = [];
  75. foreach ($this->columns as &$column) {
  76. $column = ArrayHelper::merge($this->column, $column);
  77. }
  78. return $this->render('coupon', [
  79. 'couponTypes' => $value,
  80. 'columns' => $this->columns,
  81. 'name' => $name,
  82. 'min' => $this->min,
  83. 'max' => $this->max,
  84. 'multiple' => $this->multiple,
  85. 'box_id' => !empty($this->box_id) ? $this->box_id : StringHelper::uuid('uniqid'),
  86. ]);
  87. }
  88. }
粤ICP备19079148号