Goods.php 6.0 KB

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