From 8c578b66ba1563de10f491c9b492864378718fef Mon Sep 17 00:00:00 2001 From: lanrion Date: Sun, 22 Mar 2015 11:07:47 +0800 Subject: [PATCH] 添加第三方应用授权API实现 --- Gemfile | 5 ++--- lib/qy_wechat_api.rb | 3 ++- lib/qy_wechat_api/api.rb | 12 ++++-------- lib/qy_wechat_api/api/base.rb | 2 +- lib/qy_wechat_api/api/service/base.rb | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/qy_wechat_api/api/service/suite.rb | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/qy_wechat_api/suite.rb | 7 +++++++ 7 files changed, 136 insertions(+), 13 deletions(-) create mode 100644 lib/qy_wechat_api/api/service/base.rb create mode 100644 lib/qy_wechat_api/api/service/suite.rb create mode 100644 lib/qy_wechat_api/suite.rb diff --git a/Gemfile b/Gemfile index 313b3c7..797f5e2 100644 --- a/Gemfile +++ b/Gemfile @@ -8,9 +8,8 @@ group :test, :development do gem "codeclimate-test-reporter", require: nil gem 'coveralls', require: false # For debugger - gem "pry-rails", "~> 0.3.2" - - gem "pry-debugger", "~> 0.2.2" + gem "pry-rails" + gem 'pry-byebug' end # Specify your gem's dependencies in qy_wechat_api.gemspec diff --git a/lib/qy_wechat_api.rb b/lib/qy_wechat_api.rb index bf277b8..6d5bce5 100644 --- a/lib/qy_wechat_api.rb +++ b/lib/qy_wechat_api.rb @@ -9,6 +9,7 @@ require "qy_wechat_api/config" require "qy_wechat_api/client" require "qy_wechat_api/handler" require "qy_wechat_api/api" +require "qy_wechat_api/suite" module QyWechatApi @@ -17,7 +18,7 @@ module QyWechatApi autoload(:ObjectStorage, "qy_wechat_api/storage/object_storage") autoload(:RedisStorage, "qy_wechat_api/storage/redis_storage") - ENDPOINT_URL = "https://qyapi.weixin.qq.com/cgi-bin" + ENDPOINT_URL = "https://qyapi.weixin.qq.com/cgi-bin".freeze OK_MSG = "ok".freeze OK_CODE = 0.freeze diff --git a/lib/qy_wechat_api/api.rb b/lib/qy_wechat_api/api.rb index 2eeddb4..010c507 100644 --- a/lib/qy_wechat_api/api.rb +++ b/lib/qy_wechat_api/api.rb @@ -1,8 +1,4 @@ -require "qy_wechat_api/api/base" -require "qy_wechat_api/api/department" -require "qy_wechat_api/api/media" -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" +Dir["#{File.dirname(__FILE__)}/api/**/*.rb"].each do |path| + require path +end + diff --git a/lib/qy_wechat_api/api/base.rb b/lib/qy_wechat_api/api/base.rb index 857e3e2..c3037d5 100644 --- a/lib/qy_wechat_api/api/base.rb +++ b/lib/qy_wechat_api/api/base.rb @@ -27,7 +27,7 @@ module QyWechatApi def request_url(url, params={}) use_base_url = params.delete(:use_base_url) - if use_base_url + if !use_base_url # 使用基础 +base_url+进行拼接 "#{base_url}/#{url}" else diff --git a/lib/qy_wechat_api/api/service/base.rb b/lib/qy_wechat_api/api/service/base.rb new file mode 100644 index 0000000..4696724 --- /dev/null +++ b/lib/qy_wechat_api/api/service/base.rb @@ -0,0 +1,53 @@ +# encoding: utf-8 +module QyWechatApi + module Api + module Service + class Base < Api::Base + + attr_accessor :suite_id, :suite_secret, :suite_ticket + + def initialize(suite_id, suite_secret, suite_ticket, options={}) + @suite_id = suite_id + @suite_secret = suite_secret + @suite_ticket = suite_ticket + end + + private + # 获取应用套件令牌 + # 获取suite_access_token时,还额外需要suite_ticket参数(请永远使用最新接收到的suite_ticket)。suite_ticket由企业号后台定时推送给应用套件,并每十分钟更新。 + # 注2:通过本接口获取的accesstoken不会自动续期,每次获取都会自动更新。 + def get_suite_token + params = { + suite_ticket: suite_ticket, + suite_id: suite_id, + suite_secret: suite_secret + } + Rails.cache.fetch(suite_id, expires_in: 7100.seconds) do + Rails.logger.info("Invoke #{suite_id} get_suite_token to refresh") + res = QyWechatApi.http_post_without_token( + request_url("get_suite_token", params), + params + ) + res.result["suite_access_token"] + end + end + + def http_get(url, params={}) + params.merge!({suite_id: suite_id}) + params.merge!({suite_access_token: get_suite_token}) + QyWechatApi.http_get_without_token(request_url(url, params), params) + end + + def http_post(url, payload={}, params={}) + params.merge!({suite_id: suite_id}) + # 获取suite_token时不需要 + if !params.keys.include?(:suite_ticket) + params.merge!({suite_access_token: get_suite_token}) + end + QyWechatApi.http_post_without_token(request_url(url, params), payload, params) + end + end + + end + end +end diff --git a/lib/qy_wechat_api/api/service/suite.rb b/lib/qy_wechat_api/api/service/suite.rb new file mode 100644 index 0000000..b2544ab --- /dev/null +++ b/lib/qy_wechat_api/api/service/suite.rb @@ -0,0 +1,67 @@ +# encoding: utf-8 +module QyWechatApi + module Api + module Service + class Suite < Base + + # 获取预授权码 + # 该API用于获取预授权码。预授权码用于企业号授权时的应用提供商安全验证。 + def get_pre_auth_code(appid=[]) + http_post("get_pre_auth_code", {appid: appid}) + end + + # 获取企业号的永久授权码 + # 该API用于使用临时授权码换取授权方的永久授权码,并换取授权信息、企业access_token。 + + # 注:临时授权码使用一次后即失效 + def get_permanent_code(auth_code) + http_post("get_permanent_code", {auth_code: auth_code}) + end + + # 获取企业号的授权信息 + # 该API用于通过永久授权码换取企业号的授权信息。 永久code的获取,是通过临时授权码使用get_permanent_code 接口获取到的permanent_code。 + def get_auth_info(auth_corpid, code) + params = {auth_corpid: auth_corpid, permanent_code: code} + http_post("get_auth_info", params) + end + + # 获取企业号应用 + # 该API用于获取授权方的企业号某个应用的基本信息,包括头像、昵称、帐号类型、认证类型、可见范围等信息 + def get_agent(auth_corpid, code, agent_id) + params = { + auth_corpid: auth_corpid, + permanent_code: code, + agentid: agent_id + } + http_post("get_agent", params) + end + + # 该API用于设置授权方的企业应用的选项设置信息,如:地理位置上报等。注意,获取各项选项设置信息,需要有授权方的授权。 + def set_agent(auth_corpid, permanent_code, agent_info) + params = { + auth_corpid: auth_corpid, + permanent_code: permanent_code, + agent: agent_info + } + http_post("set_agent", params) + end + + # 应用提供商在取得企业号的永久授权码并完成对企业号应用的设置之后,便可以开始通过调用企业接口(详见企业接口文档)来运营这些应用。其中,调用企业接口所需的access_token获取方法如下。 + def get_crop_token(auth_corpid, permanent_code) + params = { + auth_corpid: auth_corpid, + permanent_code: permanent_code, + } + http_post("get_crop_token", params) + end + + private + + def base_url + "/service" + end + + end + end + end +end diff --git a/lib/qy_wechat_api/suite.rb b/lib/qy_wechat_api/suite.rb new file mode 100644 index 0000000..85497cf --- /dev/null +++ b/lib/qy_wechat_api/suite.rb @@ -0,0 +1,7 @@ +module QyWechatApi + class Suite + def self.service(suite_id, suite_secret, suite_ticket) + Api::Service::Suite.new(suite_id, suite_secret, suite_ticket) + end + end +end -- libgit2 0.21.0