eric_weixin 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/README.rdoc +6 -1
  3. data/app/assets/javascripts/eric_weixin/application.js +1 -0
  4. data/app/assets/javascripts/eric_weixin/cms/base.js +6 -5
  5. data/app/controllers/eric_weixin/application_controller.rb +98 -0
  6. data/app/controllers/eric_weixin/cms/base_controller.rb +1 -1
  7. data/app/controllers/eric_weixin/cms/weixin/media_articles_controller.rb +73 -0
  8. data/app/controllers/eric_weixin/cms/weixin/media_news_controller.rb +175 -0
  9. data/app/controllers/eric_weixin/cms/weixin/media_resources_controller.rb +68 -0
  10. data/app/controllers/eric_weixin/wz/pays_controller.rb +15 -0
  11. data/app/controllers/eric_weixin/wz/weixin_controller.rb +2 -2
  12. data/app/helpers/eric_weixin/application_helper.rb +6 -0
  13. data/app/helpers/eric_weixin/cms/weixin/public_accounts_helper.rb +3 -3
  14. data/app/models/eric_weixin/access_token.rb +1 -0
  15. data/app/models/eric_weixin/media_article.rb +53 -0
  16. data/app/models/eric_weixin/media_article_news.rb +6 -0
  17. data/app/models/eric_weixin/media_news.rb +216 -0
  18. data/app/models/eric_weixin/media_resource.rb +172 -0
  19. data/app/models/eric_weixin/public_account.rb +9 -0
  20. data/app/views/eric_weixin/cms/weixin/media_articles/_media_article.html.erb +131 -0
  21. data/app/views/eric_weixin/cms/weixin/media_articles/_select_pic.html.erb +78 -0
  22. data/app/views/eric_weixin/cms/weixin/media_articles/edit.html.erb +4 -0
  23. data/app/views/eric_weixin/cms/weixin/media_articles/index.html.erb +31 -0
  24. data/app/views/eric_weixin/cms/weixin/media_articles/new.html.erb +15 -0
  25. data/app/views/eric_weixin/cms/weixin/media_news/_select_article.html.erb +90 -0
  26. data/app/views/eric_weixin/cms/weixin/media_news/_select_user.html.erb +8 -0
  27. data/app/views/eric_weixin/cms/weixin/media_news/_will_send_article.html.erb +53 -0
  28. data/app/views/eric_weixin/cms/weixin/media_news/new.html.erb +284 -0
  29. data/app/views/eric_weixin/cms/weixin/media_resources/_media_resource.html.erb +62 -0
  30. data/app/views/eric_weixin/cms/weixin/media_resources/edit.html.erb +20 -0
  31. data/app/views/eric_weixin/cms/weixin/media_resources/index.html.erb +49 -0
  32. data/app/views/eric_weixin/cms/weixin/media_resources/new.html.erb +4 -0
  33. data/app/views/eric_weixin/cms/weixin/url_encodes/_new.html.erb +6 -6
  34. data/app/views/eric_weixin/wz/pays/pay_fail.html.erb +2 -0
  35. data/app/views/eric_weixin/wz/pays/pay_ok.html.erb +2 -0
  36. data/app/views/eric_weixin/wz/pays/prepay.html.erb +49 -0
  37. data/app/views/layouts/eric_weixin/cms/base.html.erb +1 -1
  38. data/app/views/layouts/eric_weixin/wz/pays.html.erb +11 -0
  39. data/config/initializers/assets.rb +1 -1
  40. data/config/initializers/inflections.rb +19 -0
  41. data/config/routes.rb +20 -6
  42. data/config/tinymce.yml +11 -0
  43. data/db/migrate/20150813080541_add_mch_id_to_weixin_public_accounts.rb +5 -0
  44. data/db/migrate/20150817105643_alert_weixin_reply_message_rules_s_reply_message_length.rb +5 -0
  45. data/db/migrate/20150817143407_add_mch_key_to_weixin_public_account.rb +5 -0
  46. data/db/migrate/20150818090435_create_weixin_media_resources.rb +13 -0
  47. data/db/migrate/20150818094553_create_weixin_media_articles.rb +17 -0
  48. data/db/migrate/20150818095654_create_weixin_media_news.rb +18 -0
  49. data/db/migrate/20150818100348_create_weixin_media_articles_news.rb +11 -0
  50. data/db/migrate/20150904045253_add_host_name_to_public_account.rb +5 -0
  51. data/lib/eric_weixin.rb +2 -0
  52. data/lib/eric_weixin/modules/ip.rb +2 -0
  53. data/lib/eric_weixin/modules/pay.rb +31 -0
  54. data/lib/eric_weixin/version.rb +1 -1
  55. metadata +38 -2
