Class: EricWeixin::MediaNews

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/eric_weixin/media_news.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.save_news(options) ⇒ Object

will_send_article_msg: will_send_article_msg, user_group_name: user_group_name, send_at_fixed_time: send_at_fixed_time, send_fixed_date: send_fixed_date, send_fixed_time: send_fixed_time, send_save_to_history: send_save_to_history



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/models/eric_weixin/media_news.rb', line 162

def self.save_news options
  options = get_arguments_options options, [:will_send_article_msg, :user_group_name,
                                            :send_at_fixed_time, :send_fixed_date,
                                            :send_fixed_time, :send_save_to_history,
                                            :public_account_id]
  transaction do
    news = new
    if options[:send_at_fixed_time] == 'true'
      news.planned_send_time = "#{options[:send_fixed_date]} #{options[:send_fixed_time]}"
    end
    news.user_group_name = options[:user_group_name]
    news.status = 0
    news. = options[:public_account_id]
    news.save!
    article_ids = options[:will_send_article_msg].split(',')
    article_ids.each_with_index do |id, index|
      a_n = ::EricWeixin::MediaArticleNews.new
      a_n.sort = index
      article = ::EricWeixin::MediaArticle.find_by_id(id)
      a_n.media_article = article
      a_n.media_news = news
      a_n.save!
    end
    news.upload_news
    news
  end
end

.try_send_media_newsObject



97
98
99
100
101
102
103
# File 'app/models/eric_weixin/media_news.rb', line 97

def self.try_send_media_news
  not_send_media_news = self.where(status: 0)
  not_send_media_news.each do |news|
    next if news.planned_send_time.blank?
    news.send_to_openids if news.planned_send_time <= Time.now
  end
end

.update_media_news_after_sending(receive_message) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'app/models/eric_weixin/media_news.rb', line 223

def self.update_media_news_after_sending receive_message
  self.transaction do
    msg = EricWeixin::MediaNewsSentRecord.find_by_msg_id(receive_message[:MsgID]||receive_message[:MsgId])
    return if msg.blank?
    msg.update sent_count: receive_message[:SentCount],
               total_count: receive_message[:TotalCount],
               filter_count: receive_message[:FilterCount],
               error_count: receive_message[:ErrorCount],
               status: receive_message[:Status]
    news = msg.media_news
    news.filter_count = (news.filter_count.blank? ? 0 : news.filter_count) +  msg.filter_count
    news.total_count = (news.total_count.blank? ? 0 : news.total_count) +  msg.total_count
    news.sent_count = (news.sent_count.blank? ? 0 : news.sent_count) + msg.sent_count
    news.error_count = (news.error_count.blank? ? 0 : news.error_count) + msg.error_count
    news.save!
  end
end

Instance Method Details

#delete_server_newsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/eric_weixin/media_news.rb', line 79

def delete_server_news
  EricWeixin::MediaNews.transaction do
    BusinessException.raise '该图文已经发送出去,不可以删除。' if self.status == 1
    token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: self.
    url = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=#{token}"

    response = RestClient.post url, { "media_id" => self.media_id }.to_json
    pp "****************** 删除该图文 ***********************"
    pp response
    pp JSON.parse(response)["errcode"].class
    response_json = JSON.parse(response)
    BusinessException.raise response_json["errmsg"] unless response_json["errcode"] == 0
    self.media_id = nil
    self.save!
    self.reload
  end
end

#preview(openid) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/models/eric_weixin/media_news.rb', line 142

def preview openid
  token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: self.
  url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=#{token}"
  response = RestClient.post url, {
                         "touser" => openid,
                         "mpnews" => {"media_id" => self.media_id},
                         "msgtype" => "mpnews"
                     }.to_json
  pp "********************* 预览结果 ****************************"
  pp response
  response_json = JSON.parse(response)
  BusinessException.raise response_json["errmsg"] unless response_json["errcode"] == 0
end

#send_to_openidsObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/eric_weixin/media_news.rb', line 105

