| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- <?php
- namespace addons\ScoreMall\backend\controllers;
- use addons\ScoreMall\common\models\ScoreMallExchangeRecord;
- use addons\ScoreMall\common\models\ScoreMallGoods;
- use addons\ScoreMall\common\models\ScoreMallGoodsAttr;
- use addons\ScoreMall\common\enums\ScoreMallEnum;
- use common\models\goods\Goods;
- use common\models\goods\GoodsAttr;
- use common\models\goods\GoodsMarketing;
- use common\models\user\User;
- use common\enums\StatusEnum;
- use common\enums\AddonsEnum;
- use common\enums\GoodsTypeEnum;
- use Yii;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\ScoreMall\backend\controllers
- */
- class DefaultController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * 首页
- *
- * @author hepi
- * @return mixed
- */
- public function actionIndex()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $get = Yii::$app->request->get();
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['<>','status',StatusEnum::DELETE];
- if (isset($get['goods_name']) && !empty($get['goods_name'])) {
- $goodsLists = Goods::goodsSearchFromAddons('goods_name', $get['goods_name'], $this->mall_id);
- if (empty($goodsLists)) {
- return $this->success('操作成功', ['list' => []]);
- } else {
- $goodsIds = array_column($goodsLists, 'id');
- $where[] = ['goods_id' => $goodsIds];
- }
- }
- $params = [
- 'where' => $where,
- 'limit' => 10,
- 'select' => ['goods_id', 'num', 'created_at', 'money', 'type', 'price', 'score'],
- 'order' => 'created_at desc'
- ];
- $list = ScoreMallGoods::listPage($params);
- if ($list['list']) {
- $url = Yii::$app->services->setting->mall->get($this->mall_id, 'basic.web_url');
- $url = $url ?? '';
- $list['domain'] = $url;
- if (!isset($goodsLists)) {
- $goodsLists = Goods::find()
- ->select(['id', 'goods_name', 'cover_pic', 'price'])
- ->where(['in', 'id', array_column($list['list'], 'goods_id')])
- ->asArray()->indexBy('id')->all();
- }
- $goodsLists = array_column($goodsLists, null, 'id');
- foreach ($list['list'] as &$val) {
- $val['money'] = $val['money'] ?? '0.00';
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- $val['type_info'] = ScoreMallEnum::getTypeMap($val['type']);
- $val['goods_name'] = isset($goodsLists[$val['goods_id']]) ? $goodsLists[$val['goods_id']]['goods_name'] : '';
- $val['cover_pic'] = isset($goodsLists[$val['goods_id']]) ? $goodsLists[$val['goods_id']]['cover_pic'] : '';
- $val['price'] = isset($goodsLists[$val['goods_id']]) ? $goodsLists[$val['goods_id']]['price'] : '';
- }
- }
- return $this->success('操作成功', $list);
- }
- }
- return $this->render('index');
- }
- /**
- * 兑换记录
- *
- * @author hepi
- * @return mixed
- */
- public function actionExchangeRecord() {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $get = Yii::$app->request->get();
- $where[] = ['=', 'mall_id', $this->mall_id];
- if (!empty($get['date'])) {
- $date = date($get['date'] . '-01');
- }
- $start_time = strtotime($date);
- $end_time = strtotime($date . '+1 month -1 day 23:59:59');
- $where[] = ['>=', 'created_at', $start_time];
- $where[] = ['<=', 'created_at', $end_time];
- if (isset($get['user_id']) && !empty($get['user_id'])) {
- $where[] = ['=', 'user_id', $get['user_id']];
- }
- if (isset($get['goods_name']) && !empty($get['goods_name'])) {
- $goodsLists = Goods::goodsSearchFromAddons('goods_name', $get['goods_name'], $this->mall_id);
- if (empty($goodsLists)) {
- return $this->success('操作成功', ['list' => []]);
- } else {
- $goodsIds = array_column($goodsLists, 'id');
- $where[] = ['in', 'goods_id', $goodsIds];
- }
- }
- $params = [
- 'where' => $where,
- 'limit' => 10,
- 'order' => 'created_at desc',
- 'select' => ['user_id', 'goods_id', 'order_no', 'order_id', 'exchange_price', 'num', 'goods', 'created_at']
- ];
- $list = ScoreMallExchangeRecord::listPage($params);
- if ($list['list']) {
- $list['date'] = $date ?? '';
- $userList = User::find()
- ->select(['id', 'mobile', 'nickname', 'avatar_url'])
- ->where(['in', 'id', array_column($list['list'], 'user_id')])
- ->asArray()->indexBy('id')->all();
- $userList = array_column($userList, null, 'id');
- foreach ($list['list'] as &$val) {
- $goodsInfo = json_decode($val['goods'], true);
- $val['goods_name'] = $goodsInfo['goods_name'];
- $val['pic_url'] = $goodsInfo['cover_pic'];
- $val['price'] = $goodsInfo['price'];
- $val['avatar_url'] = isset($userList[$val['user_id']]) ? $userList[$val['user_id']]['avatar_url'] : '';
- $val['nickname'] = isset($userList[$val['user_id']]) ? $userList[$val['user_id']]['nickname'] : '';
- $val['mobile'] = isset($userList[$val['user_id']]) ? $userList[$val['user_id']]['mobile'] : '';
- $val['money'] = $val['money'] ?? '0.00';
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- }
- }
- return $this->success('操作成功', $list);
- }
- }
- return $this->render('exchange-record');
- }
- /**
- * 分类
- *
- * @author hpei
- * @return mixed
- */
- public function actionClassification() {
- if (Yii::$app->request->isAjax) {
- $res = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_SCORE_MALL, 'basic');
- $list = !empty($res) && isset($res['classification']) ? json_decode($res['classification'], true) : [];
- if (Yii::$app->request->isGet) {
- return $this->success('操作成功', $list);
- } else {
- $post = Yii::$app->request->post();
- $data = [];
- switch ($post['type']) {
- case 'add':
- $name = $post['name'] ?? '';
- if (empty($name)) throw new \Exception('分类名字不能为空');
- $id = array_column($list, 'id');
- $id = empty($id) ? 1 : max($id) +1 ;
- $data = ['id' => $id, 'name' => $name, 'status' => $post['status'] ?? 1, 'created_at' => date('Y-m-d H:i:s')];
- $list[] = $data;
- $insertData = ['basic' => ['classification' => json_encode($list, JSON_UNESCAPED_UNICODE)]];
- break;
- case 'updateStatus':
- $status = $post['status'] ?? 1;
- $status = $status == 1 ? 0: 1;
- $id = $post['id'];
- $list = array_column($list, null, 'id');
- if (!isset($list[$id])) throw new \Exception('该分类不存在');
- $list[$id]['status'] = $status;
- $list = array_values($list);
- $insertData = ['basic' => ['classification' => json_encode($list, JSON_UNESCAPED_UNICODE)]];
- break;
- case 'delete':
- $id = $post['id'];
- $list = array_column($list, null, 'id');
- if (!isset($list[$id])) throw new \Exception('该分类不存在');
- unset($list[$id]);
- $list = array_values($list);
- $insertData = ['basic' => ['classification' => json_encode($list, JSON_UNESCAPED_UNICODE)]];
- break;
- }
- Yii::$app->services->setting->addons->set($this->mall_id, AddonsEnum::ADDONS_NAME_SCORE_MALL, $insertData);
- return $this->success('操作成功', $data);
- }
- }
- return $this->render('classification');
- }
- /**
- * 新增&编辑积分商品
- *
- * @author hpei
- * @return mixed
- */
- public function actionEdit() {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $get = Yii::$app->request->get();
- $data = [];
- $goodsInfo = [];
- $attr = [];
- if (isset($get['goods_id']) && !empty($get['goods_id'])) {
- $where = [['goods_id' => $get['goods_id'], 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]];
- $data = ScoreMallGoods::getOne($where, true, [], false, ['goods_id', 'cate_id', 'price', 'type', 'use_coupon']);
- if (!$data) throw new \Exception('商品不存在');
- $where = [['id' => $get['goods_id'], 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]];
- $goodsInfo = Goods::getOne($where, true, [], false, ['id', 'price', 'cover_pic', 'goods_name']);
- }
- $res = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_SCORE_MALL, 'basic');
- $cateList = !empty($res) && isset($res['classification']) ? json_decode($res['classification'], true) : [];
- $cateList = array_column($cateList, null, 'id');
- $list['data'] = $data;
- $list['cate_list'] = $cateList;
- $list['goods_info'] = $goodsInfo;
- return $this->success('操作成功', $list);
- } else {
- $post = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($post, [
- [['cate_id', 'use_coupon', 'goods_id', 'type', 'attr'], 'required'],
- [['cate_id', 'use_coupon', 'goods_id'], 'integer'],
- [['money','score','price'], 'double'],// 双精度浮点型
- [['type'], 'integer', 'max' => 3, 'min' => 1],
- [['attr', 'action'], 'safe']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $goods_id = $post['goods_id'] ?? '';
- if (!$goods_id) return $this->error('商品不能为空');
- // 目前积分商城仅支持聚合供应链商品或本商城商品
- $good = Goods::find()->where(['id' => $post['goods_id']])->asArray()->one();
- $goods_sources = [
- AddonsEnum::ADDONS_NAME_JUHE_SHOP,
- AddonsEnum::ADDONS_NAME_QI_JUHE,
- AddonsEnum::ADDONS_NAME_QI_DING_DONG,
- AddonsEnum::ADDONS_NAME_QI_SHANG_TONG,
- 'StarChain'
- ];
- if ($good && !empty($good['goods_source']) && !in_array($good['goods_source'], $goods_sources)) return $this->error('目前积分商城仅支持聚合/怡亚通/企叮咚/企商通供应链商品或本商城商品');
- $attr = $post['attr'];
- $goodsAttrInstall = [];
- foreach ($attr as &$val) {
- if ($post['type'] == ScoreMallEnum::TYPE_SCORE) {
- if (!$val['scoreGoodsAttr']['score']) return $this->error('积分不能为空');
- }
- if ($post['type'] == ScoreMallEnum::TYPE_SCORE_MONEY) {
- if (!$val['scoreGoodsAttr']['score'] || !$val['scoreGoodsAttr']['money']) return $this->error('积分和金额不能为空');
- }
- if ($post['type'] == ScoreMallEnum::TYPE_LIMIT) {
- $deductionLimit = $val['scoreGoodsAttr']['deduction_limit'];
- if (($deductionLimit[0]['limit'] && !$deductionLimit[0]['score']) || ($deductionLimit[1]['limit'] && !$deductionLimit[1]['score'])) {
- return $this->error('积分不能为空');
- }
- }
- $goodsAttrInstall[] = [
- 'goods_id' => $goods_id,
- 'goods_attr_id' => $val['id'],
- 'price' => $val['price'],
- 'score' => $post['type'] != ScoreMallEnum::TYPE_LIMIT ? $val['scoreGoodsAttr']['score'] : 0,
- 'money' => $post['type'] != ScoreMallEnum::TYPE_LIMIT ? $val['scoreGoodsAttr']['money'] : 0,
- 'created_at' => time(),
- 'updated_at' => time(),
- 'deduction_limit' => $post['type'] == ScoreMallEnum::TYPE_LIMIT ? json_encode([
- ['limit' => $val['scoreGoodsAttr']['deduction_limit'][0]['limit'],'score'=>$val['scoreGoodsAttr']['deduction_limit'][0]['score']],
- ['limit' => $val['scoreGoodsAttr']['deduction_limit'][1]['limit'],'score'=>$val['scoreGoodsAttr']['deduction_limit'][1]['score']]]) : ''
- ];
- }
- try {
- $t = Yii::$app->db->beginTransaction();
- $post['mall_id'] = $this->mall_id;
- if ($post['action'] == 'add') $post['status'] = StatusEnum::ENABLED;
- $res1 = ScoreMallGoods::setData($post);
- $res2 = ScoreMallGoodsAttr::setData($goodsAttrInstall, $post['action']);
- if ($res1 && $res2) {
- $t->commit();
- } else {
- $t->rollBack();
- $msg = $res1 == false ? ScoreMallGoods::getStaticError() : ScoreMallGoodsAttr::getStaticError();
- return $this->error($msg);
- }
- return $this->success('操作成功');
- } catch (\Exception $e) {
- $t->rollBack();
- return $this->error($e->getMessage());
- }
- return $this->success('操作成功');
- }
- }
- return $this->render('edit', ['goods_id' => Yii::$app->request->get('goods_id', '')]);
- }
- /**
- * 获取商品属性列表
- *
- * @param string $goods_name
- * @author hpei
- * @return array
- */
- public function actionAttrList()
- {
- if (Yii::$app->request->isAjax) {
- $get = Yii::$app->request->get();
- $goods_id = $get['goods_id'] ?? 0;
- // $where[] = ['>', 'stock', 1];
- $where[] = ['=', 'goods_id', $goods_id];
- $with = [];
- if ($get['type'] == 'edit') {
- $with = ['scoreGoodsAttr' => function($query) {
- $query->where(['<>','status',StatusEnum::DELETE])->select(['goods_attr_id', 'score', 'money', 'deduction_limit']);
- }];
- }
- $list = GoodsAttr::getGoodsAttrs($where, $with, true, ['id', 'name', 'price']);
- if (empty($list)) return $this->error('该商品暂不可选择');
- return $this->success('操作成功', $list);
- }
- }
- public function actionDelete() {
- if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
- $goods_id = Yii::$app->request->post('goods_id', 0);
- $trans = Yii::$app->db->beginTransaction();
- try {
- $score_goods = ScoreMallGoods::setData(['goods_id' => $goods_id, 'status' => StatusEnum::DELETE]);
- if (!$score_goods) return $this->error(ScoreMallGoods::getStaticError());
- $res = ScoreMallGoodsAttr::updateAll(['status' => StatusEnum::DELETE],['goods_id' => $goods_id]);
- if (!$res) return $this->error(ScoreMallGoodsAttr::getStaticError());
- // 删除成功解除商品列表积分商品的状态
- GoodsMarketing::updateAll(['status' => StatusEnum::DELETE], ['mall_id' => $this->mall_id, 'marketing_id' => $score_goods['id'], 'marketing_type' => AddonsEnum::ADDONS_NAME_SCORE_MALL, 'goods_id' => $goods_id]);
- $trans->commit();
- } catch (\Exception $e) {
- $trans->rollBack();
- return $this->error($e->getMessage());
- }
- }
- return $this->success('操作成功');
- }
- /**
- * 获取积分商城可以选择的商品列表
- *
- * @return mixed
- */
- public function actionGoodsList()
- {
- $get = Yii::$app->request->get();
- $rules = [
- [['keyword'], 'string'],
- [['price'], 'number'],
- ];
- $res = Yii::$app->services->validate->validate($get, $rules);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $page_data = Goods::getPaginationList(15, function($query) use($get) {
- $query->select([
- 'id', 'mall_id', 'cover_pic', 'mch_id', 'cost_price', 'original_price', 'price', 'sales_num',
- 'stock', 'goods_name', 'sort', 'is_on_sale', 'goods_source', 'created_at',
- ]);
- $query->where([
- 'mall_id' => $this->mall_id,
- 'mch_id' => $this->admin->mch_id,
- 'is_on_sale' => 1,
- 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED],
- 'goods_type' => [GoodsTypeEnum::GoodsTypePhysical, GoodsTypeEnum::GoodsTypeVirtual]
- ]);
- $query->andWhere(['!=', 'goods_name', '代客下单插件商品']);
- !empty($get['keyword']) && $query->andWhere([
- 'or',
- ['=', 'id', $get['keyword']],
- ['like', 'goods_name', $get['keyword']]
- ]);
- !empty($get['price']) && $query->andWhere(['=', 'price', $get['price']]);
- }, 'sort ASC,id DESC');
- return $page_data === false
- ? $this->error(Goods::getStaticError())
- : $this->success('操作成功', $page_data);
- }
- }
|