eric_weixin 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +4 -4
  2. data/{LICENSE.txt → MIT-LICENSE} +1 -3
  3. data/README.rdoc +24 -0
  4. data/Rakefile +36 -1
  5. data/app/assets/javascripts/eric_weixin/application.js +25 -0
  6. data/app/assets/javascripts/eric_weixin/cms/base.js +22 -0
  7. data/app/assets/stylesheets/eric_weixin/application.css +15 -0
  8. data/app/assets/stylesheets/eric_weixin/cms/base.css.scss +14 -0
  9. data/app/assets/stylesheets/eric_weixin/cms/foundation_and_overrides.scss +1454 -0
  10. data/app/controllers/eric_weixin/application_controller.rb +5 -0
  11. data/app/controllers/eric_weixin/cms/base_controller.rb +19 -0
  12. data/app/controllers/eric_weixin/cms/weixin/article_datas_controller.rb +45 -0
  13. data/app/controllers/eric_weixin/cms/weixin/customs_service_records_controller.rb +8 -0
  14. data/app/controllers/eric_weixin/cms/weixin/news_datas_controller.rb +52 -0
  15. data/app/controllers/eric_weixin/cms/weixin/public_accounts_controller.rb +36 -0
  16. data/app/controllers/eric_weixin/cms/weixin/reply_message_rules_controller.rb +49 -0
  17. data/app/controllers/eric_weixin/cms/weixin/two_dimension_codes_controller.rb +36 -0
  18. data/app/controllers/eric_weixin/cms/weixin/url_encodes_controller.rb +41 -0
  19. data/app/controllers/eric_weixin/cms/weixin/users_controller.rb +19 -0
  20. data/{lib/eric_weixin/app/controllers/weixin → app/controllers/eric_weixin/wz}/weixin_controller.rb +18 -7
  21. data/app/helpers/eric_weixin/application_helper.rb +4 -0
  22. data/app/helpers/eric_weixin/cms/weixin/public_accounts_helper.rb +28 -0
  23. data/{lib/eric_weixin/app/model → app/models/eric_weixin}/access_token.rb +23 -9
  24. data/{lib/eric_weixin/app/model → app/models/eric_weixin}/article.rb +0 -0
  25. data/{lib/eric_weixin/app/model → app/models/eric_weixin}/article_news.rb +0 -0
  26. data/app/models/eric_weixin/customs_service_record.rb +99 -0
  27. data/{lib/eric_weixin/app/model → app/models/eric_weixin}/message_log.rb +8 -2
  28. data/{lib/eric_weixin/app/model → app/models/eric_weixin}/news.rb +0 -0
  29. data/{lib/eric_weixin/app/model → app/models/eric_weixin}/public_account.rb +25 -14
  30. data/{lib/eric_weixin/app/model → app/models/eric_weixin}/reply_message_rule.rb +56 -12
  31. data/app/models/eric_weixin/report/interface_data.rb +120 -0
  32. data/app/models/eric_weixin/report/msg_data.rb +292 -0
  33. data/app/models/eric_weixin/report/news_data.rb +264 -0
  34. data/app/models/eric_weixin/report/user_data.rb +98 -0
  35. data/app/models/eric_weixin/template_message_log.rb +87 -0
  36. data/{lib/eric_weixin/app/model → app/models/eric_weixin}/two_dimension_code.rb +25 -17
  37. data/app/models/eric_weixin/weixin_user.rb +155 -0
  38. data/app/views/eric_weixin/cms/weixin/article_datas/_article_data.html.erb +8 -0
  39. data/app/views/eric_weixin/cms/weixin/article_datas/edit.html.erb +36 -0
  40. data/app/views/eric_weixin/cms/weixin/article_datas/index.html.erb +21 -0
  41. data/app/views/eric_weixin/cms/weixin/article_datas/new.html.erb +36 -0
  42. data/app/views/eric_weixin/cms/weixin/article_datas/show.html.erb +5 -0
  43. data/app/views/eric_weixin/cms/weixin/customs_service_records/index.html.erb +66 -0
  44. data/app/views/eric_weixin/cms/weixin/news_datas/_news_data.html.erb +7 -0
  45. data/app/views/eric_weixin/cms/weixin/news_datas/edit.html.erb +45 -0
  46. data/app/views/eric_weixin/cms/weixin/news_datas/index.html.erb +30 -0
  47. data/app/views/eric_weixin/cms/weixin/news_datas/new.html.erb +49 -0
  48. data/app/views/eric_weixin/cms/weixin/news_datas/show.html.erb +28 -0
  49. data/app/views/eric_weixin/cms/weixin/public_accounts/_menu.html.erb +5 -0
  50. data/app/views/eric_weixin/cms/weixin/public_accounts/_public_account.html.erb +19 -0
  51. data/app/views/eric_weixin/cms/weixin/public_accounts/_weixin_user.html.erb +7 -0
  52. data/app/views/eric_weixin/cms/weixin/public_accounts/index.html.erb +27 -0
  53. data/app/views/eric_weixin/cms/weixin/public_accounts/show.html.erb +8 -0
  54. data/app/views/eric_weixin/cms/weixin/reply_message_rules/destroy.js.erb +2 -0
  55. data/app/views/eric_weixin/cms/weixin/reply_message_rules/edit.html.erb +69 -0
  56. data/app/views/eric_weixin/cms/weixin/reply_message_rules/index.html.erb +38 -0
  57. data/app/views/eric_weixin/cms/weixin/reply_message_rules/new.html.erb +69 -0
  58. data/app/views/eric_weixin/cms/weixin/two_dimension_codes/_two_dimension_code_form.html.erb +11 -0
  59. data/app/views/eric_weixin/cms/weixin/two_dimension_codes/index.html.erb +30 -0
  60. data/app/views/eric_weixin/cms/weixin/two_dimension_codes/new.html.erb +41 -0
  61. data/app/views/eric_weixin/cms/weixin/two_dimension_codes/show.html.erb +12 -0
  62. data/app/views/eric_weixin/cms/weixin/url_encodes/_new.html.erb +25 -0
  63. data/app/views/eric_weixin/cms/weixin/url_encodes/create.js.erb +1 -0
  64. data/app/views/eric_weixin/cms/weixin/url_encodes/index.html.erb +43 -0
  65. data/app/views/eric_weixin/cms/weixin/users/index.html.erb +331 -0
  66. data/app/views/layouts/eric_weixin/application.html.erb +14 -0
  67. data/app/views/layouts/eric_weixin/cms/base.html.erb +66 -0
  68. data/{lib/eric_weixin/app/views → app/views/layouts}/eric_weixin/weixin/aa.erb +0 -0
  69. data/config/initializers/assets.rb +10 -0
  70. data/config/routes.rb +36 -0
  71. data/db/migrate/20150610031604_create_weixin_report_user_data.rb +14 -0
  72. data/db/migrate/20150610031935_create_weixin_report_news_data.rb +24 -0
  73. data/db/migrate/20150610032545_create_weixin_report_msg_data.rb +17 -0
  74. data/db/migrate/20150610032852_create_weixin_report_interface_data.rb +15 -0
  75. data/db/migrate/20150610035901_add_user_data_type_to_weixin_report_user_data.rb +5 -0
  76. data/db/migrate/20150610040008_add_news_data_type_to_weixin_report_news_data.rb +5 -0
  77. data/db/migrate/20150610040059_add_msg_data_type_to_weixin_report_msg_data.rb +5 -0
  78. data/db/migrate/20150610040145_add_interface_data_type_to_weixin_report_interface_data.rb +5 -0
  79. data/db/migrate/20150610072952_add_total_online_time_and_user_source_to_weixin_report_news_data.rb +6 -0
  80. data/db/migrate/20150610084545_add_user_source_to_weixin_report_msg_data.rb +5 -0
  81. data/db/migrate/20150610090206_create_weixin_user.rb +153 -0
  82. data/db/migrate/20150612104303_add_index_to_weixin_message_logs.rb +7 -0
  83. data/db/migrate/20150625065249_delete_clomn_weixin_token_from_public_account.rb +5 -0
  84. data/lib/eric_weixin/engine.rb +9 -0
  85. data/lib/eric_weixin/modules/analyze_data.rb +124 -0
  86. data/lib/eric_weixin/modules/ip.rb +33 -0
  87. data/lib/eric_weixin/modules/mult_customer.rb +113 -0
  88. data/lib/eric_weixin/{app/moudles → modules}/reply_message.rb +3 -3
  89. data/lib/eric_weixin/{app/moudles → modules}/snsapi.rb +3 -3
  90. data/lib/eric_weixin/version.rb +1 -1
  91. data/lib/eric_weixin.rb +6 -24
  92. data/lib/tasks/eric_weixin_tasks.rake +4 -0
  93. data/test/dummy/README.rdoc +28 -0
  94. data/test/dummy/Rakefile +6 -0
  95. data/test/dummy/app/assets/javascripts/application.js +13 -0
  96. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  97. data/test/dummy/app/controllers/application_controller.rb +5 -0
  98. data/test/dummy/app/helpers/application_helper.rb +2 -0
  99. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  100. data/test/dummy/bin/bundle +3 -0
  101. data/test/dummy/bin/rails +4 -0
  102. data/test/dummy/bin/rake +4 -0
  103. data/test/dummy/config/application.rb +23 -0
  104. data/test/dummy/config/boot.rb +5 -0
  105. data/test/dummy/config/database.yml +25 -0
  106. data/test/dummy/config/environment.rb +5 -0
  107. data/test/dummy/config/environments/development.rb +37 -0
  108. data/test/dummy/config/environments/production.rb +78 -0
  109. data/test/dummy/config/environments/test.rb +39 -0
  110. data/test/dummy/config/initializers/assets.rb +8 -0
  111. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  112. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  113. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  114. data/test/dummy/config/initializers/inflections.rb +16 -0
  115. data/test/dummy/config/initializers/mime_types.rb +4 -0
  116. data/test/dummy/config/initializers/session_store.rb +3 -0
  117. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  118. data/test/dummy/config/locales/en.yml +23 -0
  119. data/test/dummy/config/routes.rb +4 -0
  120. data/test/dummy/config/secrets.yml +22 -0
  121. data/test/dummy/config.ru +4 -0
  122. data/test/dummy/public/404.html +67 -0
  123. data/test/dummy/public/422.html +67 -0
  124. data/test/dummy/public/500.html +66 -0
  125. data/test/dummy/public/favicon.ico +0 -0
  126. data/test/eric_weixin_test.rb +7 -0
  127. data/test/integration/navigation_test.rb +10 -0
  128. data/test/test_helper.rb +19 -0
  129. metadata +165 -196
  130. data/.gitignore +0 -23
  131. data/.idea/.name +0 -1
  132. data/.idea/.rakeTasks +0 -7
  133. data/.idea/encodings.xml +0 -5
  134. data/.idea/eric_weixin.iml +0 -22
  135. data/.idea/misc.xml +0 -5
  136. data/.idea/modules.xml +0 -9
  137. data/.idea/scopes/scope_settings.xml +0 -5
  138. data/.idea/vcs.xml +0 -7
  139. data/.rvmrc +0 -1
  140. data/Gemfile +0 -4
  141. data/README.md +0 -29
  142. data/eric_weixin.gemspec +0 -32
  143. data/lib/eric_weixin/app/model/template_message_log.rb +0 -75
  144. data/lib/eric_weixin/app/model/weixin_user.rb +0 -96
  145. data/lib/eric_weixin/app/moudles/mult_customer.rb +0 -57
