CustomBlocks.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\store\controller\home;
  4. use app\common\model\CustomBlockGoods as CustomBlockGoodsModel;
  5. use app\store\controller\Controller;
  6. use app\common\model\CustomBlocks as CustomBlocksModel;
  7. /**
  8. * 首页自定义模块
  9. * Class CustomBlocks
  10. * @package app\store\controller\home
  11. */
  12. class CustomBlocks extends Controller
  13. {
  14. /**
  15. * 列表
  16. * @return array
  17. * @throws \think\db\exception\DbException
  18. */
  19. public function list()
  20. {
  21. $model = new CustomBlocksModel;
  22. $list = $model->getList($this->request->param());
  23. return $this->renderSuccess(compact('list'));
  24. }
  25. /**
  26. * 获取所有记录
  27. * @return array
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\DbException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. */
  32. public function all()
  33. {
  34. $model = new CustomBlocksModel;
  35. $list = $model->getAll();
  36. return $this->renderSuccess(compact('list'));
  37. }
  38. /**
  39. * 获取详情记录
  40. * @param int $id
  41. * @return array
  42. */
  43. public function detail(int $id)
  44. {
  45. $detail = CustomBlocksModel::detail($id);
  46. return $this->renderSuccess(compact('detail'));
  47. }
  48. /**
  49. * 版块已选择的商品id集合
  50. * @param int $blockId
  51. * @return array
  52. */
  53. public function selectedGoodsIds(int $blockId)
  54. {
  55. $model = new CustomBlockGoodsModel;
  56. $goods_ids = $model->where('block_id', $blockId)->column('goods_id');
  57. return $this->renderSuccess(compact('goods_ids'));
  58. }
  59. /**
  60. * 添加
  61. * @return array
  62. */
  63. public function add()
  64. {
  65. // 新增记录
  66. $model = new CustomBlocksModel;
  67. if ($model->add($this->postForm())) {
  68. return $this->renderSuccess('添加成功');
  69. }
  70. return $this->renderError($model->getError() ?: '添加失败');
  71. }
  72. /**
  73. * 编辑
  74. * @param int $id
  75. * @return array
  76. */
  77. public function edit(int $id)
  78. {
  79. // 详情
  80. $model = CustomBlocksModel::detail($id);
  81. // 更新记录
  82. if ($model->edit($this->postForm())) {
  83. return $this->renderSuccess('更新成功');
  84. }
  85. return $this->renderError($model->getError() ?: '更新失败');
  86. }
  87. /**
  88. * 删除
  89. * @param int $id
  90. * @return array
  91. */
  92. public function delete(int $id)
  93. {
  94. // 详情
  95. $model = CustomBlocksModel::detail($id);
  96. if (!$model->delete()) {
  97. return $this->renderError($model->getError() ?: '删除失败');
  98. }
  99. return $this->renderSuccess('删除成功');
  100. }
  101. }