Commit 5378b5ba by mingyuan

use base url for db

parent 1db89fa8
...@@ -30,7 +30,7 @@ GIT ...@@ -30,7 +30,7 @@ GIT
GIT GIT
remote: https://github.com/work-design/rails_extend.git remote: https://github.com/work-design/rails_extend.git
revision: 40067af089497d0abe0ebf2fba77e813902bc868 revision: ef2aa3492d3d214a8d60cb6322c39d2e568adb74
specs: specs:
rails_extend (1.0.0) rails_extend (1.0.0)
rails (>= 6.0) rails (>= 6.0)
...@@ -181,8 +181,6 @@ GEM ...@@ -181,8 +181,6 @@ GEM
minitest (> 5.3) minitest (> 5.3)
multipart-post (2.1.1) multipart-post (2.1.1)
nio4r (2.5.8) nio4r (2.5.8)
nokogiri (1.12.4-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.12.4-x86_64-linux) nokogiri (1.12.4-x86_64-linux)
racc (~> 1.4) racc (~> 1.4)
pg (1.2.3) pg (1.2.3)
......
...@@ -9,30 +9,26 @@ module Dingtalk::Api ...@@ -9,30 +9,26 @@ module Dingtalk::Api
end end
def token def token
@client.get 'gettoken', params: { appkey: app.app_key, appsecret: app.app_secret }, base: BASE @client.get 'gettoken', params: { appkey: app.app_key, appsecret: app.app_secret }, base: app.base_url
end end
def jsapi_ticket def jsapi_ticket
get 'get_jsapi_ticket', base: BASE get 'get_jsapi_ticket', base: app.base_url
end end
def base_host def get(path, params: {}, headers: {}, base: app.base_url, as: nil)
BASE
end
def get(path, params: {}, headers: {}, base: base_host, as: nil)
with_access_token('get', path, params, headers, nil) do |processed_params, processed_headers| with_access_token('get', path, params, headers, nil) do |processed_params, processed_headers|
@client.get path, headers: processed_headers, params: processed_params, base: base, as: as @client.get path, headers: processed_headers, params: processed_params, base: base, as: as
end end
end end
def post(path, params: {}, headers: {}, base: base_host, **payload) def post(path, params: {}, headers: {}, base: app.base_url, **payload)
with_access_token('post', path, params, headers, payload) do |processed_params, processed_headers| with_access_token('post', path, params, headers, payload) do |processed_params, processed_headers|
@client.post path, payload.to_json, headers: processed_headers, params: processed_params, base: base @client.post path, payload.to_json, headers: processed_headers, params: processed_params, base: base
end end
end end
def post_file(path, file, params: {}, headers: {}, base: base_host) def post_file(path, file, params: {}, headers: {}, base: app.base_url)
with_access_token('post', path, params, headers, nil) do |processed_params, processed_headers| with_access_token('post', path, params, headers, nil) do |processed_params, processed_headers|
@client.post_file path, file, headers: processed_headers, params: processed_params, base: base @client.post_file path, file, headers: processed_headers, params: processed_params, base: base
end end
......
module Dingtalk::Api module Dingtalk::Api
module Inner::New module Inner::New
BASE = 'https://api.dingtalk.com/'
def getuserinfo(union_id) def getuserinfo(union_id)
get "v1.0/contact/users/#{union_id}" get "/v1.0/contact/users/#{union_id}"
end end
end end
......
module Dingtalk::Api module Dingtalk::Api
module Inner::Saas module Inner::Saas
#BASE = 'https://openplatform.dg-work.cn'
BASE = 'https://openplatform-pro.ding.zj.gov.cn' BASE = 'https://openplatform-pro.ding.zj.gov.cn'
def getuserinfo(code) def getuserinfo(code)
......
...@@ -3,7 +3,7 @@ module Dingtalk::Api ...@@ -3,7 +3,7 @@ module Dingtalk::Api
BASE = 'https://oapi.dingtalk.com/' BASE = 'https://oapi.dingtalk.com/'
def getuserinfo(code) def getuserinfo(code)
r = post 'topapi/v2/user/getuserinfo', code: code r = post '/topapi/v2/user/getuserinfo', code: code
if r.is_a? Hash if r.is_a? Hash
r['result'] r['result']
else else
......
module Dingtalk::Api module Dingtalk::Api
class New < Base class New < Base
BASE = 'https://api.dingtalk.com/'
include Inner::New include Inner::New
def token def token
...@@ -8,17 +7,13 @@ module Dingtalk::Api ...@@ -8,17 +7,13 @@ module Dingtalk::Api
appKey: app.app_key, appKey: app.app_key,
appSecret: app.app_secret, appSecret: app.app_secret,
} }
r = @client.post 'v1.0/oauth2/accessToken', payload.to_json, base: BASE r = @client.post 'v1.0/oauth2/accessToken', payload.to_json, base: app.base_url
{ {
'access_token' => r['accessToken'], 'access_token' => r['accessToken'],
'expires_in' => r['expireIn'] 'expires_in' => r['expireIn']
} }
end end
def base_host
BASE
end
protected protected
def with_access_token(method, path, params = {}, headers = {}, payload = {}, tries = 2) def with_access_token(method, path, params = {}, headers = {}, payload = {}, tries = 2)
app.refresh_access_token unless app.access_token_valid? app.refresh_access_token unless app.access_token_valid?
......
module Dingtalk::Api module Dingtalk::Api
class Saas < Base class Saas < Base
include Inner::Saas include Inner::Saas
#BASE = 'https://openplatform.dg-work.cn'
BASE = 'https://openplatform-pro.ding.zj.gov.cn'
def token def token
headers = sign_header('POST', '/gettoken.json') headers = sign_header('POST', '/gettoken.json')
r = @client.post '/gettoken.json', headers: headers, base: BASE r = @client.post '/gettoken.json', headers: headers, base: app.base_url
{ {
'access_token' => r.dig('content', 'data', 'accessToken'), 'access_token' => r.dig('content', 'data', 'accessToken'),
'expires_in' => r.dig('content', 'data', 'expiresIn') 'expires_in' => r.dig('content', 'data', 'expiresIn')
} }
end end
def post(path, params: {}, headers: {}, base: base_host, **payload) def post(path, params: {}, headers: {}, base: app.base_url, **payload)
with_access_token('post', path, params, headers, payload) do |processed_params, processed_headers| with_access_token('post', path, params, headers, payload) do |processed_params, processed_headers|
@client.post path, payload.to_query, headers: processed_headers, params: processed_params, base: base @client.post path, payload.to_query, headers: processed_headers, params: processed_params, base: base
end end
......
...@@ -15,6 +15,7 @@ module Dingtalk ...@@ -15,6 +15,7 @@ module Dingtalk
attribute :oauth2_state, :string attribute :oauth2_state, :string
attribute :jsapi_ticket_expires_at, :datetime attribute :jsapi_ticket_expires_at, :datetime
attribute :tenant_id, :string, comment: '专有钉钉,租户ID' attribute :tenant_id, :string, comment: '专有钉钉,租户ID'
attribute :base_url, :string, comment: '对应的 Base Host'
has_many :dingtalk_users, foreign_key: :appid, primary_key: :app_key has_many :dingtalk_users, foreign_key: :appid, primary_key: :app_key
end end
......
module Dingtalk module Dingtalk
module Model::App::NewApp module Model::App::NewApp
extend ActiveSupport::Extend
included do
attribute :base_url, :string, default: 'https://api.dingtalk.com'
end
def api def api
return @api if defined? @api return @api if defined? @api
......
...@@ -3,6 +3,8 @@ module Dingtalk ...@@ -3,6 +3,8 @@ module Dingtalk
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
attribute :base_url, :string, default: 'https://oapi.dingtalk.com'
has_one :new_app, foreign_key: :app_key, primary_key: :app_key has_one :new_app, foreign_key: :app_key, primary_key: :app_key
end end
......
...@@ -3,6 +3,7 @@ module Dingtalk ...@@ -3,6 +3,7 @@ module Dingtalk
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
attribute :base_url, :string, default: 'https://openplatform.dg-work.cn'
end end
def api def api
......
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