Module: Model::Request
- Extended by:
- ActiveSupport::Concern
- Included in:
- Wechat::Request
- Defined in:
- app/models/wechat/model/request.rb
Instance Method Summary collapse
- #do_encrypt ⇒ Object
- #generate_wechat_user ⇒ Object
- #get_reply ⇒ Object
- #get_reply! ⇒ Object
- #reply_from_response ⇒ Object
- #reply_from_rule ⇒ Object
- #reply_params ⇒ Object
- #rule_tag ⇒ Object
- #sync_to_tag ⇒ Object
- #to_wechat ⇒ Object
- #to_xml ⇒ Object
-
#typing(command = 'Typing') ⇒ Object
Typing CancelTyping.
Instance Method Details
#do_encrypt ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'app/models/wechat/model/request.rb', line 148 def do_encrypt if platform token = platform.token encoding_aes_key = platform.encoding_aes_key encrypt_appid = platform.appid elsif app.encrypt_mode token = app.token encoding_aes_key = app.encoding_aes_key encrypt_appid = appid else return self.reply_body end return if self.reply_body.blank? nonce = SecureRandom.hex(10) encrypt = Base64.strict_encode64(Wechat::Cipher.encrypt(Wechat::Cipher.pack(to_xml, encrypt_appid), encoding_aes_key)) = reply_body['CreateTime'] msg_sign = Wechat::Signature.hexdigest(token, , nonce, encrypt) self.reply_encrypt = { Encrypt: encrypt, MsgSignature: msg_sign, TimeStamp: , Nonce: nonce } end |
#generate_wechat_user ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/models/wechat/model/request.rb', line 101 def generate_wechat_user wechat_user || build_wechat_user wechat_user.appid = appid if ['SCAN', 'subscribe'].include?(event) if body.to_s.start_with?('invite_by_') wechat_user.user_inviter_id = body.delete_prefix('invite_by_') elsif body.to_s.start_with? 'invite_member_' wechat_user.member_inviter_id = body.delete_prefix('invite_member_') end end if wechat_user.new_record? self.init_wechat_user = true end end |
#get_reply ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'app/models/wechat/model/request.rb', line 131 def get_reply reply = reply_from_rule unless reply self.created_at ||= Time.current # for extractor reply = reply_from_response end if reply.is_a?(Reply) self.reply_body = reply.to_wechat self.reply_body.merge!(ToUserName: open_id) self.reply_body.merge!(FromUserName: app.user_name) if app else self.reply_body = {} end do_encrypt end |
#get_reply! ⇒ Object
126 127 128 129 |
# File 'app/models/wechat/model/request.rb', line 126 def get_reply! get_reply save end |
#reply_from_response ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'app/models/wechat/model/request.rb', line 85 def reply_from_response if body.present? res = responses.find_by(match_value: body) else res = responses[0] end res.invoke_effect(self) if res end |
#reply_from_rule ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/models/wechat/model/request.rb', line 69 def reply_from_rule filtered = RailsWechat.config.rules.find do |_, rule| if rule.slice(:msg_type, :event, :event_key) <= self.rule_tag && rule[:body] rule[:body].match? self.body end end if filtered.present? logger.debug "\e[35m Filter Key: #{filtered[1]} \e[0m" r = filtered[1][:proc].call(self) if r.has_content? r end end end |
#reply_params ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/models/wechat/model/request.rb', line 41 def reply_params if wechat_user.attributes['name'].blank? { appid: appid, news_reply_items_attributes: [ { title: '请绑定', description: '授权您的信息', url: app.oauth2_url } ] } elsif wechat_user.user.blank? { appid: appid, news_reply_items_attributes: [ { title: '请绑定', description: '绑定信息', url: Rails.application.routes.url_for(controller: 'auth/sign', action: 'sign', uid: open_id, host: app.host) } ] } else {} end end |
#rule_tag ⇒ Object
33 34 35 36 37 38 39 |
# File 'app/models/wechat/model/request.rb', line 33 def rule_tag { msg_type: msg_type, event: event&.downcase, event_key: event_key }.compact end |
#sync_to_tag ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'app/models/wechat/model/request.rb', line 117 def sync_to_tag tag || build_tag user_tag || build_user_tag if user_tag.new_record? self.init_user_tag = true end end |
#to_wechat ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 |
# File 'app/models/wechat/model/request.rb', line 175 def to_wechat if reply_encrypt.present? reply_encrypt.to_xml( root: 'xml', children: 'item', skip_instruct: true, skip_types: true) else to_xml end end |
#to_xml ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'app/models/wechat/model/request.rb', line 187 def to_xml if reply_body.blank? 'success' else reply_body.to_xml( root: 'xml', children: 'item', skip_instruct: true, skip_types: true ) end end |
#typing(command = 'Typing') ⇒ Object
Typing CancelTyping
97 98 99 |
# File 'app/models/wechat/model/request.rb', line 97 def typing(command = 'Typing') app.api.(wechat_user.uid, command) end |