Goods.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 app\common\exception\BaseException;
  14. use app\store\model\Goods as GoodsModel;
  15. /**
  16. * 商品管理控制器
  17. * Class Goods
  18. * @package app\store\controller
  19. */
  20. class Goods extends Controller
  21. {
  22. /**
  23. * 商品列表
  24. * @return array
  25. * @throws \think\db\exception\DbException
  26. */
  27. public function list()
  28. {
  29. // 获取列表记录
  30. $model = new GoodsModel;
  31. $list = $model->getList($this->request->param());
  32. return $this->renderSuccess(compact('list'));
  33. }
  34. /**
  35. * 根据商品ID集获取列表记录
  36. * @param array $goodsIds
  37. * @return array
  38. */
  39. public function listByIds(array $goodsIds)
  40. {
  41. // 获取列表记录
  42. $model = new GoodsModel;
  43. $list = $model->getListByIds($goodsIds);
  44. return $this->renderSuccess(compact('list'));
  45. }
  46. /**
  47. * 商品详情
  48. * @param int $goodsId
  49. * @return array
  50. * @throws BaseException
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function detail(int $goodsId)
  56. {
  57. // 获取商品详情
  58. $model = new GoodsModel;
  59. $goodsInfo = $model->getDetail($goodsId);
  60. return $this->renderSuccess(compact('goodsInfo'));
  61. }
  62. /**
  63. * 添加商品
  64. * @return array
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. public function add()
  70. {
  71. $model = new GoodsModel;
  72. if ($model->add($this->postForm())) {
  73. return $this->renderSuccess('添加成功');
  74. }
  75. return $this->renderError($model->getError() ?: '添加失败');
  76. }
  77. /**
  78. * 编辑商品
  79. * @param int $goodsId
  80. * @return array
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. */
  85. public function edit(int $goodsId)
  86. {
  87. // 商品详情
  88. $model = GoodsModel::detail($goodsId);
  89. // 更新记录
  90. if ($model->edit($this->postForm())) {
  91. return $this->renderSuccess('更新成功');
  92. }
  93. return $this->renderError($model->getError() ?: '更新失败');
  94. }
  95. /**
  96. * 修改商品状态(上下架)
  97. * @param array $goodsIds 商品id集
  98. * @param bool $state 为true表示上架
  99. * @return array
  100. */
  101. public function state(array $goodsIds, bool $state)
  102. {
  103. $model = new GoodsModel;
  104. if (!$model->setStatus($goodsIds, $state)) {
  105. return $this->renderError($model->getError() ?: '操作失败');
  106. }
  107. return $this->renderSuccess('操作成功');
  108. }
  109. /**
  110. * 删除商品
  111. * @param array $goodsIds
  112. * @return array
  113. */
  114. public function delete(array $goodsIds)
  115. {
  116. $model = new GoodsModel;
  117. if (!$model->setDelete($goodsIds)) {
  118. return $this->renderError($model->getError() ?: '删除失败');
  119. }
  120. return $this->renderSuccess('删除成功');
  121. }
  122. }