Goods.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace app\store\controller\apps\sharp;
  3. use app\store\controller\Controller;
  4. use app\store\model\Goods as GoodsModel;
  5. use app\store\service\Goods as GoodsService;
  6. use app\store\model\sharp\Goods as SharpGoodsModel;
  7. /**
  8. * 秒杀商品管理
  9. * Class Goods
  10. * @package app\store\controller\apps\sharp
  11. */
  12. class Goods extends Controller
  13. {
  14. /**
  15. * 秒杀商品列表
  16. * @param string $search
  17. * @return mixed
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\ModelNotFoundException
  20. * @throws \think\exception\DbException
  21. */
  22. public function index($search = '')
  23. {
  24. $model = new SharpGoodsModel;
  25. $list = $model->getList($search);
  26. return $this->fetch('index', compact('list'));
  27. }
  28. /**
  29. * 添加秒杀商品
  30. * @param int $step
  31. * @param null $goods_id
  32. * @return array|bool|mixed
  33. * @throws \Exception
  34. */
  35. public function add($step = 1, $goods_id = null)
  36. {
  37. if ($step == 2) {
  38. return $this->step2($goods_id);
  39. }
  40. return $this->step1();
  41. }
  42. /**
  43. * 添加秒杀商品:步骤1
  44. * @return mixed
  45. */
  46. private function step1()
  47. {
  48. return $this->fetch('step1');
  49. }
  50. /**
  51. * 添加秒杀商品:步骤2
  52. * @param $goodsId
  53. * @return array|bool|mixed
  54. * @throws \Exception
  55. */
  56. private function step2($goodsId)
  57. {
  58. $model = new SharpGoodsModel;
  59. // 验证商品ID能否被添加
  60. if (!$model->validateGoodsId($goodsId)) {
  61. $this->renderError($model->getError());
  62. }
  63. // 商品信息
  64. $goods = GoodsModel::detail($goodsId);
  65. $specData = GoodsService::getSpecData($goods);
  66. // 填写商品信息页面
  67. if (!$this->request->isAjax()) {
  68. return $this->fetch('step2', compact('goods', 'specData'));
  69. }
  70. // 表单提交
  71. if ($model->add($goods, $this->postData('goods'))) {
  72. return $this->renderSuccess('添加成功', url('apps.sharp.goods/index'));
  73. }
  74. return $this->renderError($model->getError() ?: '添加失败');
  75. }
  76. /**
  77. * 编辑秒杀商品
  78. * @param $sharp_goods_id
  79. * @return array|bool|mixed
  80. * @throws \think\exception\DbException
  81. * @throws \Exception
  82. */
  83. public function edit($sharp_goods_id)
  84. {
  85. // 砍价活动详情
  86. $model = SharpGoodsModel::detail($sharp_goods_id, ['sku']);
  87. // 商品信息
  88. $goods = GoodsModel::detail($model['goods_id']);
  89. // 商品多规格信息
  90. $specData = $model->getSpecData($goods);
  91. if (!$this->request->isAjax()) {
  92. return $this->fetch('edit', compact('model', 'goods', 'specData'));
  93. }
  94. // 更新记录
  95. if ($model->edit($goods, $this->postData('goods'))) {
  96. return $this->renderSuccess('更新成功', url('apps.sharp.goods/index'));
  97. }
  98. return $this->renderError($model->getError() ?: '更新失败');
  99. }
  100. /**
  101. * 删除秒杀商品
  102. * @param $sharp_goods_id
  103. * @return array
  104. * @throws \think\exception\DbException
  105. */
  106. public function delete($sharp_goods_id)
  107. {
  108. // 秒杀商品详情
  109. $model = SharpGoodsModel::detail($sharp_goods_id);
  110. if (!$model->setDelete()) {
  111. return $this->renderError($model->getError() ?: '删除失败');
  112. }
  113. return $this->renderSuccess('删除成功');
  114. }
  115. }