diff --git a/README.md b/README.md index 3b3083d..f655eea 100644 --- a/README.md +++ b/README.md @@ -2,20 +2,29 @@ 部门、成员、标签均可以在开发环境调试 -``` +```ruby gem "qy_wechat_api", git: "https://github.com/lanrion/qy_wechat_api.git" ``` -**暂未对access_token做缓存处理,为了确保在开发过程不会出现token过期问题,请确保不要使用全局变量做client** +**暂未对access_token做缓存处理,为了确保在开发过程不会出现token过期问题,请确保不要使用全局变量做app_client变量。** ## 基本用法 + ```ruby -client = QyWechatApi::Client.new(QyWechatApi.corpid, QyWechatApi.corpsecret) +app_client = QyWechatApi::Client.new(corpid, corpsecret) # 创建自定义菜单 # menu_json的生成方法请参考: https://github.com/lanrion/weixin_rails_middleware/wiki/DIY-menu -client.menu.create(menu_json, agent_id) +app_client.menu.create(menu_json, agent_id) + +# Oauth 用法 +# 先要配置你应用的 可信域名 2458023e.ngrok.com +# state 为开发者自定义参数,可选 +app_client.oauth.authorize_url("http://2458023e.ngrok.com", "state") +# 获取code后,获取用户信息 +# app_id: 跳转链接时所在的企业应用ID +app_client.oauth.get_user_info("code", "app_id") ``` diff --git a/lib/qy_wechat_api.rb b/lib/qy_wechat_api.rb index ef3f2ee..a4552c3 100644 --- a/lib/qy_wechat_api.rb +++ b/lib/qy_wechat_api.rb @@ -13,10 +13,12 @@ module QyWechatApi OK_CODE = 0.freeze class << self + # for test def corpid "wxb9ce1d023fe6eb69" end + # for test def corpsecret "UOofFIah4PVLmkG8xMH3lpDxj6NTnQSKMrFt-HubiPB4kjB09EmTVcUjgNeermps" end @@ -44,5 +46,10 @@ module QyWechatApi en_msg = result_hash.delete("errmsg") ResultHandler.new(code, en_msg, result_hash) end + + def open_endpoint(url) + "https://open.weixin.qq.com#{url}" + end + end end diff --git a/lib/qy_wechat_api/api.rb b/lib/qy_wechat_api/api.rb index b6eacb7..2eeddb4 100644 --- a/lib/qy_wechat_api/api.rb +++ b/lib/qy_wechat_api/api.rb @@ -5,3 +5,4 @@ require "qy_wechat_api/api/message" require "qy_wechat_api/api/tag" require "qy_wechat_api/api/user" require "qy_wechat_api/api/menu" +require "qy_wechat_api/api/oauth" diff --git a/lib/qy_wechat_api/api/base.rb b/lib/qy_wechat_api/api/base.rb index 708a912..9c65f17 100644 --- a/lib/qy_wechat_api/api/base.rb +++ b/lib/qy_wechat_api/api/base.rb @@ -3,10 +3,11 @@ module QyWechatApi module Api class Base - attr_accessor :access_token + attr_accessor :access_token, :corp_id - def initialize(access_token) + def initialize(access_token, corp_id=nil) @access_token = access_token + @corp_id = corp_id end private @@ -22,6 +23,10 @@ module QyWechatApi QyWechatApi.http_post_without_token(request_url, payload, params) end + def base_url + "" + end + end end end diff --git a/lib/qy_wechat_api/api/oauth.rb b/lib/qy_wechat_api/api/oauth.rb new file mode 100644 index 0000000..26967f9 --- /dev/null +++ b/lib/qy_wechat_api/api/oauth.rb @@ -0,0 +1,27 @@ +# encoding: utf-8 +module QyWechatApi + module Api + class Oauth < Base + + # appid 是 企业的CorpID + # redirect_uri 是 授权后重定向的回调链接地址,请使用urlencode对链接进行处理 + # response_type 是 返回类型,此时固定为:code + # scope 是 应用授权作用域,此时固定为:snsapi_base + # state 否 重定向后会带上state参数,企业可以填写a-zA-Z0-9的参数值 + # #wechat_redirect 是微信终端使用此参数判断是否需要带上身份信息 + # https://open.weixin.qq.com/connect/oauth2/authorize?appid=CORPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect + def authorize_url(redirect_uri, state="qy_wechat") + require "erb" + redirect_uri = ERB::Util.url_encode(redirect_uri) + QyWechatApi.open_endpoint("/connect/oauth2/authorize?appid=#{corp_id}&redirect_uri=#{redirect_uri}&response_type=code&scope=snsapi_base&state=#{state}#wechat_redirect") + end + + # 根据code获取成员信息 + # https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE&agentid=AGENTID + def get_user_info(code, agent_id) + http_get("user/getuserinfo", {code: code, agentid: agent_id}) + end + + end + end +end diff --git a/lib/qy_wechat_api/client.rb b/lib/qy_wechat_api/client.rb index 26927b7..1a3e712 100644 --- a/lib/qy_wechat_api/client.rb +++ b/lib/qy_wechat_api/client.rb @@ -38,6 +38,10 @@ module QyWechatApi Api::Menu.new(get_access_token) end + def oauth + Api::Oauth.new(get_access_token, corp_id) + end + private def get_access_token self.access_token ||= get_token.result[:access_token] -- libgit2 0.21.0