@@ -0,0 +1,15 @@
1
+ class EricWeixin::Wz::PaysController < ApplicationController
2
+
3
+ def prepay
4
+ @prepay_id = ::EricWeixin::Pay.generate_prepay_id params
5
+ end
6
+
7
+ def pay_ok
8
+ render text: '您支付成功,谢谢。'
9
+ end
10
+
11
+ def pay_fail
12
+ render text: '您支付失败,请返回重新尝试,谢谢。'
13
+ end
14
+
15
+ end
@@ -12,8 +12,8 @@ module EricWeixin
12
12
  def reply
13
13
  request_body = request.body.read
14
14
  public_account = PublicAccount.find_by_weixin_app_id params[:app_id]
15
- BusinessException.raise 'ip不正确' unless Ip.is_ip_exist? public_account_id: public_account.id,
16
- ip: get_ip
15
+ # BusinessException.raise 'ip不正确' unless Ip.is_ip_exist? public_account_id: public_account.id,
16
+ # ip: get_ip
17
17
  "message from wechat: ".to_logger
18
18
  request_body.to_logger
19
19
  weixin_message = MultiXml.parse(request_body).deep_symbolize_keys[:xml]
@@ -1,4 +1,10 @@
1
1
  module EricWeixin
2
2
  module ApplicationHelper
3
+ def get_show_value default_value, options
4
+ return options[:v1] unless options[:v1].blank?
5
+ return options[:v2] unless options[:v2].blank?
6
+ return options[:v3] unless options[:v3].blank?
7
+ default_value||''
8
+ end
3
9
  end
4
10
  end
@@ -1,8 +1,8 @@
1
1
  module EricWeixin::Cms::Weixin::PublicAccountsHelper
2
2
  def weixin_menu_show_tag json
3
- puts "****"*300
4
- puts json.class
5
- puts "****"*300
3
+ # puts "****"*300
4
+ # puts json.class
5
+ # puts "****"*300
6
6
  return '<h6>还没有任何菜单哦!</h6>' if json.blank? || json == "null"
7
7
  result = '<table border="1" style="word-break:break-all;"><thead><th width="140">button name</th><th width="140">sub_button name</th><th width="60">type</th><th width="150">key</th><th>url</th></thead><tbody>'
8
8
  obj = JSON.parse json
@@ -52,6 +52,7 @@ module EricWeixin
52
52
  # * public_account_id #公众账号 ID
53
53
  # ===调用示例
54
54
  # ::EricWeixin::AccessToken.get_new_token '5e3b98ca0000959946657212739fd535'
55
+ # https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=12&secret=23
55
56
  def self.get_new_token public_account_id
56
57
  account = ::EricWeixin::PublicAccount.find_by_id public_account_id
57
58
  BusinessException.raise 'account 不存在' if account.blank?
