SpecsController.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace addons\Store\client\controllers;
  3. use backend\controllers\MallController;
  4. use backend\modules\mall\services\SpecsService;
  5. use Yii;
  6. use yii\helpers\Json;
  7. /**
  8. * 自定义规格控制器
  9. * SpecsController class
  10. */
  11. class SpecsController extends BaseController
  12. {
  13. /**
  14. * @var SpecsService
  15. */
  16. protected $service;
  17. public function init()
  18. {
  19. parent::init();
  20. $this->service = new SpecsService(['mall_id' => $this->mall_id]);
  21. }
  22. /**
  23. * 首页
  24. *
  25. * @return void
  26. */
  27. public function actionIndex()
  28. {
  29. if ($this->request->isAjax) {
  30. return $this->success(
  31. 'ok',
  32. $this->service->getPageList($this->request->get(), $this->store_id)
  33. );
  34. }
  35. return $this->render($this->action->id);
  36. }
  37. /**
  38. * 添加规格
  39. *
  40. * @return void
  41. */
  42. public function actionAdd()
  43. {
  44. if ($this->request->isAjax) {
  45. $id = $this->request->post('id', null);
  46. $params = $this->request->post();
  47. if (Yii::$app->services->validate->validate($params, [
  48. [['name'], 'string'],
  49. [['values', 'name'], 'required']
  50. ])) {
  51. $res = empty($id)
  52. ? $this->service->add($params['name'], $params['values'], $this->store_id)
  53. : $this->service->edit($id, $params['name'], $params['values'], $this->store_id);
  54. return $res
  55. ? $this->success('ok')
  56. : $this->error($this->service->error);
  57. }
  58. }
  59. }
  60. /**
  61. * 删除规格
  62. *
  63. * @return void
  64. */
  65. public function actionDel()
  66. {
  67. if ($this->request->isAjax) {
  68. $id = $this->request->get('id', null);
  69. return $this->service->del($id)
  70. ? $this->success('ok')
  71. : $this->error($this->service->error);
  72. }
  73. }
  74. }