Module: MpWeixin::ServerHelper

Defined in:
lib/mp_weixin/server_helper.rb

Instance Method Summary collapse

Instance Method Details

#generate_signature(token, timestamp, nonce) ⇒ String

generate a signature string through sha1 encrypt token, timestamp, nonce .

Parameters:

  • token (String)

    the token value

  • timestamp (String)

    the timestamp value from weixin

  • nonce (String)

    the random num from weixin

    加密/校验流程如下:

    1. 将token、timestamp、nonce三个参数进行字典序排序

    2. 将三个参数字符串拼接成一个字符串进行sha1加密

    3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信

Returns:

  • (String)


17
18
19
20
# File 'lib/mp_weixin/server_helper.rb', line 17

def generate_signature(token, timestamp, nonce)
  signature_content = [token.to_s, timestamp.to_s, nonce.to_s].sort.join("")
  Digest::SHA1.hexdigest(signature_content)
end

#reply_image_message(attributes = {}, &block) ⇒ Object

initialize an ImageReplyMessage

Parameters:

  • attributes (Hash) (defaults to: {})

See Also:

  • MpWeixin::ServerHelper.'spec/mp_weixin/models/reply_message_spec'spec/mp_weixin/models/reply_message_spec.rb'


46
47
48
49
50
51
# File 'lib/mp_weixin/server_helper.rb', line 46

def reply_image_message(attributes = {}, &block)
  reply_message = MpWeixin::ImageReplyMessage.new(attributes)
  block.call(reply_message) if block_given?

  reply_message
end

#reply_music_message(attributes = {}, &block) ⇒ Object

initialize an MusicReplyMessage

Parameters:

  • attributes (Hash) (defaults to: {})

See Also:

  • MpWeixin::ServerHelper.'spec/mp_weixin/models/reply_message_spec'spec/mp_weixin/models/reply_message_spec.rb'


76
77
78
79
80
81
# File 'lib/mp_weixin/server_helper.rb', line 76

def reply_music_message(attributes = {}, &block)
  reply_message = MpWeixin::MusicReplyMessage.new(attributes)
  block.call(reply_message) if block_given?

  reply_message
end

#reply_news_message(attributes = {}, &block) ⇒ Object

initialize an NewsReplyMessage

Parameters:

  • attributes (Hash) (defaults to: {})

See Also:

  • MpWeixin::ServerHelper.'spec/mp_weixin/models/reply_message_spec'spec/mp_weixin/models/reply_message_spec.rb'


86
87
88
89
90
91
92
# File 'lib/mp_weixin/server_helper.rb', line 86

def reply_news_message(attributes = {}, &block)
  reply_message = MpWeixin::NewsReplyMessage.new(attributes)

  block.call(reply_message) if block_given?

  reply_message
end

#reply_text_message(attributes = {}) ⇒ Object

initialize an TextReplyMessage

Parameters:

  • attributes (Hash) (defaults to: {})

See Also:

  • MpWeixin::ServerHelper.'spec/mp_weixin/models/reply_message_spec'spec/mp_weixin/models/reply_message_spec.rb'


39
40
41
# File 'lib/mp_weixin/server_helper.rb', line 39

def reply_text_message(attributes = {})
  MpWeixin::TextReplyMessage.new(attributes)
end

#reply_video_message(attributes = {}, &block) ⇒ Object

initialize an VideoReplyMessage

Parameters:

  • attributes (Hash) (defaults to: {})

See Also:

  • MpWeixin::ServerHelper.'spec/mp_weixin/models/reply_message_spec'spec/mp_weixin/models/reply_message_spec.rb'


66
67
68
69
70
71
# File 'lib/mp_weixin/server_helper.rb', line 66

def reply_video_message(attributes = {}, &block)
  reply_message = MpWeixin::VideoReplyMessage.new(attributes)
  block.call(reply_message) if block_given?

  reply_message
end

#reply_voice_message(attributes = {}, &block) ⇒ Object

initialize an VoiceReplyMessage

Parameters:

  • attributes (Hash) (defaults to: {})

See Also:

  • MpWeixin::ServerHelper.'spec/mp_weixin/models/reply_message_spec'spec/mp_weixin/models/reply_message_spec.rb'


56
57
58
59
60
61
# File 'lib/mp_weixin/server_helper.rb', line 56

def reply_voice_message(attributes = {}, &block)
  reply_message = MpWeixin::VoiceReplyMessage.new(attributes)
  block.call(reply_message) if block_given?

  reply_message
end

#valid_signature?(signature, timestamp, nonce) ⇒ Boolean

Whether or not the signature is eql with local_signature

Parameters:

  • signature (String)

    the signature value need validate

  • timestamp (String)

    the timestamp value from weixin

  • nonce (String)

    the nonce value

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'lib/mp_weixin/server_helper.rb', line 29

def valid_signature?(signature, timestamp, nonce)
  token = Config.token

  local_signature = generate_signature(token,timestamp,nonce)
  local_signature.eql? signature
end