DeleteBannerImgForm.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace hd\models\homePageConfig;
  3. use bizHd\homePageConfig\classes\HomePageConfigClass;
  4. use hd\models\BaseForm;
  5. use Yii;
  6. /**
  7. * 删除单张轮播图 OSS
  8. */
  9. class DeleteBannerImgForm extends BaseForm
  10. {
  11. public $filePath;
  12. public function rules()
  13. {
  14. return [
  15. ['filePath', 'required', 'message' => '参数错误'],
  16. ['filePath', 'validateFilePath'],
  17. ];
  18. }
  19. public function attributeLabels()
  20. {
  21. return [
  22. 'filePath' => '图片路径',
  23. ];
  24. }
  25. public function validateFilePath($attribute)
  26. {
  27. $filePath = ltrim(strval($this->$attribute), '/');
  28. $this->$attribute = $filePath;
  29. if ($filePath === '') {
  30. $this->addError($attribute, '参数错误');
  31. return;
  32. }
  33. $controller = Yii::$app->controller;
  34. $mainId = intval($controller->mainId ?? 0);
  35. $shopId = intval($controller->shopId ?? 0);
  36. $prefix = HomePageConfigClass::BANNER_OSS_ROOT . '/' . $mainId . '/' . $shopId . '/';
  37. if (strpos($filePath, $prefix) !== 0) {
  38. $this->addError($attribute, '无权删除该图片');
  39. }
  40. }
  41. /**
  42. * @return string
  43. */
  44. public function getFilePath()
  45. {
  46. return ltrim(strval($this->filePath), '/');
  47. }
  48. }