| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- namespace addons\ScoreMall\api\modules\v1\controllers;
- use addons\ScoreMall\common\enums\ScoreMallEnum;
- use addons\ScoreMall\common\models\ScoreMallExchangeRecord;
- use addons\ScoreMall\common\models\ScoreMallGoods;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\logic\common\GoodsLogic;
- use common\models\common\AddonsMarketingSetting;
- use Yii;
- use api\controllers\OnAuthController;
- use common\models\goods\Goods;
- use yii\helpers\Json;
- /**
- * 商品控制器
- *
- * Class GoodsController
- * @package addons\ScoreMall\api\controllers
- */
- class GoodsController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['index', 'list', 'detail'];
- /**
- * @desc:积分商城首页
- * @Author: hua
- * @Date: 2021/12/20
- * @Time: 16:00
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionIndex()
- {
- $data = AddonsMarketingSetting::getOne([['mall_id' => $this->mall_id], ['addons_name' => ScoreMallEnum::ADDONS_NAME], ['status' => StatusEnum::ENABLED]]);
- if ($data == false) return $this->error(AddonsMarketingSetting::getStaticError());
- $content = json_decode($data['content'], true);
- if (!empty($content)) {
- if(empty($content['3']['data']['listType'])){
- $active_data = array_pop($content);
- $goods_id = [];
- if(!empty($active_data['data']['selGoodsList'])){
- foreach ($active_data['data']['selGoodsList'] as $item) {
- $goods_id[]=$item['params']['id']??'';
- }
- }
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['goods_id' => $goods_id];
- $where[] = ['status' => StatusEnum::ENABLED];
- $order = 'id desc';
- $index = 'goods_id';
- $select = "id,goods_id,cate_id,score,money,type,created_at";
- $with = [
- 'goods' => function($query){
- $query->select("id,goods_name,price,cover_pic,subtitle");
- },
- 'attr' => function($query){
- $query->select("goods_id,goods_attr_id,score,money,deduction_limit,price");
- }];
- $params = compact('where', 'order', 'select', 'with','index');
- $list = ScoreMallGoods::lists($params,true);
- $exchange_price_arr = [];
- if(!empty($list)){
- $score_config = Yii::$app->services->setting->mall->get($this->mall_id, 'score');
- foreach ($list as $item){
- $exchange_price = ScoreMallGoods::getExchangePrice($item['type'], $item['attr'][0], $score_config, 1, $this->user_id);
- $exchange_price_arr[$item['goods_id']] = $exchange_price['exchange_price'];
- }
- }
- if(!empty($active_data['data']['selGoodsList'])) {
- foreach ($active_data['data']['selGoodsList'] as &$item) {
- if (isset($item['params']['id']) && isset($exchange_price_arr[$item['params']['id']])) {
- $item['params']['exchange_price'] = $exchange_price_arr[$item['params']['id']];
- }
- }
- }
- $content[] = $active_data;
- $data['content'] = json_encode($content);
- }
- }
- return $this->success('操作成功', $data);
- }
- /**
- * 积分商品列表
- * @Author: lun
- * @DateTime: 2022/1/7 0007 11:43
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return string
- */
- public function actionList()
- {
- $params = \Yii::$app->request->get();
- $res = \Yii::$app->services->validate->validate($params, [
- [['cate_id','page','limit'], 'integer'],
- [['ids'], 'string'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $where = [];
- // 根据积分商品id查询
- if (!empty($params['ids'])) {
- $where[] = ['in', 'id', Json::decode($params['ids'], true)];
- }
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- // 根据分类搜索
- if (!empty($params['cate_id'])) {
- $where[] = ['=', 'cate_id', $params['cate_id']];
- }
- $with = ['goods' => function($query){
- $query->select("id,goods_name,price,cover_pic,original_price");
- }];
- $order = 'created_at desc';
- $select = ['goods_id','score','money','type'];
- $params = array('select' => $select,'where' => $where, 'order' => $order, 'with' => $with, 'limit' => $params['limit']);
- $list = ScoreMallGoods::listPage($params, false, true);
- if (empty($list)) return $this->success('获取成功');
- // 获取积分配置
- $score_config = Yii::$app->services->setting->mall->get($this->mall_id, 'score');
- // 计算积分
- foreach ($list['list'] as &$item) {
- if ($item['type'] == 3) {
- $item['score_deduct'] = '';
- } else {
- $score_info = ScoreMallGoods::getExchangePrice($item['type'], $item, $score_config);
- $item['score_deduct'] = $score_info['exchange_price'];
- }
- unset($item['score'], $item['money']);
- }
- $list = ScoreMallGoods::getScoreMallExtraFields($this->mall_id, $list);
- return $this->success('获取成功', $list);
- }
- /**
- * 积分商品详情
- * @Author: lun
- * @DateTime: 2022/1/7 0007 11:43
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return string
- */
- public function actionDetail()
- {
- $params = \Yii::$app->request->get();
- $res = \Yii::$app->services->validate->validate($params, [
- [['goods_id'], 'integer'],
- [['scene'], 'string'],
- [['addonsFields'], 'safe']
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- // 判断该商品是否存在于积分商品中
- $score_goods = ScoreMallGoods::getOne([['goods_id' => $params['goods_id'], 'mall_id' => $this->mall_id]],true, ['attr' => function($query){
- $query->select("goods_id,goods_attr_id,score,money,deduction_limit,price");
- }],false, "goods_id,type,money,score");
- if (empty($score_goods)) return $this->error("该商品并非积分商品");
- // 根据商品id获取商品详情信息
- $goods = GoodsLogic::GetDetail($this->mall_id, $params, $this->user);
- if (empty($goods['attr'])) return $this->error('商品规格不存在或已删除');
- $goods['member_price'] > 0 && $goods['member_price'] = 0;
- $goods['original_price'] = $goods['price']; //积分商城商品划线价:修改成售价
- if ($params['addonsFields']) {
- $params['addonsFields'] = is_string($params['addonsFields']) ? explode(',', $params['addonsFields']) : $params['addonsFields'];
- $goods = Goods::getAddonsGoodsSetting($this->mall_id, $params['addonsFields'], $goods, 'detail');
- }
- // 获取积分配置
- $score_config = Yii::$app->services->setting->mall->get($this->mall_id, 'score');
- // 增加积分商品兑换价
- $score_info = ScoreMallGoods::getExchangePrice($score_goods['type'], $score_goods['attr'][0], $score_config, 1, $this->user_id ?? 0);
- $prefix = '兑换价:';
- $goods['score_deduct'] = $prefix . $score_info['exchange_price'];
- $goods['marketing_type'] = AddonsEnum::ADDONS_NAME_SCORE_MALL;
- // 商品的规格修改
- $score_attr = array_column($score_goods['attr'], null,'goods_attr_id');
- foreach ($goods['attr'] as &$attr) {
- $score_info = ScoreMallGoods::getExchangePrice($score_goods['type'], $score_attr[$attr['id']], $score_config, 1, $this->user_id ?? 0);
- $attr['score_deduct'] = $prefix . $score_info['exchange_price'];
- }
- return $this->success('获取成功', $goods);
- }
- /**
- * 兑换记录
- * @Author: lun
- * @DateTime: 2022/1/12 0012 18:58
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionExchangeLog()
- {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['page','limit'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['=', 'user_id', $this->user_id];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $order = 'created_at desc';
- $select = ['goods_id','goods','exchange_price','num','order_no','order_id','created_at'];
- $params = array('select' => $select, 'where' => $where, 'order' => $order, 'limit' => $params['limit']);
- $list = ScoreMallExchangeRecord::listPage($params, false, true);
- if (empty($list)) return $this->success('获取成功');
- return $this->success('获取成功', $list);
- }
- }
|