module WechatPublicApi::Account

Public Instance Methods

get_qrscene(sceneid) click to toggle source

获取临时场景带惨二维码,30天有效 @param <int> sceneid – 场景值ID,临时二维码时为32位非0整型,永久二维码时最大值为100000(目前参数只支持1–100000) @return <string> url – 二维码网址

# 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) click to toggle source

获取永久二维码 @param <int> sceneid – 场景值ID,临时二维码时为32位非0整型,永久二维码时最大值为100000(目前参数只支持1–100000) @return <string> url – 二维码网址

# 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) click to toggle source

@param <string> longurl – 需要被压缩的url

@return <json> shorturl – 返回短链接 {“errcode”:0,“errmsg”:“ok”,“short_url”:“//w.url.cn/s/AvCo6Ih”} if false @return {“errcode”:40013,“errmsg”:“invalid appid”}

# 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) click to toggle source

保存带参数二维码到指定位置 @param <string> path – 例如: “#{Rails.root}/public/qrcode” @param <string> filename – 文件名,可选参数,默认不填写则使用时间戳+随机数的方式命名

@return <string> path – 二维码的保存路径

# 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