Module: Model::Receive
- Extended by:
- ActiveSupport::Concern
- Included in:
- Wechat::Receive
- Defined in:
- app/models/wechat/model/receive.rb
Constant Summary collapse
- MSG_TYPE =
{ 'text' => 'Wechat::TextRequest', 'image' => 'Wechat::ImageRequest', 'voice' => 'Wechat::VoiceRequest', 'video' => 'Wechat::VideoRequest', 'shortvideo' => 'Wechat::ShortVideoRequest', 'location' => 'Wechat::LocationRequest', 'link' => 'Wechat::LinkRequest', 'event' => 'Wechat::EventRequest' }.freeze
- EVENT =
{ 'subscribe' => 'Wechat::SubscribeRequest', 'unsubscribe' => 'Wechat::UnsubscribeRequest', 'LOCATION' => 'Wechat::LocationRequest', # 公众号与企业微信通用 'CLICK' => 'Wechat::ClickRequest', 'VIEW' => 'Wechat::ViewRequest', 'SCAN' => 'Wechat::ScanRequest', 'click' => 'Wechat::Request', 'view' => 'Wechat::Request', # 企业微信使用 'scancode_push' => 'Wechat::Request', 'scancode_waitmsg' => 'Wechat::Request', 'pic_sysphoto' => 'Wechat::Request', 'pic_photo_or_album' => 'Wechat::Request', 'pic_weixin' => 'Wechat::Request', 'location_select' => 'Wechat::Request', 'enter_agent' => 'Wechat::Request', 'batch_job_result' => 'Wechat::Request' # 企业微信使用 }.freeze
Instance Method Summary collapse
- #check_app ⇒ Object
- #compute_type ⇒ Object
- #decrypt_data ⇒ Object
- #parse_content ⇒ Object
- #parse_message_hash ⇒ Object
Instance Method Details
#check_app ⇒ Object
114 115 116 |
# File 'app/models/wechat/model/receive.rb', line 114 def check_app app.update user_name: ['ToUserName'] if app end |
#compute_type ⇒ Object
71 72 73 74 75 76 77 |
# File 'app/models/wechat/model/receive.rb', line 71 def compute_type if msg_type == 'event' EVENT[['Event']] else MSG_TYPE[msg_type] end end |
#decrypt_data ⇒ Object
57 58 59 60 61 62 63 |
# File 'app/models/wechat/model/receive.rb', line 57 def decrypt_data aes_key = platform ? platform.encoding_aes_key : app.encoding_aes_key r = Wechat::Cipher.decrypt(Base64.decode64(encrypt_data), aes_key) content, _ = Wechat::Cipher.unpack(r) self. = Hash.from_xml(content).fetch('xml', {}) end |
#parse_content ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'app/models/wechat/model/receive.rb', line 79 def parse_content request || build_request(type: compute_type) request.appid = appid request.open_id = open_id request.msg_type = msg_type request.raw_body = .except('ToUserName', 'FromUserName', 'CreateTime', 'MsgType') case msg_type when 'text' request.body = ['Content'] when 'event' request.event = ['Event'] request.event_key = ['EventKey'] || .dig('ScanCodeInfo', 'ScanResult') if request.event == 'subscribe' request.body = request.event_key.delete_prefix('qrscene_') else request.body = request.event_key end when 'image' request.body = ['PicUrl'] when 'voice' request.body = ['Recognition'].presence || ['MediaId'] when 'video', 'shortvideo' request.body = ['MediaId'] when 'location' request.body = "#{['Location_X']}:#{['Location_Y']}" when 'link' request.body = ['Url'] else warn "Don't know how to parse message as #{['MsgType']}", uplevel: 1 end request.generate_wechat_user # Should before get reply self.save # will auto save wechat request end |
#parse_message_hash ⇒ Object
65 66 67 68 69 |
# File 'app/models/wechat/model/receive.rb', line 65 def self.open_id = ['FromUserName'] self.msg_type = ['MsgType'] self.msg_id = ['MsgId'] end |