weixin_rails_middleware 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/generators/templates/install_weixin_rails_middleware.rb +0 -4
- data/lib/generators/templates/weixin_controller.rb +75 -9
- data/lib/generators/weixin_rails_middleware/install_generator.rb +1 -1
- data/lib/weixin_rails_middleware.rb +0 -1
- data/lib/weixin_rails_middleware/configuration.rb +0 -12
- data/lib/weixin_rails_middleware/helpers/weixin_authorize_helper.rb +28 -13
- data/lib/weixin_rails_middleware/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 328982300f97b4e56ae76baac5df06ae9b8e5605
|
4
|
+
data.tar.gz: 330a98f5c4c6b13cd86a0740b2a91cf6f2a3c4cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 999b82f09d4bf3cc3731e6ab1bf7dd7ad19b3183e2cbb0aef72c49c688e53065b4b201407ed28e4720e0bfd7d1f7792c6180ff20438b35aba771d239e0b646a9
|
7
|
+
data.tar.gz: 9f5ae33310059880dda908ad38bb7faf4aebce6763e5df0c04eb0da65cbc4c5c689a213206dc0017eb4ec2e4ecfdec3fc9cd09b088174fe029abf8efa667476d
|
@@ -16,8 +16,4 @@ WeixinRailsMiddleware.configure do |config|
|
|
16
16
|
# using to weixin server url to validate the token can be trusted.
|
17
17
|
# config.weixin_secret_string = '<%= WeiXinUniqueToken.generate(generator: :urlsafe_base64, size: 24) %>'
|
18
18
|
|
19
|
-
## Router configure ##
|
20
|
-
# Default is "/", and recommend you use default directly.
|
21
|
-
# config.engine_path = "/"
|
22
|
-
|
23
19
|
end
|
@@ -1,7 +1,11 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# 1: get weixin xml params
|
3
|
+
# @weixin_message
|
4
|
+
# 2: public_account_class instance if you setup, otherwise return nil
|
5
|
+
# @weixin_public_account
|
1
6
|
WeixinRailsMiddleware::WeixinController.class_eval do
|
7
|
+
before_filter :set_keyword, only: :reply
|
2
8
|
|
3
|
-
# There are two instance: @weixin_message,
|
4
|
-
# @weixin_public_account(token_model instance if you setup, otherwise return nil)
|
5
9
|
def reply
|
6
10
|
render xml: send("response_#{@weixin_message.MsgType}_message", {})
|
7
11
|
end
|
@@ -9,30 +13,92 @@ WeixinRailsMiddleware::WeixinController.class_eval do
|
|
9
13
|
private
|
10
14
|
|
11
15
|
def response_text_message(options={})
|
12
|
-
reply_text_message("Your Message: #{@
|
16
|
+
reply_text_message("Your Message: #{@keyword}")
|
13
17
|
end
|
14
18
|
|
19
|
+
# <Location_X>23.134521</Location_X>
|
20
|
+
# <Location_Y>113.358803</Location_Y>
|
21
|
+
# <Scale>20</Scale>
|
22
|
+
# <Label><![CDATA[位置信息]]></Label>
|
15
23
|
def response_location_message(options={})
|
16
|
-
|
24
|
+
@lx = @weixin_message.Location_X
|
25
|
+
@ly = @weixin_message.Location_Y
|
26
|
+
@scale = @weixin_message.Scale
|
27
|
+
@label = @weixin_message.Label
|
28
|
+
reply_text_message("Your Location: #{@lx}, #{@ly}, #{@scale}, #{@label}")
|
17
29
|
end
|
18
30
|
|
31
|
+
# <PicUrl><![CDATA[this is a url]]></PicUrl>
|
32
|
+
# <MediaId><![CDATA[media_id]]></MediaId>
|
19
33
|
def response_image_message(options={})
|
20
|
-
|
34
|
+
@pic_url = @weixin_message.PicUrl
|
35
|
+
@media_id = @weixin_message.MediaId # 可以调用多媒体文件下载接口拉取数据。
|
36
|
+
reply_text_message("回复图片信息")
|
21
37
|
end
|
22
38
|
|
39
|
+
# <Title><![CDATA[公众平台官网链接]]></Title>
|
40
|
+
# <Description><![CDATA[公众平台官网链接]]></Description>
|
41
|
+
# <Url><![CDATA[url]]></Url>
|
23
42
|
def response_link_message(options={})
|
24
|
-
|
43
|
+
@title = @weixin_message.Title
|
44
|
+
@desc = @weixin_message.Description
|
45
|
+
@url = @weixin_message.Url
|
46
|
+
reply_text_message("回复链接信息")
|
25
47
|
end
|
26
48
|
|
27
49
|
def response_event_message(options={})
|
28
|
-
|
50
|
+
event_type = @weixin_message.Event
|
51
|
+
case event_type
|
52
|
+
when "subscribe" # 关注公众账号
|
53
|
+
if @keyword.present?
|
54
|
+
# 扫描带参数二维码事件: 1. 用户未关注时,进行关注后的事件推送
|
55
|
+
reply_text_message("扫描带参数二维码事件: 1. 用户未关注时,进行关注后的事件推送, keyword: #{@keyword}")
|
56
|
+
end
|
57
|
+
reply_text_message("关注公众账号")
|
58
|
+
when "unsubscribe" # 取消关注
|
59
|
+
reply_text_message("取消关注")
|
60
|
+
when "SCAN" # 扫描带参数二维码事件: 2用户已关注时的事件推送
|
61
|
+
reply_text_message("扫描带参数二维码事件: 2用户已关注时的事件推送, keyword: #{@keyword}")
|
62
|
+
when "LOCATION" # 上报地理位置事件
|
63
|
+
@lat = @weixin_message.Latitude
|
64
|
+
@lgt = @weixin_message.Longitude
|
65
|
+
@precision = @weixin_message.Precision
|
66
|
+
reply_text_message("Your Location: #{@lat}, #{@lgt}, #{@precision}")
|
67
|
+
when "CLICK" # 点击菜单拉取消息时的事件推送
|
68
|
+
reply_text_message("你点击了: #{@keyword}")
|
69
|
+
when "VIEW" # 点击菜单跳转链接时的事件推送
|
70
|
+
reply_text_message("你点击了: #{@keyword}")
|
71
|
+
else
|
72
|
+
reply_text_message("处理无法识别的事件")
|
73
|
+
end
|
74
|
+
|
29
75
|
end
|
30
76
|
|
77
|
+
# <MediaId><![CDATA[media_id]]></MediaId>
|
78
|
+
# <Format><![CDATA[Format]]></Format>
|
31
79
|
def response_voice_message(options={})
|
32
|
-
|
80
|
+
@media_id = @weixin_message.MediaId # 可以调用多媒体文件下载接口拉取数据。
|
81
|
+
@format = @weixin_message.format
|
82
|
+
reply_text_message("回复语音信息: #{@keyword}")
|
33
83
|
end
|
34
84
|
|
85
|
+
# <MediaId><![CDATA[media_id]]></MediaId>
|
86
|
+
# <ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId>
|
35
87
|
def response_video_message(options={})
|
36
|
-
|
88
|
+
@media_id = @weixin_message.MediaId # 可以调用多媒体文件下载接口拉取数据。
|
89
|
+
# 视频消息缩略图的媒体id,可以调用多媒体文件下载接口拉取数据。
|
90
|
+
@thumb_media_id = @weixin_message.ThumbMediaId
|
91
|
+
reply_text_message("回复视频信息")
|
92
|
+
end
|
93
|
+
|
94
|
+
def set_keyword
|
95
|
+
@keyword = @weixin_message.Content || # 文本消息
|
96
|
+
@weixin_message.EventKey || # 事件推送
|
97
|
+
@weixin_message.Recognition # 接收语音识别结果
|
98
|
+
end
|
99
|
+
|
100
|
+
# http://apidock.com/rails/ActionController/Base/default_url_options
|
101
|
+
def default_url_options(options={})
|
102
|
+
{ weichat_id: @weixin_message.FromUserName }
|
37
103
|
end
|
38
104
|
end
|
@@ -8,7 +8,7 @@ module WeixinRailsMiddleware
|
|
8
8
|
desc 'Creates a WeixinRailsMiddleware initializer for your application.'
|
9
9
|
|
10
10
|
def install
|
11
|
-
route 'mount WeixinRailsMiddleware::Engine, at:
|
11
|
+
route 'mount WeixinRailsMiddleware::Engine, at: "/"'
|
12
12
|
end
|
13
13
|
|
14
14
|
def copy_initializer
|
@@ -18,20 +18,12 @@ module WeixinRailsMiddleware
|
|
18
18
|
# use 'public_account_class': if the token is saved in SomeModel, then find token by it
|
19
19
|
# use 'weixin_token': if the token is a String, just use it,
|
20
20
|
attr_accessor :public_account_class, :weixin_token_string, :weixin_secret_string
|
21
|
-
attr_accessor :engine_path
|
22
|
-
|
23
|
-
def initialize
|
24
|
-
@engine_path = DEFAULT_ENGINE_PATH
|
25
|
-
end
|
26
21
|
|
27
22
|
end
|
28
23
|
|
29
24
|
module ConfigurationHelpers
|
30
25
|
extend ActiveSupport::Concern
|
31
26
|
|
32
|
-
def engine_path
|
33
|
-
@engine_path ||= WeixinRailsMiddleware.config.engine_path
|
34
|
-
end
|
35
27
|
|
36
28
|
def weixin_token_string
|
37
29
|
@weixin_token_string ||= WeixinRailsMiddleware.config.weixin_token_string.to_s
|
@@ -45,10 +37,6 @@ module WeixinRailsMiddleware
|
|
45
37
|
@weixin_secret_string ||= WeixinRailsMiddleware.config.weixin_secret_string.to_s
|
46
38
|
end
|
47
39
|
|
48
|
-
def is_default_engine_path?
|
49
|
-
engine_path == DEFAULT_ENGINE_PATH # "/"
|
50
|
-
end
|
51
|
-
|
52
40
|
def token_model_class
|
53
41
|
if token_model.blank?
|
54
42
|
raise "You need to config `public_account_class` in 'config/initializers/weixin_rails_middleware.rb'"
|
@@ -5,34 +5,49 @@ module WeixinRailsMiddleware
|
|
5
5
|
protected
|
6
6
|
|
7
7
|
def check_weixin_params
|
8
|
-
if is_weixin_secret_key_valid? && is_signature_invalid?
|
9
|
-
render text: "Forbidden", status: 403
|
10
|
-
end
|
11
|
-
end
|
12
8
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
render text: "
|
9
|
+
# if config weixin token string
|
10
|
+
if weixin_token_string.present?
|
11
|
+
if !is_weixin_secret_string_valid?
|
12
|
+
puts "WeixinSecretStringNotMatch"
|
13
|
+
render text: "WeixinSecretStringNotMatch", status: 403
|
18
14
|
return false
|
19
15
|
end
|
16
|
+
# if use database to store public_account
|
20
17
|
else
|
21
|
-
if
|
22
|
-
|
18
|
+
if !is_weixin_secret_key_valid?
|
19
|
+
puts "RecordNotFound"
|
20
|
+
render text: "RecordNotFound - Couldn't find #{token_model} with weixin_secret_key=#{current_weixin_secret_key} ", status: 404
|
23
21
|
return false
|
24
22
|
end
|
25
23
|
end
|
24
|
+
|
25
|
+
if !is_signature_valid?
|
26
|
+
puts "WeixinSignatureNotMatch"
|
27
|
+
render text: "WeixinSignatureNotMatch", status: 403
|
28
|
+
return false
|
29
|
+
end
|
26
30
|
true
|
27
31
|
end
|
28
32
|
|
29
|
-
|
33
|
+
# check the token from Weixin Service is exist in local store.
|
34
|
+
def is_weixin_secret_key_valid?
|
35
|
+
if weixin_token_string.blank?
|
36
|
+
current_weixin_public_account.present?
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def is_weixin_secret_string_valid?
|
41
|
+
current_weixin_secret_key == weixin_secret_string
|
42
|
+
end
|
43
|
+
|
44
|
+
def is_signature_valid?
|
30
45
|
signature = params[:signature] || ''
|
31
46
|
timestamp = params[:timestamp] || ''
|
32
47
|
nonce = params[:nonce] || ''
|
33
48
|
sort_params = [current_weixin_token, timestamp, nonce].sort.join
|
34
49
|
current_signature = Digest::SHA1.hexdigest(sort_params)
|
35
|
-
return true if current_signature
|
50
|
+
return true if current_signature == signature
|
36
51
|
false
|
37
52
|
end
|
38
53
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weixin_rails_middleware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- lanrion
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|