ActivityConstants::RESOURCE_POP_RANGE_RECENT, 'index_index' => ActivityConstants::RESOURCE_POP_RANGE_MALE, 'index_index_girl' => ActivityConstants::RESOURCE_POP_RANGE_FEMALE, 'index_index_boy' => ActivityConstants::RESOURCE_POP_RANGE_MALE, 'user_index' => ActivityConstants::RESOURCE_POP_RANGE_PERSONAL, 'category_index' => ActivityConstants::RESOURCE_POP_RANGE_CATEGORY, 'book_list_free' => ActivityConstants::RESOURCE_POP_RANGE_FREE, 'book_list_finish' => ActivityConstants::RESOURCE_POP_RANGE_COMPLETE, ]; $type = ArrayHelper::array_get($_GET, 'type', ''); if($type){ $key = implode('_', [$controllerName, $actionName, $type]); }else{ $key = implode('_', [$controllerName, $actionName]); } $pop_range = ArrayHelper::array_get($relation, $key, ActivityConstants::RESOURCE_POP_RANGE_RECENT); $time = Request::instance()->server('REQUEST_TIME'); $user = UserService::instance(); $show_type = []; //商品资源展示对象 if ($user->is_pay == UserConstants::USER_IS_PAY_NO) { $show_type['goods']['pay_type'] = ActivityConstants::GOODS_SHOW_TYPE_PAY_NO; //未付费 } else { $show_type['goods']['pay_type'] = ActivityConstants::GOODS_SHOW_TYPE_PAY_YES; //普通付费 } //用户类别 if ($user->createtime < strtotime(date("Ymd"))) { //老用户 $show_type['goods']['register_type'] = ActivityConstants::GOODS_SHOW_TYPE_USER_OLD; } else { //新用户 $show_type['goods']['register_type'] = ActivityConstants::GOODS_SHOW_TYPE_USER_NEW; } //活动资源展示对象 if ($user->is_pay == UserConstants::USER_IS_PAY_NO) { $show_type['resource']['pay_type'] = ActivityConstants::RESOURCE_PAY_NOT; //未付费 } else { $show_type['resource']['pay_type'] = ActivityConstants::RESOURCE_PAY_NORMAL; //普通付费 } //年费会员 if ($user->vip_endtime > $time) { $show_type['resource']['pay_type'] = ActivityConstants::RESOURCE_PAY_YEAR; //年费会员 } $activityRes = $this->getActivity($pop_range, $show_type, $channel_id, $user_id); if ($activityRes) { return $activityRes; } return null; } /** * 获取活动的资源,如果能关联到活动商品,以商品的展示对象为准;如果不能,则以资源的展示对象为准 * @param $aid 活动id * @return array 活动对象+是否超出活动范围(1超出;0正常展示) */ public function getActivityResource($aid) { $goodsShowTypes = $this->_makeGoodsShowType();//通过当前用户登录信息确定"展示对象" $arRedisKey = 'ARK:1:'.md5(json_encode($goodsShowTypes)).":".$aid; $arRedisData = Redis::instance()->get($arRedisKey); if ($arRedisData === false) { $resource = $this->getResourceModel()->join(['goods' => 'g'], 'g.id=resource.goods_id') ->field(['resource.id']) ->whereIn('g.show_type', $goodsShowTypes) ->where('activity_id', $aid) ->order('resource.weigh', 'desc') ->select(); if (!empty($resource)) { $result = [current($resource), 0]; Redis::instance()->set($arRedisKey, json_encode($result), 60); } else { $resourceShowTypes = $this->_makeResourceShowType(); $arRedisKey2 = 'ARK:2:'.md5(json_encode($resourceShowTypes)).":".$aid; $arRedisData2 = Redis::instance()->get($arRedisKey2); if ($arRedisData2 === false) { $resource = $this->getResourceModel()->field(['resource.id']) ->whereIn('show_type', $resourceShowTypes) ->where('activity_id', $aid) ->order('weigh', 'desc') ->select(); if (!empty($resource)) { $result = [current($resource), 0]; Redis::instance()->set($arRedisKey2, json_encode($result), 60); } else { $arRedisKey3 = 'ARK:3:'.":".$aid; $arRedisData3 = Redis::instance()->get($arRedisKey3); if ($arRedisData3 === false) { $resource = $this->getResourceModel()->where('activity_id', $aid) ->find(); $result = [$resource, 1]; Redis::instance()->set($arRedisKey3, json_encode($result), 60); } else { $result = json_decode($arRedisData3, true); } } } else { $result = json_decode($arRedisData2, true); } } } else { $result = json_decode($arRedisData, true); } return $result; } /** * 构造商品的显示范围 * @return array */ private function _makeGoodsShowType() { $user = UserService::instance()->getUserInfo(); $showType = $user->is_pay == UserConstants::USER_IS_PAY_NO ? PayConstants::GOODS_SHOW_TYPE_UN_RECHARGED : PayConstants::GOODS_SHOW_TYPE_RECHARGED; return [PayConstants::GOODS_SHOW_TYPE_ALL, $showType]; } /** * 构造资源的显示范围 * @return array */ private function _makeResourceShowType() { $time = Request::instance()->server('REQUEST_TIME'); $user = UserService::instance()->getUserInfo(); if ($user->is_pay == UserConstants::USER_IS_PAY_NO) { $showType = ActivityConstants::RESOURCE_PAY_NOT; //未付费 } else { $showType = ActivityConstants::RESOURCE_PAY_NORMAL; //普通付费 } if ($user->vip_endtime > $time) { $showType = ActivityConstants::RESOURCE_PAY_YEAR; //年费会员 } return [ActivityConstants::RESOURCE_ALL, $showType]; } public function getActivity($pop_range, $show_type, $channel_id, $user_id) { if ($channel_id) { $group_id = model('AuthGroupAccess')->getGroupId($channel_id); $ex_info = model('AdminExtend')->getInfo($channel_id); //-------------------渠道自定义活动 //平台没有活动时,检查渠道的活动 if (AdminConstants::ADMIN_GROUP_ID_CHANNEL == $group_id) { if ($res = $this->getChannelActivity($pop_range, $show_type['goods'], $channel_id, $user_id)) { return $res; } } //检查配号代理商 if (AdminConstants::ADMIN_GROUP_ID_AGENT == $group_id && intval($ex_info['distribute'])) { //渠道有活动返回渠道的 if ($res = $this->getChannelActivity($pop_range, $show_type['goods'], $ex_info['create_by'], $user_id)) { return $res; } //检查配号代理商 if ($res = $this->getChannelActivity($pop_range, $show_type['goods'], $channel_id, $user_id)) { return $res; } } //渠道没有时检查普通代理商 if (AdminConstants::ADMIN_GROUP_ID_AGENT == $group_id && !intval($ex_info['distribute'])) { if ($res = $this->getChannelActivity($pop_range, $show_type['goods'], $ex_info['create_by'], $user_id)) { return $res; } } //平台有活动时显示平台的 if ($res = $this->getPlatformActivity($pop_range, $show_type['goods'])) { return $res; } //-------------------赠币活动 //平台没有活动时,检查渠道的活动 //判断是否关闭了赠币 $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($channel_id); if ($adminConfig && $adminConfig['give_kandian'] == 0) { return null; } if (AdminConstants::ADMIN_GROUP_ID_CHANNEL == $group_id) { if ($res = $this->getGiveActivity($pop_range, $show_type['resource'], $channel_id, $user_id)) { return $res; } } //检查配号代理商 if (AdminConstants::ADMIN_GROUP_ID_AGENT == $group_id && intval($ex_info['distribute'])) { //渠道有活动返回渠道的 if ($res = $this->getGiveActivity($pop_range, $show_type['resource'], $ex_info['create_by'], $user_id)) { return $res; } //检查配号代理商 if ($res = $this->getGiveActivity($pop_range, $show_type['resource'], $channel_id, $user_id)) { return $res; } } //渠道没有时检查普通代理商 if (AdminConstants::ADMIN_GROUP_ID_AGENT == $group_id && !intval($ex_info['distribute'])) { if ($res = $this->getGiveActivity($pop_range, $show_type['resource'], $ex_info['create_by'], $user_id)) { return $res; } } } return null; } /** * 获取平台活动 * @param $pop_range * @param $show_type * @return mixed * @throws \think\db\exception\BindParamException * @throws \think\exception\PDOException */ private function getPlatformActivity($pop_range, $show_type) { if (!is_array($show_type)) { $show_type = [$show_type]; } $res = null; $key = "S-P-T-P:N:"; $redis = Redis::instance(); foreach ($show_type as $user_type) { $positionKey[] = "{$pop_range}:{$user_type}"; } $redisVals = $redis->hMGet($key, $positionKey); foreach ($redisVals as $k => $redisVal) { if ($redisVal != false) { $data = json_decode($redisVal, true); //判断活动结束时间 if ($data && isset($data['endtime'])) { if ($data['endtime'] <= time()) { //结束啦 continue; } } $res = $data; break; } } return $res; } /** * @param $resourceShowType * @return array */ private function _makeGoodShowTypeFromResource($resourceShowType) { $goodsShowTypes = [PayConstants::GOODS_SHOW_TYPE_ALL]; switch ($resourceShowType) { case ActivityConstants::RESOURCE_PAY_NOT: $goodsShowTypes[] = PayConstants::GOODS_SHOW_TYPE_UN_RECHARGED; break; case ActivityConstants::RESOURCE_PAY_NORMAL: $goodsShowTypes[] = PayConstants::GOODS_SHOW_TYPE_RECHARGED; break; case ActivityConstants::RESOURCE_PAY_YEAR: $goodsShowTypes[] = PayConstants::GOODS_SHOW_TYPE_RECHARGED; break; default: break; } return $goodsShowTypes; } /** * 获取渠道商活动 * @param $pop_range * @param $show_type * @param $channel_id * @param $user_id * @return mixed */ private function getChannelActivity($pop_range, $show_type, $channel_id, $user_id) { if (!is_array($show_type)) { $show_type = [$show_type]; } $res = null; $key = "S-P-T-C:N:{$channel_id}"; $redis = Redis::instance(); foreach ($show_type as $user_type) { $positionKey[] = "{$pop_range}:{$user_type}"; } $redisVals = $redis->hMGet($key, $positionKey); foreach ($redisVals as $k => $redisVal) { if ($redisVal != false) { $data = json_decode($redisVal, true); //判断活动结束时间 if ($data && isset($data['endtime'])) { if ($data['endtime'] <= time()) { //结束啦 continue; } } //检查限制 if ($data && array_key_exists('limited', $data)) { if (intval($data['limited'])) { //用户在渠道自定义的活动充值次数统计 $pay_count = intval($redis->hGet("ATPU:{$data['aid']}", $user_id)); if (intval($data['limited']) <= $pay_count) { continue; } } } $res = $data; break; } } if(isset($show_type['pay_type']) && isset($res['pop_ispay']) && $res['pop_ispay']!=1){ if($res['pop_ispay'] == 3 && $show_type['pay_type'] == 1){ $res = null; } if($res['pop_ispay'] ==2 && $show_type['pay_type'] == 2 ){ $res = null; } } return $res; } /** * 获取渠道商自定义活动列表,供充值页面使用 * @param UserObject $userInfo * @return array|mixed */ public function getRechargePageChannelActivity($userInfo) { $channelId = $userInfo->channel_id; $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($channelId); if ($adminConfig['activity_show_recharge_page'] == 0) { return []; } $activityConfig = Config::get('site.activity_config'); $redis = Redis::instance(); $userId = $userInfo->id; $key = "CIEN:".$channelId; if ($redis->exists($key)) { $res = json_decode($redis->get($key), true); } else { $time = time(); $res = []; $activitySimpleList = []; $activityList = $this->getActivityModel() ->field(['id', 'config_id', 'limited', 'pop_status']) ->where('starttime', '<', $time) ->where('endtime', '>', $time) ->where('status', '1') ->where('type', '1') ->where('show_for_recharge_page', 1) ->where('admin_id', $channelId) ->order('createtime desc') ->select(); if ($activityList) { $resourceIds = []; foreach ($activityList as $activity) { //获取活动的ConfigID $config_id = $activity['config_id'] ?? null; if (!$config_id) { LogService::error("Activity Error activity_id:{$activity['id']} ConfigID is Null"); continue; } $config = $activityConfig['config'][$config_id] ?? null; if (!$config) { LogService::error("Activity Error activity_id:{$activity['id']} config_id:{$config_id} Config Not Find"); continue; } if ($config && intval($config['status']) && (isset($config['resource_id']) && $config['resource_id'])) { $item = [ 'activity_id' => $activity['id'], 'config_id' => $activity['config_id'], 'limited' => $activity['limited'], 'resource_id' => $config['resource_id'], ]; $activitySimpleList[] = $item; $resourceIds[] = $config['resource_id']; } else { continue; } } $actCategoryId = GoodsService::instance()->getActivityGoodsCategoryId(); $resourceList = $this->getResourceModel()->alias('r')->join(['goods' => 'g'], 'r.goods_id = g.id') ->field([ 'r.id' => 'rid', 'g.id', 'category_id', 'title', 'g.money', 'g.first_description', 'g.second_description', 'g.kandian', 'g.free_kandian', 'g.icon', 'g.show_type', 'g.type' ]) ->whereIn('r.id', $resourceIds) ->where('resource_type', ActivityConstants::ACTIVITY_TYPE_CUSTOM) ->where('r.status', '1') ->where("find_in_set($actCategoryId,g.category_id)") ->order('r.weigh', 'desc') ->select(); foreach ($activitySimpleList as $activitySimpleItem) { foreach ($resourceList as $resItem) { if ($activitySimpleItem['resource_id'] == $resItem['rid']) { $item = $activitySimpleItem; $item['money'] = $resItem['money']; $item['show_type'] = $resItem['show_type']; $item['id'] = $resItem['id']; $item['category_id'] = $resItem['category_id']; $item['kandian'] = $resItem['kandian']; $item['free_kandian'] = $resItem['free_kandian']; $item['title'] = $resItem['title']; $item['icon'] = $resItem['icon']; $item['first_description'] = $resItem['first_description']; $item['second_description'] = $resItem['second_description']; $res[] = $item; } } } } $redis->set($key, json_encode($res, JSON_UNESCAPED_UNICODE), 60); } if (intval($userInfo->is_pay) == 0) { $goodsShowTypes = [PayConstants::GOODS_SHOW_TYPE_ALL, PayConstants::GOODS_SHOW_TYPE_UN_RECHARGED]; } else { $goodsShowTypes = [PayConstants::GOODS_SHOW_TYPE_ALL, PayConstants::GOODS_SHOW_TYPE_RECHARGED]; } //检查次数限制 foreach ($res as $item) { if (intval($item['limited'])) { //用户在渠道自定义的活动充值次数统计 $pay_count = intval($redis->hGet("ATPU:{$item['activity_id']}", $userId)); if (intval($item['limited']) <= $pay_count) { unset($item); continue; } if (in_array($item['show_type'], $goodsShowTypes)) { unset($item); continue; } } } return $res; } /** * 获取赠币活动 * @param $pop_range * @param $show_type * @param $channel_id * @param $user_id * @return mixed */ private function getGiveActivity($pop_range, $show_type, $channel_id, $user_id) { if (!is_array($show_type)) { $show_type = [$show_type]; } $res = null; $key = "S-P-T-G:N:{$channel_id}"; $redis = Redis::instance(); foreach ($show_type as $user_type) { $positionKey[] = "{$pop_range}:{$user_type}"; } $redisVals = $redis->hMGet($key, $positionKey); foreach ($redisVals as $k => $redisVal) { if ($redisVal != false) { $data = json_decode($redisVal, true); //判断活动结束时间 if ($data && isset($data['endtime'])) { if ($data['endtime'] <= time()) { //结束啦 continue; } } //检查限制 if ($data && array_key_exists('limited', $data)) { if (intval($data['limited'])) { //用户在渠道自定义的活动充值次数统计 $pay_count = intval($redis->hGet("ATGP:{$data['aid']}", $user_id)); if (intval($data['limited']) <= $pay_count) { continue; } } } $res = $data; break; } } if(isset($show_type['pay_type']) && isset($res['pop_ispay']) && $res['pop_ispay']!=1){ if($res['pop_ispay'] == 3 && $show_type['pay_type'] == 2){ $res = null; } if($res['pop_ispay'] ==2 && $show_type['pay_type'] == 1 ){ $res = null; } } return $res; } /** * 获取活动资源信息,其中type属性以goods表为准(1=看点充值,2=VIP充值) * @param $rid 资源id * @return array|false|\PDOStatement|string|\think\Model|null */ public function getResourceForFront($rid) { //加缓存 $key = 'A-R-R:'.$rid; if (Redis::instance()->exists($key)) { $res = json_decode(Redis::instance()->get($key), true); } else { $resource = $this->getResourceModel()->getInfo($rid); if (empty($resource)) { $res = []; } else { if ($resource['goods_id'] == 0) { $resource['type'] = $resource['type'] == 1 ? 2 : 1; $res = $resource; } else { $goods = $this->getGoodsModel()->find($resource['goods_id']); $res = [ 'day' => $goods['day'], 'type' => $goods['type'], 'money' => $goods['money'], 'kandian' => $goods['kandian'], 'free_kandian' => $goods['free_kandian'], 'free_day' => $goods['free_day'], 'activity_id' => $resource['activity_id'], 'name' => $resource['name'], ]; } } Redis::instance()->set($key, json_encode($res, JSON_UNESCAPED_UNICODE), 60); } return $res; } public function getActivityInfo($aid) { $resourceInfo = $this->getResourceModel()->where('activity_id', $aid)->find(); $res = []; try { if (empty($resourceInfo)) {//渠道商自定义活动 $aInfo = $this->getActivityModel()->where('id', $aid)->find(); if (empty($aInfo)) { throw new Exception('没有找到活动,aId:' . $aid); } $configId = $aInfo['config_id']; $activity_config = Config::get('site.activity_config'); $config = $activity_config['config'][$configId] ?? null; if (empty($config)) { throw new Exception('没有找到config,config_id:' . $configId); } if ($config && (isset($config['resource_id']) && $config['resource_id']) ) { $resourceInfo = $this->getResourceModel()->where('id', $config['resource_id'])->find(); if (empty($resourceInfo)) { throw new Exception('渠道商自定义活动没有找到资源,rid:' . $config['resource_id']); } $res = $this->makeActivityInfo($resourceInfo); } return $res; } else {//平台活动 $res = $this->makeActivityInfo($resourceInfo); return $res; } } catch (Exception $e) { LogService::error($e->getMessage()); $res['free_day'] = Config::get('site.kandian_free_day'); return $res; } } private function makeActivityInfo($resourceInfo) { if (empty($resourceInfo['goods_id'])) { $res['free_day'] = $resourceInfo['free_day']; } else { $goodsInfo = $this->getGoodsModel()->getGoodsInfoById($resourceInfo['goods_id']); if (empty($goodsInfo)) { throw new Exception('没有找到商品,goodsId:' . $resourceInfo['goods_id']); } $res['free_day'] = $goodsInfo['free_day']; } return $res; } /** * 获取有效的活动 * @return false|\PDOStatement|string|\think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getValidActivities($type = 0) { $map['type'] = ['in', [0, 1, 2]]; if ($type) { $map['type'] = ['eq', $type]; } //$activity = new Activity(); $data = $this->getActivityModel() ->where($map) ->where('status', 'eq', 1) ->where('starttime', '<', time()) ->where('endtime', '>', time()) ->select(); $aActivity = []; if($data){ foreach ($data as $activity) { $aActivity[$activity->id] = $activity->name; } } return $aActivity; } }