@@ -0,0 +1,99 @@
1
+ class EricWeixin::CustomsServiceRecord < ActiveRecord::Base
2
+ self.table_name = 'weixin_customs_service_records'
3
+ belongs_to :public_account, class_name: "PublicAccount", foreign_key: "weixin_public_account_id"
4
+ OPERCODE = {
5
+ 1000 => "创建未接入会话",
6
+ 1001 => "接入会话",
7
+ 1002 => "主动发起会话",
8
+ 1004 => "关闭会话",
9
+ 1005 => "抢接会话",
10
+ 2001 => "公众号收到消息",
11
+ 2002 => "客服发送消息",
12
+ 2003 => "客服收到消息"
13
+ }
14
+
15
+ validates_uniqueness_of :time, scope: [:openid, :weixin_public_account_id, :text], message: '客服聊天记录不能重复。'
16
+
17
+ def self.create_one options
18
+ self.transaction do
19
+ options = get_arguments_options options, [:weixin_public_account_id, :openid, :opercode, :text, :time, :worker]
20
+ re = self.new options
21
+ re.save!
22
+ re.reload
23
+ re
24
+ end
25
+
26
+ end
27
+
28
+ def self.exist_one options
29
+ options = get_arguments_options options, [:weixin_public_account_id, :openid, :opercode, :text, :time, :worker]
30
+ self.where( options ).count >= 1
31
+ end
32
+
33
+
34
+ # 批量获取用户与客服的聊天记录保存到数据库.
35
+ # ===参数说明
36
+ # * chat_date #聊天日期
37
+ # ===调用说明
38
+ # ::EricWeixin::CustomsServiceRecord.batch_get_customs_records '2015-6-9'.to_time
39
+ def self.batch_get_customs_records chat_date
40
+ self.transaction do
41
+ chat_date = chat_date.to_time
42
+ start_time_unix = chat_date.change(hour: 0, min: 0, sec: 0).to_i
43
+ end_time_unix = chat_date.change(hour: 23, min:59, sec: 59).to_i
44
+ message_logs = ::EricWeixin::MessageLog.where("create_time between ? and ? ", start_time_unix, end_time_unix)
45
+ message_logs = message_logs.where(event_name: 'kf_create_session', process_status: 1).group(:openid, :weixin_public_account_id)
46
+ message_logs.each do |message_log|
47
+ options = {
48
+ :weixin_public_account_id=>message_log.weixin_public_account_id,
49
+ :openid=>message_log.openid,
50
+ :starttime=>start_time_unix,
51
+ :endtime=>end_time_unix,
52
+ :pageindex=>1
53
+ }
54
+ i = 1
55
+ has_record = true
56
+ while has_record
57
+ options[:pageindex] = i
58
+ result_code, has_record = ::EricWeixin::MultCustomer.get_customer_service_messages options
59
+ BusinessException.raise '获取聊天记录失败' unless result_code == 0
60
+ i += 1
61
+ BusinessException.raise '此人聊天记录竟然上了5000条!' if i >= 100
62
+ end
63
+ all_message_logs = ::EricWeixin::MessageLog.where("create_time between ? and ? ", start_time_unix, end_time_unix)
64
+ all_message_logs = all_message_logs.where(event_name: 'kf_create_session', process_status: 1, openid: message_log.openid, weixin_public_account_id: message_log.weixin_public_account_id)
65
+ all_message_logs.each{ |ml| ml.update_attribute :process_status, 0 }
66
+ end
67
+ end
68
+ end
69
+
70
+ def nick_name
71
+ ::EricWeixin::WeixinUser.find_by_openid(self.openid).nickname rescue ''
72
+ end
73
+
74
+ def self.common_query options
75
+ records = self.all
76
+
77
+ records = records.where(weixin_public_account_id: options[:public_account_id]) unless options[:public_account_id].blank?
78
+
79
+ records = records.where(opercode: options[:opercode]) unless options[:opercode].blank?
80
+
81
+ unless options[:chat_date].blank?
82
+ start_time = options[:chat_date].to_time.change(hour:0, min:0, sec:0).to_i
83
+ end_time = options[:chat_date].to_time.change(hour:23, min:59, sec:59).to_i
84
+ records = records.where("time between ? and ?", start_time, end_time)
85
+ end
86
+
87
+ #todo 这里的查询方式有可能有问题,建议取消此类查询。
88
+ records = records.where("text like ?", "%#{options[:chat_content]}%") unless options[:chat_content].blank?
89
+
90
+ records = records.where("worker like ?", "%#{options[:worker]}%") unless options[:worker].blank?
91
+
92
+ unless options[:nick_name].blank?
93
+ records = records.joins('LEFT JOIN weixin_users ON weixin_users.openid = weixin_customs_service_records.openid')
94
+ records = records.where("weixin_users.nickname like ?", "%#{CGI::escape(options[:nick_name])}%")
95
+ end
96
+
97
+ records
98
+ end
99
+ end
@@ -1,5 +1,11 @@
1
1
  class EricWeixin::MessageLog < ActiveRecord::Base
