GoodsController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace addons\Purchase\backend\controllers;
  3. use addons\Purchase\common\models\PurchaseGoods;
  4. use common\enums\StatusEnum;
  5. use common\models\goods\Goods;
  6. use common\models\goods\GoodsAttr;
  7. use Yii;
  8. /**
  9. * 商品控制器
  10. *
  11. * Class DefaultController
  12. * @package addons\Purchase\backend\controllers
  13. */
  14. class GoodsController extends BaseController
  15. {
  16. public $enableCsrfValidation = false;
  17. /**
  18. * @notes:商品列表
  19. * @return mixed|string|null
  20. * @author: lun
  21. * @Time: 2022/10/22 9:46
  22. */
  23. public function actionIndex()
  24. {
  25. if (Yii::$app->request->isAjax) {
  26. if (Yii::$app->request->isGet) {
  27. $get = Yii::$app->request->get();
  28. // 检查提交的参数
  29. $res = Yii::$app->services->validate->validate($get,[[['goods_id'], 'integer']]);
  30. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  31. $where[] = ['mall_id' => $this->mall_id];
  32. $where[] = ['=','status',StatusEnum::ENABLED];
  33. if (!empty($get['goods_id'])) $where[] = ['=', 'goods_id', $get['goods_id']];
  34. $with = ['goods'];
  35. $order = 'created_at desc';
  36. $params = array('where' => $where, 'order'=>$order, 'with' => $with, 'limit' => $get['limit'] ?? 20);
  37. $list = PurchaseGoods::listPage($params,false, true);
  38. return $this->success('操作成功', $list);
  39. }
  40. }
  41. return $this->render('index');
  42. }
  43. /**
  44. * @notes:新增或编辑
  45. * @return mixed|string|null
  46. * @author: lun
  47. * @Time: 2022/10/22 11:08
  48. */
  49. public function actionEdit()
  50. {
  51. if (Yii::$app->request->isAjax) {
  52. if (Yii::$app->request->isGet) {
  53. $get = Yii::$app->request->get();
  54. // 检查提交的参数
  55. $res = Yii::$app->services->validate->validate($get,[[['goods_id'], 'integer']]);
  56. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  57. // 根据商品id获取商品信息
  58. if (!empty($get['goods_id'])) {
  59. $where = ['mall_id' => $this->mall_id, 'goods_id' => $get['goods_id'], 'status' => StatusEnum::ENABLED];
  60. $detail = PurchaseGoods::find()->where($where)->with(['goods', 'attr', 'cats'])->asArray()->one();
  61. $detail['goods'] = [$detail['goods']];
  62. // 获取规格商品信息
  63. if (!empty($detail['attr'])) {
  64. $attr_ids = array_column($detail['attr'], 'goods_attr_id');
  65. $attr_arr = GoodsAttr::find()->where(['id' => $attr_ids])->indexBy('id')->asArray()->all();
  66. foreach ($detail['attr'] as &$attr) {
  67. $attr['name'] = $attr_arr[$attr['goods_attr_id']]['name'] ?? '默认';
  68. $attr['price'] = $attr_arr[$attr['goods_attr_id']]['price'] ?? 0;
  69. $attr['min_limit'] = $attr['min_limit'] == 1 ? true : false;
  70. $attr['max_limit'] = $attr['max_limit'] == 1 ? true : false;
  71. }
  72. }
  73. }
  74. $data['detail'] = $detail ?? [];
  75. return $this->success('操作成功', $data);
  76. } else {
  77. $post = Yii::$app->request->post();
  78. $res = Yii::$app->services->validate->validate($post, [
  79. [['goods_id', 'cats', 'attr'], 'required'],
  80. [['id','goods_id'], 'integer'],
  81. [['cats','attr'], 'safe']
  82. ]);
  83. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  84. // 检测商品是否是供应链商品,供应链商品不允许加入到积分商城中
  85. $good = Goods::find()->where(['id' => $post['goods_id']])->asArray()->one();
  86. if ($good && $good['goods_source']) return $this->error('不能添加供应链商品,请选择本商场商品');
  87. // 保存数据
  88. $res = PurchaseGoods::setData($this->mall_id, $post);
  89. if ($res === false) return $this->error('保存失败:'.PurchaseGoods::getStaticError());
  90. return $this->success('保存成功');
  91. }
  92. }
  93. return $this->render('edit');
  94. }
  95. /**
  96. * @notes:多商品设置
  97. * @return mixed|string|null
  98. * @author: lun
  99. * @Time: 2022/11/15 11:48
  100. */
  101. public function actionEditMore()
  102. {
  103. if (Yii::$app->request->isAjax) {
  104. if (Yii::$app->request->isPost) {
  105. $post = Yii::$app->request->post();
  106. $res = Yii::$app->services->validate->validate($post, [
  107. [['goods', 'cats', 'attr'], 'required'],
  108. [['cats','attr','goods'], 'safe']
  109. ]);
  110. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  111. // 获取商品id
  112. $good_ids = array_column($post['goods'], 'id');
  113. // 检测商品是否是供应链商品,供应链商品不允许加入到积分商城中
  114. $good = Goods::find()->where(['id' => $good_ids])->asArray()->one();
  115. if ($good && $good['goods_source']) return $this->error('不能添加供应链商品,请选择本商场商品');
  116. // 保存数据
  117. $res = PurchaseGoods::batchSetData($this->mall_id, $post);
  118. if ($res === false) return $this->error('保存失败:'.PurchaseGoods::getStaticError());
  119. return $this->success('保存成功');
  120. }
  121. }
  122. return $this->render('edit-more');
  123. }
  124. /**
  125. * @notes:获取商品规格
  126. * @return mixed|void|null
  127. * @author: lun
  128. * @Time: 2022/10/22 14:32
  129. */
  130. public function actionAttrList() {
  131. if (Yii::$app->request->isAjax) {
  132. $get = Yii::$app->request->get();
  133. $goods_id = $get['goods_id'] ?? 0;
  134. $where[] = ['=', 'goods_id', $goods_id];
  135. $where[] = ['=', 'status', StatusEnum::ENABLED];
  136. $list = GoodsAttr::getGoodsAttrs($where, [], true, ['id', 'name', 'price','pic_url']);
  137. if (empty($list)) return $this->success('该商品暂不可选择', $list);
  138. return $this->success('操作成功', $list);
  139. }
  140. }
  141. /**
  142. * @notes:商品删除
  143. * @return mixed|void|null
  144. * @author: lun
  145. * @Time: 2022/10/22 17:14
  146. */
  147. public function actionDelete()
  148. {
  149. if (Yii::$app->request->isAjax) {
  150. $post = Yii::$app->request->post();
  151. $res = Yii::$app->services->validate->validate($post, [[['id'], 'required'], [['id'], 'integer']]);
  152. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  153. $res = PurchaseGoods::del($this->mall_id, $post['id']);
  154. if ($res === false) return $this->success('删除失败:' . PurchaseGoods::getStaticError());
  155. return $this->success('删除成功');
  156. }
  157. }
  158. }