module MpWeixin::ServerHelper
Public Instance Methods
generate a signature string through sha1 encrypt token, timestamp, nonce .
@param [String] token the token value @param [String] timestamp the timestamp value from weixin @param [String] nonce the random num from weixin
加密/校验流程如下: 1. 将token、timestamp、nonce三个参数进行字典序排序 2. 将三个参数字符串拼接成一个字符串进行sha1加密 3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
@return [String]
# 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
initialize an ImageReplyMessage
@param [Hash] attributes @see ‘spec/mp_weixin/models/reply_message_spec.rb’
# 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
initialize an MusicReplyMessage
@param [Hash] attributes @see ‘spec/mp_weixin/models/reply_message_spec.rb’
# 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
initialize an NewsReplyMessage
@param [Hash] attributes @see ‘spec/mp_weixin/models/reply_message_spec.rb’
# 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
initialize an TextReplyMessage
@param [Hash] attributes @see ‘spec/mp_weixin/models/reply_message_spec.rb’
# File lib/mp_weixin/server_helper.rb, line 39 def reply_text_message(attributes = {}) MpWeixin::TextReplyMessage.new(attributes) end
initialize an VideoReplyMessage
@param [Hash] attributes @see ‘spec/mp_weixin/models/reply_message_spec.rb’
# 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
initialize an VoiceReplyMessage
@param [Hash] attributes @see ‘spec/mp_weixin/models/reply_message_spec.rb’
# 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
Whether or not the signature is eql with local_signature
@param [String] signature the signature value need validate @param [String] timestamp the timestamp value from weixin @param [String] nonce the nonce value
@return [Boolean]
# 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