2
+
3
+ STATUS = {0 => '正常', 1 => '待处理'}
2
4
  self.table_name = "weixin_message_logs"
5
+ belongs_to :public_account, class_name: "::EricWeixin::PublicAccount", foreign_key: "weixin_public_account_id"
6
+ validates_presence_of :public_account,
7
+ message: '公众账号必须存在'
8
+
3
9
  class << self
4
10
 
5
11
  def create_public_account_receive_message_log options
@@ -15,8 +21,8 @@ class EricWeixin::MessageLog < ActiveRecord::Base
15
21
  end
16
22
 
17
23
  def create_message_log options
18
- EricWeixin::MessageLog.transaction do
19
- log = EricWeixin::MessageLog.new options.select{|k,v| [:openid,:event_key, :weixin_public_account_id, :message_type, :message_id, :data, :account_receive_flg, :passive_reply_message, :process_status, :event_name, :create_time, :parent_id].include? k }
24
+ ::EricWeixin::MessageLog.transaction do
25
+ log = ::EricWeixin::MessageLog.new options.select{|k,v| [:openid,:event_key, :weixin_public_account_id, :message_type, :message_id, :data, :account_receive_flg, :passive_reply_message, :process_status, :event_name, :create_time, :parent_id].include? k }
20
26
  log.save!
