| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- <?php
- namespace addons\Distribution\api\modules\v1\controllers;
- use addons\Distribution\common\enums\DistributionEnum;
- use addons\Distribution\common\models\DistributionApply;
- use addons\Distribution\common\models\DistributionLevel;
- use addons\Distribution\common\models\Distribution;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\models\common\CapitalLog;
- use common\models\common\GlobalVarMap;
- use common\models\order\OrderDetail;
- use common\models\user\User;
- use Yii;
- use api\controllers\OnAuthController;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\Distribution\api\modules\v1\controllers
- */
- class DefaultController extends OnAuthController
- {
- public $modelClass = '';
- public $from_type = ['Distribution'];
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['index'];
- /**
- * 首页
- *
- * @return string
- */
- public function actionIndex()
- {
- return 'Hello world';
- }
- /**
- * @desc:经销商详情
- * @Author: hua
- * @Date: 2021/11/4
- * @Time: 16:47
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionInfos()
- {
- $data['user_id'] = $this->user_id;
- $data['nickname'] = empty($this->user->nickname) ? '' : $this->user->nickname;
- $data['avatar_url'] = empty($this->user->avatar_url) ? '' : $this->user->avatar_url;
- $distribution = Distribution::getOne([['user_id' => $this->user_id],['status'=>StatusEnum::ENABLED],['mall_id'=>$this->mall_id]], true, ['distributionLevel']);
- if(empty($distribution)){
- $res = DistributionApply::getOne([['user_id' => $this->user_id],['status'=>StatusEnum::ENABLED],['mall_id'=>$this->mall_id]],true,[],'id desc');
- $setting = \Yii::$app->services->setting->addons->get($this->mall_id, DistributionEnum::ADDONS_NAME, 'basic');
- if(!empty($res)){
- if($res['check_status']==0)return $this->error('',$result = ['is_apply'=>2,'msg'=>'您已经申请了,请等待审核','is_distribution'=>0]);
- if($res['check_status']==2)return $this->error('',$result = ['is_apply'=>3,'protocol'=>$setting['protocol'],'msg'=>$res['check_remark'],'is_distribution'=>0]);
- }
- $result = ['is_apply'=>$setting['is_apply'],'protocol'=>$setting['protocol'],'is_distribution'=>0];
- return $this->error('您不是分销商', $result);
- }
- $data['parent_name'] = '平台方';
- $data['parent_avatar_url'] = '';
- $data['parent_level_name'] = '';
- $parent_id = $this->user->parent_id??0;
- if($parent_id){
- $parent = User::findOne($parent_id);
- $data['parent_name']= $parent['nickname']??'';
- $data['parent_avatar_url']= $parent['avatar_url']??'';
- $parent_distribution = Distribution::findOne(['user_id'=>$parent_id]);
- $data['parent_level_name'] = $parent_distribution->distributionLevel->name??'默认等级';
- }
- $data['is_distribution'] = 0;
- $data['level_name'] = '';
- $data['frozen_price'] = 0;
- $data['total_price'] = 0;
- $data['total_income'] = 0;
- $data['yesterday_price'] = 0;
- if ($distribution) {
- $data['is_distribution'] = 1;
- $data['level_name'] = $distribution['distributionLevel']['name'] ?? '默认等级';
- $wallet_type = 'commission';
- $yesterday = date('Y-m-d', strtotime('-1 day'));
- $start_time = "{$yesterday} 00:00:00";
- $end_time = "{$yesterday} 23:59:59";
- CapitalLog::$capitalType = "{$wallet_type}_frozen";
- $where = ['user_id' => $this->user_id, 'from_type' => DistributionEnum::ADDONS_NAME, 'status' => CapitalLog::STATUS_WAIT_SEND];
- $query = CapitalLog::find();
- $frozen_price = $query->where($where)->sum('change_num');
- $yesterday_frozen_price = $query->where($where)
- ->andWhere(['<=', 'created_at', $end_time])
- ->andWhere(['>=', 'created_at', $start_time])
- ->sum('change_num');
- if (empty($yesterday_frozen_price)) $yesterday_frozen_price = 0;
- CapitalLog::$capitalType = "{$wallet_type}";
- $where = ['user_id' => $this->user_id, 'from_type' => DistributionEnum::ADDONS_NAME];
- $query = CapitalLog::find();
- $total_price = $query->where($where)->sum('change_num');
- $yesterday_total_price = $query->where($where)
- ->andWhere(['<=', 'created_at', $end_time])
- ->andWhere(['>=', 'created_at', $start_time])
- ->sum('change_num');
- if (empty($yesterday_total_price)) $yesterday_total_price = 0;
- $data['total_price'] = empty($total_price) ? 0 : $total_price;
- $data['frozen_price'] = empty($frozen_price) ? 0 : $frozen_price;
- $data['total_income'] = bcadd($data['total_price'] , $data['frozen_price'],2);
- $data['yesterday_price'] = $yesterday_total_price + $yesterday_frozen_price;
- }
- $is_show_share_level = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_DISTRIBUTION, 'basic.is_show_share_level');
- $data['is_show_share_level'] = $is_show_share_level??0;
- return $this->success('操作成功', $data);
- }
- public function actionInfo()
- {
- $data = Distribution::distributionInfo($this->user,$this->mall_id);
- if(!empty($data['msg'])) return $this->error($data['msg'],$data['result']);
- return $this->success('操作成功', $data);
- }
- /**
- * @desc:奖励列表
- * @Author: hua
- * @Date: 2021/11/4
- * @Time: 17:00
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionLogList()
- {
- $params = \Yii::$app->request->get();
- // $where[] = ['mall_id' => $this->mall_id];
- // $where[] = ['user_id' => $this->user_id];
- // $where[] = ['from_type' => DistributionEnum::ADDONS_NAME];
- // $params['order'] = 'id desc';
- // $params['where'] = $where;
- // $params['select'] = 'id,change_num,source_table,source_table_id,from_type,capital_type';
- // $wallet_type = 'commission';
- // CapitalLog::$capitalType = "{$wallet_type}";
- // $list = CapitalLog::listPage($params);
- // if (!empty($list)) {
- // $source_table_id_arr_by_order_detail = [];
- // foreach ($list['list'] as $item) {
- // if ($item['source_table'] == 'order_detail') {
- // $source_table_id_arr_by_order_detail[] = $item['source_table_id'];
- // }
- // }
- // //商品订单的团队佣金
- // if ($source_table_id_arr_by_order_detail) {
- // $order_detail_arr = [];
- // $params = [];
- // $params['where'] = [['id' => $source_table_id_arr_by_order_detail]];
- // $params['with'] = ['user' => function ($query) {
- // $query->select('id,nickname,avatar_url');
- // }, 'order' => function ($query) {
- // $query->select('id,order_no');
- // }];
- // $order_detail_list = OrderDetail::lists($params);
- // foreach ($order_detail_list as $value) {
- // $order_detail_arr[$value['id']] = [
- // 'goods_name' => $value['goods_name'],
- // 'goods_price' => $value['goods_price'],
- // 'created_at' => date('Y-m-d H:i:s', $value['created_at']),
- // 'order_no' => $value['order']['order_no'],
- // 'user_id' => $value['user']['id'],
- // 'nickname' => $value['user']['nickname'],
- // 'avatar_url' => $value['user']['avatar_url'],
- // ];
- // }
- // }
- //
- //
- // foreach ($list['list'] as &$item) {
- // if ($item['source_table'] == 'order_detail') {
- // $item['user_id'] = '';
- // $item['nickname'] = '';
- // $item['avatar_url'] = '';
- // $item['order_no'] = '';
- // $item['created_at'] = '';
- // $item['goods_price'] = '';
- // $item['goods_name'] = '';
- // if (!empty($order_detail_arr[$item['source_table_id']])) {
- // $item['user_id'] = $order_detail_arr[$item['source_table_id']]['user_id'];
- // $item['nickname'] = $order_detail_arr[$item['source_table_id']]['nickname'];
- // $item['avatar_url'] = $order_detail_arr[$item['source_table_id']]['avatar_url'];
- // $item['order_no'] = $order_detail_arr[$item['source_table_id']]['order_no'];
- // $item['created_at'] = $order_detail_arr[$item['source_table_id']]['created_at'];
- // $item['goods_price'] = $order_detail_arr[$item['source_table_id']]['goods_price'];
- // $item['goods_name'] = $order_detail_arr[$item['source_table_id']]['goods_name'];
- // }
- // }
- // }
- // }
- //新增查询未结算记录
- if($params['list_type'] ==1){//未结算
- $wallet_type = 'commission_frozen';
- $params['status'] = StatusEnum::DISABLED;
- }else{
- $wallet_type = 'commission';
- }
- $params['mall_id'] = $this->mall_id;
- $params['user_id'] = $this->user_id;
- $list = CapitalLog::getCapitalLogList($params, $wallet_type, DistributionEnum::ADDONS_NAME);
- $list['map'] = DistributionEnum::getMap();
- if (!empty(CapitalLog::getStaticError())) return $this->error(CapitalLog::getStaticError());
- if (!empty($list['list'])) {
- $commission_map = GlobalVarMap::getEnumMapByType('FromTypeMap');
- $capital_type_map = GlobalVarMap::getEnumMapByType('CapitalTypeMap');
- foreach ($list['list'] as &$item) {
- $item['source_text'] = ($commission_map[$item['from_type']] ?? '').($capital_type_map[$item['capital_type']]
- ?'-'.$capital_type_map[$item['capital_type']]:'');
- }
- }
- return $this->success('操作成功', $list);
- }
- /**
- * @desc:等级列表
- * @Author: hua
- * @Date: 2021/11/5
- * @Time: 16:20
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionLevelList(){
- $params = [];
- $params['where'] = [['mall_id'=>$this->mall_id],['status'=>StatusEnum::ENABLED]];
- $params['select'] = 'id,name,explain';
- $list = DistributionLevel::lists($params);
- return $this->success('操作成功', $list);
- }
- /**
- * @desc:申请接口
- * @Author: hua
- * @Date: 2021/11/25
- * @Time: 16:39
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionApply(){
- $mall_id = $this->mall_id;
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['name', 'mobile'], 'required'],
- [['remark'], 'safe'],
- ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $mall_id;
- $params['user_id'] = $this->user_id;
- if (empty($this->user)) return $this->error('不存在此会员');
- $distribution = Distribution::getOne([['user_id' => $this->user_id], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]], ['mall_id' => $this->mall_id]], true);
- if (!empty($distribution)) return $this->error('您已经是分销商');
- $distribution_apply = DistributionApply::getOne([['user_id' => $this->user_id], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]], ['mall_id' => $this->mall_id]], true, [], 'id desc');
- if (!empty($distribution_apply) && empty($distribution_apply['check_status'])) return $this->error('您已经提交申请,请等待客服审核');
- $setting = \Yii::$app->services->setting->addons->get($this->mall_id, DistributionEnum::ADDONS_NAME, 'basic');
- if (empty($setting['is_check'])) $params['check_status'] = 1; //不需要审核
- $res = DistributionApply::setData($params);
- if ($res === false) return $this->error(DistributionApply::getStaticError());
- return $this->success('操作成功');
- }
- if (\Yii::$app->request->isGet) {
- $params['mall_id'] = $mall_id;
- $params['user_id'] = $this->user_id;
- if (empty($this->user)) return $this->error('不存在此会员');
- $distribution_apply = DistributionApply::getOne([['user_id' => $this->user_id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ['mall_id' => $this->mall_id]], true, [], 'id desc','id,user_id,check_status,check_remark');
- return $this->success('操作成功',$distribution_apply);
- }
- }
- }
|