Goods.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. */
  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. * @return Json
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. */
  70. public function add(): Json
  71. {
  72. $model = new GoodsModel;
  73. if ($model->add($this->postForm())) {
  74. return $this->renderSuccess('添加成功');
  75. }
  76. return $this->renderError($model->getError() ?: '添加失败');
  77. }
  78. /**
  79. * 编辑商品
  80. * @param int $goodsId
  81. * @return Json
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\DbException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. */
  86. public function edit(int $goodsId): Json
  87. {
  88. // 商品详情
  89. $model = GoodsModel::detail($goodsId);
  90. // 更新记录
  91. if ($model->edit($this->postForm())) {
  92. return $this->renderSuccess('更新成功');
  93. }
  94. return $this->renderError($model->getError() ?: '更新失败');
  95. }
  96. /**
  97. * 修改商品状态(上下架)
  98. * @param array $goodsIds 商品id集
  99. * @param bool $state 为true表示上架
  100. * @return Json
  101. */
  102. public function state(array $goodsIds, bool $state): Json
  103. {
  104. $model = new GoodsModel;
  105. if (!$model->setStatus($goodsIds, $state)) {
  106. return $this->renderError($model->getError() ?: '操作失败');
  107. }
  108. return $this->renderSuccess('操作成功');
  109. }
  110. /**
  111. * 删除商品
  112. * @param array $goodsIds
  113. * @return array
  114. */
  115. public function delete(array $goodsIds): Json
  116. {
  117. $model = new GoodsModel;
  118. if (!$model->setDelete($goodsIds)) {
  119. return $this->renderError($model->getError() ?: '删除失败');
  120. }
  121. return $this->renderSuccess('删除成功');
  122. }
  123. }