Product.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace app\shop\controller\product;
  3. use app\shop\model\product\Product as ProductModel;
  4. use app\shop\model\product\Category as CategoryModel;
  5. use app\shop\service\ProductService;
  6. use app\shop\controller\Controller;
  7. /**
  8. * 商品管理控制器
  9. */
  10. class Product extends Controller
  11. {
  12. /**
  13. * 商品列表(全部)
  14. */
  15. public function index()
  16. {
  17. // 获取全部商品列表
  18. $model = new ProductModel;
  19. $list = $model->getList(array_merge(['status' => -1], $this->postData()));
  20. // 商品分类
  21. $category = CategoryModel::getCacheTree();
  22. // 数量
  23. $product_count = [
  24. 'sell' => $model->getCount('sell'),
  25. 'recovery' => $model->getCount('recovery'),
  26. 'lower' => $model->getCount('lower'),
  27. 'audit' => $model->getCount('audit'),
  28. 'no_audit' => $model->getCount('no_audit')
  29. ];
  30. return $this->renderSuccess('', compact('list', 'category', 'product_count'));
  31. }
  32. /**
  33. * 商品列表(在售)
  34. */
  35. public function lists()
  36. {
  37. // 获取全部商品列表
  38. $model = new ProductModel;
  39. $list = $model->getLists($this->postData());
  40. // 商品分类
  41. $catgory = CategoryModel::getCacheTree();
  42. return $this->renderSuccess('', compact('list', 'catgory'));
  43. }
  44. /**
  45. * 添加商品
  46. */
  47. public function add($scene = 'add')
  48. {
  49. // get请求
  50. if($this->request->isGet()){
  51. return $this->getBaseData();
  52. }
  53. //post请求
  54. $data = json_decode($this->postData()['params'], true);
  55. if($scene == 'copy'){
  56. unset($data['create_time']);
  57. unset($data['sku']['product_sku_id']);
  58. unset($data['sku']['product_id']);
  59. unset($data['product_sku']['product_sku_id']);
  60. unset($data['product_sku']['product_id']);
  61. if($data['spec_type'] == 20){
  62. foreach ($data['spec_many']['spec_list'] as &$spec){
  63. $spec['product_sku_id'] = 0;
  64. }
  65. }
  66. //初始化销量等数据
  67. $data['sales_initial'] = 0;
  68. }
  69. $model = new ProductModel;
  70. if (isset($data['product_id'])) {
  71. $data['product_id'] = 0;
  72. }
  73. if ($model->add($data)) {
  74. return $this->renderSuccess('添加成功');
  75. }
  76. return $this->renderError($model->getError() ?: '添加失败');
  77. }
  78. /**
  79. * 获取基础数据
  80. */
  81. public function getBaseData()
  82. {
  83. return $this->renderSuccess('', array_merge(ProductService::getEditData(null, 'add'), []));
  84. }
  85. /**
  86. * 获取编辑数据
  87. */
  88. public function getEditData($product_id, $scene = 'edit')
  89. {
  90. $model = ProductModel::detail($product_id);
  91. return $this->renderSuccess('', array_merge(ProductService::getEditData($model, $scene), compact('model')));
  92. }
  93. /**
  94. * 商品编辑
  95. */
  96. public function edit($product_id, $scene = 'edit')
  97. {
  98. if($this->request->isGet()){
  99. $model = ProductModel::detail($product_id);
  100. return $this->renderSuccess('', array_merge(ProductService::getEditData($model, $scene), compact('model')));
  101. }
  102. if ($scene == 'copy') {
  103. return $this->add($scene);
  104. }
  105. // 商品详情
  106. $model = ProductModel::detail($product_id);
  107. // 更新记录
  108. if ($model->edit(json_decode($this->postData()['params'], true))) {
  109. return $this->renderSuccess('更新成功');
  110. }
  111. return $this->renderError($model->getError() ?: '更新失败');
  112. }
  113. /**
  114. * 修改商品状态
  115. */
  116. public function state($product_id, $state)
  117. {
  118. // 商品详情
  119. $model = ProductModel::detail($product_id);
  120. if (!$model->setStatus($state)) {
  121. return $this->renderError('操作失败');
  122. }
  123. return $this->renderSuccess('操作成功');
  124. }
  125. /**
  126. * 强制下架、再上架
  127. */
  128. public function audit($product_id, $state)
  129. {
  130. // 商品详情
  131. $model = ProductModel::detail($product_id);
  132. if (!$model->setAudit($state)) {
  133. return $this->renderError('操作失败');
  134. }
  135. return $this->renderSuccess('操作成功');
  136. }
  137. /**
  138. * 删除商品
  139. */
  140. public function delete($product_id)
  141. {
  142. // 商品详情
  143. $model = ProductModel::detail($product_id);
  144. if (!$model->setDelete()) {
  145. return $this->renderError($model->getError() ?: '删除失败');
  146. }
  147. return $this->renderSuccess('删除成功');
  148. }
  149. }