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

Instance Method Details

#check_appObject



114
115
116
# File 'app/models/wechat/model/receive.rb', line 114

def check_app
  app.update user_name: message_hash['ToUserName'] if app
end

#compute_typeObject



71
72
73
74
75
76
77
# File 'app/models/wechat/model/receive.rb', line 71

def compute_type
  if msg_type == 'event'
    EVENT[message_hash['Event']]
  else
    MSG_TYPE[msg_type]
  end
end

#decrypt_dataObject



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.message_hash = Hash.from_xml(content).fetch('xml', {})
end

#parse_contentObject



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 = message_hash.except('ToUserName', 'FromUserName', 'CreateTime', 'MsgType')
  case msg_type
  when 'text'
    request.body = message_hash['Content']
  when 'event'
    request.event = message_hash['Event']
    request.event_key = message_hash['EventKey'] || message_hash.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 = message_hash['PicUrl']
  when 'voice'
    request.body = message_hash['Recognition'].presence || message_hash['MediaId']
  when 'video', 'shortvideo'
    request.body = message_hash['MediaId']
  when 'location'
    request.body = "#{message_hash['Location_X']}:#{message_hash['Location_Y']}"
  when 'link'
    request.body = message_hash['Url']
  else
    warn "Don't know how to parse message as #{message_hash['MsgType']}", uplevel: 1
  end
  request.generate_wechat_user  # Should before get reply

  self.save  # will auto save wechat request
end

#parse_message_hashObject



65
66
67
68
69
# File 'app/models/wechat/model/receive.rb', line 65

def parse_message_hash
  self.open_id = message_hash['FromUserName']
  self.msg_type = message_hash['MsgType']
  self.msg_id = message_hash['MsgId']
end