_id)) { $eventParams[] = [ 'log_id' => $logObj->_id, ]; } } // 新用户注册时,也算一次登录. self::login($data); // 触发事件. event(new InnerUserNewIncEvent($eventParams)); return true; } /** * 登录 * @param array $data * @return bool */ public function login(array $data) { // 丢job构造函数时,必须精简数据. $jobData = []; if (!empty($data)) { foreach ($data as $item) { $jobData[] = [ 'agent_uid' => $item['agent_uid'], 'channel_uid' => $item['channel_uid'], 'created_at' => $item['created_at'], 'device_type' => $item['device_type'], 'mom_app_uid' => $item['mom_app_uid'], 'user_uid' => $item['user_uid'], ]; } } if (!empty($jobData)) { dispatch(new UserHyperLogLogJob($jobData)); } return true; } /** * 随机扣量标记. cpa扣量 * @param array $item * @return array */ public function oneRandDeduction(array $item) { $item['is_deduction'] = 0; //获取代理扣量配置 $deductionConfig = app(ConfigRepoistory::class)->pickByKeys(['preferential_user', 'cpa_deduction_switch'], 'sys'); if ($deductionConfig['cpa_deduction_switch']['svalue'] == 0) { return $item; } $iTodayRegUser = UsersModel::where('channel_uid', $item['channel_uid'])->count(); //每日前几单不扣 if ($iTodayRegUser < $deductionConfig['preferential_user']['svalue']) { return $item; } $agentInfo = app(AgentRepository::class)->findBy('uid', $item['agent_uid'], ['deduction_switch', 'cpa_deduction_rule']); if ($agentInfo->deduction_switch == 2 && !empty($agentInfo->cpa_deduction_rule)) { $item['is_deduction'] = $this->isDeduction($item, $agentInfo->cpa_deduction_rule); } else if ($agentInfo->deduction_switch == 0 || $agentInfo->deduction_switch == 3) { //获取扣量规则 $rulesInfo = app(AppDeductionRuleRepository::class)->findByWhere(['app_uid' => $item['mom_app_uid'], 'deduction_type' => 'cpa'], ['deduction_pro']); if (empty($rulesInfo)) { return $item; } if ($agentInfo->deduction_switch == 3) { $method = 3; } else { $method = 1; } //超过每日设定的默认单就开始扣量 $item['is_deduction'] = $this->isDeduction($item, $rulesInfo->deduction_pro, $method); } else { $item['is_deduction'] = 0; } return $item; } /** * 扣量 * * @param $item * @param $deductionPro * @param $method * @return int|mixed */ private function isDeduction($item, $deductionPro, $method = '') { $isDeduction = 0; //设置扣量参数 $ratio = $deductionPro; $ratio = explode('-',$ratio); if ($ratio[1] == 0) { return $isDeduction; } $hashKey = RedisKeysConstant::DEDUCTION_RULES_HASH_CPA . $item['mom_app_uid'] . $item['channel_uid'] . $method; //扣量标记 $areaKey = $item['channel_uid']; $deductionMark = Redis::hget($hashKey, $areaKey); $mark = [0,0]; if ($deductionMark) { $mark = explode('-', $deductionMark); if ($mark[0] == $ratio[0] && $mark[1] == $ratio[1]) { Redis::hDel($hashKey, $areaKey); $mark = [0,0]; } //如果扣量达到,但是一轮还没结束则直接返回 if ($mark[1] == $ratio[1]) { // $mark[0] + 1 参与订单总数加1 Redis::hset($hashKey, $areaKey, $mark[0] + 1 . '-' . $mark[1]); return $isDeduction; } } //必扣计算 if (($ratio[0] - $mark[0]) <= ($ratio[1] - $mark[1])) { $isDeduction = 1; } else { $isDeduction = mt_rand(0, 1); } if ($isDeduction == 1) { $mark[1] += 1; } $mark[0] += 1; Redis::hset($hashKey, $areaKey, $mark[0] . '-' . $mark[1]); return $isDeduction; } }