Goods.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\controller;
  13. use think\response\Json;
  14. use app\store\model\Goods as GoodsModel;
  15. use app\common\exception\BaseException;
  16. /**
  17. * 商品管理控制器
  18. * Class Goods
  19. * @package app\store\controller
  20. */
  21. class Goods extends Controller
  22. {
  23. /**
  24. * 商品列表
  25. * @return Json
  26. * @throws \think\db\exception\DbException
  27. */
  28. public function list(): Json
  29. {
  30. // 获取列表记录
  31. $model = new GoodsModel;
  32. $list = $model->getList($this->request->param());
  33. return $this->renderSuccess(compact('list'));
  34. }
  35. /**
  36. * 根据商品ID集获取列表记录
  37. * @param array $goodsIds
  38. * @return Json
  39. */
  40. public function listByIds(array $goodsIds): Json
  41. {
  42. // 获取列表记录
  43. $model = new GoodsModel;
  44. $list = $model->getListByIds($goodsIds);
  45. return $this->renderSuccess(compact('list'));
  46. }
  47. /**
  48. * 商品详情
  49. * @param int $goodsId
  50. * @return Json
  51. * @throws BaseException
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. * @throws \cores\exception\BaseException
  56. */
  57. public function detail(int $goodsId): Json
  58. {
  59. // 获取商品详情
  60. $model = new GoodsModel;
  61. $goodsInfo = $model->getDetail($goodsId);
  62. return $this->renderSuccess(compact('goodsInfo'));
  63. }
  64. /**
  65. * 添加商品
  66. * @return Json
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\DbException
  69. * @throws \think\db\exception\ModelNotFoundException
  70. * @throws \cores\exception\BaseException
  71. */
  72. public function add(): Json
  73. {
  74. $model = new GoodsModel;
  75. if ($model->add($this->postForm())) {
  76. return $this->renderSuccess('添加成功');
  77. }
  78. return $this->renderError($model->getError() ?: '添加失败');
  79. }
  80. /**
  81. * 编辑商品
  82. * @param int $goodsId
  83. * @return Json
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. * @throws \cores\exception\BaseException
  88. */
  89. public function edit(int $goodsId): Json
  90. {
  91. // 商品详情
  92. $model = GoodsModel::detail($goodsId);
  93. // 更新记录
  94. if ($model->edit($this->postForm())) {
  95. return $this->renderSuccess('更新成功');
  96. }
  97. return $this->renderError($model->getError() ?: '更新失败');
  98. }
  99. /**
  100. * 修改商品状态(上下架)
  101. * @param array $goodsIds 商品id集
  102. * @param bool $state 为true表示上架
  103. * @return Json
  104. */
  105. public function state(array $goodsIds, bool $state): Json
  106. {
  107. $model = new GoodsModel;
  108. if (!$model->setStatus($goodsIds, $state)) {
  109. return $this->renderError($model->getError() ?: '操作失败');
  110. }
  111. return $this->renderSuccess('操作成功');
  112. }
  113. /**
  114. * 删除商品
  115. * @param array $goodsIds
  116. * @return Json
  117. */
  118. public function delete(array $goodsIds): Json
  119. {
  120. $model = new GoodsModel;
  121. if (!$model->setDelete($goodsIds)) {
  122. return $this->renderError($model->getError() ?: '删除失败');
  123. }
  124. return $this->renderSuccess('删除成功');
  125. }
  126. }