BannerForm.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 BannerForm 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. public $md_id;
  25. /**
  26. * @return array
  27. * 验证
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['store_id', 'pic_url',], 'required'],
  33. [['store_id', 'type', 'md_id'], 'integer'],
  34. [['pic_url', 'page_url','open_type'], 'string'],
  35. [['title'], 'string', 'max' => 255],
  36. [['title'], 'default', 'value' => '暂无标题'],
  37. [['sort'], 'default', 'value' => 0],
  38. [['sort'], 'integer', 'min' => 0, 'max' => 999999]
  39. ];
  40. }
  41. /**
  42. * @return array
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'store_id' => '商城id',
  48. 'pic_url' => '图片',
  49. 'title' => '标题',
  50. 'page_url' => '页面路径',
  51. 'sort' => '排序',
  52. 'is_delete' => '是否删除:0=未删除,1=已删除',
  53. ];
  54. }
  55. /**
  56. * 获取列表
  57. * @return array
  58. */
  59. public function getList($store_id, $mch_id = 0)
  60. {
  61. $query = Banner::find()->andWhere(['is_delete' => 0, 'store_id' => $store_id, 'mch_id' => intval($mch_id), 'type' => Banner::TYPE_FOOD]);
  62. if ($this->md_id) {
  63. $query->andWhere(['md_id' => $this->md_id]);
  64. }
  65. $query->select('*')->orderBy('sort DESC');
  66. $list = pagination_make($query);
  67. return [
  68. 'code' => 0,
  69. 'msg' => 'success',
  70. 'data' => [
  71. 'data' => $list['list'],
  72. 'pageNo' => $list['pageNo'],
  73. 'totalCount' => $list['totalCount'],
  74. ],
  75. ];
  76. }
  77. public function save($params = [])
  78. {
  79. if ($this->validate()) {
  80. $banner = $this->banner;
  81. if ($banner->isNewRecord) {
  82. $banner->is_delete = 0;
  83. $banner->created_at = time();
  84. if($params['mch']) {
  85. $banner->mch_id = $params['mch_id'];
  86. }
  87. }
  88. $banner->md_id = $this->md_id;
  89. $banner->type = $this->type;
  90. $banner->attributes = $this->attributes;
  91. return $banner->saveBanner();
  92. } else {
  93. return $this->getErrorSummary(false)[0];
  94. }
  95. }
  96. }