Goods.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace app\store\controller;
  3. use app\store\model\Goods as GoodsModel;
  4. use app\store\model\Category as CategoryModel;
  5. use app\store\service\Goods as GoodsService;
  6. /**
  7. * 商品管理控制器
  8. * Class Goods
  9. * @package app\store\controller
  10. */
  11. class Goods extends Controller
  12. {
  13. /**
  14. * 商品列表(出售中)
  15. * @return mixed
  16. * @throws \think\exception\DbException
  17. */
  18. public function index()
  19. {
  20. // 获取全部商品列表
  21. $model = new GoodsModel;
  22. $list = $model->getList(array_merge(['status' => -1], $this->request->param()));
  23. // 商品分类
  24. $catgory = CategoryModel::getCacheTree();
  25. return $this->fetch('index', compact('list', 'catgory'));
  26. }
  27. /**
  28. * 添加商品
  29. * @return array|mixed
  30. * @throws \think\exception\PDOException
  31. */
  32. public function add()
  33. {
  34. if (!$this->request->isAjax()) {
  35. return $this->fetch(
  36. 'add',
  37. array_merge(GoodsService::getEditData(null, 'add'), [])
  38. );
  39. }
  40. $model = new GoodsModel;
  41. if ($model->add($this->postData('goods'))) {
  42. return $this->renderSuccess('添加成功', url('goods/index'));
  43. }
  44. return $this->renderError($model->getError() ?: '添加失败');
  45. }
  46. /**
  47. * 一键复制
  48. * @param $goods_id
  49. * @return array|mixed
  50. * @throws \think\exception\PDOException
  51. */
  52. public function copy($goods_id)
  53. {
  54. // 商品详情
  55. $model = GoodsModel::detail($goods_id);
  56. if (!$this->request->isAjax()) {
  57. return $this->fetch(
  58. 'edit',
  59. array_merge(GoodsService::getEditData($model, 'copy'), compact('model'))
  60. );
  61. }
  62. $model = new GoodsModel;
  63. if ($model->add($this->postData('goods'))) {
  64. return $this->renderSuccess('添加成功', url('goods/index'));
  65. }
  66. return $this->renderError($model->getError() ?: '添加失败');
  67. }
  68. /**
  69. * 商品编辑
  70. * @param $goods_id
  71. * @return array|bool|mixed
  72. */
  73. public function edit($goods_id)
  74. {
  75. // 商品详情
  76. $model = GoodsModel::detail($goods_id);
  77. if (!$this->request->isAjax()) {
  78. return $this->fetch(
  79. 'edit',
  80. array_merge(GoodsService::getEditData($model), compact('model'))
  81. );
  82. }
  83. // 更新记录
  84. if ($model->edit($this->postData('goods'))) {
  85. return $this->renderSuccess('更新成功', url('goods/index'));
  86. }
  87. return $this->renderError($model->getError() ?: '更新失败');
  88. }
  89. /**
  90. * 修改商品状态
  91. * @param $goods_id
  92. * @param boolean $state
  93. * @return array
  94. */
  95. public function state($goods_id, $state)
  96. {
  97. // 商品详情
  98. $model = GoodsModel::detail($goods_id);
  99. if (!$model->setStatus($state)) {
  100. return $this->renderError('操作失败');
  101. }
  102. return $this->renderSuccess('操作成功');
  103. }
  104. /**
  105. * 删除商品
  106. * @param $goods_id
  107. * @return array
  108. */
  109. public function delete($goods_id)
  110. {
  111. // 商品详情
  112. $model = GoodsModel::detail($goods_id);
  113. if (!$model->setDelete()) {
  114. return $this->renderError($model->getError() ?: '删除失败');
  115. }
  116. return $this->renderSuccess('删除成功');
  117. }
  118. }