Class: AdSpace::Api::V1::AdvertisementsController

Inherits:
ActionController::API
  • Object
show all
Defined in:
app/controllers/ad_space/api/v1/advertisements_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject

before_action :prepare_params!, only: %w



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/ad_space/api/v1/advertisements_controller.rb', line 4

def index
  builder = AdSpace::Advertisement.includes(:slot).where(platform: params[:platform])
  builder = builder.published.unexpired
  builder = builder.joins(:slot).where("ad_space_slots.uuid": params[:slot_ids].split(",")) unless params[:slot_ids].blank?
  # TODO: 动态可配置,不同项目可以自定义特殊的查询条件,基于builder
  # 0. 判断是否有配置动态用户信息,proc
  # 1. 通过token获取用户信息 proc
  # 2. 通过用户信息判断用户是否为会员
  # 3. 如果是会员,则不展示广告位为“开屏广告”的广告
  # 4. 如果不是会员,则展示所有广告
  # 5. 如果没有配置动态用户信息,则展示所有广告
  # has_paid 开屏逻辑、member 逻辑
  # token = ApiKey.get_token(params[:token])
  # if token.present?
  #   current_user = token.user
  #   if current_user && current_user&.paid?
  #     # 写死 slot_id
  #     builder = builder.where.not(position_code: '10003')
  #   end
  # end

  width = params[:screen_width].to_f
  height = params[:screen_height].to_f
  screen_ratio = width.zero? ? 0 : height / width
  size = if screen_ratio < 1.7
          "little"
        elsif screen_ratio >= 1.7 && screen_ratio < 2.0
          "middle"
        else
          "large"
        end

  unless params[:version_code].blank?
    array = [0]
    array.push(params[:version_code].to_i)
    codes = PG::TextEncoder::Array.new.encode(array.presence || [0]).force_encoding("utf-8")
    builder = builder.where("target_version_codes && ?", codes)
  end

  advertisements = builder.order(weight: :desc)

  render json: advertisements, each_serializer: V1::AdvertisementSerializer, size: size
end