| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace addons\Purchase\backend\controllers;
- use addons\Purchase\common\models\PurchaseGoods;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use common\models\goods\GoodsAttr;
- use Yii;
- /**
- * 商品控制器
- *
- * Class DefaultController
- * @package addons\Purchase\backend\controllers
- */
- class GoodsController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * @notes:商品列表
- * @return mixed|string|null
- * @author: lun
- * @Time: 2022/10/22 9:46
- */
- public function actionIndex()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $get = Yii::$app->request->get();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($get,[[['goods_id'], 'integer']]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['=','status',StatusEnum::ENABLED];
- if (!empty($get['goods_id'])) $where[] = ['=', 'goods_id', $get['goods_id']];
- $with = ['goods'];
- $order = 'created_at desc';
- $params = array('where' => $where, 'order'=>$order, 'with' => $with, 'limit' => $get['limit'] ?? 20);
- $list = PurchaseGoods::listPage($params,false, true);
- return $this->success('操作成功', $list);
- }
- }
- return $this->render('index');
- }
- /**
- * @notes:新增或编辑
- * @return mixed|string|null
- * @author: lun
- * @Time: 2022/10/22 11:08
- */
- public function actionEdit()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $get = Yii::$app->request->get();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($get,[[['goods_id'], 'integer']]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 根据商品id获取商品信息
- if (!empty($get['goods_id'])) {
- $where = ['mall_id' => $this->mall_id, 'goods_id' => $get['goods_id'], 'status' => StatusEnum::ENABLED];
- $detail = PurchaseGoods::find()->where($where)->with(['goods', 'attr', 'cats'])->asArray()->one();
- $detail['goods'] = [$detail['goods']];
- // 获取规格商品信息
- if (!empty($detail['attr'])) {
- $attr_ids = array_column($detail['attr'], 'goods_attr_id');
- $attr_arr = GoodsAttr::find()->where(['id' => $attr_ids])->indexBy('id')->asArray()->all();
- foreach ($detail['attr'] as &$attr) {
- $attr['name'] = $attr_arr[$attr['goods_attr_id']]['name'] ?? '默认';
- $attr['price'] = $attr_arr[$attr['goods_attr_id']]['price'] ?? 0;
- $attr['min_limit'] = $attr['min_limit'] == 1 ? true : false;
- $attr['max_limit'] = $attr['max_limit'] == 1 ? true : false;
- }
- }
- }
- $data['detail'] = $detail ?? [];
- return $this->success('操作成功', $data);
- } else {
- $post = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($post, [
- [['goods_id', 'cats', 'attr'], 'required'],
- [['id','goods_id'], 'integer'],
- [['cats','attr'], 'safe']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 检测商品是否是供应链商品,供应链商品不允许加入到积分商城中
- $good = Goods::find()->where(['id' => $post['goods_id']])->asArray()->one();
- if ($good && $good['goods_source']) return $this->error('不能添加供应链商品,请选择本商场商品');
- // 保存数据
- $res = PurchaseGoods::setData($this->mall_id, $post);
- if ($res === false) return $this->error('保存失败:'.PurchaseGoods::getStaticError());
- return $this->success('保存成功');
- }
- }
- return $this->render('edit');
- }
- /**
- * @notes:多商品设置
- * @return mixed|string|null
- * @author: lun
- * @Time: 2022/11/15 11:48
- */
- public function actionEditMore()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isPost) {
- $post = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($post, [
- [['goods', 'cats', 'attr'], 'required'],
- [['cats','attr','goods'], 'safe']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 获取商品id
- $good_ids = array_column($post['goods'], 'id');
- // 检测商品是否是供应链商品,供应链商品不允许加入到积分商城中
- $good = Goods::find()->where(['id' => $good_ids])->asArray()->one();
- if ($good && $good['goods_source']) return $this->error('不能添加供应链商品,请选择本商场商品');
- // 保存数据
- $res = PurchaseGoods::batchSetData($this->mall_id, $post);
- if ($res === false) return $this->error('保存失败:'.PurchaseGoods::getStaticError());
- return $this->success('保存成功');
- }
- }
- return $this->render('edit-more');
- }
- /**
- * @notes:获取商品规格
- * @return mixed|void|null
- * @author: lun
- * @Time: 2022/10/22 14:32
- */
- public function actionAttrList() {
- if (Yii::$app->request->isAjax) {
- $get = Yii::$app->request->get();
- $goods_id = $get['goods_id'] ?? 0;
- $where[] = ['=', 'goods_id', $goods_id];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $list = GoodsAttr::getGoodsAttrs($where, [], true, ['id', 'name', 'price','pic_url']);
- if (empty($list)) return $this->success('该商品暂不可选择', $list);
- return $this->success('操作成功', $list);
- }
- }
- /**
- * @notes:商品删除
- * @return mixed|void|null
- * @author: lun
- * @Time: 2022/10/22 17:14
- */
- public function actionDelete()
- {
- if (Yii::$app->request->isAjax) {
- $post = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($post, [[['id'], 'required'], [['id'], 'integer']]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $res = PurchaseGoods::del($this->mall_id, $post['id']);
- if ($res === false) return $this->success('删除失败:' . PurchaseGoods::getStaticError());
- return $this->success('删除成功');
- }
- }
- }
|