Goods.php 3.7 KB

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