SpreadController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace addons\DoubleCopy\backend\controllers;
  3. use common\enums\StatusEnum;
  4. use addons\DoubleCopy\common\models\DoubleCopySpread;
  5. use addons\DoubleCopy\common\models\DoubleCopyUser;
  6. use Yii;
  7. use common\models\user\User;
  8. use addons\DoubleCopy\service\SpreadService as Spread;
  9. use common\enums\RedisKeyEnum;
  10. use common\helpers\LockHelper;
  11. use yii\db\Exception;
  12. /**
  13. * Class SettingController
  14. * @package addons\BossBonus\backend\controllers
  15. */
  16. class SpreadController extends BaseController
  17. {
  18. public $enableCsrfValidation = false;
  19. /**
  20. * @return mixed|string|void
  21. */
  22. public function actionIndex()
  23. {
  24. $retuest = Yii::$app->request;
  25. if ($retuest->isAjax && $retuest->isGet) {
  26. // 检查提交的参数
  27. $get = $retuest->get();
  28. $res = Yii::$app->services->validate->validate($get,[
  29. [['page','limit','pull_user_id','pull_mobile','push_user_id','push_mobile'], 'integer'],
  30. [['start_at','end_at'], 'safe'],
  31. ]);
  32. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  33. $where = $list = [];
  34. $where[] = ['=', 'mall_id', $this->mall_id];
  35. !empty($get['pull_user_id']) && $where[] = ['=', 'pull_user_id', $get['pull_user_id']];
  36. if (!empty($get['pull_mobile'])) {
  37. $user_id = User::find()->select('id')->where(['mall_id' => $this->mall_id, 'mobile' => $get['pull_mobile'], 'status' => StatusEnum::ENABLED])->scalar() ?? 0;
  38. if (empty($user_id)) $this->success('获取成功',compact('list'));
  39. $where[] = ['=', 'pull_user_id', $user_id];
  40. }
  41. !empty($get['push_user_id']) && $where[] = ['=', 'push_user_id', $get['push_user_id']];
  42. if (!empty($get['push_mobile'])) {
  43. $user_id = User::find()->select('id')->where(['mall_id' => $this->mall_id, 'mobile' => $get['push_mobile'], 'status' => StatusEnum::ENABLED])->scalar() ?? 0;
  44. if (empty($user_id)) $this->success('获取成功',compact('list'));
  45. $where[] = ['=', 'push_user_id', $user_id];
  46. }
  47. if (!empty($get['start_at']) && !empty($get['end_at'])) {
  48. $where[] = ['between', 'created_at', strtotime($get['start_at']), strtotime($get['end_at'])];
  49. }
  50. $with = ['pullUser','pushUser','originalUser'];
  51. $order = 'id desc';
  52. $params = array('where' => $where, 'order' => $order, 'with' => $with, 'limit' => $get['limit']);
  53. $list = DoubleCopySpread::listPage($params, false, true);
  54. if (empty($list)) return $this->error('获取失败');
  55. return $this->success('获取成功', compact('list'));
  56. }
  57. return $this->render('index');
  58. }
  59. public function actionEdit()
  60. {
  61. if (\Yii::$app->request->isAjax) {
  62. if (\Yii::$app->request->isPost) {
  63. $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_DOUBLE_SPREAD_ATTR, $this->mall_id);
  64. $identification = uniqid();
  65. if (!LockHelper::lock($lock_key, $identification, 30, 60)) return $this->error('当前访问人数过多');
  66. $postData = \Yii::$app->request->post();
  67. $res = Yii::$app->services->validate->validate($postData, [
  68. [['push_user_id', 'pull_user_id'], 'required'],
  69. [['push_user_id', 'pull_user_id'], 'integer'],
  70. ]);
  71. try {
  72. $transaction = Yii::$app->db->beginTransaction();
  73. if ($res === false) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  74. $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();
  75. if (!$push_user) throw new Exception('绑定上级用户不存在');
  76. $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();
  77. if (!$pull_user) throw new Exception('选择下级用户不存在');
  78. if($pull_user['pid']==$push_user['user_id']) throw new Exception('输入要绑定的下级已经是要上级的用户');
  79. if(!Spread::valPidIsMall($postData['pull_user_id'],$postData['push_user_id'])) throw new Exception('只可绑定主商城的关系链!');
  80. $postData['mall_id'] = $this->mall_id;
  81. Spread::bindingHandle($postData['pull_user_id'],$postData['push_user_id'],$this->mall_id,$this->admin);
  82. $transaction->commit();
  83. LockHelper::uLock($lock_key, $identification);
  84. return $this->success('操作成功');
  85. } catch (\Exception $e) {
  86. // 发生异常时回滚事务
  87. $transaction->rollBack();
  88. LockHelper::uLock($lock_key, $identification);
  89. return $this->error($e->getMessage());
  90. }
  91. }
  92. }
  93. return $this->error();
  94. }
  95. public function actionEditPlatform()
  96. {
  97. if (\Yii::$app->request->isAjax) {
  98. if (\Yii::$app->request->isPost) {
  99. $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_DOUBLE_SPREAD_ATTR, $this->mall_id);
  100. $identification = uniqid();
  101. if (!LockHelper::lock($lock_key, $identification, 30, 60)) return $this->error('当前访问人数过多');
  102. $postData = \Yii::$app->request->post();
  103. $res = Yii::$app->services->validate->validate($postData, [
  104. [['pull_user_id'], 'required'],
  105. [['pull_user_id'], 'integer'],
  106. ]);
  107. try {
  108. $transaction = Yii::$app->db->beginTransaction();
  109. if ($res === false) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  110. $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();
  111. if (!$pull_user) throw new Exception('选择下级用户不存在');
  112. $postData['mall_id'] = $this->mall_id;
  113. Spread::bindingHandlePlatform($postData['pull_user_id'],$this->mall_id,$this->admin);
  114. $transaction->commit();
  115. LockHelper::uLock($lock_key, $identification);
  116. return $this->success('操作成功');
  117. } catch (\Exception $e) {
  118. // 发生异常时回滚事务
  119. $transaction->rollBack();
  120. LockHelper::uLock($lock_key, $identification);
  121. return $this->error($e->getMessage());
  122. }
  123. }
  124. }
  125. return $this->error();
  126. }
  127. }