Goods.php 3.5 KB

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