| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- <?php
- /*
- * @Descripttion: 推三返一控制器
- * @copyright: Copyright (c) 2021 广东七件事集团
- * @Author: lun
- * @Date: 2021-12-28 21:23:39
- */
- namespace addons\Rebate\api\modules\v1\controllers;
- use addons\Rebate\common\models\Rebate;
- use addons\Rebate\common\models\RebateBuy;
- use addons\Rebate\common\models\RebateRecommendNum;
- use addons\Rebate\common\models\RebateRewardLog;
- use addons\Rebate\common\models\RebateShare;
- use addons\Rebate\common\services\SettingService;
- use api\controllers\OnAuthController;
- use app\plugins\rebate\models\RebateAd;
- use common\enums\AddonsEnum;
- use common\enums\OrderStatusEnum;
- use common\enums\StatusEnum;
- use common\globals\GlobalInvoke;
- use common\logic\common\AdLogic;
- use common\logic\common\GoodsLogic;
- use common\models\order\Order;
- use common\models\user\User;
- use common\models\goods\Goods;
- use Yii;
- use yii\base\DynamicModel;
- use yii\db\Expression;
- use yii\helpers\Json;
- use function Clue\StreamFilter\fun;
- class IndexController extends OnAuthController
- {
- public $modelClass = '';
- public $enableCsrfValidation = false;
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['index','detail'];
- const SEARCH_TYPE_ALL = 'all';
- const SEARCH_TYPE_ACHIEVED = 'achieved';
- const SEARCH_TYPE_UNACHIEVED = 'unachieved';
- const SEARCH_TYPE_EXPIRED = 'expired';
- /**
- * 活动列表
- *
- */
- public function actionIndex()
- {
- $settingService = new SettingService(['mall_id' => $this->mall_id]);
- if (Yii::$app->request->isGet) {
- // $params = Yii::$app->request->get();
- // $time = time();
- // $where = [];
- // $where[] = ['=', 'mall_id', $this->mall_id];
- // $where[] = ['<=', 'start_at', $time];
- // $where[] = ['>=', 'end_at', $time];
- // $where[] = ['=', 'status', StatusEnum::ENABLED];
- // $order = 'created_at desc';
- // $select = "id,item_id,created_at";
- // $limit = $params['limit'] ?? 20;
- // $with = ['goods' => function($query){
- // $query->select("id,goods_name,price,cover_pic");
- // }];
- // $params = compact('where','order','select','with','limit');
- // $list = Rebate::listPage($params, false, true);
- //获取全部商品
- $all_goods_list = Rebate::getRebateAllGoodsId($this->mall_id);
- //用商品ID进行分页
- $goods_ids = array_column($all_goods_list, 'item_id');
- $goods_ids = array_unique($goods_ids);
- $limit = $params['limit'] ?? 20;
- $order = '';
- if ($goods_ids) {
- // 生成 FIELD() 排序条件
- $order = new \yii\db\Expression("FIELD(id, " . implode(',', $goods_ids) . ")");
- }
- $where = [];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['in', 'id', $goods_ids];
- $select = "id,goods_name,price,cover_pic";
- $params = compact('where','select', 'order','limit');
- $list = Goods::listPage($params, false, true);
- foreach ($list['list'] as &$item) {
- $item['goods'] = $item;
- }
- // 获取广告
- $ad = AdLogic::getList($this->mall_id, AddonsEnum::ADDONS_NAME_REBATE);
- // 获取插件名称
- $addons_name = $settingService->getSetting()['addons_name'];
- return $this->success('获取成功', compact('list','ad', 'addons_name'));
- }
- }
- /**
- * @Author: lun
- * @Date: 2022-01-01
- * @Time: 17:42
- * @Note:我的活动接口
- * @return \yii\web\Response
- */
- public function actionMyActivity()
- {
- if (Yii::$app->request->isGet) {
- $params = Yii::$app->request->get();
- $user_id = $this->user_id;
- $now_time = time();
- $where = [];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['=', 'user_id', $user_id];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $order = 'created_at desc';
- $select = "id,r_id,share_num,pay_num";
- $limit = $params['limit'] ?? 20;
- $with = ['activity' => function($query){
- $query->select("id,title,start_at,end_at,item_type,item_id,item_info,status,is_freeze_first_reward");
- }];
- $params = compact('where','order','select','with','limit');
- $list = RebateRecommendNum::listPage($params, false, true);
- if($list) {
- foreach($list['list'] as $key => &$item) {
- $item['goods'] = Json::decode($item['activity']['item_info'], true);
- $item['recommend_num'] = RebateBuy::GetRecommendNumCount($this->mall_id, $this->user_id, $item['r_id'], $item['order_no']);
- // 判断是否已结束
- $activity = $item['activity'];
- if($activity['status'] == 1 && $activity['end_at'] <= $now_time) $item['activity']['status'] = 2;
- }
- }
- return $this->success('获取成功', compact('list','now_time'));
- }
- }
- /**
- * @Author: lun
- * @Date: 2022-01-01
- * @Time: 17:42
- * @Note:邀请人数列表接口
- * @return \yii\web\Response
- */
- public function actionInviteNum()
- {
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- // 验证
- $res = Yii::$app->services->validate->validate($params, [
- [['r_id'], 'required'],
- [['r_id'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $user_id = $this->user_id;
- $where = [];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['=', 'r_id', $params['r_id']];
- $where[] = ['=', 'pid', $user_id];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $order = 'created_at desc';
- $select = "id,user_id,created_at";
- $limit = $params['limit'] ?? 20;
- // 关联查询用户
- $with = ['user' => function($query){
- $query->select("id,nickname,avatar_url");
- }];
- $params = compact('where','order','select','with','limit');
- $list = RebateShare::listPage($params, false, true);
- return $this->success('获取成功', $list);
- }
- }
- /**
- * @Author: lun
- * @Date: 2022-01-01
- * @Time: 17:42
- * @Note:推广下单数量列表接口
- * @return \yii\web\Response
- */
- public function actionPayNum()
- {
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- // 验证
- $res = Yii::$app->services->validate->validate($params, [
- [['r_id'], 'required'],
- [['r_id'], 'integer'],
- [['type'], 'string'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $user_id = $this->user_id;
- $where = [];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['=', 'r_id', $params['r_id']];
- $where[] = ['=', 'pid', $user_id];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $order = 'created_at desc';
- $select = "id,user_id,order_no,pay_at,pay_money,goods_info,order_settlement_status,refund_id";
- $limit = $params['limit'] ?? 20;
- if (!empty($params['type'])) {
- switch ($params['type']) {
- case self::SEARCH_TYPE_ACHIEVED:
- $order = sprintf('settlement_at desc, %s', $order);
- $where[] = ['=', 'order_settlement_status', StatusEnum::ENABLED];
- break;
- case self::SEARCH_TYPE_UNACHIEVED:
- $where[] = ['=', 'order_settlement_status', StatusEnum::DISABLED];
- $where[] = ['=', 'refund_id', StatusEnum::DISABLED];
- break;
- case self::SEARCH_TYPE_EXPIRED:
- $order = sprintf('refund_at desc, %s', $order);
- $where[] = ['=', 'order_settlement_status', StatusEnum::DISABLED];
- $where[] = ['>', 'refund_id', StatusEnum::DISABLED];
- break;
- default:
- break;
- }
- }
-
- // 关联查询用户
- $with = ['user' => function($query){
- $query->select("id,nickname,avatar_url");
- }];
- $params = compact('where','order','select','with','limit');
- $list = RebateBuy::listPage($params, false, true);
- $orderNoArr = array_column($list['list'], 'order_no');
- $orderList = Order::find()->where(['order_no' => $orderNoArr])->select('id,order_no,order_status')->asArray()->indexBy('order_no')->all();
- if($list['list']) {
- foreach ($list['list'] as $key => $item) {
- $list['list'][$key]['goods_info'] = Json::decode($item['goods_info'], true);
- // 兼容虚拟订单问题,虚拟订单状态当作已完成
- $list['list'][$key]['order'] = $orderList[$item['order_no']] ?? ['id' => 0, 'order_no' => $item['order_no'], 'order_status' => OrderStatusEnum::ACCOMPLISH];
- }
- }
- return $this->success('获取成功', $list);
- }
- }
- public function actionGetTabOption()
- {
- $rid = $this->request->get('rid', 0);
- if ($rid && is_numeric($rid)) {
- $select = "COUNT(*) AS order_total," .
- "COUNT(IF(order_settlement_status = 1, 1, NULL)) AS achieved_total," .
- "COUNT(IF(order_settlement_status = 0 AND refund_id = 0, 1, NULL)) AS unachieved_total," .
- "COUNT(IF(order_settlement_status = 0 AND refund_id > 0, 1, NULL)) AS expired_total";
- $params = [
- 'select' => new Expression($select),
- 'where' => [
- [
- 'mall_id' => $this->mall_id,
- 'status' => StatusEnum::ENABLED,
- 'r_id' => $rid,
- 'pid' => $this->user_id
- ]
- ]
- ];
- $find = RebateBuy::find();
- RebateBuy::paramPack($params, $find);
- $statistics = $find->asArray()->one();
- }
- $this->success('success', [
- [
- 'name' => sprintf('全部(%d)', $statistics['order_total'] ?? 0),
- 'search_type' => self::SEARCH_TYPE_ALL,
- ],
- [
- 'name' => sprintf('已达标(%d)', $statistics['achieved_total'] ?? 0),
- 'search_type' => self::SEARCH_TYPE_ACHIEVED,
- ],
- [
- 'name' => sprintf('未达标(%d)', $statistics['unachieved_total'] ?? 0),
- 'search_type' => self::SEARCH_TYPE_UNACHIEVED,
- ],
- [
- 'name' => sprintf('已失效(%d)', $statistics['expired_total'] ?? 0),
- 'search_type' => self::SEARCH_TYPE_EXPIRED,
- ],
- ]);
- }
- /**
- * 商品详情
- * @Author: lun
- * @DateTime: 2022/1/18 0018 19:20
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionDetail()
- {
- $params = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['goods_id','pid'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 判断是否是分享
- $parent_id = $this->user['parent_id'] ?? 0;
- $headers = Yii::$app->request->headers;
- if (!empty($params['pid']) && $parent_id == $headers['x-parent-id']) {
- // 判断是否符合分享条件,并保存数据
- RebateShare::checkShare($this->mall_id, $params['goods_id'], $this->user_id, $parent_id);
- }
- // 根据商品id获取商品详情信息
- $goods = GoodsLogic::GetDetail($this->mall_id, $params, $this->user);
- if ($goods == false) return $this->error("获取失败," . GoodsLogic::getStaticError());
- // 智能表单
- $smart_form_template = GlobalInvoke::exec(GlobalInvoke::GOODS_NEED_SMARTFORM,
- $this->mall_id, ['user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'goods_id' => $params['goods_id']]);
- if (!empty($smart_form_template)) $goods = array_merge($goods, $smart_form_template);
- // 商品详情公共调用
- $invoke_data = GlobalInvoke::exec(GlobalInvoke::API_GOODS_DETAIL, $this->mall_id,
- ['mall_id' => $this->mall_id, 'goods' => $goods, 'user' => $this->user]);
- if (!empty($invoke_data['goods'])) $goods = $invoke_data['goods'];
- return $this->success('获取成功', $goods);
- }
- /**
- * 活动规则接口
- *
- * @return void
- */
- public function actionExplain()
- {
- $settingService = new SettingService(['mall_id' => $this->mall_id]);
- if (Yii::$app->request->isGet) {
- $explain = $settingService->getSetting()['explain'];
- return $this->success('获取成功', $explain);
- }
- }
- /**
- * 奖励明细列表
- *
- * @return void
- */
- public function actionRewardLog()
- {
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['r_id'], 'required'],
- [['r_id'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $user_id = $this->user_id;
- $where = [];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['=', 'r_id', $params['r_id']];
- $where[] = ['=', 'user_id', $user_id];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $order = 'created_at desc';
- $select = "reward_title,content,created_at,is_frozen";
- $limit = $params['limit'] ?? 20;
- $params = compact('where','order','select','limit');
- $list = RebateRewardLog::listPage($params, false, true);
- return $this->success('获取成功', $list);
- }
- }
- }
|