Skip to content

Instantly share code, notes, and snippets.

@panfu
Last active April 19, 2016 02:18
Show Gist options
  • Save panfu/babf33ad1096395a9533 to your computer and use it in GitHub Desktop.
Save panfu/babf33ad1096395a9533 to your computer and use it in GitHub Desktop.
批量创建微信公众号二维码
require 'rubygems'
require "json"
require 'cgi'
require 'http'
APPID = '--------------'
SECRET = '--------------------------------'
def get_token
token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=#{APPID}&secret=#{SECRET}"
token_msg = HTTP.get(token_url)
p token_dat = JSON.parse(token_msg)
token_dat['access_token']
end
def create_qr(scene_id, access_token=nil)
if scene_id
access_token ||= get_token
qr_ticket_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=#{access_token}"
qr_ticket_post_json = {
"action_name" => "QR_LIMIT_SCENE",
"action_info" => {
"scene" => {
"scene_id" => "#{scene_id}"
}
}
}
qr_ticket_msg = HTTP.post(qr_ticket_url, :json => qr_ticket_post_json)
qr_ticket_dat = JSON.parse(qr_ticket_msg)
p qr_ticket_dat
qr_pic_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=#{qr_ticket_dat['ticket']}"
%x(curl #{qr_pic_url} -o s#{scene_id}.jpeg)
puts "二维码图片已下载到当前目录,文件为 s#{scene_id}.jpeg。"
%w(open s#{ARGV[0]}.jpeg)
else
puts "需要输入一个自然数作为场景ID,例如:ruby create_qr.rb 1"
end
end
if ARGV[0] && ARGV[1]
access_token = get_token
(ARGV[0]..ARGV[1]).each do |scene_id|
create_qr(scene_id, access_token)
end
elsif ARGV[0]
create_qr(ARGV[0])
else
puts "需要输入自然数作为场景ID,例如:ruby create_qr.rb 1 生成 scene_id 为 1 的二维码,也可以用 ruby create_qr.rb 1 100 生成 scene_id 1 到 100 的二维码。"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment