BannerForm.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\food\models\form;
  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. /**
  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, 'store_id' => $store_id, 'type' => Banner::TYPE_FOOD])->select('*')->orderBy('sort ASC');
  61. $list = pagination_make($query);
  62. return [
  63. 'code' => 0,
  64. 'msg' => 'success',
  65. 'data' => [
  66. 'data' => $list['list'],
  67. 'pageNo' => $list['pageNo'],
  68. 'totalCount' => $list['totalCount'],
  69. ],
  70. ];
  71. }
  72. public function save()
  73. {
  74. if ($this->validate()) {
  75. $banner = $this->banner;
  76. if ($banner->isNewRecord) {
  77. $banner->is_delete = 0;
  78. $banner->created_at = time();
  79. }
  80. $banner->type = $this->type;
  81. $banner->attributes = $this->attributes;
  82. return $banner->saveBanner();
  83. } else {
  84. return $this->getErrorSummary(false)[0];
  85. }
  86. }
  87. }