Goods.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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\sharing;
  10. use app\store\controller\Controller;
  11. use app\store\model\user\Grade as GradeModel;
  12. use app\store\model\Delivery as DeliveryModel;
  13. use app\store\model\sharing\Goods as GoodsModel;
  14. use app\store\model\sharing\Category as CategoryModel;
  15. /**
  16. * 拼团商品管理控制器
  17. * Class Goods
  18. * @package app\store\controller\apps\sharing
  19. */
  20. class Goods extends Controller
  21. {
  22. /**
  23. * 商品列表(出售中)
  24. * @return mixed
  25. * @throws \think\exception\DbException
  26. */
  27. public function index()
  28. {
  29. // 获取全部商品列表
  30. $model = new GoodsModel;
  31. $list = $model->getList(array_merge(['status' => -1], $this->request->param()));
  32. // 商品分类
  33. $catgory = CategoryModel::getCacheTree();
  34. return $this->fetch('index', compact('list', 'catgory'));
  35. }
  36. /**
  37. * 添加商品
  38. * @return array|mixed
  39. * @throws \think\exception\PDOException
  40. */
  41. public function add()
  42. {
  43. if (!$this->request->isAjax()) {
  44. // 商品分类
  45. $catgory = CategoryModel::getCacheTree();
  46. // 配送模板
  47. $delivery = DeliveryModel::getAll();
  48. // 会员等级列表
  49. $gradeList = GradeModel::getUsableList();
  50. return $this->fetch('add', compact('catgory', 'delivery', 'gradeList'));
  51. }
  52. $model = new GoodsModel;
  53. if ($model->add($this->postData('goods'))) {
  54. return $this->renderSuccess('添加成功', url('apps.sharing.goods/index'));
  55. }
  56. return $this->renderError($model->getError() ?: '添加失败');
  57. }
  58. /**
  59. * 复制主商城商品
  60. * @param $goods_id
  61. * @return array|mixed
  62. * @throws \think\exception\PDOException
  63. */
  64. public function copy_master($goods_id)
  65. {
  66. // 商品详情
  67. $model = \app\store\model\Goods::detail($goods_id);
  68. if (!$model || $model['is_delete']) {
  69. return $this->renderError('商品信息不存在');
  70. }
  71. if (!$this->request->isAjax()) {
  72. // 商品分类
  73. $catgory = CategoryModel::getCacheTree();
  74. // 配送模板
  75. $delivery = DeliveryModel::getAll();
  76. // 商品sku数据
  77. $specData = 'null';
  78. if ($model['spec_type'] == 20) {
  79. $specData = json_encode($model->getManySpecData($model['spec_rel'], $model['sku']), JSON_UNESCAPED_SLASHES);
  80. }
  81. // 会员等级列表
  82. $gradeList = GradeModel::getUsableList();
  83. return $this->fetch('copy_master', compact('model', 'catgory', 'delivery', 'specData', 'gradeList'));
  84. }
  85. // 新增拼团商品
  86. $model = new GoodsModel;
  87. if ($model->add($this->postData('goods'))) {
  88. return $this->renderSuccess('添加成功', url('apps.sharing.goods/index'));
  89. }
  90. return $this->renderError($model->getError() ?: '添加失败');
  91. }
  92. /**
  93. * 一键复制
  94. * @param $goods_id
  95. * @return array|mixed
  96. * @throws \think\exception\PDOException
  97. */
  98. public function copy($goods_id)
  99. {
  100. // 商品详情
  101. $model = GoodsModel::detail($goods_id);
  102. if (!$this->request->isAjax()) {
  103. // 商品分类
  104. $catgory = CategoryModel::getCacheTree();
  105. // 配送模板
  106. $delivery = DeliveryModel::getAll();
  107. // 商品sku数据
  108. $specData = 'null';
  109. if ($model['spec_type'] == 20) {
  110. $specData = json_encode($model->getManySpecData($model['spec_rel'], $model['sku']), JSON_UNESCAPED_SLASHES);
  111. }
  112. // 会员等级列表
  113. $gradeList = GradeModel::getUsableList();
  114. return $this->fetch('edit', compact('model', 'catgory', 'delivery', 'specData', 'gradeList'));
  115. }
  116. $model = new GoodsModel;
  117. if ($model->add($this->postData('goods'))) {
  118. return $this->renderSuccess('添加成功', url('apps.sharing.goods/index'));
  119. }
  120. return $this->renderError($model->getError() ?: '添加失败');
  121. }
  122. /**
  123. * 商品编辑
  124. * @param $goods_id
  125. * @return array|mixed
  126. * @throws \think\exception\PDOException
  127. */
  128. public function edit($goods_id)
  129. {
  130. // 商品详情
  131. $model = GoodsModel::detail($goods_id);
  132. if (!$this->request->isAjax()) {
  133. // 商品分类
  134. $catgory = CategoryModel::getCacheTree();
  135. // 配送模板
  136. $delivery = DeliveryModel::getAll();
  137. // 商品sku数据
  138. $specData = 'null';
  139. if ($model['spec_type'] == 20) {
  140. $specData = json_encode($model->getManySpecData($model['spec_rel'], $model['sku']), JSON_UNESCAPED_SLASHES);
  141. }
  142. // 会员等级列表
  143. $gradeList = GradeModel::getUsableList();
  144. return $this->fetch('edit', compact('model', 'catgory', 'delivery', 'specData', 'gradeList'));
  145. }
  146. // 更新记录
  147. if ($model->edit($this->postData('goods'))) {
  148. return $this->renderSuccess('更新成功', url('apps.sharing.goods/index'));
  149. }
  150. return $this->renderError($model->getError() ?: '更新失败');
  151. }
  152. /**
  153. * 修改商品状态
  154. * @param $goods_id
  155. * @param boolean $state
  156. * @return array
  157. */
  158. public function state($goods_id, $state)
  159. {
  160. // 商品详情
  161. $model = GoodsModel::detail($goods_id);
  162. if (!$model->setStatus($state)) {
  163. return $this->renderError('操作失败');
  164. }
  165. return $this->renderSuccess('操作成功');
  166. }
  167. /**
  168. * 删除商品
  169. * @param $goods_id
  170. * @return array
  171. */
  172. public function delete($goods_id)
  173. {
  174. // 商品详情
  175. $model = GoodsModel::detail($goods_id);
  176. if (!$model->setDelete()) {
  177. return $this->renderError('删除失败');
  178. }
  179. return $this->renderSuccess('删除成功');
  180. }
  181. }