瀏覽代碼

add live broad manage function

abiao 5 年之前
父節點
當前提交
968c783967
共有 3 個文件被更改,包括 147 次插入0 次删除
  1. 103 0
      app/models/live_broad.rb
  2. 15 0
      config/locales/models/live_broad.yml
  3. 29 0
      db/migrate/20210104114641_create_live_broads.rb

+ 103 - 0
app/models/live_broad.rb

@@ -0,0 +1,103 @@
+class LiveBroad < ActiveRecord::Base
+    has_paper_trail
+    self.table_name = "live_broads"
+    validates :room_id, presence: true
+    attr_accessor :v_cover
+    before_save :before_save
+
+    def before_save
+      #自动取消其他置顶项
+      if self.show
+        live_broads = LiveBroad.find_by_sql("select * from live_broads ")
+        # 创建商品图片
+        live_broads.each do |l|
+          l.show=false
+          l.save
+        end
+      end
+    end
+
+    IMG_STORE_PATH = "live_broads"
+
+    rails_admin do
+        navigation_label '直播管理'
+        weight -100
+        list do 
+          filters [:id,:name]
+          field :id      
+          field :title
+          field :room_id
+          field :cover do
+            formatted_value do
+              bindings[:view].tag(:img,{:src => bindings[:object].get_cover_img,
+              :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px',
+              :onClick => "javascript:window.open('#{bindings[:object].get_cover_img}')"})
+            end
+          end
+          field :begin_time
+          field :end_time
+          field :show
+          field :remark
+          field :created_at
+
+        end
+
+        show do
+          field :id
+          field :title
+          field :room_id
+          field :cover do
+            formatted_value do
+              bindings[:view].tag(:img,{:src => bindings[:object].get_cover_img,
+              :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px',
+              :onClick => "javascript:window.open('#{bindings[:object].get_cover_img}')"})
+            end
+          end
+          field :begin_time
+          field :end_time
+          field :show
+          field :remark
+          field :created_at
+          field :updated_at
+        end
+
+        edit do
+          field :title
+          field :room_id
+          field :v_cover, :file_upload do
+            pretty_value do
+              bindings[:view].tag(:img, {:src => bindings[:object].get_cover_img, :class => 'preview'})
+            end
+          end
+          field :begin_time
+          field :end_time
+          field :show
+          field :remark
+        end
+    end
+
+    def get_cover_img
+      url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{self.cover}"
+      return url
+    end
+
+    def v_cover=file
+      unless file.blank?
+        clear_cover_img
+        file_name = "#{UUID.new.generate[0...8].downcase}.jpg"
+        file_path = "#{IMG_STORE_PATH}/#{file_name}"
+        Ali::Oss.store(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path, file.read)
+        self.cover = file_path
+        self.save
+      end
+    end
+
+    def clear_cover_img
+      file_path = "#{self.cover}"
+      Ali::Oss.delete_object(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path)
+    end
+
+    def after_destroy
+      clear_cover_img
+    end
+end

+ 15 - 0
config/locales/models/live_broad.yml

@@ -0,0 +1,15 @@
+zh-CN:
+  activerecord:
+    models:
+      live_broad: 直播管理
+    attributes:
+      live_broad:
+        title: 直播标题
+        room_id: 房间号
+        cover: 直播封面
+        begin_time: 开播时间
+        end_time: 结束时间
+        show: 置顶
+        remark: 备注
+        created_at: 创建时间
+        updated_at: 更新时间

+ 29 - 0
db/migrate/20210104114641_create_live_broads.rb

@@ -0,0 +1,29 @@
+# encoding:utf-8
+class CreateLiveBroads < ActiveRecord::Migration
+  def up
+    #直播间管理
+    create_table :live_broads do |t|
+      # 直播标题
+      t.column :title, :string,:limit => 128, :null=>false
+      #封面
+      t.column :cover, :string,:limit => 256, :null=>false
+      #房间号
+      t.column :room_id, :integer,:null=>false, :default => 0
+      #开始时间
+      t.column :begin_time,:datetime
+      #结束时间
+      t.column :end_time,:datetime
+      # 置顶
+      t.column :show, :boolean, :default=>false
+      # 备注
+      t.column :remark, :string,:limit => 256, :null=>false
+      t.timestamps
+    end
+    add_index :live_broads, :room_id
+
+  end
+
+  def down
+  	drop_table :live_broads
+  end
+end