def send_to_openids
  BusinessException.raise 'media_id不可以为空。' if self.media_id.blank?
  EricWeixin::MediaNews.transaction do
    openids = ::Weixin::Process.__send__ self.user_group_name
    pp "****************** 将要发送的openids *********************"
    pp openids
    1.upto(100000).each do |i|
      start_number = i*10000 - 10000
      end_number = i*10000-1
      needopenids = openids[start_number..end_number]
      break if needopenids.blank?
      token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: self.
      url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=#{token}"
      data = {
          "touser" => needopenids,
          "mpnews" => {"media_id" => self.media_id},
          "msgtype" => "mpnews"
      }
      pp "************************* 群发的参数 *******************************"
      pp data
      response = RestClient.post url, data.to_json
      pp "******************* 群发的结果 ******************************"
      pp response
      response_json = JSON.parse(response)
      BusinessException.raise response_json["errmsg"] unless response_json["errcode"] == 0
      media_message = ::EricWeixin::MediaNewsSentRecord.new
      media_message.msg_id = response_json["msg_id"]
      media_message.media_news = self
      media_message.save!
    end
    self.send_time = Time.now
    self.status = 1
    self.save!
    self.reload
  end
end

#update_news(options) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'app/models/eric_weixin/media_news.rb', line 190

def update_news options
  options = ::EricWeixin::MediaNews.get_arguments_options options, [:will_send_article_msg, :user_group_name,
                                            :send_at_fixed_time, :send_fixed_date,
                                            :send_fixed_time, :send_save_to_history,
                                            :public_account_id]
  ::EricWeixin::MediaNews.transaction do
    news = self
    if options[:send_at_fixed_time] == 'true'
      news.planned_send_time = "#{options[:send_fixed_date]} #{options[:send_fixed_time]}"
    end
    news.user_group_name = options[:user_group_name]
    news. = options[:public_account_id]
    news.save!

    # 清空图文与文章的关联
    news.media_articles = []

    article_ids = options[:will_send_article_msg].split(',')
    article_ids.each_with_index do |id, index|
      a_n = ::EricWeixin::MediaArticleNews.new
      a_n.sort = index
      article = ::EricWeixin::MediaArticle.find_by_id(id)
      a_n.media_article = article
      a_n.media_news = news
      a_n.save!
    end
    news.reload
    news.delete_server_news
    news.upload_news
    news
  end
end

#upload_newsObject

def upload_news_old

EricWeixin::MediaNews.transaction do
  h = {"articles" => []}
  pp "************* 图文 **************"
  pp self
  pp "************** 该图文包含的文章 *******************"
  pp self.media_articles
  self.media_articles.each do |article|
    article_hash = {
        "title" => article.title,
        "thumb_media_id" => article.media_resource.media_id,
        "author" => article.author,
        "content_source_url" => article.content_source_url,
        "content" => article.content.force_encoding("UTF-8"),
        "digest" => article.digest,
        "show_cover_pic" => if article.show_cover_pic then 1 else 0 end
    }
    pp "*************** 文章内容 **********************"
    pp article_hash
    h["articles"] << article_hash
  end

  token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: self.
  url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=#{token}"
  response = RestClient.post url, CGI::unescape(h.to_json)
  pp "********************* 上传该图文 **********************"
  pp response
  response_json = JSON.parse(response)
  BusinessException.raise response_json["errmsg"] unless response_json["errmsg"].blank?
  self.media_id = response_json["media_id"]
  self.save!
  self.reload
end

end



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/eric_weixin/media_news.rb', line 42

def upload_news
  EricWeixin::MediaNews.transaction do
    h = {"articles" => []}
    pp "************* 图文 **************"
    pp self
    pp "************** 该图文包含的文章 *******************"
    pp self.media_articles
    self.media_articles.each do |article|
      article_hash = {
          "title" => article.title,
          "thumb_media_id" => article.media_resource.media_id,
          "author" => article.author,
          "content_source_url" => article.content_source_url,
          "content" => article.content.gsub("\"","'"),
          "digest" => article.digest,
          "show_cover_pic" => if article.show_cover_pic then 1 else 0 end
      }
      pp "*************** 文章内容 **********************"
      pp article_hash
      h["articles"] << article_hash
    end

    token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: self.
    url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=#{token}"
    pp "***************************** CGI::unescape(h.to_json) ********************************"
    pp CGI::unescape(h.to_json)
    response = RestClient.post url, CGI::unescape(h.to_json)
    pp "********************* 上传该图文 **********************"
    pp response
    response_json = JSON.parse(response)
    BusinessException.raise response_json.to_s if response_json["media_id"].blank?
    self.media_id = response_json["media_id"]
    self.save!
    self.reload
  end
end