21
27
  log
22
28
  end
@@ -1,27 +1,31 @@
1
1
  class EricWeixin::PublicAccount < ActiveRecord::Base
2
+
2
3
  require "rest-client"
3
4
  self.table_name = "weixin_public_accounts"
4
- has_many :weixin_users, :class_name => '::EricWeixin::WeixinUser', foreign_key: "weixin_public_account_id"
5
- has_many :two_dimension_codes, :class_name => '::EricWeixin::TwoDimensionCode', foreign_key: "weixin_public_account_id"
6
- has_one :access_token, :class_name => '::EricWeixin::AccessToken', foreign_key: "public_account_id"
7
-
5
+ has_many :weixin_users, :class_name => 'WeixinUser', foreign_key: "weixin_public_account_id"
6
+ has_many :two_dimension_codes, :class_name => 'TwoDimensionCode', foreign_key: "weixin_public_account_id"
7
+ has_one :access_token, :class_name => 'AccessToken', foreign_key: "public_account_id"
8
+ has_many :customs_service_records, class_name: 'CustomsServiceRecord', foreign_key: "weixin_public_account_id"
8
9
  #
9
10
  # def self.get_secret app_id
10
11
  # account = ::EricWeixin::PublicAccount.where(weixin_app_id: app_id).first
11
12
  # account.weixin_secret_key
