Module: WechatPublicApi::Account

Included in:
WechatPublicApi
Defined in:
lib/wechat_public_api/account.rb

Instance Method Summary collapse

Instance Method Details

#get_qrscene(sceneid) ⇒ string

获取临时场景带惨二维码,30天有效

Parameters:

  • sceneid (int)

    – 场景值ID,临时二维码时为32位非0整型,永久二维码时最大值为100000(目前参数只支持1–100000)

Returns:

  • (string)

    url – 二维码网址



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/wechat_public_api/account.rb', line 15

def get_qrscene(sceneid)
  access_token = get_access_token()

  # 获取ticket
  uri = URI.parse("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=#{access_token}")
  post_data = {
      'expire_seconds' => 2592000,
      'action_name' => 'QR_SCENE',
      'action_info' => {'scene' => {'scene_id' => sceneid}}}
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Post.new("/cgi-bin/qrcode/create?access_token=#{access_token}")
  request.add_field('Content-Type', 'application/json')
  request.body = post_data.to_json
  response = http.request(request)
  content = JSON.parse(response.body)
  ticket = content['ticket']

  # 通过ticket换取二维码
  "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=#{ticket}"
end

#get_qrsrtscene(sceneid) ⇒ string

获取永久二维码

Parameters:

  • sceneid (int)

    – 场景值ID,临时二维码时为32位非0整型,永久二维码时最大值为100000(目前参数只支持1–100000)

Returns:

  • (string)

    url – 二维码网址



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/wechat_public_api/account.rb', line 43

def get_qrsrtscene(sceneid)
  access_token = get_access_token()

  # 获取ticket
  uri = URI.parse("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=#{access_token}")
  post_data = {
      'action_name' => 'QR_LIMIT_STR_SCENE',
      'action_info' => {'scene' => {'scene_id' => sceneid}}}
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Post.new("/cgi-bin/qrcode/create?access_token=#{access_token}")
  request.add_field('Content-Type', 'application/json')
  request.body = post_data.to_json
  response = http.request(request)
  content = JSON.parse(response.body)
  ticket = content['ticket']

  # 通过ticket换取二维码
  "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=#{ticket}"
end

#get_shorturl(longurl) ⇒ json, ...

if false

Parameters:

  • longurl (string)

    – 需要被压缩的url

Returns:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/wechat_public_api/account.rb', line 97

def get_shorturl(longurl)
  access_token = get_access_token()
  uri = URI.parse("https://api.weixin.qq.com/cgi-bin/shorturl?access_token=#{access_token}")
  post_data = {action: "long2short", long_url: longurl}
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Post.new("/cgi-bin/shorturl?access_token=#{access_token}")
  request.add_field('Content-Type', 'application/json')
  request.body = post_data.to_json
  response = http.request(request)

  JSON.parse(response.body)
end

#save_qrcode(path, *filename) ⇒ string

保存带参数二维码到指定位置

Parameters:

  • path (string)

    – 例如: “#Rails.root/public/qrcode”

  • filename (string)

    – 文件名,可选参数,默认不填写则使用时间戳+随机数的方式命名

Returns:

  • (string)

    path – 二维码的保存路径



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/wechat_public_api/account.rb', line 71

def save_qrcode(path, *filename)
  # 判断是否需要新建文件
  unless File.exist?(path)
    FileUtils.makedirs(path)
  end

  if filename
    path = "#{path}/#{filename}"
  else
    path = "#{path}/#{Time.now.to_i.to_s}_#{rand 1000.9999}"
  end

  File.open(path, 'wb') do |f|
    f.write(HTTParty.get(url).body)
  end

  path
end