@@ -0,0 +1,53 @@
1
+ class EricWeixin::MediaArticle < ActiveRecord::Base
2
+ self.table_name = 'weixin_media_articles'
3
+ has_many :media_article_news, foreign_key: 'weixin_media_article_id'
4
+ has_many :media_news, through: :media_article_news
5
+
6
+ belongs_to :media_resource, foreign_key: 'thumb_media_id'
7
+
8
+ validates_presence_of :thumb_media_id, message: '缩略图必须有。'
9
+ validates_presence_of :title, message: '标题不能为空。'
10
+ validates :title, length: { maximum: 64, message: "标题最长为64" }
11
+ validates :author, length: { maximum: 8, message: "作者字数最大为8"}
12
+ validates :digest, length: { maximum: 120, message: "摘要字数最大为120" }
13
+
14
+
15
+ def self.create_media_article options
16
+ options = get_arguments_options options, [:tag, :thumb_media_id, :author, :title,
17
+ :digest, :show_cover_pic, :is_first_article,
18
+ :content, :content_source_url, :public_account_id],
19
+ show_cover_pic: true, is_first_article: false
20
+ transaction do
21
+ media_article = ::EricWeixin::MediaArticle.new options
22
+ media_article.save!
23
+ media_article
24
+ end
25
+ end
26
+
27
+ def update_media_article options
28
+ options = EricWeixin::MediaArticle.get_arguments_options options, [:tag, :thumb_media_id, :author, :title,
29
+ :digest, :show_cover_pic, :is_first_article,
30
+ :content, :content_source_url, :public_account_id]
31
+ EricWeixin::MediaArticle.transaction do
32
+ self.update! options
33
+ self
34
+ end
35
+ end
36
+
37
+ def self.common_query options
38
+ articles = self.all
39
+ articles = articles.where(public_account_id: options[:public_account_id]) unless options[:public_account_id].blank?
40
+ articles = articles.where("weixin_media_articles.tag like ?", "%#{options[:tag]}%") unless options[:tag].blank?
41
+ unless options[:start_date].blank?
42
+ start_date = options[:start_date].to_time
43
+ start_date = start_date.change(hour: 0, min:0, sec:0)
44
+ articles = articles.where("weixin_media_articles.created_at >= ?", start_date)
45
+ end
46
+ unless options[:end_date].blank?
47
+ end_date = options[:end_date].to_time
48
+ end_date = end_date.change(hour: 23, min:59, sec:59)
49
+ articles = articles.where("weixin_media_articles.created_at <= ?", end_date)
50
+ end
51
+ articles
52
+ end
53
+ end
@@ -0,0 +1,6 @@
1
+ class EricWeixin::MediaArticleNews < ActiveRecord::Base
2
+ self.table_name = 'weixin_media_articles_news'
3
+ belongs_to :media_article, foreign_key: 'weixin_media_article_id'
4
+ belongs_to :media_news, foreign_key: 'weixin_media_news_id'
5
+
6
+ end
@@ -0,0 +1,216 @@
1
+ class EricWeixin::MediaNews < ActiveRecord::Base
2
+ self.table_name = 'weixin_media_news'
3
+ has_many :media_article_news, foreign_key: 'weixin_media_news_id'
4
+ has_many :media_articles, through: :media_article_news
5
+ # def upload_news_old
6
+ #
7
+ # EricWeixin::MediaNews.transaction do
8
+ # h = {"articles" => []}
9
+ # pp "************* 图文 **************"
10
+ # pp self
11
+ # pp "************** 该图文包含的文章 *******************"
12
+ # pp self.media_articles
13
+ # self.media_articles.each do |article|
14
+ # article_hash = {
15
+ # "title" => article.title,
16
+ # "thumb_media_id" => article.media_resource.media_id,
17
+ # "author" => article.author,
18
+ # "content_source_url" => article.content_source_url,
19
+ # "content" => CGI::escape(article.content).force_encoding("UTF-8"),
20
+ # "digest" => article.digest,
21
+ # "show_cover_pic" => if article.show_cover_pic then 1 else 0 end
22
+ # }
23
+ # pp "*************** 文章内容 **********************"
24
+ # pp article_hash
25
+ # h["articles"] << article_hash
26
+ # end
27
+ #
28
+ # token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: self.public_account_id
29
+ # url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=#{token}"
30
+ # response = RestClient.post url, CGI::unescape(h.to_json)
31
+ # pp "********************* 上传该图文 **********************"
32
+ # pp response
33
+ # response_json = JSON.parse(response)
34
+ # BusinessException.raise response_json["errmsg"] unless response_json["errmsg"].blank?
35
+ # self.media_id = response_json["media_id"]
36
+ # self.save!
37
+ # self.reload
38
+ # end
39
+ # end
40
+
41
+ def upload_news
42
+ EricWeixin::MediaNews.transaction do
43
+ h = {"articles" => []}
44
+ pp "************* 图文 **************"
45
+ pp self
46
+ pp "************** 该图文包含的文章 *******************"
47
+ pp self.media_articles
48
+ self.media_articles.each do |article|
49
+ article_hash = {
50
+ "title" => article.title,
51
+ "thumb_media_id" => article.media_resource.media_id,
52
+ "author" => article.author,
53
+ "content_source_url" => article.content_source_url,
54
+ "content" => CGI::escape(article.content.gsub("\"","'")),
55
+ "digest" => article.digest,
56
+ "show_cover_pic" => if article.show_cover_pic then 1 else 0 end
57
+ }
58
+ pp "*************** 文章内容 **********************"
59
+ pp article_hash
60
+ h["articles"] << article_hash
61
+ end
62
+
63
+ token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: self.public_account_id
64
+ url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=#{token}"
65
+ pp "***************************** CGI::unescape(h.to_json) ********************************"
66
+ pp CGI::unescape(h.to_json)
67
+ response = RestClient.post url, CGI::unescape(h.to_json)
68
+ pp "********************* 上传该图文 **********************"
69
+ pp response
70
+ response_json = JSON.parse(response)
71
+ BusinessException.raise response_json.to_s if response_json["media_id"].blank?
72
+ self.media_id = response_json["media_id"]
73
+ self.save!
74
+ self.reload
75
+ end
76
+ end
77
+
78
+ def delete_server_news
79
+ EricWeixin::MediaNews.transaction do
80
+ token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: self.public_account_id
81
+ url = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=#{token}"
82
+
83
+ response = RestClient.post url, { "media_id" => self.media_id }.to_json
84
+ pp "****************** 删除该图文 ***********************"
85
+ pp response
86
+ pp JSON.parse(response)["errcode"].class
87
+ response_json = JSON.parse(response)
88
+ BusinessException.raise response_json["errmsg"] unless response_json["errcode"] == 0
89
+ self.media_id = nil
90
+ self.save!
91
+ self.reload
92
+ end
93
+ end
94
+
95
+ def self.try_send_media_news
96
+ not_send_media_news = self.where(status: 0)
97
+ not_send_media_news.each do |news|
98
+ next if news.planned_send_time.blank?
99
+ news.send_to_openids if news.planned_send_time <= Time.now
100
+ end
101
+ end
102
+
103
+ def send_to_openids
104
+ BusinessException.raise '' if self.media_id.blank?
105
+ EricWeixin::MediaNews.transaction do
106
+ openids = ::Weixin::Process.__send__ self.user_group_name
107
+ pp "****************** 将要发送的openids *********************"
108
+ pp openids
109
+ 1.upto(100000).each do |i|
110
+ start_number = i*10000 - 10000
111
+ end_number = i*10000-1
112
+ needopenids = openids[start_number..end_number]
113
+ break if needopenids.blank?
114
+ token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: self.public_account_id
115
+ url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=#{token}"
116
+ data = {
117
+ "touser" => needopenids,
118
+ "mpnews" => {"media_id" => self.media_id},
119
+ "msgtype" => "mpnews"
120
+ }
121
+ pp "************************* 群发的参数 *******************************"
122
+ pp data
123
+ response = RestClient.post url, data.to_json
124
+ pp "******************* 群发的结果 ******************************"
125
+ pp response
126
+ response_json = JSON.parse(response)
127
+ BusinessException.raise response_json["errmsg"] unless response_json["errcode"] == 0
128
+ end
129
+ self.status = 1
130
+ self.save!
131
+ self.reload
132
+ end
133
+ end
134
+
135
+ def preview openid
136
+ token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: self.public_account_id
137
+ url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=#{token}"
138
+ response = RestClient.post url, {
139
+ "touser" => openid,
140
+ "mpnews" => {"media_id" => self.media_id},
141
+ "msgtype" => "mpnews"
142
+ }.to_json
143
+ pp "********************* 预览结果 ****************************"
144
+ pp response
145
+ response_json = JSON.parse(response)
146
+ BusinessException.raise response_json["errmsg"] unless response_json["errcode"] == 0
147
+ end
148
+
149
+ # will_send_article_msg: will_send_article_msg,
150
+ # user_group_name: user_group_name,
151
+ # send_at_fixed_time: send_at_fixed_time,
152
+ # send_fixed_date: send_fixed_date,
153
+ # send_fixed_time: send_fixed_time,
154
+ # send_save_to_history: send_save_to_history
155
+ def self.save_news options
156
+ options = get_arguments_options options, [:will_send_article_msg, :user_group_name,
157
+ :send_at_fixed_time, :send_fixed_date,
158
+ :send_fixed_time, :send_save_to_history,
159
+ :public_account_id]
160
+ transaction do
161
+ news = new
162
+ if options[:send_at_fixed_time] == 'true'
163
+ news.planned_send_time = "#{options[:send_fixed_date]} #{options[:send_fixed_time]}"
164
+ end
165
+ news.user_group_name = options[:user_group_name]
166
+ news.status = 0
167
+ news.public_account_id = options[:public_account_id]
168
+ news.save!
169
+ article_ids = options[:will_send_article_msg].split(',')
170
+ article_ids.each_with_index do |id, index|
171
+ a_n = ::EricWeixin::MediaArticleNews.new
172
+ a_n.sort = index
173
+ article = ::EricWeixin::MediaArticle.find_by_id(id)
174
+ a_n.media_article = article
175
+ a_n.media_news = news
176
+ a_n.save!
177
+ end
178
+ news.upload_news
179
+ news
180
+ end
181
+ end
182
+
183
+ def update_news options
184
+ options = ::EricWeixin::MediaNews.get_arguments_options options, [:will_send_article_msg, :user_group_name,
185
+ :send_at_fixed_time, :send_fixed_date,
186
+ :send_fixed_time, :send_save_to_history,
187
+ :public_account_id]
188
+ ::EricWeixin::MediaNews.transaction do
189
+ news = self
190
+ if options[:send_at_fixed_time] == 'true'
191
+ news.planned_send_time = "#{options[:send_fixed_date]} #{options[:send_fixed_time]}"
192
+ end
193
+ news.user_group_name = options[:user_group_name]
194
+ news.public_account_id = options[:public_account_id]
195
+ news.save!
196
+
197
+ # 清空图文与文章的关联
198
+ news.media_articles = []
199
+
200
+ article_ids = options[:will_send_article_msg].split(',')
201
+ article_ids.each_with_index do |id, index|
202
+ a_n = ::EricWeixin::MediaArticleNews.new
203
+ a_n.sort = index
204
+ article = ::EricWeixin::MediaArticle.find_by_id(id)
205
+ a_n.media_article = article
206
+ a_n.media_news = news
207
+ a_n.save!
208
+ end
209
+ news.reload
210
+ news.delete_server_news
211
+ news.upload_news
212
+ news
213
+ end
214
+ end
215
+
216
+ end
@@ -0,0 +1,172 @@
1
+ class EricWeixin::MediaResource < ActiveRecord::Base
2
+
3
+ self.table_name = 'weixin_media_resources'
4
+
5
+ has_many :media_articles, foreign_key: 'thumb_media_id'
6
+
7
+ RESOURCE_TYPE = {
8
+ 'pic_in_article' => '文章内图片',
9
+ 'thumbnail' => '缩略图',
10
+ 'audio' => '音频',
11
+ 'video' => '视频'
12
+ }
13
+
14
+ #保存图文内图片
15
+ # ===参数说明
16
+ # * public_account_id 需要处理的公众账号
17
+ # * pic 图片
18
+ # * tags 标签
19
+ # ===调用示例
20
+ #
21
+ # ::EricWeixin::MediaResource.save_pic_in_article pic: File.read('/Users/ericliu/Pictures/1.pic.jpg'),
22
+ # tags: 'test',
23
+ # public_account_id: 1
24
+ # #
25
+ def self.save_pic_in_article options, file
26
+ pic_in_article_path = '/uploads/wechat_pic/file_in_content/'
27
+ unless Dir.exist? Rails.root.join("public").to_s + pic_in_article_path
28
+ BusinessException.raise "请创建目录#{ Rails.root.join("public").to_s + pic_in_article_path}"
29
+ end
30
+ EricWeixin::MediaResource.transaction do
31
+ # pp options[:pic].methods
32
+ resource = EricWeixin::MediaResource.new tags: options[:tags],
33
+ category_name: 'pic_in_article',
34
+ public_account_id: options[:public_account_id]
35
+ resource.save!
36
+
37
+
38
+ file_name = "#{EricTools.uuid}-#{file.original_filename}"
39
+ origin_name_with_path = Rails.root.join("public#{pic_in_article_path}", file_name)
40
+ File.open(origin_name_with_path, 'wb') do |f|
41
+ f.write(file.read)
42
+ end
43
+
44
+ url = EricWeixin::MediaResource.upload_pic_in_article pic: File.new(origin_name_with_path),
45
+ public_account_id: options[:public_account_id]
46
+
47
+ resource.wechat_link = url
48
+ resource.local_link = "#{pic_in_article_path}#{file_name}"
49
+ resource.save!
50
+
51
+ end
52
+ end
53
+
54
+
55
+ # 上传图文内图片.
56
+ # ===参数说明
57
+ # * pic 图片文件。
58
+ # * public_account_id 公众账号id
59
+ # ===调用示例
60
+ # ::EricWeixin::MediaResource.upload_pic_in_article pic: File.new('/Users/ericliu/Pictures/1.pic.jpg'),
61
+ # public_account_id: 1
62
+ def self.upload_pic_in_article options
63
+ token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: options[:public_account_id]
64
+ url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=#{token}"
65
+ response = RestClient.post url, :media => options[:pic]
66
+ response_json = JSON.parse(response)
67
+ url = response_json["url"]
68
+ BusinessException.raise response_json["errmsg"] if url.blank?
69
+ url
70
+ end
71
+
72
+
73
+ #保存图文内图片
74
+ # ===参数说明
75
+ # * public_account_id 需要处理的公众账号
76
+ # * media 资源
77
+ # * tags 标签
78
+ # * type 资源类型
79
+ # ===调用示例
80
+ #
81
+ # ::EricWeixin::MediaResource.save_media media: File.new('/Users/ericliu/Pictures/1.pic.jpg'),
82
+ # tags: 'test',
83
+ # public_account_id: 1,
84
+ # type: xx
85
+ # #
86
+ def self.save_media options, file
87
+ BusinessException.raise "媒体类型不正确" unless EricWeixin::MediaResource::RESOURCE_TYPE.keys.include?(options[:type])
88
+ file_path = "/uploads/wechat_pic/#{options[:type]}/"
89
+ unless Dir.exist? Rails.root.join("public").to_s + file_path
90
+ BusinessException.raise "请创建目录#{ Rails.root.join("public").to_s + file_path}"
91
+ end
92
+ EricWeixin::MediaResource.transaction do
93
+ resource = EricWeixin::MediaResource.new tags: options[:tags],
94
+ category_name: options[:type],
95
+ public_account_id: options[:public_account_id]
96
+ resource.save!
97
+
98
+ file_name = "#{EricTools.uuid}-#{file.original_filename}"
99
+ origin_name_with_path = Rails.root.join("public#{file_path}", file_name)
100
+ File.open(origin_name_with_path, 'wb') do |f|
101
+ f.write(file.read)
102
+ end
103
+
104
+
105
+ json = EricWeixin::MediaResource.upload_media media: File.new(origin_name_with_path),
106
+ type: case options[:type]
107
+ when 'thumbnail'
108
+ 'thumb'
109
+ when 'audio'
110
+ 'voice'
111
+ when 'video'
112
+ 'video'
113
+ when 'image'
114
+ 'image'
115
+ end,
116
+ public_account_id: options[:public_account_id]
117
+ url = json["url"]
118
+ media_id = json["media_id"]
119
+ resource.local_link = "#{file_path}#{file_name}"
120
+ resource.wechat_link = url
121
+ resource.media_id = media_id
122
+ resource.save!
123
+
124
+
125
+ end
126
+ end
127
+
128
+
129
+ # 上传永久性素材.
130
+ # ===参数说明
131
+ # * media 图片文件。
132
+ # * type 素材类型:包含以下:image voice video thumb
133
+ # * public_account_id 公众账号id
134
+ # ===调用示例
135
+ # ::EricWeixin::MediaResource.upload_media media: File.new('/Users/ericliu/Pictures/1.pic.jpg'),
136
+ # public_account_id: 1,
137
+ # type: 'image'
138
+
139
+ def self.upload_media options
140
+ BusinessException.raise '资源类型不正确,应该是image voice video thumb 的其中一种' unless ["image", "voice", "video", "thumb"].include? options[:type]
141
+ token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: options[:public_account_id]
142
+ url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=#{token}"
143
+
144
+ response = RestClient.post url, :media => options[:media], :type => options[:type]
145
+ pp response
146
+ response_json = JSON.parse(response)
147
+ BusinessException.raise response_json["errmsg"] unless response_json["errmsg"].blank?
148
+ response_json
149
+ end
150
+
151
+ def delete_server_resource
152
+ EricWeixin::MediaResource.transaction do
153
+ token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: self.public_account_id
154
+ url = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=#{token}"
155
+
156
+ response = RestClient.post url, { "media_id" => self.media_id }.to_json
157
+ pp "****************** 删除该图文 ***********************"
158
+ pp response
159
+ response_json = JSON.parse(response)
160
+ BusinessException.raise response_json["errmsg"] unless response_json["errcode"] == 0
161
+ self.media_id = nil
162
+ self.save!
163
+ end
164
+ end
165
+
166
+ def delete_self
167
+ BusinessException.raise '该资源有其他文章素材引用,不能删除。' unless self.media_articles.blank?
168
+ self.delete_server_resource
169
+ self.destroy!
170
+ end
171
+ end
172
+