12
13
  # end
13
14
 
14
- #::EricWeixin::PublicAccount.first.get_user_data_from_weixin_api 'osyUtswoeJ9d7p16RdpC5grOeukQ'
15
- #返回Hash信息
15
+ # 获取用户基本信息.
16
+ # ===参数说明
17
+ # * openid #用户openid
18
+ # ===调用示例
19
+ # ::EricWeixin::PublicAccount.first.get_user_data_from_weixin_api 'osyUtswoeJ9d7p16RdpC5grOeukQ'
16
20
  def get_user_data_from_weixin_api openid
17
- token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: self.id
18
- response = RestClient.get "https://api.weixin.qq.com/cgi-bin/user/info?access_token=#{token}&openid=#{openid}&lang=zh_CN"
19
- response = JSON.parse response.body
20
- response["nickname"] = CGI::escape(response["nickname"]) if not response["nickname"].blank?
21
- response
21
+ ::EricWeixin::WeixinUser.get_user_data_from_weixin_api self.id, openid
22
22
  end
23
23
 
24
- #::EricWeixin::PublicAccount.first.weixin_menus
24
+ # 获取微信菜单.
25
+ # ===参数说明
26
+ # * 无。
27
+ # ===调用示例
28
+ # ::EricWeixin::PublicAccount.first.weixin_menus
25
29
  def weixin_menus
26
30
  token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: self.id
27
31
  response = RestClient.get "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=#{token}"
@@ -29,7 +33,10 @@ class EricWeixin::PublicAccount < ActiveRecord::Base
29
33
  response['menu']
30
34
  end
31
35
 
32
- #
36
+ # 创建新的公众号菜单.
37
+ # ===参数说明
38
+ # * menu_json #要添加的公众号菜单 json 内容
39
+ # ===调用示例
33
40
  # ::EricWeixin::PublicAccount.first.create_menu '{
34
41
  # "button":[
35
42
  # {
@@ -80,6 +87,10 @@ class EricWeixin::PublicAccount < ActiveRecord::Base
80
87
  end
81
88
  end
82
89
 
90
+ # 获取用户列表,并把最新的用户信息存到数据库.
91
+ # ===参数说明
92
+ # * next_openid #拉取列表的后一个用户的 next_openid,用户列表未拉取完时存在。
93
+ # ===调用示例
83
94
  # ::EricWeixin::PublicAccount.first.rebuild_users
84
95
  def rebuild_users next_openid = nil
85
96
  token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: self.id
@@ -183,4 +194,4 @@ class EricWeixin::PublicAccount < ActiveRecord::Base
183
194
  48001 => "api功能未授权",
184
195
  50001 => "用户未授权该api"
185
196
  }
186
- end
197
+ end
@@ -3,10 +3,17 @@ class EricWeixin::ReplyMessageRule < ActiveRecord::Base
3
3
  scope :valid, -> { where(:is_valid => true) }
4
4
  belongs_to :weixin_public_account, :class_name => '::EricWeixin::PublicAccount', foreign_key: "weixin_public_account_id"
5
5
  delegate :name, to: :weixin_public_account, prefix: true, allow_nil: true
6
-
7
6
  KEY_WORD_TYPE_LABEL = {"keyword" => '字符', 'regularexpr' => '正则表达式'}
8
7
  REPLY_TYPE_LABEL = {"text" => '静态字符串', 'wx_function' => '动态运行', 'news' => '多图文'}
9
8
 
9
+ validates_presence_of :key_word, message: "关键词不能为空。"
10
+ validates_presence_of :reply_message, message: "回复信息不能为空。"
11
+ validates_presence_of :weixin_public_account, message: "对应的微信公众账号不能为空。"
12
+ validates_inclusion_of :reply_type, in: REPLY_TYPE_LABEL.collect{|r| r.first}, message: "不正确的回复类型。"
13
+ validates_inclusion_of :key_word_type, in: KEY_WORD_TYPE_LABEL.collect{|r| r.first}, message: "不正确的关键词类型。"
14
+
15
+
16
+
10
17
  class << self
