Commit bf02b244 by lanrion

Merge pull request #6 from lanrion/add-generate-qrcode-api

Add generate qrcode api
parents 6e73330a 80c74284
...@@ -5,6 +5,7 @@ require "weixin_authorize/api/user" ...@@ -5,6 +5,7 @@ require "weixin_authorize/api/user"
require "weixin_authorize/api/menu" require "weixin_authorize/api/menu"
require "weixin_authorize/api/custom" require "weixin_authorize/api/custom"
require "weixin_authorize/api/groups" require "weixin_authorize/api/groups"
require "weixin_authorize/api/qrcode"
require "weixin_authorize/client" require "weixin_authorize/client"
module WeixinAuthorize module WeixinAuthorize
......
# encoding: utf-8
module WeixinAuthorize
module Api
module Qrcode
# http://mp.weixin.qq.com/wiki/index.php?title=生成带参数的二维码
# 临时二维码
def create_qr_scene(scene_id, expire_seconds=1800)
post_url = "#{qrcode_base_url}/create"
qrcode_infos = {action_name: "QR_SCENE", expire_seconds: expire_seconds}
qrcode_infos = qrcode_infos.merge(action_info(scene_id))
http_post(post_url, qrcode_infos)
end
# 临时二维码
def create_qr_limit_scene(scene_id)
post_url = "#{qrcode_base_url}/create"
qrcode_infos = {action_name: "QR_LIMIT_SCENE"}
qrcode_infos = qrcode_infos.merge(action_info(scene_id))
http_post(post_url, qrcode_infos)
end
# 通过ticket换取二维码, 直接访问即可显示!
def qr_code_url(ticket)
"https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=#{ticket}"
end
private
def qrcode_base_url
"/qrcode"
end
def action_info(scene_id)
{action_info: {scene: {scene_id: scene_id}}}
end
end
end
end
...@@ -8,6 +8,7 @@ module WeixinAuthorize ...@@ -8,6 +8,7 @@ module WeixinAuthorize
include Api::Menu include Api::Menu
include Api::Custom include Api::Custom
include Api::Groups include Api::Groups
include Api::Qrcode
attr_accessor :app_id, :app_secret, :expired_at # Time.now + expires_in attr_accessor :app_id, :app_secret, :expired_at # Time.now + expires_in
attr_accessor :access_token, :redis_key attr_accessor :access_token, :redis_key
......
require "spec_helper"
describe WeixinAuthorize::Api::Qrcode do
it "#create_qr_scene" do
response = $client.create_qr_scene("123")
expect(response.keys).to eq(["ticket", "expire_seconds"])
expect(response["expire_seconds"]).to eq(1800)
end
it "#create_qr_limit_scene" do
response = $client.create_qr_limit_scene("1234")
expect(response.keys).to eq(["ticket"])
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment