Commit f2c296f9 by lanrion

添加 qrcode_authorize_url接口实现网页二维码登录

parent 8e717992
...@@ -5,6 +5,7 @@ if defined? Yajl ...@@ -5,6 +5,7 @@ if defined? Yajl
else else
require "json" require "json"
end end
require "erb"
require "weixin_authorize/carrierwave/weixin_uploader" require "weixin_authorize/carrierwave/weixin_uploader"
require "weixin_authorize/config" require "weixin_authorize/config"
......
...@@ -55,7 +55,7 @@ module WeixinAuthorize ...@@ -55,7 +55,7 @@ module WeixinAuthorize
def generate_media(msg_type, media_info, option) def generate_media(msg_type, media_info, option)
msg_type = msg_type.to_s msg_type = msg_type.to_s
if not MSG_TYPE.include?(msg_type) if not MSG_TYPE.include?(msg_type)
raise MediaTypeException, "#{msg_type} is a valid msg_type" raise MediaTypeException, "#{msg_type} is a invalid msg_type"
end end
{ {
msg_type => convert_media_info(msg_type, media_info), msg_type => convert_media_info(msg_type, media_info),
......
...@@ -3,6 +3,13 @@ module WeixinAuthorize ...@@ -3,6 +3,13 @@ module WeixinAuthorize
module Api module Api
module Oauth module Oauth
# 网站应用微信登录授权URL
# 文档:http://t.cn/RyZVWEY
def qrcode_authorize_url(uri, scope="snsapi_login", state="web_wx_login")
uri = encode_url(redirect_uri)
WeixinAuthorize.open_endpoint("/connect/qrconnect?appid=#{app_id}&redirect_uri=#{uri}&response_type=code&scope=#{scope}&state=#{state}#wechat_redirect")
end
# 应用授权作用域: scope # 应用授权作用域: scope
# snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid), # snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),
# snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息) # snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)
...@@ -11,9 +18,8 @@ module WeixinAuthorize ...@@ -11,9 +18,8 @@ module WeixinAuthorize
# 如果用户点击同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。若用户禁止授权,则重定向后不会带上code参数,仅会带上state参数redirect_uri?state=STATE # 如果用户点击同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。若用户禁止授权,则重定向后不会带上code参数,仅会带上state参数redirect_uri?state=STATE
def authorize_url(redirect_uri, scope="snsapi_base", state="weixin") def authorize_url(redirect_uri, scope="snsapi_base", state="weixin")
require "erb" uri = encode_url(redirect_uri)
redirect_uri = ERB::Util.url_encode(redirect_uri) WeixinAuthorize.open_endpoint("/connect/oauth2/authorize?appid=#{app_id}&redirect_uri=#{uri}&response_type=code&scope=#{scope}&state=#{state}#wechat_redirect")
WeixinAuthorize.open_endpoint("/connect/oauth2/authorize?appid=#{app_id}&redirect_uri=#{redirect_uri}&response_type=code&scope=#{scope}&state=#{state}#wechat_redirect")
end end
# 首先请注意,这里通过code换取的网页授权access_token,与基础支持中的access_token不同。公众号可通过下述接口来获取网页授权access_token。如果网页授权的作用域为snsapi_base,则本步骤中获取到网页授权access_token的同时,也获取到了openid,snsapi_base式的网页授权流程即到此为止。 # 首先请注意,这里通过code换取的网页授权access_token,与基础支持中的access_token不同。公众号可通过下述接口来获取网页授权access_token。如果网页授权的作用域为snsapi_base,则本步骤中获取到网页授权access_token的同时,也获取到了openid,snsapi_base式的网页授权流程即到此为止。
...@@ -33,6 +39,12 @@ module WeixinAuthorize ...@@ -33,6 +39,12 @@ module WeixinAuthorize
WeixinAuthorize.http_get_without_token("/sns/userinfo?access_token=#{oauth_token}&openid=#{openid}&lang=#{lang}", {}, "api") WeixinAuthorize.http_get_without_token("/sns/userinfo?access_token=#{oauth_token}&openid=#{openid}&lang=#{lang}", {}, "api")
end end
private
def encode_url(uri)
ERB::Util.url_encode(uri)
end
end end
end end
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