channel_qrcode.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # encoding:utf-8
  2. class ChannelQrcode < ActiveRecord::Base
  3. has_paper_trail
  4. self.table_name = "channel_qrcodes"
  5. belongs_to :wx_gongzhonghao
  6. belongs_to :parent_signup_channel, :foreign_key => :parent_sign_up_channel_id
  7. belongs_to :push_after_sub
  8. validates :remark, :wx_gongzhonghao, presence: true
  9. attr_accessor :v_qrcode_img
  10. IMG_STORE_PATH = "channelqrcode"
  11. after_destroy :after_destroy
  12. after_create :after_create
  13. # after_update :after_update
  14. validate :generate_validation
  15. def generate_validation
  16. if (self.is_permanent == true) && (!self.expired_at.blank? && self.expired_at.to_i > 0)
  17. self.errors.add(:expired_at,"永久性和过期时间不能同时填写")
  18. elsif (self.is_permanent == false) && (self.expired_at.blank?)
  19. self.errors.add(:expired_at,"永久性和过期时间不能同时为空")
  20. end
  21. end
  22. def v_qrcode_img=file
  23. unless file.blank?
  24. clear_qrcode_img
  25. file_name = "#{UUID.new.generate[0...8].downcase}.jpg"
  26. file_path = "#{IMG_STORE_PATH}/thumb/#{file_name}"
  27. Ali::Oss.store(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path, file.read)
  28. self.qrcode_img = file_path
  29. self.save
  30. end
  31. end
  32. def clear_qrcode_img
  33. file_path = "#{self.qrcode_img}"
  34. Ali::Oss.delete_object(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path)
  35. end
  36. def get_qrcode_img
  37. return self.qrcode_img
  38. end
  39. def after_destroy
  40. clear_qrcode_img
  41. end
  42. rails_admin do
  43. navigation_label '二维码管理'
  44. weight -500
  45. list do
  46. filters [:wx_gongzhonghao, :is_permanent, :remark]
  47. field :id
  48. field :wx_gongzhonghao
  49. field :scene_id
  50. field :scene_str
  51. field :remark
  52. field :qrcode_img do
  53. formatted_value do
  54. bindings[:view].tag(:img, {:src => bindings[:object].get_qrcode_img, :style => 'width:50px;cursor:pointer', :onClick => "javascript:window.open('#{bindings[:object].get_qrcode_img}')"})
  55. end
  56. end
  57. field :qrcode_img_url
  58. field :push_after_sub
  59. field :is_permanent
  60. field :scan_times
  61. field :parent_signup_channel
  62. field :expired_at
  63. field :ad_cost do
  64. label "投放广告费"
  65. end
  66. end
  67. show do
  68. field :id
  69. field :wx_gongzhonghao
  70. field :scene_id
  71. field :scene_str do
  72. label "渠道场景标识符"
  73. end
  74. field :remark
  75. field :qrcode_img do
  76. formatted_value do
  77. bindings[:view].tag(:img, {:src => bindings[:object].get_qrcode_img, :style => 'width:50px;cursor:pointer', :onClick => "javascript:window.open('#{bindings[:object].get_qrcode_img}')"})
  78. end
  79. end
  80. field :qrcode_img_url
  81. field :push_after_sub
  82. field :is_permanent
  83. field :scan_times
  84. field :parent_signup_channel
  85. field :expired_at
  86. field :created_at
  87. end
  88. edit do
  89. field :wx_gongzhonghao
  90. field :scene_str do
  91. label "渠道场景标识符"
  92. end
  93. field :remark
  94. field :v_qrcode_img, :file_upload do
  95. pretty_value do
  96. bindings[:view].tag(:img, {:src => bindings[:object].get_qrcode_img, :class => 'preview'})
  97. end
  98. end
  99. field :push_after_sub
  100. field :is_permanent
  101. field :parent_signup_channel
  102. field :expired_at
  103. field :ad_cost
  104. end
  105. end
  106. def qrcode_img_url
  107. url = "#{CONFIG_FILE["api_host"]}/railsadmin/create_qrcode/#{self.id}"
  108. if !self.scene_str.blank?
  109. url = "#{CONFIG_FILE["api_host"]}/railsadmin/create_qrcode/scene_str/#{self.id}"
  110. end
  111. if self.qrcode_img.blank?
  112. open(url)
  113. end
  114. return url
  115. end
  116. def after_create
  117. self.scene_id = self.id
  118. self.save
  119. end
  120. end