Goods.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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 cores\exception\BaseException;
  15. use app\store\model\Goods as GoodsModel;
  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. */
  56. public function detail(int $goodsId): Json
  57. {
  58. // 获取商品详情
  59. $model = new GoodsModel;
  60. $goodsInfo = $model->getDetail($goodsId);
  61. return $this->renderSuccess(compact('goodsInfo'));
  62. }
  63. /**
  64. * 商品详情(基础信息)
  65. * @param int $goodsId
  66. * @return Json
  67. * @throws BaseException
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function basic(int $goodsId): Json
  73. {
  74. // 获取商品详情
  75. $model = new GoodsModel;
  76. $detail = $model->getBasic($goodsId);
  77. return $this->renderSuccess(compact('detail'));
  78. }
  79. /**
  80. * 添加商品
  81. * @return Json
  82. * @throws BaseException
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. */
  87. public function add(): Json
  88. {
  89. $model = new GoodsModel;
  90. if ($model->add($this->postForm())) {
  91. return $this->renderSuccess('添加成功');
  92. }
  93. return $this->renderError($model->getError() ?: '添加失败');
  94. }
  95. /**
  96. * 商品编辑
  97. * @param int $goodsId
  98. * @return Json
  99. * @throws BaseException
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\DbException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. */
  104. public function edit(int $goodsId): Json
  105. {
  106. // 商品详情
  107. $model = GoodsModel::detail($goodsId);
  108. // 更新记录
  109. if ($model->edit($this->postForm())) {
  110. return $this->renderSuccess('更新成功');
  111. }
  112. return $this->renderError($model->getError() ?: '更新失败');
  113. }
  114. /**
  115. * 修改商品状态(上下架)
  116. * @param array $goodsIds 商品id集
  117. * @param bool $state 为true表示上架
  118. * @return Json
  119. */
  120. public function state(array $goodsIds, bool $state): Json
  121. {
  122. $model = new GoodsModel;
  123. if (!$model->setStatus($goodsIds, $state)) {
  124. return $this->renderError($model->getError() ?: '操作失败');
  125. }
  126. return $this->renderSuccess('操作成功');
  127. }
  128. /**
  129. * 删除商品
  130. * @param array $goodsIds
  131. * @return Json
  132. */
  133. public function delete(array $goodsIds): Json
  134. {
  135. $model = new GoodsModel;
  136. if (!$model->setDelete($goodsIds)) {
  137. return $this->renderError($model->getError() ?: '删除失败');
  138. }
  139. return $this->renderSuccess('删除成功');
  140. }
  141. }