SpecsController.php 1.9 KB

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