Select.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace addons\Wechat\merchant\widgets\selector;
  3. use Yii;
  4. use yii\widgets\InputWidget;
  5. use common\helpers\StringHelper;
  6. use common\helpers\Html;
  7. use addons\Wechat\common\models\Attachment;
  8. use addons\Wechat\common\models\AttachmentNews;
  9. use addons\Wechat\common\enums\AttachmentTypeEnum;
  10. /**
  11. * Class Select
  12. * @package addons\Wechat\merchant\widgets\selector
  13. * @author jianyan74 <751393839@qq.com>
  14. */
  15. class Select extends InputWidget
  16. {
  17. public $type;
  18. public $block;
  19. /**
  20. * 盒子ID
  21. *
  22. * @var
  23. */
  24. protected $boxId;
  25. /**
  26. * @throws \yii\base\InvalidConfigException
  27. * @throws \Exception
  28. */
  29. public function init()
  30. {
  31. parent::init();
  32. $name = $this->hasModel() ? \yii\helpers\Html::getInputName($this->model, $this->attribute) : $this->name;
  33. $this->boxId = md5($name) . StringHelper::uuid('uniqid');
  34. $this->value = $this->hasModel() ? Html::getAttributeValue($this->model, $this->attribute) : $this->value;
  35. $this->name = $this->hasModel() ? Html::getInputName($this->model, $this->attribute) : $this->name;
  36. }
  37. /**
  38. * @return string
  39. */
  40. public function run()
  41. {
  42. if ($this->type != AttachmentTypeEnum::NEWS) {
  43. if (empty($this->value) || empty(($model = Attachment::findOne([
  44. 'media_id' => $this->value,
  45. 'merchant_id' => Yii::$app->services->merchant->getId()
  46. ])))) {
  47. $model = new Attachment();
  48. $model = $model->loadDefaultValues();
  49. }
  50. } else {
  51. $data = AttachmentNews::find()
  52. ->where(['sort' => 0, 'attachment_id' => $this->value])
  53. ->andWhere(['merchant_id' => Yii::$app->services->merchant->getId()])
  54. ->one();
  55. $model = new Attachment();
  56. if (!empty($data)) {
  57. $model->file_name = $data->title;
  58. $model->media_url = $data->thumb_url;
  59. }
  60. }
  61. return $this->render('select', [
  62. 'name' => $this->name,
  63. 'value' => $this->value,
  64. 'model' => $model,
  65. 'boxId' => $this->boxId,
  66. 'type' => $this->type,
  67. 'block' => $this->block,
  68. ]);
  69. }
  70. }
粤ICP备19079148号