11
18
 
12
19
  def create_reply_message_rule options
@@ -20,33 +27,40 @@ class EricWeixin::ReplyMessageRule < ActiveRecord::Base
20
27
 
21
28
  def update_reply_message_rule(rule_id, options)
22
29
  options = get_arguments_options options, [:weixin_public_account_id, :key_word, :reply_message, :key_word_type, :order, :reply_type, :is_valid]
23
- EricWeixin::ReplyMessageRule.transaction do
24
- rule = EricWeixin::ReplyMessageRule.find(rule_id)
30
+ ::EricWeixin::ReplyMessageRule.transaction do
31
+ rule = ::EricWeixin::ReplyMessageRule.find(rule_id)
25
32
  rule.update_attributes(options)
26
33
  rule.save!
27
34
  rule
28
35
  end
29
36
  end
30
37
 
38
+
39
+ #todo xiameng 注释 勾子使用方法
31
40
  def process_rule(receive_message, public_account)
32
41
  business_type = "#{receive_message[:MsgType]}~#{receive_message[:Event]}"
33
- pa = ::EricWeixin::PublicAccount.find_by_weixin_number receive_message[:ToUserName]
42
+
43
+ #兼容腾讯的一个坑....有的是MsgId, 有的是MsgID
44
+ receive_message[:MsgId] = receive_message[:MsgID] if (!receive_message[:MsgID].blank? and receive_message[:MsgId].blank?)
45
+
46
+
34
47
  log = ::EricWeixin::MessageLog.create_public_account_receive_message_log openid: receive_message[:FromUserName],
35
- weixin_public_account_id: pa.id,
48
+ weixin_public_account_id: public_account.id,
36
49
  message_type: receive_message[:MsgType],
37
- message_id: receive_message[:MsgId],
50
+ message_id: receive_message[:MsgId] || receive_message[:MsgID],
38
51
  data: receive_message.to_json,
39
52
  process_status: 0, #在这里假设都处理完毕,由业务引起的更新请在工程的Process中进行修改。
40
53
  event_name: receive_message[:Event],
41
54
  event_key: receive_message[:EventKey], #事件值
42
55
  create_time: receive_message[:CreateTime]
56
+ business_type.to_debug
43
57
 
44
58
  reply_message = case business_type
45
59
  #订阅
46
60
  when /event~subscribe/
61
+ ::EricWeixin::WeixinUser.create_weixin_user public_account.id, receive_message[:FromUserName],receive_message[:EventKey]
47
62
  result = ::Weixin::Process.subscribe receive_message
48
63
  if result == true
49
- ::EricWeixin::WeixinUser.create_weixin_user public_account.id, receive_message[:FromUserName],receive_message[:EventKey]
50
64
  match_key_words 'subscribe', public_account.id, receive_message
51
65
  else
52
66
  result
@@ -89,6 +103,25 @@ class EricWeixin::ReplyMessageRule < ActiveRecord::Base
89
103
  result
90
104
  end
91
105
 
106
+ when /event~kf_close_session/
107
+ result = ::Weixin::Process.kv_close_session receive_message
108
+ if result == true
109
+ ''
110
+ else
111
+ result
112
+ end
113
+
114
+ when /event~kf_create_session/
115
+ #待取回客服聊天列表,所以标记为待处理
116
+ log.process_status = 1
117
+ log.save!
118
+ result = ::Weixin::Process.kv_create_session receive_message
119
+ if result == true
120
+ ''
121
+ else
122
+ result
123
+ end
124
+
92
125
  #用户自动上报地理位置信息
93
126
  when /event~LOCATION/
94
127
  result = ::Weixin::Process.auto_location_event receive_message
@@ -99,7 +132,7 @@ class EricWeixin::ReplyMessageRule < ActiveRecord::Base
99
132
  end
100
133
 
101
134
  #用户共享地理位置信息
102
- when /location/
135
+ when /location~/
103
136
  result = ::Weixin::Process.location_event receive_message
104
137
  if result == true
105
138
  ''
@@ -122,8 +155,18 @@ class EricWeixin::ReplyMessageRule < ActiveRecord::Base
122
155
  result
123
156
  end
124
157
 
158
+ when /link~/
159
+ result = ::Weixin::Process.link_event receive_message
160
+ if result == true
161
+ ''
162
+ else
163
+ result
164
+ end
165
+
125
166
  #暂时识别不了的消息
126
167
  else
168
+ "暂时未处理的场景".to_logger
169
+ receive_message.to_logger
127
170
  result = ::Weixin::Process.another_event receive_message
128
171
  if result == true
129
172
  match_key_words 'unknow~words', public_account.id, receive_message
