SlideForm.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Banner;
  9. use yii\base\Model;
  10. use yii\data\Pagination;
  11. class SlideForm extends Model
  12. {
  13. /**
  14. * @var $banner Banner
  15. */
  16. public $banner;
  17. public $store_id;
  18. public $pic_url;
  19. public $title;
  20. public $page_url;
  21. public $sort;
  22. public $open_type;
  23. public $type;
  24. /**
  25. * @return array
  26. * 验证
  27. */
  28. public function rules()
  29. {
  30. return [
  31. [['store_id', 'pic_url',], 'required'],
  32. [['store_id', 'type'], 'integer'],
  33. [['pic_url', 'page_url','open_type'], 'string'],
  34. [['title'], 'string', 'max' => 255],
  35. [['title'], 'default', 'value' => '暂无标题'],
  36. [['sort'], 'default', 'value' => 0],
  37. [['sort'], 'integer', 'min' => 0, 'max' => 999999]
  38. ];
  39. }
  40. /**
  41. * @return array
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'store_id' => '商城id',
  47. 'pic_url' => '图片',
  48. 'title' => '标题',
  49. 'page_url' => '页面路径',
  50. 'sort' => '排序',
  51. 'is_delete' => '是否删除:0=未删除,1=已删除',
  52. ];
  53. }
  54. /**
  55. * 获取列表
  56. * @return array
  57. */
  58. public function getList($store_id)
  59. {
  60. $query = Banner::find()->andWhere(['is_delete' => 0, 'type' => $this->type]);
  61. if ($this->type != Banner::TYPE_SAAS) {
  62. $query->andWhere(['store_id' => $store_id]);
  63. }
  64. $query->select('*')->orderBy('sort ASC');
  65. $list = pagination_make($query);
  66. return [
  67. 'code' => 0,
  68. 'msg' => 'success',
  69. 'data' => [
  70. 'data' => $list['list'],
  71. 'pageNo' => $list['pageNo'],
  72. 'totalCount' => $list['totalCount'],
  73. ],
  74. ];
  75. }
  76. public function save()
  77. {
  78. if ($this->validate()) {
  79. $banner = $this->banner;
  80. if ($banner->isNewRecord) {
  81. $banner->is_delete = 0;
  82. $banner->created_at = time();
  83. }
  84. $banner->type = $this->type;
  85. $banner->attributes = $this->attributes;
  86. return $banner->saveBanner();
  87. } else {
  88. return [
  89. 'code' => 1,
  90. 'msg' => $this->getErrorSummary(false)[0]
  91. ];
  92. }
  93. }
  94. }