CustomBlockGoods.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\store\controller\home;
  4. use app\store\controller\Controller;
  5. use app\common\model\CustomBlockGoods as CustomBlockGoodsModel;
  6. /**
  7. * 首页自定义模块商品
  8. * Class home
  9. * @package app\store\controller\home
  10. */
  11. class CustomBlockGoods extends Controller
  12. {
  13. /**
  14. * 列表
  15. * @return array
  16. * @throws \think\db\exception\DbException
  17. */
  18. public function list()
  19. {
  20. $model = new CustomBlockGoodsModel;
  21. $list = $model->getList($this->request->param());
  22. return $this->renderSuccess(compact('list'));
  23. }
  24. /**
  25. * 添加
  26. * @return array
  27. */
  28. public function add()
  29. {
  30. // 新增记录
  31. $model = new CustomBlockGoodsModel;
  32. if ($model->add($this->postForm())) {
  33. return $this->renderSuccess('添加成功');
  34. }
  35. return $this->renderError($model->getError() ?: '添加失败');
  36. }
  37. /**
  38. * 移除
  39. * @param int $id
  40. * @return array
  41. */
  42. public function delete(int $id = 0, $blockId = 0, $goodsId = 0)
  43. {
  44. // 详情
  45. if ($id) {
  46. $model = CustomBlockGoodsModel::detail($id);
  47. } elseif ($blockId && $goodsId) {
  48. $model = CustomBlockGoodsModel::getByGoodsIdBlockId($blockId, $goodsId);
  49. } else {
  50. return $this->renderError('参数无效');
  51. }
  52. if (!$model->delete()) {
  53. return $this->renderError($model->getError() ?: '删除失败');
  54. }
  55. return $this->renderSuccess('删除成功');
  56. }
  57. /**
  58. * 编辑
  59. * @param int $id
  60. * @return array
  61. */
  62. public function edit(int $id)
  63. {
  64. // 详情
  65. $model = CustomBlockGoodsModel::detail($id);
  66. // 更新记录
  67. if ($model->edit($this->postForm())) {
  68. return $this->renderSuccess('更新成功');
  69. }
  70. return $this->renderError($model->getError() ?: '更新失败');
  71. }
  72. }