@@ -133,6 +176,7 @@ class EricWeixin::ReplyMessageRule < ActiveRecord::Base
133
176
  end
134
177
  "message_to_wechat:".to_logger
135
178
  reply_message.to_logger
179
+
136
180
  unless receive_message.to_s.blank?
137
181
  log.passive_reply_message = reply_message.to_s
138
182
  log.save!
@@ -142,11 +186,11 @@ class EricWeixin::ReplyMessageRule < ActiveRecord::Base
142
186
  end
143
187
 
144
188
  def match_key_words wx_key_word, public_account_id, receive_message,need_to_mult_service=true
145
- matched_rule = EricWeixin::ReplyMessageRule.order(order: :desc).
189
+ matched_rule = ::EricWeixin::ReplyMessageRule.order(order: :desc).
146
190
  where(:key_word => wx_key_word, :weixin_public_account_id => public_account_id, :key_word_type=>(receive_message[:MsgType]||"keyword")).first
147
191
  if matched_rule.nil?
148
192
  if need_to_mult_service
149
- return EricWeixin::ReplyMessage::transfer_mult_customer_service ToUserName: receive_message[:FromUserName],
193
+ return ::EricWeixin::ReplyMessage::transfer_mult_customer_service ToUserName: receive_message[:FromUserName],
150
194
  FromUserName: receive_message[:ToUserName]
151
195
  else
152
196
  return '' #当匹配不上,也不需要去多客服的时候,就直接返回。
@@ -155,12 +199,12 @@ class EricWeixin::ReplyMessageRule < ActiveRecord::Base
155
199
  end
156
200
  reply_msg = case matched_rule.reply_type
157
201
  when "text"
158
- EricWeixin::ReplyMessage.get_reply_user_message_text ToUserName: receive_message[:FromUserName],
202
+ ::EricWeixin::ReplyMessage.get_reply_user_message_text ToUserName: receive_message[:FromUserName],
159
203
  FromUserName: receive_message[:ToUserName],
160
204
  Content: matched_rule.reply_message
161
205
  when "news"
162
206
  weixin_news = ::EricWeixin::News.find_by_match_key matched_rule.reply_message
163
- EricWeixin::ReplyMessage::get_reply_user_message_image_text ToUserName: receive_message[:FromUserName],
207
+ ::EricWeixin::ReplyMessage::get_reply_user_message_image_text ToUserName: receive_message[:FromUserName],
164
208
  FromUserName: receive_message[:ToUserName],
165
209
  news: weixin_news.weixin_articles
166
210
  when "wx_function"
