Commit 8c578b66ba1563de10f491c9b492864378718fef

Authored by lanrion
1 parent 6559d864
Exists in master

添加第三方应用授权API实现

Gemfile
... ... @@ -8,9 +8,8 @@ group :test, :development do
8 8 gem "codeclimate-test-reporter", require: nil
9 9 gem 'coveralls', require: false
10 10 # For debugger
11   - gem "pry-rails", "~> 0.3.2"
12   -
13   - gem "pry-debugger", "~> 0.2.2"
  11 + gem "pry-rails"
  12 + gem 'pry-byebug'
14 13 end
15 14  
16 15 # Specify your gem's dependencies in qy_wechat_api.gemspec
... ...
lib/qy_wechat_api.rb
... ... @@ -9,6 +9,7 @@ require "qy_wechat_api/config"
9 9 require "qy_wechat_api/client"
10 10 require "qy_wechat_api/handler"
11 11 require "qy_wechat_api/api"
  12 +require "qy_wechat_api/suite"
12 13  
13 14 module QyWechatApi
14 15  
... ... @@ -17,7 +18,7 @@ module QyWechatApi
17 18 autoload(:ObjectStorage, "qy_wechat_api/storage/object_storage")
18 19 autoload(:RedisStorage, "qy_wechat_api/storage/redis_storage")
19 20  
20   - ENDPOINT_URL = "https://qyapi.weixin.qq.com/cgi-bin"
  21 + ENDPOINT_URL = "https://qyapi.weixin.qq.com/cgi-bin".freeze
21 22 OK_MSG = "ok".freeze
22 23 OK_CODE = 0.freeze
23 24  
... ...
lib/qy_wechat_api/api.rb
1   -require "qy_wechat_api/api/base"
2   -require "qy_wechat_api/api/department"
3   -require "qy_wechat_api/api/media"
4   -require "qy_wechat_api/api/message"
5   -require "qy_wechat_api/api/tag"
6   -require "qy_wechat_api/api/user"
7   -require "qy_wechat_api/api/menu"
8   -require "qy_wechat_api/api/oauth"
  1 +Dir["#{File.dirname(__FILE__)}/api/**/*.rb"].each do |path|
  2 + require path
  3 +end
  4 +
... ...
lib/qy_wechat_api/api/base.rb
... ... @@ -27,7 +27,7 @@ module QyWechatApi
27 27  
28 28 def request_url(url, params={})
29 29 use_base_url = params.delete(:use_base_url)
30   - if use_base_url
  30 + if !use_base_url
31 31 # 使用基础 +base_url+进行拼接
32 32 "#{base_url}/#{url}"
33 33 else
... ...
lib/qy_wechat_api/api/service/base.rb 0 → 100644
... ... @@ -0,0 +1,53 @@
  1 +# encoding: utf-8
  2 +module QyWechatApi
  3 + module Api
  4 + module Service
  5 + class Base < Api::Base
  6 +
  7 + attr_accessor :suite_id, :suite_secret, :suite_ticket
  8 +
  9 + def initialize(suite_id, suite_secret, suite_ticket, options={})
  10 + @suite_id = suite_id
  11 + @suite_secret = suite_secret
  12 + @suite_ticket = suite_ticket
  13 + end
  14 +
  15 + private
  16 + # 获取应用套件令牌
  17 + # 获取suite_access_token时,还额外需要suite_ticket参数(请永远使用最新接收到的suite_ticket)。suite_ticket由企业号后台定时推送给应用套件,并每十分钟更新。
  18 + # 注2:通过本接口获取的accesstoken不会自动续期,每次获取都会自动更新。
  19 + def get_suite_token
  20 + params = {
  21 + suite_ticket: suite_ticket,
  22 + suite_id: suite_id,
  23 + suite_secret: suite_secret
  24 + }
  25 + Rails.cache.fetch(suite_id, expires_in: 7100.seconds) do
  26 + Rails.logger.info("Invoke #{suite_id} get_suite_token to refresh")
  27 + res = QyWechatApi.http_post_without_token(
  28 + request_url("get_suite_token", params),
  29 + params
  30 + )
  31 + res.result["suite_access_token"]
  32 + end
  33 + end
  34 +
  35 + def http_get(url, params={})
  36 + params.merge!({suite_id: suite_id})
  37 + params.merge!({suite_access_token: get_suite_token})
  38 + QyWechatApi.http_get_without_token(request_url(url, params), params)
  39 + end
  40 +
  41 + def http_post(url, payload={}, params={})
  42 + params.merge!({suite_id: suite_id})
  43 + # 获取suite_token时不需要
  44 + if !params.keys.include?(:suite_ticket)
  45 + params.merge!({suite_access_token: get_suite_token})
  46 + end
  47 + QyWechatApi.http_post_without_token(request_url(url, params), payload, params)
  48 + end
  49 + end
  50 +
  51 + end
  52 + end
  53 +end
