Module: WechatPublicApi::Account
- Included in:
- WechatPublicApi
- Defined in:
- lib/wechat_public_api/account.rb
Instance Method Summary collapse
-
#get_qrscene(sceneid) ⇒ string
获取临时场景带惨二维码,30天有效.
-
#get_qrsrtscene(sceneid) ⇒ string
获取永久二维码.
-
#get_shorturl(longurl) ⇒ json, ...
if false.
-
#save_qrcode(path, *filename) ⇒ string
保存带参数二维码到指定位置.
Instance Method Details
#get_qrscene(sceneid) ⇒ string
获取临时场景带惨二维码,30天有效
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
获取永久二维码
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
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
保存带参数二维码到指定位置
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 |