@@ -0,0 +1,120 @@
1
+ class EricWeixin::Report::InterfaceData < ActiveRecord::Base
2
+ self.table_name = 'weixin_report_interface_data'
3
+
4
+ INTERFACE_DATA_TYPE = ['summary', 'summary_hour']
5
+
6
+ validates_presence_of :interface_data_type, message: "数据类型不可以为空。"
7
+ validates_inclusion_of :interface_data_type, in: INTERFACE_DATA_TYPE, message: "不正确的数据类型,只能为summary、summary_hour其中一个。"
8
+
9
+ # 自动去微信服务器拉取当日之前的统计模块的接口数据.
10
+ # ===参数说明
11
+ # * weixin_public_account_id # 微信公众号ID
12
+ # ===调用实例
13
+ # ::EricWeixin::Report::InterfaceData.auto_execute_get_and_save_data_from_weixin 1
14
+ def self.auto_execute_get_and_save_data_from_weixin weixin_public_account_id
15
+ self.transaction do
16
+ yesterday = (Time.now - 1.day).to_date.to_s
17
+ # 取当天的前一天数据
18
+ get_and_save_interface_summary yesterday, yesterday, weixin_public_account_id
19
+ get_and_save_interface_summary_hour yesterday, yesterday, weixin_public_account_id
20
+ end
21
+ end
22
+
23
+ # 创建一个微信接口数据interface_data.
24
+ # ===参数说明
25
+ # * ref_date # 数据的日期
26
+ # * ref_hour # 数据的小时,包括从000到2300,分别代表的是[000,100)到[2300,2400),即每日的第1小时和最后1小时
27
+ # * callback_count # 通过服务器配置地址获得消息后,被动回复用户消息的次数
28
+ # * fail_count # 上述动作的失败次数
29
+ # * total_time_cost # 总耗时,除以callback_count即为平均耗时
30
+ # * max_time_cost # 最大耗时
31
+ # * weixin_public_account_id # 公众账号ID
32
+ # * interface_data_type # 数据类型,包括summary、summary_hour
33
+ # ===调用实例
34
+ # options = {ref_date: '2015-6-6', ref_hour: 0, weixin_public_account_id: 1, interface_data_type: 'summary' ... }
35
+ # ::EricWeixin::Report::InterfaceData.create_interface_data options
36
+ def self.create_interface_data options
37
+ self.transaction do
38
+ options = get_arguments_options options, [:ref_date, :ref_hour, :callback_count, :fail_count, :total_time_cost, :max_time_cost,
39
+ :weixin_public_account_id, :interface_data_type]
40
+ interface_data = self.new options
41
+ interface_data.save!
42
+ interface_data.reload
43
+ interface_data
44
+ end
45
+ end
46
+
47
+ # 通过参数确定是否存在这样一个微信接口数据interface_data.
48
+ # ===参数说明
49
+ # * ref_date # 数据的日期
50
+ # * ref_hour # 数据的小时,包括从000到2300,分别代表的是[000,100)到[2300,2400),即每日的第1小时和最后1小时
51
+ # * callback_count # 通过服务器配置地址获得消息后,被动回复用户消息的次数
52
+ # * fail_count # 上述动作的失败次数
53
+ # * total_time_cost # 总耗时,除以callback_count即为平均耗时
54
+ # * max_time_cost # 最大耗时
55
+ # * weixin_public_account_id # 公众账号ID
56
+ # * interface_data_type # 数据类型,包括summary、summary_hour
57
+ # ===调用实例
58
+ # options = {ref_date: '2015-6-6', ref_hour: 0, weixin_public_account_id: 1, interface_data_type: 'summary' ... }
59
+ # ::EricWeixin::Report::InterfaceData.exist options
60
+ # ===返回
61
+ # true 代表存在
62
+ # false 代表不存在
63
+ def self.exist options
64
+ options = get_arguments_options options, [:ref_date, :ref_hour, :callback_count, :fail_count, :total_time_cost, :max_time_cost,
65
+ :weixin_public_account_id, :interface_data_type]
66
+ self.where( options ).count >= 1
67
+ end
68
+
69
+ # 获得公众平台官网数据统计模块中接口分析数据&保存到数据库.
70
+ # ===参数说明
71
+ # * begin_date # 获取数据的起始日期,begin_date和end_date的差值需小于30
72
+ # * end_date # 获取数据的结束日期,end_date允许设置的最大值为昨日
73
+ # * weixin_public_account_id # 公众号ID
74
+ # ===调用实例
75
+ # ::EricWeixin::Report::InterfaceData.get_and_save_interface_summary '2015-5-1', '2015-5-30', 1
76
+ def self.get_and_save_interface_summary begin_date, end_date, weixin_public_account_id
77
+ self.transaction do
78
+ options = {
79
+ :begin_date => begin_date,
80
+ :end_date => end_date,
81
+ :weixin_public_account_id => weixin_public_account_id
82
+ }
83
+ interface_summary = ::EricWeixin::AnalyzeData.get_interface_summary options
84
+ pp "############################ interface_summary ####################################"
85
+ pp interface_summary
86
+ list_summary = interface_summary["list"]
87
+ list_summary.each do |s|
88
+ s = s.merge(interface_data_type: 'summary').merge(weixin_public_account_id: weixin_public_account_id)
89
+ self.create_interface_data s unless self.exist s
90
+ end unless list_summary.blank?
91
+
92
+ end
93
+ end
94
+
95
+ # 获得公众平台官网数据统计模块中接口分析分时数据&保存到数据库.
96
+ # ===参数说明
97
+ # * begin_date # 获取数据的起始日期,begin_date和end_date的差值需小于1
98
+ # * end_date # 获取数据的结束日期,end_date允许设置的最大值为昨日
99
+ # * weixin_public_account_id # 公众号ID
100
+ # ===调用实例
101
+ # ::EricWeixin::Report::InterfaceData.get_and_save_interface_summary_hour '2015-5-1', '2015-5-1', 1
102
+ def self.get_and_save_interface_summary_hour begin_date, end_date, weixin_public_account_id
103
+ self.transaction do
104
+ options = {
105
+ :begin_date => begin_date,
106
+ :end_date => end_date,
107
+ :weixin_public_account_id => weixin_public_account_id
108
+ }
109
+ interface_summary_hour = ::EricWeixin::AnalyzeData.get_interface_summary_hour options
110
+ pp "############################ interface_summary_hour ####################################"
111
+ pp interface_summary_hour
112
+ list_summary_hour = interface_summary_hour["list"]
113
+ list_summary_hour.each do |s|
114
+ s = s.merge(interface_data_type: 'summary_hour').merge(weixin_public_account_id: weixin_public_account_id)
115
+ self.create_interface_data s unless self.exist s
116
+ end unless list_summary_hour.blank?
117
+
118
+ end
119
+ end
120
+ end