| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace addons\DoubleCopy\backend\controllers;
- use common\enums\StatusEnum;
- use addons\DoubleCopy\common\models\DoubleCopySpread;
- use addons\DoubleCopy\common\models\DoubleCopyUser;
- use Yii;
- use common\models\user\User;
- use addons\DoubleCopy\service\SpreadService as Spread;
- use common\enums\RedisKeyEnum;
- use common\helpers\LockHelper;
- use yii\db\Exception;
- /**
- * Class SettingController
- * @package addons\BossBonus\backend\controllers
- */
- class SpreadController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * @return mixed|string|void
- */
- public function actionIndex()
- {
- $retuest = Yii::$app->request;
- if ($retuest->isAjax && $retuest->isGet) {
- // 检查提交的参数
- $get = $retuest->get();
- $res = Yii::$app->services->validate->validate($get,[
- [['page','limit','pull_user_id','pull_mobile','push_user_id','push_mobile'], 'integer'],
- [['start_at','end_at'], 'safe'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
-
- $where = $list = [];
- $where[] = ['=', 'mall_id', $this->mall_id];
- !empty($get['pull_user_id']) && $where[] = ['=', 'pull_user_id', $get['pull_user_id']];
- if (!empty($get['pull_mobile'])) {
- $user_id = User::find()->select('id')->where(['mall_id' => $this->mall_id, 'mobile' => $get['pull_mobile'], 'status' => StatusEnum::ENABLED])->scalar() ?? 0;
- if (empty($user_id)) $this->success('获取成功',compact('list'));
- $where[] = ['=', 'pull_user_id', $user_id];
- }
- !empty($get['push_user_id']) && $where[] = ['=', 'push_user_id', $get['push_user_id']];
- if (!empty($get['push_mobile'])) {
- $user_id = User::find()->select('id')->where(['mall_id' => $this->mall_id, 'mobile' => $get['push_mobile'], 'status' => StatusEnum::ENABLED])->scalar() ?? 0;
- if (empty($user_id)) $this->success('获取成功',compact('list'));
- $where[] = ['=', 'push_user_id', $user_id];
- }
- if (!empty($get['start_at']) && !empty($get['end_at'])) {
- $where[] = ['between', 'created_at', strtotime($get['start_at']), strtotime($get['end_at'])];
- }
- $with = ['pullUser','pushUser','originalUser'];
- $order = 'id desc';
- $params = array('where' => $where, 'order' => $order, 'with' => $with, 'limit' => $get['limit']);
- $list = DoubleCopySpread::listPage($params, false, true);
- if (empty($list)) return $this->error('获取失败');
- return $this->success('获取成功', compact('list'));
- }
- return $this->render('index');
- }
- public function actionEdit()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_DOUBLE_SPREAD_ATTR, $this->mall_id);
- $identification = uniqid();
- if (!LockHelper::lock($lock_key, $identification, 30, 60)) return $this->error('当前访问人数过多');
- $postData = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($postData, [
- [['push_user_id', 'pull_user_id'], 'required'],
- [['push_user_id', 'pull_user_id'], 'integer'],
- ]);
- try {
- $transaction = Yii::$app->db->beginTransaction();
- if ($res === false) throw new Exception(Yii::$app->services->validate->getErrorMessage());
- $push_user = DoubleCopyUser::find()->select('id,user_id,pid')->where(['mall_id'=>$this->mall_id,'user_id'=>$postData['push_user_id'],'status' => StatusEnum::ENABLED])->asArray()->one();
- if (!$push_user) throw new Exception('绑定上级用户不存在');
- $pull_user = DoubleCopyUser::find()->select('id,user_id,pid')->where(['mall_id'=>$this->mall_id,'user_id'=>$postData['pull_user_id'],'status' => StatusEnum::ENABLED])->asArray()->one();
- if (!$pull_user) throw new Exception('选择下级用户不存在');
- if($pull_user['pid']==$push_user['user_id']) throw new Exception('输入要绑定的下级已经是要上级的用户');
-
- if(!Spread::valPidIsMall($postData['pull_user_id'],$postData['push_user_id'])) throw new Exception('只可绑定主商城的关系链!');
- $postData['mall_id'] = $this->mall_id;
- Spread::bindingHandle($postData['pull_user_id'],$postData['push_user_id'],$this->mall_id,$this->admin);
- $transaction->commit();
- LockHelper::uLock($lock_key, $identification);
- return $this->success('操作成功');
- } catch (\Exception $e) {
- // 发生异常时回滚事务
- $transaction->rollBack();
- LockHelper::uLock($lock_key, $identification);
- return $this->error($e->getMessage());
- }
- }
- }
- return $this->error();
- }
- public function actionEditPlatform()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_DOUBLE_SPREAD_ATTR, $this->mall_id);
- $identification = uniqid();
- if (!LockHelper::lock($lock_key, $identification, 30, 60)) return $this->error('当前访问人数过多');
- $postData = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($postData, [
- [['pull_user_id'], 'required'],
- [['pull_user_id'], 'integer'],
- ]);
- try {
- $transaction = Yii::$app->db->beginTransaction();
- if ($res === false) throw new Exception(Yii::$app->services->validate->getErrorMessage());
- $pull_user = DoubleCopyUser::find()->select('id,user_id,pid')->where(['mall_id'=>$this->mall_id,'user_id'=>$postData['pull_user_id'],'status' => StatusEnum::ENABLED])->asArray()->one();
- if (!$pull_user) throw new Exception('选择下级用户不存在');
- $postData['mall_id'] = $this->mall_id;
- Spread::bindingHandlePlatform($postData['pull_user_id'],$this->mall_id,$this->admin);
- $transaction->commit();
- LockHelper::uLock($lock_key, $identification);
- return $this->success('操作成功');
- } catch (\Exception $e) {
- // 发生异常时回滚事务
- $transaction->rollBack();
- LockHelper::uLock($lock_key, $identification);
- return $this->error($e->getMessage());
- }
- }
- }
- return $this->error();
- }
- }
|