CateJsTreeController.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace addons\RfDemo\merchant\controllers;
  3. use Yii;
  4. use common\helpers\ResultHelper;
  5. use common\traits\MerchantCurd;
  6. use addons\RfDemo\common\models\Cate;
  7. /**
  8. * Class CateJsTreeController
  9. * @package addons\RfDemo\merchant\controllers
  10. * @author jianyan74 <751393839@qq.com>
  11. */
  12. class CateJsTreeController extends BaseController
  13. {
  14. use MerchantCurd;
  15. /**
  16. * @var Cate
  17. */
  18. public $modelClass = Cate::class;
  19. /**
  20. * Lists all Tree models.
  21. * @return mixed
  22. */
  23. public function actionIndex()
  24. {
  25. return $this->render('index', [
  26. 'data' => Yii::$app->rfDemoService->cate->findAll()
  27. ]);
  28. }
  29. /**
  30. * 编辑/创建
  31. *
  32. * @return mixed
  33. */
  34. public function actionEdit()
  35. {
  36. $id = Yii::$app->request->get('id', null);
  37. $model = $this->findModel($id);
  38. $model->pid = Yii::$app->request->get('pid', null) ?? $model->pid; // 父id
  39. if ($model->load(Yii::$app->request->post())) {
  40. if (!$model->save()) {
  41. return ResultHelper::json(422, $this->getError($model));
  42. }
  43. return ResultHelper::json(200, '修改成功', $model);
  44. }
  45. $map = ['0' => '顶级'];
  46. if ($model->pid && $parent = Yii::$app->rfDemoService->cate->findById($model->pid)) {
  47. $map = [$parent['id'] => $parent['title']];
  48. }
  49. return $this->renderAjax($this->action->id, [
  50. 'model' => $model,
  51. 'map' => $map,
  52. ]);
  53. }
  54. /**
  55. * 移动
  56. *
  57. * @param $id
  58. * @param int $pid
  59. */
  60. public function actionMove($id, $pid = 0)
  61. {
  62. $model = $this->findModel($id);
  63. $model->pid = $pid;
  64. $model->save();
  65. }
  66. /**
  67. * 删除
  68. *
  69. * @param $id
  70. * @return mixed
  71. * @throws \Throwable
  72. * @throws \yii\db\StaleObjectException
  73. */
  74. public function actionDelete($id)
  75. {
  76. if ($this->findModel($id)->delete()) {
  77. return ResultHelper::json(200, '删除成功');
  78. }
  79. return ResultHelper::json(422, '删除失败');
  80. }
  81. }
粤ICP备19079148号