Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
weixin_authorize
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ikcrm_common
weixin_authorize
Commits
973a187c
Commit
973a187c
authored
Mar 16, 2014
by
lanrion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor client.rb
parent
0986bddd
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
14 deletions
+25
-14
lib/weixin_authorize/client.rb
+25
-14
No files found.
lib/weixin_authorize/client.rb
View file @
973a187c
...
@@ -10,19 +10,22 @@ module WeixinAuthorize
...
@@ -10,19 +10,22 @@ module WeixinAuthorize
attr_accessor
:app_id
,
:app_secret
,
:expired_at
# Time.now + expires_in
attr_accessor
:app_id
,
:app_secret
,
:expired_at
# Time.now + expires_in
attr_accessor
:access_token
attr_accessor
:access_token
def
initialize
(
app_id
=
""
,
app_secret
=
""
,
expired_at
=
nil
)
# 对于多用户微信营销平台的对接,需要把每次的expired_at, access_token保存在Redis中
# 每次使用,则可以从Redis中获取expired_at和access_token,即
# @client = WeixinAuthorize::Client.new(appid, appsecret, expired_at, access_token)
# 获取access_token,则仍然是:@client.get_access_token 来获取
def
initialize
(
app_id
=
""
,
app_secret
=
""
,
expired_at
=
nil
,
access_token
=
nil
)
@app_id
=
app_id
@app_id
=
app_id
@app_secret
=
app_secret
@app_secret
=
app_secret
@expired_at
=
(
expired_at
.
to_i
||
Time
.
now
.
to_i
)
@expired_at
=
(
expired_at
.
to_i
||
Time
.
now
.
to_i
)
@access_token
=
access_token
yield
self
if
block_given?
yield
self
if
block_given?
end
end
# return token
# return token
def
get_access_token
def
get_access_token
# 如果当前token过期时间小于现在的时间,则重新获取一次
# 如果当前token过期时间小于现在的时间,则重新获取一次
if
@expired_at
<=
Time
.
now
.
to_i
authenticate
if
token_expired?
authenticate
end
@access_token
@access_token
end
end
...
@@ -33,26 +36,22 @@ module WeixinAuthorize
...
@@ -33,26 +36,22 @@ module WeixinAuthorize
self
.
expired_at
=
Time
.
now
.
to_i
+
hash_infos
[
"expires_in"
]
self
.
expired_at
=
Time
.
now
.
to_i
+
hash_infos
[
"expires_in"
]
end
end
def
token_expired?
@expired_at
<=
Time
.
now
.
to_i
end
private
private
def
authenticate_options
def
authenticate_options
{
grant_type:
"client_credential"
,
appid:
app_id
,
secret:
app_secret
}
{
grant_type:
"client_credential"
,
appid:
app_id
,
secret:
app_secret
}
end
end
def
plain_endpoint
"https://api.weixin.qq.com/cgi-bin"
end
def
file_endpoint
"http://file.api.weixin.qq.com/cgi-bin"
end
def
access_token_param
def
access_token_param
{
access_token:
get_access_token
}
{
access_token:
get_access_token
}
end
end
def
http_get_without_token
(
url
,
options
=
{},
endpoint
=
"plain"
)
def
http_get_without_token
(
url
,
options
=
{},
endpoint
=
"plain"
)
get_api_url
=
send
(
"
#{
endpoint
}
_endpoint"
)
+
url
get_api_url
=
endpoint_url
(
endpoint
)
+
url
JSON
.
parse
(
RestClient
.
get
(
get_api_url
,
:params
=>
options
))
JSON
.
parse
(
RestClient
.
get
(
get_api_url
,
:params
=>
options
))
end
end
...
@@ -63,9 +62,21 @@ module WeixinAuthorize
...
@@ -63,9 +62,21 @@ module WeixinAuthorize
# Refactor
# Refactor
def
http_post
(
url
,
options
=
{},
endpoint
=
"plain"
)
def
http_post
(
url
,
options
=
{},
endpoint
=
"plain"
)
post_api_url
=
send
(
"
#{
endpoint
}
_endpoint"
)
+
url
+
"?access_token=
#{
get_access_token
}
"
post_api_url
=
endpoint_url
(
endpoint
)
+
url
+
"?access_token=
#{
get_access_token
}
"
JSON
.
parse
(
RestClient
.
post
(
post_api_url
,
options
))
JSON
.
parse
(
RestClient
.
post
(
post_api_url
,
options
))
end
end
def
endpoint_url
(
endpoint
)
send
(
"
#{
endpoint
}
_endpoint"
)
end
def
plain_endpoint
"https://api.weixin.qq.com/cgi-bin"
end
def
file_endpoint
"http://file.api.weixin.qq.com/cgi-bin"
end
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment