Module: Model::App
- Extended by:
- ActiveSupport::Concern
- Included in:
- Wechat::App
- Defined in:
- app/models/wechat/model/app.rb
Instance Method Summary collapse
- #api ⇒ Object
- #default_menus ⇒ Object
- #host ⇒ Object
- #jsapi_ticket ⇒ Object
- #jsapi_ticket_valid? ⇒ Boolean
- #menu ⇒ Object
- #oauth2_qrcode_url(**host_options) ⇒ Object
- #refresh_access_token ⇒ Object
- #refresh_jsapi_ticket ⇒ Object
- #store_access_token(token_hash) ⇒ Object
- #store_jsapi_ticket(ticket_hash) ⇒ Object
- #sync_menu ⇒ Object
- #sync_tags ⇒ Object
-
#sync_template_configs ⇒ Object
公众号.
-
#sync_templates ⇒ Object
小程序.
- #template_ids(notifiable_type, *code) ⇒ Object
- #url ⇒ Object
- #within_menus ⇒ Object
Instance Method Details
#api ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'app/models/wechat/model/app.rb', line 130 def api return @api if defined? @api if secret.present? @api = Wechat::Api::Public.new(self) elsif agency @api = Wechat::Api::Public.new(agency) else raise 'Must has secret or under agency' end end |
#default_menus ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'app/models/wechat/model/app.rb', line 73 def if organ && organ.respond_to?(:limit_wechat_menu) limit = 3 - organ. else limit = 3 end Menu.where(parent_id: nil, appid: nil).limit(limit).as_json end |
#host ⇒ Object
195 196 197 198 199 |
# File 'app/models/wechat/model/app.rb', line 195 def host if oauth_enable domain.presence || organ_domain&.identifier || organ_domains.first&.identifier end end |
#jsapi_ticket ⇒ Object
105 106 107 108 109 110 111 |
# File 'app/models/wechat/model/app.rb', line 105 def jsapi_ticket if jsapi_ticket_valid? super else refresh_jsapi_ticket end end |
#jsapi_ticket_valid? ⇒ Boolean
113 114 115 116 |
# File 'app/models/wechat/model/app.rb', line 113 def jsapi_ticket_valid? return false unless jsapi_ticket_expires_at.acts_like?(:time) jsapi_ticket_expires_at > Time.current end |
#menu ⇒ Object
67 68 69 70 71 |
# File 'app/models/wechat/model/app.rb', line 67 def { button: + } end |
#oauth2_qrcode_url(**host_options) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 |
# File 'app/models/wechat/model/app.rb', line 141 def oauth2_qrcode_url(**) q = { appid: appid, redirect_uri: Rails.application.routes.url_for(controller: 'wechat/apps', action: 'show', id: id, **), scope: 'snsapi_login', response_type: 'code', state: SecureRandom.hex(16) } "https://open.weixin.qq.com/connect/qrconnect?#{q.to_query}#wechat_redirect" end |
#refresh_access_token ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'app/models/wechat/model/app.rb', line 90 def refresh_access_token r = api.token if r['access_token'] store_access_token(r) else logger.debug "\e[35m #{r['errmsg']} \e[0m" end end |
#refresh_jsapi_ticket ⇒ Object
118 119 120 121 122 |
# File 'app/models/wechat/model/app.rb', line 118 def refresh_jsapi_ticket r = api.jsapi_ticket store_jsapi_ticket(r) jsapi_ticket end |
#store_access_token(token_hash) ⇒ Object
99 100 101 102 103 |
# File 'app/models/wechat/model/app.rb', line 99 def store_access_token(token_hash) self.access_token = token_hash['access_token'] self.access_token_expires_at = Time.current + token_hash['expires_in'].to_i self.save end |
#store_jsapi_ticket(ticket_hash) ⇒ Object
124 125 126 127 128 |
# File 'app/models/wechat/model/app.rb', line 124 def store_jsapi_ticket(ticket_hash) self.jsapi_ticket = ticket_hash['ticket'] self.jsapi_ticket_expires_at = Time.current + ticket_hash['expires_in'].to_i self.save end |
#sync_menu ⇒ Object
62 63 64 65 |
# File 'app/models/wechat/model/app.rb', line 62 def api. api. end |
#sync_tags ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'app/models/wechat/model/app.rb', line 153 def results = api. results.fetch('tags', []).each do |result| tag = .find_or_initialize_by(name: result['name']) tag.count = result['count'] tag.tag_id = result['id'] tag.save end .where(tag_id: nil).each do |tag| tag.sync_to_wechat_later end end |
#sync_template_configs ⇒ Object
公众号
178 179 180 181 182 183 184 185 186 187 188 |
# File 'app/models/wechat/model/app.rb', line 178 def sync_template_configs templates = api.templates templates.each do |template| template_config = TemplatePublic.new(title: template['title']) data_keys = Template.new(content: template['content']).data_keys data_keys.each do |key| template_config.template_key_words.build(name: key) end template_config.save end end |
#sync_templates ⇒ Object
小程序
167 168 169 170 171 172 173 174 175 |
# File 'app/models/wechat/model/app.rb', line 167 def sync_templates templates = api.templates templates.each do |template| template = templates.find_or_initialize_by(template_id: template['priTmplId']) template.template_type = template['type'] template.assign_attributes template.slice('title', 'content', 'example') template.save end end |
#template_ids(notifiable_type, *code) ⇒ Object
190 191 192 193 |
# File 'app/models/wechat/model/app.rb', line 190 def template_ids(notifiable_type, *code) ids = TemplateConfig.where(notifiable_type: notifiable_type, code: code).pluck(:id) templates.where(template_config_id: ids).pluck(:template_id) end |
#url ⇒ Object
58 59 60 |
# File 'app/models/wechat/model/app.rb', line 58 def url Rails.application.routes.url_for(controller: 'wechat/wechats', action: 'show', id: self.id, host: host) end |
#within_menus ⇒ Object
82 83 84 85 86 87 88 |
# File 'app/models/wechat/model/app.rb', line 82 def if organ && organ.respond_to?(:limit_wechat_menu) self..limit(organ.).where(parent_id: nil).order(position: :asc).as_json else self..where(parent_id: nil).order(position: :asc).as_json end end |