... ...
lib/qy_wechat_api/api/service/suite.rb 0 → 100644
... ... @@ -0,0 +1,67 @@
  1 +# encoding: utf-8
  2 +module QyWechatApi
  3 + module Api
  4 + module Service
  5 + class Suite < Base
  6 +
  7 + # 获取预授权码
  8 + # 该API用于获取预授权码。预授权码用于企业号授权时的应用提供商安全验证。
  9 + def get_pre_auth_code(appid=[])
  10 + http_post("get_pre_auth_code", {appid: appid})
  11 + end
  12 +
  13 + # 获取企业号的永久授权码
  14 + # 该API用于使用临时授权码换取授权方的永久授权码,并换取授权信息、企业access_token。
  15 +
  16 + # 注:临时授权码使用一次后即失效
  17 + def get_permanent_code(auth_code)
  18 + http_post("get_permanent_code", {auth_code: auth_code})
  19 + end
  20 +
  21 + # 获取企业号的授权信息
  22 + # 该API用于通过永久授权码换取企业号的授权信息。 永久code的获取,是通过临时授权码使用get_permanent_code 接口获取到的permanent_code。
  23 + def get_auth_info(auth_corpid, code)
  24 + params = {auth_corpid: auth_corpid, permanent_code: code}
  25 + http_post("get_auth_info", params)
  26 + end
  27 +
  28 + # 获取企业号应用
  29 + # 该API用于获取授权方的企业号某个应用的基本信息,包括头像、昵称、帐号类型、认证类型、可见范围等信息
  30 + def get_agent(auth_corpid, code, agent_id)
  31 + params = {
  32 + auth_corpid: auth_corpid,
  33 + permanent_code: code,
  34 + agentid: agent_id
  35 + }
  36 + http_post("get_agent", params)
  37 + end
  38 +
  39 + # 该API用于设置授权方的企业应用的选项设置信息,如:地理位置上报等。注意,获取各项选项设置信息,需要有授权方的授权。
  40 + def set_agent(auth_corpid, permanent_code, agent_info)
  41 + params = {
  42 + auth_corpid: auth_corpid,
  43 + permanent_code: permanent_code,
  44 + agent: agent_info
  45 + }
  46 + http_post("set_agent", params)
  47 + end
  48 +
  49 + # 应用提供商在取得企业号的永久授权码并完成对企业号应用的设置之后,便可以开始通过调用企业接口(详见企业接口文档)来运营这些应用。其中,调用企业接口所需的access_token获取方法如下。
  50 + def get_crop_token(auth_corpid, permanent_code)
  51 + params = {
  52 + auth_corpid: auth_corpid,
  53 + permanent_code: permanent_code,
  54 + }
  55 + http_post("get_crop_token", params)
  56 + end
  57 +
  58 + private
  59 +
  60 + def base_url
  61 + "/service"
  62 + end
  63 +
  64 + end
  65 + end
  66 + end
  67 +end
... ...
lib/qy_wechat_api/suite.rb 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +module QyWechatApi
  2 + class Suite
  3 + def self.service(suite_id, suite_secret, suite_ticket)
  4 + Api::Service::Suite.new(suite_id, suite_secret, suite_ticket)
  5 + end
  6 + end
  7 +end
... ...