Class: WeixinPam::PublicAccount

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
WeixinRailsMiddleware::AutoGenerateWeixinTokenSecretKey
Defined in:
app/models/weixin_pam/public_account.rb

Instance Method Summary collapse

Instance Method Details

#build_menuObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/weixin_pam/public_account.rb', line 21

def build_menu
  Jbuilder.encode do |json|
    json.button(parent_menus) do |menu|
      json.name menu.name
      if menu.has_sub_menu?
        json.sub_button(menu.sub_menus) do |sub_menu|
          json.type sub_menu.button_type
          json.name sub_menu.name
          sub_menu.button_type_json(json)
        end
      else
        json.type menu.button_type
        menu.button_type_json(json)
      end
    end
  end
end

#clientObject



12
13
14
# File 'app/models/weixin_pam/public_account.rb', line 12

def client
  @client ||= WeixinAuthorize::Client.new(app_id, app_secret)
end

#download_menu!Object



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
68
69
70
71
72
73
74
75
# File 'app/models/weixin_pam/public_account.rb', line 43

def download_menu!
  ret = client.menu
  fail ApiError::FailedResult.new(ret, '下载公众号菜单失败') unless ret.ok?
  # Reset menu
  diymenus.where(parent: nil).update_all(is_show: false)
  data = ret.result
  return unless data.key?('menu') && data['menu'].key?('button')

  i = 0
  data['menu']['button'].each do |button|
    i += 1
    sub_buttons = button.delete('sub_button')
    button['button_type'] = Diymenu::button_types[button.delete('type')]
    parent_menu = diymenus.find_or_initialize_by(button)
    parent_menu.parent = nil
    parent_menu.is_show = true
    parent_menu.sort = i
    parent_menu.save! if parent_menu.changed?
    parent_menu.diymenus.update_all(parent_id: nil, is_show: false)

    j = 0
    sub_buttons.each do |sub_button|
      j += 1
      sub_button.delete('sub_button')
      sub_button['button_type'] = Diymenu::button_types[sub_button.delete('type')]
      sub_menu = diymenus.find_or_initialize_by(sub_button)
      sub_menu.parent = parent_menu
      sub_menu.is_show = true
      sub_menu.sort = j
      sub_menu.save! if sub_menu.changed?
    end
  end
end

#reply_weixin(message, keyword) ⇒ Object



16
17
18
19
# File 'app/models/weixin_pam/public_account.rb', line 16

def reply_weixin(message, keyword)
  klass = reply_class.present? ? reply_class.constantize : PublicAccountReply
  klass.new(self, message, keyword).reply
end

#temp_qrcode(sence_id = 1) ⇒ Object



77
78
79
# File 'app/models/weixin_pam/public_account.rb', line 77

def temp_qrcode(sence_id = 1)
  client.qr_code_url(client.create_qr_scene(sence_id).result['ticket'])
end

#upload_menuObject



39
40
41
# File 'app/models/weixin_pam/public_account.rb', line 39

def upload_menu
  client.create_menu(build_menu)
end